Thread: Undeclared/un-initialized variable

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    13

    Undeclared/un-initialized variable

    What is the difference between an undeclared variable & an un-initialized variable?

    The example I have is

    Code:
    #include <stdio.h>
    main() {
         int prime = 21;
    
         for (k = 1; i < prime; k++) {
              if (k < prime) {
                   printf("k is less than prime.\n");
              }
         }
    }
    That is the example that I have been given (I have to tell what is wrong with it) and I can see what the problem is, I just am not sure if it's undeclared or uninitialized because I'm not sure of the difference.

    thanks.
    Last edited by jadedreality; 10-25-2007 at 08:26 PM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Uninitialized:
    Code:
    int prime;
    Initialized:
    Code:
    int prime = 21;
    Avoid unitialized variables because they can have any value. Instead initialize them before using them. If you're assigning them directly without using it, that's fine. Some compilers will generate a warning if you don't initialize variables and some compilers will also throw an error if you use an uninitialized variable when running your app.

    But what is this undeclared? Variables are declared before you use them:
    Code:
    int prime = 21;
    Variable prime is now declared. You can't use variables without declaring them first:
    Code:
    int x;
    x = 7; // OK, variable declared.
    y = 6; // Not OK, variable not declared; compiler will complain.
    Or was there something else you wanted to know?

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    13
    Quote Originally Posted by Elysia View Post
    Uninitialized:
    Code:
    int prime;
    Initialized:
    Code:
    int prime = 21;
    Avoid unitialized variables because they can have any value. Instead initialize them before using them. If you're assigning them directly without using it, that's fine. Some compilers will generate a warning if you don't initialize variables and some compilers will also throw an error if you use an uninitialized variable when running your app.

    But what is this undeclared? Variables are declared before you use them:
    Code:
    int prime = 21;
    Variable prime is now declared. You can't use variables without declaring them first:
    Code:
    int x;
    x = 7; // OK, variable declared.
    y = 6; // Not OK, variable not declared; compiler will complain.
    Or was there something else you wanted to know?



    Thanks, that helped .

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    So is the problem an uninitialized variable or an undeclared variable? Where is the problem?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    k and i were not declared before use.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I think this was a homework assignment, so perhaps just giving the answer might not be the best idea.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I think this was a homework assignment, so perhaps just giving the answer might not be the best idea.
    hmm... I assumed that you were glossing over the OP's post since the OP already replied "Thanks, that helped ."

    It could be a trick to get later users to answer the question outright, but if so, you made me fall for jadedreality's trick
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to put a check on an extern variable set as flag
    By rebelsoul in forum C Programming
    Replies: 18
    Last Post: 05-25-2009, 03:13 AM
  2. how do I know a variable is not initialized?
    By patiobarbecue in forum C++ Programming
    Replies: 7
    Last Post: 01-17-2009, 06:49 AM
  3. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  4. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  5. Replies: 2
    Last Post: 04-12-2004, 01:37 AM