Thread: redefinition of an int

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    84

    redefinition of an int

    I solved the problem generated by the below code by removing
    int but just out of interest why can you not redifine a data type.
    Sure there is an obvious reason but i cant think of it right now.

    I presume it is something about how the compiler works . Can
    anyone give me an explanation please?

    Code:
    #include<iostream>
    using namespace std;
    
    int main(void)
    {
    	int x=5;
    	cout << x << endl;
    
                    int x=2;	
    	cout << x << endl;
    
    	return(0);
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The definition of a variable declaration is to have the type of variable name you need followed by a name for the variable of that type. By doing what you are showing in the above code, you are telling the compiler that you want to create a new instance of an integer variable and call it the same thing as something you have already defined. YOU CAN'T DO THIS.

    Removing the keyword "int" from in front of the variable "x" in the second use leaves you with just "x = 5;" which in the syntax of C tells the compiler you are using a variable called "x" and storing the value 5 in that variable. This is how it should be in this case.

    I think this whole issue is simply a problem you may be having with the syntax of C. Other languages have different syntax and it may be perfectly acceptable in those languages to do what you have above, you just aren't allowed to do it using C.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Posts
    107
    Sure the compiler could use the same name for another variable. But that doesn't make the code any better if your variables have different meanings in different places. The maintainability would not be easy either.
    I am not using Dev-C++.
    #!/usr/bin/env python
    import sys;file=open(sys.argv[0]);print file.read();file.close()

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    just out of interest why can you not redifine a data type

    Well, if you think about it, why would you want to? Defining a variable happens when you specify it's type, just as hk_mp5kpdw said. if you want to change a variable's type half way through a function that's using it then there's something wrong with your code.

    When defining your variables, you're telling the compiler that you need space to store a particular piece of information. Does it really make sense to tell it AGAIN once it's been defined? I don't think it does.

    If you do want to do this kind of thing, then just use different scopes... like so:

    Code:
    #include<iostream>
    using namespace std;
    
    int main(void)
    {
        { // define a scope
            int x=5;
            cout << x << endl;
        } // end scope, therefore x doesn't exist any more
    
        { // define another scope
            int x=2;  // this is now fine since the above x no longer exists
            cout << x << endl;
        } // end scope again
    
        return(0);
    }
    hope that helps.

    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  3. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 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. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 07:45 AM