Thread: codeblocks compilation error, C Tutorial

  1. #1
    Registered User
    Join Date
    Sep 2010
    Location
    Eastern Nebraska
    Posts
    3

    Question codeblocks compilation error, C Tutorial

    In Windows I'm getting a compilation error in code blocks when doing the c tutorial. Wondering if someone may know why? I've even copy and pasted the code from the tutorial with the same return. However the program runs fine when compiled and run in the terminal in Linux.
    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;
    }
    Compile errors:
    ||=== Variable1, Debug ===|
    C:\Users\Corey\Documents\CProjects\Variable1\Varia ble1\main.c|7|error: expected '=', ',', ';', 'asm' or '__attribute__' before 'printf'|
    C:\Users\Corey\Documents\CProjects\Variable1\Varia ble1\main.c|8|error: 'this_is_a_number' undeclared (first use in this function)|
    C:\Users\Corey\Documents\CProjects\Variable1\Varia ble1\main.c|8|error: (Each undeclared identifier is reported only once|
    C:\Users\Corey\Documents\CProjects\Variable1\Varia ble1\main.c|8|error: for each function it appears in.)|
    ||=== Build finished: 4 errors, 0 warnings ===|

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Check your line endings.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Location
    Guangdong,China
    Posts
    2
    Code:
    int this_is_a_number;You forget ";" here

  4. #4
    Registered User
    Join Date
    Sep 2010
    Location
    Eastern Nebraska
    Posts
    3
    thanks guy's, can't beleive I did'nt see that. Must of had tunnel vision.

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    15
    Can you omit getchar () from the program? It doesn't seem to affect the program. If someone else could weigh in that would be great.

  6. #6
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    You could have read what the compiler said about it, it was quite clear.

    @lvl99, it's probably there to keep the terminal window open, to see the program results. I agree it is not needed there, since this is a console application. The IDE should have put a "pause" after the application terminated automatically, if it runs the app in new terminal window.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yes, you can omit the getchar() in this case.

    Since scanf() leaves behind a newline char, in the keyboard buffer, it's a good habit to get into having a getchar() right after each newline - but it's not always necessary, when all you want scanf() to get, is a number. Char's are the BIG problem for scanf(), when a newline is in the keyboard buffer from a previous scanf().

    You may also use getchar() to hold the console window open, so you can read the output, before the console window disappears. Whether or not the console window will disappear at the end of your C program, is up to your OS, and/or IDE.

  8. #8
    Registered User
    Join Date
    Sep 2010
    Location
    Eastern Nebraska
    Posts
    3
    Quote Originally Posted by Xupicor View Post
    You could have read what the compiler said about it, it was quite clear
    You are correct, it was quite clear and I see that now. Sorry for being such a newb. My mistake!

  9. #9
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    Nah, everyone was a newb at some point. You'll get familiar with compiler error messages, in time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Singly linked lists tutorial, problem with malloc
    By Protolocke in forum C Programming
    Replies: 13
    Last Post: 04-23-2010, 11:14 PM
  2. Codeblocks crashes linux?
    By Shakti in forum Tech Board
    Replies: 1
    Last Post: 03-25-2009, 07:26 AM
  3. My new website
    By joeprogrammer in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 03-17-2006, 07:38 PM
  4. Cprog tutorial: Design Patterns
    By maes in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2004, 01:41 AM
  5. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM