Please check the C code
Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Please check the C code

  1. #1
    Senior Member
    Join Date
    Dec 2001
    Posts
    134

    Please check the C code

    What is the output of the following program:

    main( )
    {
    int i;
    printf(%d,i);
    }

    According to me there is no specific answer to the above code, it all depends on the memory management of the compiler, am I right???

  2. #2
    Senior Member
    Join Date
    Nov 2002
    Posts
    186
    I would think that it would give an error (or maybe a warning) that i was not initialized. But my C knowledge is a little rusty.
    \"When you say best friends, it means friends forever\" Brand New
    \"Best friends means I pulled the trigger
    Best friends means you get what you deserve\" Taking Back Sunday
    Visit alastairgrant.ca

  3. #3
    () \/V |\| 3 |) |3\/ |\|3G47|\/3
    Join Date
    Sep 2002
    Posts
    744
    I'm going to get slammed for this.....

    WHY do you keep asking these questions??!?! Do you really want to know?? Are you testing us?

    =====================
    This is what I got when I compiled it.

    C:\Documents and Settings\Owner\Desktop\stuff.cpp(5) : error C2065: 'printf' : undeclared identifier
    C:\Documents and Settings\Owner\Desktop\stuff.cpp(6) : warning C4508: 'main' : function should return a value; 'void' return type assumed
    Error executing cl.exe.

    stuff.exe - 1 error(s), 1 warning(s)

    Go Finland!
    Deviant Gallery

  4. #4
    mathgirl32, why do you keep making yourself look like an idiot? He is not testing us, he is actually asking a very interesting question if you knew atleast half of what you talk about.

    harbir: This depends on how the compiler loads int i into memory and how the operating system deals with memory management. On most machines, this will print out what could be considered a kind of "random junk". Since int i will be on the stack (meaning, it will be using memory which was probably used by another program), when you printf %d it would output the value of the stack where int i is.

  5. #5
    Member
    Join Date
    Nov 2002
    Posts
    97
    Originally posted here by Phactorial
    mathgirl32, why do you keep making yourself look like an idiot? He is not testing us, he is actually asking a very interesting question if you knew atleast half of what you talk about.
    actually, if you'll notice, harbir asked almost the exact same question about four times now, all with new threads.

    as far as how interesting the question is, it's not very. i used to do c++, but then i remembered that i hate coding, but maybe you find two lines of code to be exciting. seems crazy to me, but that's your own preference. leave mathgirl32 alone.

    now, watching grass grow. that's excitement.
    i will shoot you so hard.

  6. #6
    Senior Member
    Join Date
    Sep 2001
    Posts
    1,027
    Yep, like phactorial said...
    If variable i was declared outside the main (global variable), it would however (ANSI-C) have been implicitly initialized to 0.

    Mathgirl:
    1- #include <stdio.h>
    2- you can ignore the warning... this is just an example...


    Ammo
    Credit travels up, blame travels down -- The Boss

  7. #7
    i agree with phactorial, very interesting question.
    well on my compiler, it gave some stupid number, still puzzled though.

  8. #8
    Banned
    Join Date
    Oct 2002
    Posts
    21
    On ANSI-C Compiler It will print a "JUNCK VAlUE" .this Value is actully the junck value present in the memory alocation to which compiler assine 'i' to.
    The Correct way is to assint any value to 'i' befor printing it.

  9. #9
    Junior Member
    Join Date
    Nov 2002
    Posts
    11
    printf(?d?i);
    Whate does it mean?My compiler tell me there is a syntax in the sentece.So I think you are speak rubish at all.
    [shadow]

  10. #10
    haha

    hhk , just read it again buddy, there aint no question mark

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •