Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5430
Number of posts: 16951

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Odd behaviour of Turbo C compiler? Posted by shuvam on 25 Jan 2006 at 11:58 PM
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");
}
Report
Re: Odd behaviour of Turbo C compiler? Posted by stephl on 27 Jan 2006 at 9:56 AM
: 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.
Report
Re: Odd behaviour of Turbo C compiler? Posted by shuvam on 28 Jan 2006 at 9:16 AM
: : 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.
Report
Re: Odd behaviour of Turbo C compiler? Posted by stober on 29 Jan 2006 at 11:55 AM
The correct program is
void main()
{
int i=2;
printf("%d",i);
} 

Report
Re: Odd behaviour of Turbo C compiler? Posted by IDK on 29 Jan 2006 at 12:48 PM
: 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:

mov ax,2
call printf

and printf("%d",2); would be compiled to
mov ax,2
call printf


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
Niklas Ulvinge aka IDK

Report
Re: Odd behaviour of Turbo C compiler? Posted by shuvam on 29 Jan 2006 at 11:54 PM
: : 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:
:
:
: mov ax,2
: call printf
: 

: and printf("%d",2); would be compiled to
:
: mov ax,2
: call printf
: 

:
: 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
: Niklas Ulvinge aka IDK
:
:
Thanks Niklas I think you hit upon the right reason.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.