Thread: printf error

  1. #1
    Registered User
    Join Date
    Apr 2008
    Location
    honolulu
    Posts
    2

    printf error

    hi, im very new to programming so sorry if my question seems a bit stupid

    i found a basic default code i wanted to try:

    Code:
    int main()
    {
      int this_is_a_number;
    
      printf( "please enter a number: " );
      scanf( "%d", &this_is_a_number );
      printf( "you entered %d", this_is_a_number );
      getchar();
    }
    and when i try to compile it it gives me this error:

    Error: 1 actual argument expected for printf, had 2

    the compiler says the problem is here:

    Code:
    printf( "you entered %d", this_is_a_number );
    i don't understand why though. any kind of help will be useful

  2. #2
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Code:
    #include <stdio.h>
    Put that at the top of your program.

    Notice the important changes in red:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      int this_is_a_number;
    
      printf( "please enter a number: " );
      fflush(stdout);
      scanf( "&#37;d", &this_is_a_number );
      printf( "you entered %d", this_is_a_number );
      getchar();
      
      return 0;
    }
    Last edited by JDGATX; 04-10-2008 at 02:21 PM.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Location
    honolulu
    Posts
    2
    ok thank you

  4. #4
    Registered User
    Join Date
    Apr 2008
    Location
    Sydney, Australia
    Posts
    6
    Hi All,

    JDGATX, why did you add the fflush(stdout) part? Cheers.

    coolboarderguy

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by coolboarderguy View Post
    Hi All,

    JDGATX, why did you add the fflush(stdout) part? Cheers.

    coolboarderguy
    Since there is no newline ('\n') at the end of the format string for the printf, there is no guarantee that printf will actually output anything before scanf() is reading data from the keyboard. Further, there is no guarantee that the second printf outputs anything before getchar() is performed either, so putting a fflush(stdout) after the second printf would guarantee that too.

    Many C runtime libraries will automatically flush stdout on request of input for stdin - but this is by no means guaranteed - it is typical undefined behaviour of the kind where nothing will go horribly wrong, but it will be annoying.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  2. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  3. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM