Undeclared/un-initialized variable

This is a discussion on Undeclared/un-initialized variable within the C++ Programming forums, part of the General Programming Boards category; What is the difference between an undeclared variable & an un-initialized variable? The example I have is Code: #include <stdio.h> ...

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    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,317
    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
    20,980
    k and i were not declared before use.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    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
    20,980
    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
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 05:49 AM
  3. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 11:07 AM
  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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21