#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Post Simple printf statment not working


    I have not been programming in C very long, but for some reason I can't even get a simple printf statement to work, even though it looks identical to another statement that does work.

    Code:
    #include <stdio.h>
    
    int main() {
    printf("Hi\n");
    return 0;
    }
    When I compile I get no error (compiling on Linux with gcc).
    When I run it there is no output at all. It returns to the next line waiting for another command (back to the ~$ prompt)

    Any ideas as to what I'm doing wrong? I can't figure it out.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,966
    Rep Power
    481
    Code:
    $ cat > c.c
    #include <stdio.h>
    
    int main() {
    printf("Hi\n");
    return 0;
    }
    $ gcc c.c
    $ ./a.out
    Hi
    $
    by the way, I recommend puts("Hi") rather than the long winded and inefficient printf("this string has to be scanned for format specifiers\n")

    Comments on this post

    • LightningBlade agrees
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0
    Originally Posted by b49P23TIvg
    by the way, I recommend puts("Hi") rather than the long winded and inefficient printf("this string has to be scanned for format specifiers\n")
    I'm actually trying to test an output for another program I'm writing, and I was putting it into a separate program (the other one won't compile yet) and it is a little more complex than just "Hi", but for some reason I couldn't get it to print anything, and so I tried several different things, and even "Hi" wouldn't print. So, that's when I came with the question.

    --Edit--
    I tried compiling it exactly like you did ($ gcc c.c) and it works. When I try compiling it like this ($ gcc -o test c.c) it doesn't. Is this normal, or do I have to compile to a.out all the time?
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,966
    Rep Power
    481
    test is ...


    $ which test
    /usr/bin/test


    Run your program with

    ./test

    or, worse, change your PATH variable.

    Comments on this post

    • Lux Perpetua agrees : I (and probably a lot of people) have made this exact mistake in the past.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0
    Thanks for your help.

IMN logo majestic logo threadwatch logo seochat tools logo