Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return to nowhere in C source code(?) [closed]

Tags:

c

Take look at the code that follows.

"Hello " "World!";
"The number is ", 37;
int x=23;
char *y="232";
x,x+2,x*3;
atoi(y) - x;

It is a perfectly valid fragment of C(99) source. But! All that expressions return to nowhere! How can one trace or even use all this anonymous values? Where are they stored and what is their purpose?

like image 623
Rizo Avatar asked Jul 05 '10 20:07

Rizo


People also ask

Why does my C program close automatically?

So when you write small programs, they execute very fast. When the execution is completed, the program exits immediately and hence they close. To prevent this, you can type the following at the end of your main function, before the 'return 0;' line, if you are using one in your code. at the beginning of your code.

Can you return break in C?

No. A "break" is not an int, so it cannot be returned by your function.

What is C in stackoverflow?

Stack overflow occurs when − C) If a function is called recursively by itself infinite times then stack will be unable to store large number of local variables, so stack overflow will occur − void calculate(int a) { if (a== 0) return; a = 6; calculate(a); } int main() { int a = 5; calculate(a); }


2 Answers

These values go nowhere. You can not retrieve them. In fact, most are optimized out by the compiler.

That entire fragment can be simplified to atoi("232") because function calls usually can't be optimized out.

like image 64
Forrest Voight Avatar answered Sep 20 '22 16:09

Forrest Voight


Those particular expressions are useless. On the other hand, this is allowed because some expressions have side effects(and perhaps return values), and sometimes only the side effect is needed.

like image 29
Bwmat Avatar answered Sep 18 '22 16:09

Bwmat