I C language how do we know when to return 0; and when to return a value.Give a code example for each type and explain
In C language, there are mainly two types of functions:
Main function
The return value for main indicates how the program exited. Normal exit is generally represented by a 0 return value from main. Abnormal termination is usually signaled by a non-zero return .This is the reason why void main() is explicitly prohibited by the C/C++ standard and shouldn't be used.
The valid C/C++ main signatures are:
int main()
and
int main(int argc, char* argv[])
which is equivalent to
int main(int argc, char** argv)
Hence, main should always return a value 0 and have a data type of int.
User defined functions:
In user defined functions, "return type" indicates what kind of data this function will return. In C programming, all executable code resides within a function. A function is a named block of code that performs a task and then returns control and the computed value to a caller. You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division is such that each function performs a specific task and is called in the main() function as every C program has at least one function, which is main(), and all the most trivial programs can define additional functions.
These functions may return values of any data type such as int, float, double and sometimes may not return any value and such functions have void as a data type.
Hence, these functions must return some computed value other than(may be 0 as per conditions) to the caller function according to their data type. Caller function then may store this returned value in some variable.
Example:
int square(int x)
{
int square_of_x;
square_of_x = x * x;
return square_of_x;
}
OR YOU CAN WRITE THE SAME FUNCTION AS
int square(int x)
{
return x * x;
}
int main(void)
{
int i,s;
i=2;
s= square(i);
printf(“square of %d is %d :”,%i,%s);
return 0;
}
Here, we have declared a square function which computes square of given integer value and returns it to the caller function i e main(). We pass the value of int type to the function and store the square returned in int s. And then we print the result using printf. In main we return 0 to know if the program executed normally or not.
I C language how do we know when to return 0; and when to return a...
How to find the length of an integer string in C language? I know that the strlen() function only works for finding the length of a regular string, but I want to know how to find the length of an integer string in C language.
C++: what is i and j. IMPORTANT I know it's 3 and 3 but can someone please explain how we can get to that conclusion by just reading the code, can it be explained to me because when i try to do it I get a different answer for i and j I got 1 and 4. #include <iostream> using namespace std; int main() { int i = 1; int j = 0; if (i++ == 1) { j =...
these are right answer but i dont know how to do it, can u explain mystery5(12,9) and mystery5(128,343) O BJP3 Self-Check 12.14: mystery5 Language/Type: 血Java recursion recursive tracing Whitaker Brand (on 2013/04/01) Author: For each call to the following method, indicate what value is returned public int mystery5(int x, int y) if(x<0) { } else if (y<) } else if (x && y j else t -mystery$(-х, у); return return -mystery5(x, -y); 0) { return 0; return 100 * mystery5(x...
[10 marks] We know from our discussion that the language Onlnln-0} is not regular. Is the language L {0"w1nIn 〉 0, w E {0, 1)'} regular! Be sure to prove your answer [10 marks] We know from our discussion that the language Onlnln-0} is not regular. Is the language L {0"w1nIn 〉 0, w E {0, 1)'} regular! Be sure to prove your answer
how do I know if a compound is a base or an acid just by looking at its formula? i know how to determine acid and bases based on the whole equation, but it is hard to know just by looking at the compound, for example, Hco3, is it a base or an acid and how do we know, are there steps I can use? please give me examples with the explanation when I look at the formula for Hco3,...
How do we know when to modify or terminate a successful behavior change procedure? provide an example.
I do NOT want C++ code, Unless there is just no other language in your head. I want pseudo code. Something you could give a 4th grader and they could do the work following your pseudo code. Or your English language instructions. If at some point it is just easier to write a line of C++ code. O.K. but I would prefer broken English. I expect Pseudo code for both problems. I expect a discussion of your general or overall...
How can I express this C language code "loops" to MIPS language program. Thank you int main() { int Z=2; int i; i=0; while(1){ if(i>20) break; Z++; i+=2; } do { Z++; } while (Z<100); while(i > 0) { Z--; i--; }
I am writing a program in c programming and it is supposed to do the following using mc9s12dg256.h microcontroller When both DIP switches #8 and #1 are high, turn on all LEDS. When both DIP switches #8 and #1 are low, turn off all LEDs When DIP switch #8 is high and #1 is low, turn on all the even numbered LEDs. When DIP switch #1 is high and #8 is low, turn on all the odd numbered LEDs. Your...
LANGUAGE: C Below we are dissecting the structure of atoi and its pitfalls using invalid calls Below are invalid calls to atoi Please answer the following questions just in the context/frame provided based on your understanding of how this function works. Yes there are warnings when you run this code, but I am just trying to conceptually understand why these calls are invalid. We already know they are invalid. a) "&num" returns 2, I am aware that 'num' -'0' has...