Thread: Help me understand what the next program does

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    11

    Post Help me understand what the next program does

    Hi somebody can help me to understand what this code does
    tanks...

    Code:
    void main()
    {
    
        int a;
        printf("%d", a);
    
    }




  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It is poorly written to begin with. A better example:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int a = 123;
        printf("%d\n", a);
        return 0;
    }
    In which case you should read up on what printf does if you cannot tell what the program does.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    The first one is liable to set off ICBMs, cause military drones to fire hellfire missiles at your house, or possibly just print some garbage values (if you're lucky), due to undefined behavior

  4. #4
    Registered User
    Join Date
    Oct 2013
    Posts
    8
    Laserlight is showing a good example.

    The "main" line, is opening up the main program. You need this to run any program.
    The "int a" is declaring an integer. You need to declare things as integers, floating numbers, characters, etc. for you to use them later on.
    now, the "printf" line is trying to print to your monitor a demical integer (that's what the %d is for) and the number it is looking for is assigned to that "a" integer you declared earlier.
    Unfortunately, the a variable was never set, so you won't be able to output anything.

    Try compiling and running laserlight's program. You should see an output of "123". that's because the declared value of "a" is 123. You could change this to whatever you want, and call upon it with printf.

    Good luck and happy coding

  5. #5
    Registered User
    Join Date
    Sep 2011
    Location
    Dongargarh, India
    Posts
    16
    the code will print some garbage value as the variable 'a' is not initialized, ie. it does not contain any value at the time it is being used by printf().

    Code:
    void main()
    {
    
        int a;
        printf("%d", a);
    
    }


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't seem to understand what's wrong with this program :(
    By aditi_babydoll in forum C++ Programming
    Replies: 5
    Last Post: 10-10-2013, 01:52 PM
  2. Can you help me understand this program?
    By SCRIPT_KITTEH in forum C Programming
    Replies: 7
    Last Post: 07-23-2013, 04:03 AM
  3. Help me understand this program
    By DCICJay in forum C Programming
    Replies: 2
    Last Post: 06-06-2011, 08:52 PM
  4. program that i couldn't understand
    By elton_fan in forum C Programming
    Replies: 9
    Last Post: 03-24-2007, 03:18 PM
  5. Can someone help me understand this example program
    By Guti14 in forum C Programming
    Replies: 6
    Last Post: 09-06-2004, 12:19 PM

Tags for this Thread