views:

217

answers:

6

_

#include<stdio.h>

int main()
{
  int a=5,b=10;
  printf("%d %d");
  return 0;
}
+7  A: 

Undefined behavior: it will output two undefined numbers.

Sometimes it may print values stored in variables because of the way the stack works, but this is far from guaranteed.

Alexander Rafferty
bt why the output is : 10 5 in tc compiler
alfesani
@alfesani, check my answer. It is because you primed the stack before the call with stack variables containing 5 and 10.
Amigable Clark Kant
@alfesani:its garbage.When I run the program I would be getting something else.
fahad
@amigable, will u pls say me which compiler r u using
alfesani
@alfesani, I use GCC in Linux. I used to use TC. If you are coding for DOS on an 80286 or lower, stick with TC. But you are on the Internet, so I guess you are using Windows. Try out DevCPP or CodeBlocks. http://www.codeblocks.org/
Amigable Clark Kant
+2  A: 

A good compiler will issue a warning about this. The output is undefined, but in practice, since printf() expects two extra arguments, it will grab the two integers from the place where it expects its arguments: the stack. For example, when I tried it with gcc, the output was

15224820 134513712

ptomato
+1  A: 

It will output garbage.

enriqueM
no garbage is displayed rather two values are displayed : 10 5. why such output i n tc compiler
alfesani
@alfesani:Because these two values are in your stack memory.Try to paste your code at ideone.com and then check the output.
fahad
@alfesani: those 10 and 5 ARE garbage.
Donotalo
@Donotalo, maybe to you, but to me they are beautiful numbers... :-P
Amigable Clark Kant
+3  A: 

It will print two "random" values from the stack because you have improper arguments being passed to printf. You need to pass a and b as arguments.

Tyler
+2  A: 

Try this one:

#include<stdio.h>
void f(int x,int a,int b) {}
int main()
{
  int a=5,b=10;
  f(0,a,b);
  printf("%d %d");
  return 0;
}

Edit: Well, I compile it with gcc:

$ gcc hehe.c
hehe.c: In function ‘main’:
hehe.c:8: warning: too few arguments for format
$ ./a.out
5 10$ 
Kostya
this snippet goes together well with ttreat31's answer :-)
michael
an error message is coming : system is unable to run the program
alfesani
+9  A: 

It will as everybody said, output two undefined numbers. It is not though, as if the compiler invents two undefined arguments just to have something to print.

What undefined really means, is that the result will differ depending on how the compiler is implemented, and the compiler may do whatever it wants in this situation, even abort execution.

The reason though, you see the numbers 5 and 10 being output when you compile this in TC, is that the way TC works, is that the numbers 5 and 10 are pushed on the stack by the initialization of a and b variables right before the printf call.

a and b are both on the stack and

printf(), in turn works by reading what is on the stack as arguments.

Try for instance with static variables instead of auto variables, like so:

#include<stdio.h>

int main()
{
  static int a=5,b=10;
  printf("%d %d");
  return 0;
}

static variables are not put on the stack, and hence, when you call printf() there will be something entirely different on the stack, like function return addresses or the arguments to main() or something else entirely. The output will then not be 5 and 10, but something else. Try it!

(TC is actually a very interesting compiler to learn C in, because it is pretty simple in how it works. The output of modern compilers and calling conventions often are more difficult to follow.)

Amigable Clark Kant
Also in this case, enabling optimisation on the same compiler might mean that `a` and `b` are elided, and again you get different behaviour. I don't know if/how optimisation can be enabled in TC.
Steve Jessop
@Steve Jessop, it can be enabled, but I don't know to what extent, and I also have a feeling it will not affect calling conventions and maybe not even register allocation for variables. In those days mixing assembler and C was pretty common and the compiler could not make too many assumptions. Also, 8086 is not a register heavy machine, and doing most anything meant putting things on the stack anyway. Nice memories! I am very fond of the for its time _excellent_ Turbo C IDE. Still unrivaled today in some respects.
Amigable Clark Kant
@Amigable: sure, but in this case `a` and `b` are completely unused. So I don't mean that with optimisations they might be stored somewhere else, I mean that with optimisations they might be stripped out of the program entirely, as though they had never been there. Pretty much any amount of data-flow analysis will identify them as unused.
Steve Jessop
@Steve Jessop, that is pretty heavy stuff and I am rather sure TC could never do those kinds of optimizations. I think it could do things like detecting that i = i + 1 can be reduced to i++ and thus one instruction, little things like that.
Amigable Clark Kant
@Amigable: wow, it's like the stone age or something ;-) I was programming before 1989, but not in C.
Steve Jessop
This is a compiler which probably runs comfortably with 128kbyte RAM left. With the IDE running it might need some more. :) Turbo C also worked in CPM if I recall correctly and the 8080 or Z80 CPUs used for CPM had 64kbyte address space in total. :-)
Amigable Clark Kant
@Steve, I _love_ the stone age. If somebody would pay me to program in Turbo C I would switch jobs immediately. And don't get me started on Amiga 500 and 68000 assembler... :-)
Amigable Clark Kant