Thread: declaration

  1. #1
    1337
    Join Date
    Jul 2008
    Posts
    135

    declaration

    Hi, yesterday i tried this
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int x = 0;
    
        if (x ==0)
         int y = 1;
    
        int z = y+x;
        printf("%d",z);
        getchar();
        return 0;
    }
    I am not very good at scope. But from what i understood about scope is it will have a difference scope if the declaration is outside of main.

    However in this case, i am declaring 'y' in the main() function, and why is it still saying that y is not declared in scope?

    Thanks

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by valthyx View Post
    However in this case, i am declaring 'y' in the main() function, and why is it still saying that y is not declared in scope?
    No, you're declaring it within the scope of the surrounding if-statement. It would be more obvious if you had used curly braces. Think about it -- if x had not been 0, then the "int y" line would not be hit, so now you have a variable that either exists or doesn't exist based on the value of some runtime data, which is impossible.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by valthyx
    However in this case, i am declaring 'y' in the main() function, and why is it still saying that y is not declared in scope?
    Because your if statement introduces another block with scope. In this case, that block of scope consists of just one statement. Perhaps using braces will clarify:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int x = 0;
    
        if (x ==0)
        {
            int y = 1;
        }
    
        int z = y+x;
        printf("%d",z);
        getchar();
        return 0;
    }
    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

  4. #4
    1337
    Join Date
    Jul 2008
    Posts
    135
    Is there any other way to declare or make it available globally?

    Or can i use y as a pointer here? So that it could be accessed outside the if scope.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Declaring y as a pointer will not change its scope. A pointer is a variable which is used to contain addresses.

    Another good practice would be to declare all the variables just after main so that it becomes available to the entire main block. Something like this

    [insert]
    Code:
    int main(void)
    {
    
       int x, y, z;
       float a,b;
       
       // body of other conditions like if-else
    
       return 0;
    }
    Also you can declare variables outside the main that makes it global . But AFAIK its not a good practice.

  6. #6
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Use extern keyword.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by roaan
    Another good practice would be to declare all the variables just after main so that it becomes available to the entire main block.
    That is not necessarily a good practice. Rather, declare variables near first use (and thus in the most restricted scope necessary), and if one may not declare variables mixed with code, then declare the variables at the start of the block in which they are first used. In this case, the block where y is first used is that of the main function, so declaring y at the start of the main function is fine.
    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

  8. #8
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    @ Laserlight

    Thanks for correcting :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 08:13 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM