Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Odd behaviour of Turbo C compiler?

shuvamshuvam Posts: 9Member
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");
}

Comments

  • stephlstephl Posts: 422Member
    : 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.
  • shuvamshuvam Posts: 9Member
    : : 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.
  • stoberstober Posts: 9,765Member ✭✭✭
    The correct program is
    [code]
    void main()
    {
    int i=2;
    printf("%d"[red],i);[/red]
    }
    [/code]
  • IDKIDK Posts: 1,784Member
    : 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]

  • shuvamshuvam Posts: 9Member
    : : 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.
Sign In or Register to comment.