Thread: Simple question about the C programming tutorial #1

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    NE Washinton
    Posts
    38

    Simple question about the C programming tutorial #1

    well, I' running linux, and compiling with gcc, how I tried the variables example program in the first tutorial for C programming on this website, and something funny happens when I run the program. Here is the code exactly how it is copy/pasted from the tutorial

    Code:
    #include <stdio.h>
    
    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();
      return 0;
    }
    now when I compile this, and ./ the program, I get this...

    beatzz@AMD5200:~/haxor$ ./var
    Please enter a number: 22
    You entered 22beatzz@AMD5200:~/haxor$
    beatzz@AMD5200:~/haxor$


    see how after hitting enter, after entering the number 22, it outputs the next line of code along with my usual shell prompt?

    You entered 22beatzz@AMD5200:~/haxor$

    why? I have tried adding in a /n after the %d in the code, to no avail. No other idea, can someone provide some simple help for a simple question?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    After which %d did you add the \n?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And also, did you do /n (bad) or \n (good)?

  4. #4
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    Quote Originally Posted by elsheepo View Post
    beatzz@AMD5200:~/haxor$ ./var
    Hax0r sounds more leet.
    OS: Linux Mint 13(Maya) LTS 64 bit.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    Quote Originally Posted by happyclown View Post
    Hax0r sounds more leet.
    gave a benefit of the doubt....

  6. #6
    Registered User
    Join Date
    Mar 2008
    Location
    NE Washinton
    Posts
    38
    well, I don't remember if I did the correct /n or \n, but what I do know, is I compiled the exact code that was posted in the tutorial, and it came out looking like that. I will try messing with it a few times again this morning, see if I cant answer my own question, but hey thanks for all the comments It's nice to see a forum that is active!!! Perhaps someone that knows C well should edit the tutorial so that it is correct, not complaining, I LOVE the tutorials on this site, but perhaps together we can make them better

  7. #7
    Registered User
    Join Date
    Mar 2008
    Location
    NE Washinton
    Posts
    38
    okay, tabstop thank you for correcting me on the proper \n, that did separate the output from my prompt. so now I'm here
    Code:
    #include <stdio.h>
    
    int main()
    {
      int this_is_a_number;
    
      printf( "Please enter a number: " );
      scanf( "%d", &this_is_a_number );
      printf( "You entered %d\n", this_is_a_number );
      getchar();
      return 0;
    }
    Now, my next question is, why is it that it closes the program immediatly after it prints out the number that was inputed. Knowing that
    Code:
    getchar();
    should require the user hitting enter before closing the program. Hmmm..

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by elsheepo View Post
    Now, my next question is, why is it that it closes the program immediatly after it prints out the number that was inputed. Knowing that
    Code:
    getchar();
    should require the user hitting enter before closing the program. Hmmm..
    Very perceptive. This is a nuisance element of getchar(), at least in linux. You almost always have to use it like this:
    Code:
    if (getchar()=='\n') getchar();
    Because there is so often a trailing newline from some previous operation, in this case scanf. For example, this will sort of solve the problem too:
    Code:
    scanf( "%d\n", &this_is_a_number );
    But it causes another one.

    Using fflush(stdout) or fflush(stdin) does not work because you can't erase the newline in the console buffer (which means it is beyond the reach of your program) -- again, this may be a linux specific issue (don't worry, there aren't many in C).

    What a curious exercise.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Mar 2008
    Location
    NE Washinton
    Posts
    38
    well well well, after 2 solid days of trying different combinations of code, compiling, and forum posting, and thanks to tips of MK27 and tabstop, I have solved the problem, and gotten to the desired solution here is the code that gives you the program your really looking for. Perhaps someone who has admin privileges on the website should edit the tutorial to look like this
    Code:
    #include <stdio.h>
    
    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 );
      if (getchar()=='\n') getchar();
      return 0;
    }
    at least for linux users perhaps which I'd imagine is the majority of coders anyhow. Anyways thanks guys for the help! Till next thread!!!!!
    Last edited by elsheepo; 01-19-2009 at 06:58 PM.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by elsheepo View Post
    for linux users perhaps which I'd imagine is the majority of coders anyhow.
    Don't be so sure. Some of them may have failed to form proper allegiances. And if they come up with enough games and video drivers, this is all bound to change.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Mar 2008
    Location
    NE Washinton
    Posts
    38
    lol indeed. I figured I should have taken that out when I edited it, I don't meant to offend any Windows coders :x much <3 for the ease and....well, I got much <3 for the ease of windows

  12. #12
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    Quote Originally Posted by MK27 View Post
    And if they come up with enough games and video drivers, this is all bound to change.
    + USB printer drivers
    + USB modem drivers
    + USB external device drivers

    If linux was like Windows XP, I'd give it a go!
    OS: Linux Mint 13(Maya) LTS 64 bit.

  13. #13
    Registered User
    Join Date
    Mar 2008
    Location
    NE Washinton
    Posts
    38
    Those are the first things on my "to do" list once i learn how to program!!!

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I have a USB mp3 stick I use under linux.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM