How can I break out of a function [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to check for certain condition. If that is true, certain operation will be performed and then need to break out of function. If condition not satisfied then proceeding normal operations in the function will continue.

The return keyword is used to exit from a function.
If the function has a non-void return type, an expression of the appropriate type must be used with return to provide the return value:
/* Returns 2 * abs(a), where abs(a) denotes the absolute value of a. */
int twice_positive(int a)
{
if(a < 0)
return -2 * a;
return 2 * a;
}
Some people consider "early return" from long(er) functions to be evil stylistically and to be avoided; I don't agree but I thought I should mention it.

Related

C programming : Stackoverflow is a cause of abrupt termination [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
In case of stack overflow in C programming, why do we tell it abrupt termination of loop. Shack-overflow is not a cause of infinite looping. But causing abrupt termination, though we cannot see, where is it terminating.
right??
Say for this program, it is causing a stack overflow but not a case of infinite looping
int foo(int val) {
int x=0;
while(val > 0) {
x = x + foo(val--);
}
return val;
}
But why we cannot say it as an infinite looping??and why is it saying as abrupt termination??
Since this is recursion, every call of the function foo() will increase the stack. And as we know the loop never ends so at some point stack will increase so much that the OS will terminate the code and give stack-overflow error.

Which is the best place for parameter restrictions - inside or outside function? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Option 1: In function
int funcA(int a){
if(a < 0){
return -1;
}
else{
...
return 0;
}
}
Option 2: In main
int main(){
int a;
...
if(a < 0){
...
}
else{
funcA(a);
}
}
Option 3: Both places? If you have some suggestions I will be grateful.
There is no hard and fast rule as to what is better -- there's basically two cases you'd want to consider.
If the constraints are unique to this specific usage of the function, then it would be preferable to put them outside the function, to let the function be more generalized and limit only this specific call to it.
If the constraints are something integral to the function and are always going to be expected whenever the function is called, then it would be preferable to put them inside the function, so that they do not have to be duplicated every place that the function will be called.
It is the callers responsibility to make sure it calls a function with valid arguments - per the documentation.
It is the called functions interest to guard against invalid values when it can - and again, per the documentation.
There is no inherently "right" or "wrong" choice without a contract (documentation) for funcA which is highly dependent on what the function does and how it is expected to be used.

What does predicate mean in C? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have the following code:
int (*predicate)(char) = 0;
Can anyone tell me what this code means? What is the meaning of the word predicate in C?
The sentence is a declaration and definition of a pointer to a function taking one argument (char) and returning int. The pointer is initialized to the null pointer value.
The word "predicate" is the programmer's choice for the variable name.
Reference: cdecl
One might use predicate like this:
/* UNTESTED */
int IsLower(char c) { return c >= 'a' && c <= 'z'; }
int main () {
int (*predicate)(char);
predicate = IsLower;
if ( (*predicate)('f') == 1 ) printf("'f' is lower case!\n");
}

counting words containing character [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
#include <stdio.h>
int hledejznak(x)
{
int c;
int pocitadlo=0;
while((c=getchar())!=EOF)
{
if(x==c){
pocitadlo++;
while((c=getchar())!=32)
{
printf("%d\n",c);
};
};
};
return pocitadlo;
}
int main(int argc,char *argv[])
{
int znak=*argv[1];
printf("answer is %d",hledejznak(znak));
return 0;
}
Hi people, I need to count words containing character specified as argument at terminal
example: echo 'hello babe' | ./main e
Answer is 2
....because there are two words containing letter "e"
My code doesn't work, can you help me?
Thanks
Don't nest your loops; keep the outer one that processes each character read
Have a boolean variable initalized to false & set to true whenever you see the desired character.
Whenever a word ends, increment your counter if the flag is true. Either way, set the flag to false (to get ready for the next word). (Note that the last word may NOT end with a space.)
Only when you're processed all of the input should you print the value of the counter.

Any one enplane output of the if statement? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
What will be output if you will compile and execute the following c statement?
if(printf("This is"))
printf(" tricky question");
the output is
This is tricky question
The docs for printf explain why this happens
Return value
Upon successful return, these functions return the number of
characters printed (excluding the null byte used to end output to
strings).
printf("This is") returns 7 so your if condition succeeds.
Return type of printf function is integer which returns number of character it prints including blank spaces.
So printf function inside if condition will return 7. And print massage This is
In if condition any non- zero number means true so else part will not execute. And print massage tricky question

Resources