You are here:

C/c programming

Advertisement


Question
QUESTION: 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?

Answer
1. 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

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Joseph Moore

Expertise

I've been programming in one form or another since my brother taught me BASIC when I was 6. I've been programing professionally since I was 20, first web development with HTML, JS, DHTML, CSS, etc., then I became a video game developer, writing code in C, C++, C#, SQL, assembly, and various scripting languages. I've even written my own scripting languages, custom designed for the games I was making. I also dabble in Java, PHP, and Perl. I've worked on pretty much every aspect of game development, including graphics, audio, gameplay, tool, UI, input, animation, and physics.

Experience

I've been writing C code for 12 years, both on my own in my spare time and professionally.

Organizations
IGDA

Education/Credentials
Bachelor of Science in Game Design and Development, Full Sail University, Winter Park, FL

Awards and Honors
Salutatorian and Advanced Achiever Awards at Full Sail; Independent Games Festival Student Showcase winner, 2004; Featured article on Gamasutra about an experimental game developed in 2004

©2012 About.com, a part of The New York Times Company. All rights reserved.