Thread: Why do I get this error message when compiling this code?

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    4

    Why do I get this error message when compiling this code?

    The error message is this:
    test.c(9):warning *** 'auto' variable 'n' may be used before initialization at test.c(8)

    My code is this:
    Code:
    1   #include <gb/gb.h>
    2   #include <stdio.h>
    3    
    4   void main()
    5   {
    6       int n;
    7       while(1) {
    8           printf("%d\n",n);
    9           n++;
    10      }
    11  }
    Can you see anything in this code that would cause the compiler to generate the above error message?
    Last edited by Videogamer555; 09-26-2014 at 03:26 PM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    You attempt to print (and afterward increment) the value 'n' before it is given a value (and it is not initialized to anything).

    The warning told you so explicitly.

    Also: FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    4
    Quote Originally Posted by Matticus View Post
    You attempt to print (and afterward increment) the value 'n' before it is given a value (and it is not initialized to anything).

    The warning told you so explicitly.

    Also: FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com
    As I'm most familiar with Visual Basic, which always initializes numerical variables to 0 at the moment they are defined ("dim n as long in" VB is the same as "int n;" in C) so I was not aware that in C I would need a separate line like "n=0;" after the "int n;". I assumed that most programing languages automatically caused any variable to be equal to zero at the instant that the variable was defined.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    I found the following topic after a quick search - the top answer might give you some insight: c - why global variables are always initialized to '0', but not local variables? - Stack Overflow

    However, don't be tempted to declare your variables globally for this effect!

    And you don't need a separate line of code to initialize 'n' - just do:

    Code:
    int n = 0;

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Videogamer555 View Post
    I assumed that most programing languages automatically caused any variable to be equal to zero at the instant that the variable was defined.
    You assumed wrong. Most programming languages do not require initialisation of non-static variables. VB is one of the exceptions in this case, not C.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Sep 2014
    Posts
    1
    You should assign a value to the variable you defined. If you don't do this in the C programming language, you get an error while you are trying to print the variable you defined without assigning an initial value.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error message when compiling source code
    By vangodo in forum C Programming
    Replies: 19
    Last Post: 09-24-2011, 04:07 PM
  2. Replies: 3
    Last Post: 05-11-2011, 02:53 PM
  3. Replies: 0
    Last Post: 11-05-2010, 07:30 PM
  4. compiling my code, i can't fix error
    By howru in forum C Programming
    Replies: 2
    Last Post: 04-22-2008, 03:38 AM
  5. error message code
    By Flex in forum C Programming
    Replies: 1
    Last Post: 02-27-2002, 12:21 PM