Odd behaviour of Turbo C compiler?

Comments

: If you compile the following code in the Turbo C/C++ compiler the output is always "0". WHY?: : #include"stdio.h": void main(): {: printf("%d");: }: When printf is called with such an argument string, it expects that an integer value is also supplied as a second argument. If you do not supply a second argument, it is an error an what is printed is garbage. The C compiler cannot warn you since the printf function can take any number of parameters, and so it just checks whether at least an argument string is provided as the first argument. This is not a strange behaviour of Turbo C. I think every compiler would act the same way.

: : If you compile the following code in the Turbo C/C++ compiler the output is always "0". WHY?: : : : #include"stdio.h": : void main(): : {: : printf("%d");: : }: : : When printf is called with such an argument string, it expects that an integer value is also supplied as a second argument. If you do not supply a second argument, it is an error an what is printed is garbage. The C compiler cannot warn you since the printf function can take any number of parameters, and so it just checks whether at least an argument string is provided as the first argument. This is not a strange behaviour of Turbo C. I think every compiler would act the same way.: : Steph.:

but if I write this:

void main(){ int i=2; printf("%d");}

The Output is 2

I can assign "i" any integer value the output will be that integer value.

: : but if I write this:: : : : void main(): : {: : int i=2;: : printf("%d");: : }: : : : The Output is 2: : : : I can assign "i" any integer value the output will be that integer value.: : : : I think that if you look at the compiled code of this it will convert to:: : [code]: mov ax,2: call printf: [/code]: and printf("%d",2); would be compiled to: [code]: mov ax,2: call printf: [/code]: : The same...: : The guys who designed the compiler forgot that possibility. It should be undefined behavior or something, so that's what happens.: : Happy coding wishes: the one and only: [b]Niklas Ulvinge[/b] [white]aka [b]IDK[/b][/white]: : Thanks Niklas I think you hit upon the right reason.