Thread: output of following c code

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    5

    output of following c code

    Code:
    #include<stdio.h>
    void main()
    {
    int a;
    printf("a contains %d\n");
    }
    this is giving some junk value where as
    Code:
    #include<stdio.h>
    void main()
    {
    float a;
    printf("a contains %f\n");
    }
    this is not giving any value.I am not getting why is it so and could you please tell me what is the concept behind it?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    %d and %f tell printf() than an additional argument has been passed. Your code does not pass an additional argument, so exhibits undefined behaviour.

    Your first example needs to be
    Code:
    #include<stdio.h>
    int main()
    {
    int a;
    printf("a contains %d\n", a);
    }
    Note the changes I have highlighted in red.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    a still needs to be initialised though, before trying to print it.
    int a = 42;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    It's int main(void), not void main(). Let me guess...Turbo C?

    And this should be in C Programming, not C++.

    EDIT: Oops. grumpy already covered the int main issue.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is the output of this code
    By C_Enthusiast in forum C Programming
    Replies: 2
    Last Post: 01-07-2011, 06:07 AM
  2. what is the output of this code?
    By karthik537 in forum C Programming
    Replies: 15
    Last Post: 09-16-2009, 11:20 AM
  3. Output of code
    By lesodk in forum C++ Programming
    Replies: 5
    Last Post: 05-17-2009, 08:56 AM
  4. What will be the output of the following code?
    By developersubham in forum C++ Programming
    Replies: 13
    Last Post: 01-02-2007, 07:36 AM
  5. same code different output
    By kashifk in forum C Programming
    Replies: 4
    Last Post: 03-18-2003, 02:51 PM

Tags for this Thread