C/c programming
Expert: Joseph Moore - 2/6/2010
QuestionQUESTION: 1.what is return 0; plz explain.
2.explain the process of compilation and execution.what is difference between them? plz explain in simple language.
3.whenever a expression contains logical and operator and logical or operator then logical and has higher priority.then why we evaluate the expression from left to right instead of firstly evaluate logical and?
ex-> x=y=z=1;
z=++x||++y&&++z; //x=2,y=1,z=1;
//why not? x=1,y=2,z=1
ANSWER: 1. A function is allowed to return a value. When you put "return 0;" you are simply returning a value of 0.
2. Compiling code takes higher-level code (such as C or C++) and turns it into a language that the machine can understand. The computer does not read C or C++ code, it reads machine code, which is purely binary.
Execution is taking the compiled code and running it.
3. The statement here can be considered to be something along the lines of:
z = ++x || (++y && ++z)
However, in C and C++, there are short-circuits put into place for logical operations. In an or statement, if the first statement is true, then it doesn't matter what the second statement is. In an and, if the first statement is false, then it doesn't matter what the second statement is. this is used to great advantage when dealing with pointers. Consider the following:
int myFunc(int* input)
{
if (input && (*input == 5))
{
return 1;
}
return 0;
}
You cannot dereference a null pointer, so the if statement first checks to see if the pointer is valid before ever checking the rest of the statement. This keeps the code from crashing.
---------- FOLLOW-UP ----------
QUESTION: 1.If I use return 0; in main(),then it should have prototype int not void.But what is it use?is it have any special use?
2.what is l-value and r-value?plz sir explain with example.
3.a>b?g=a:g=b;
does it show error?
4.what is structured programming?
Answer1. The value returned by main can be referenced by other programs (assuming that they called main and store the return value). It's often times used as an error code (if it's used at all).
2. If you see code referencing "l-value" and "r-value," they generally mean "left value" and "right value." Consider:
int i = 0; int j = 2;
if (i < j)
// do something
In the above, the < operator is actually a function (you can even override or otherwise create functions that use these operators) and i is considered to be the l-value and j is considered to be the r-value.
3. This is perfectly fine, executable code (assuming the variables have been declared). What you are seeing is the only trinary operator in C/C++. This is often referred to as the "question-mark colon operator". It's just a short way of writing:
if (condition)
do this
else
do that
The condition is the part before the ?, the "do this" part is the part between the ? and :, and the "do that" part is the part after the :.
4. Wikipedia has an article that can do the subject far more justice than I can.
http://en.wikipedia.org/wiki/Structured_programming