Thread: Difference between "int a;" and "int(a);"?

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    8

    Difference between "int a;" and "int(a);"?

    Hello!

    What's the difference between declaring a variable in these ways:

    1) int a;
    2) int(a);

    Both compile fine on GCC 3.4.5. If they are semantically identical, perhaps there any other differences, for instance in generated code?

    And one more thing, where is the C++ language specification online? Does it say anything about these two variants of variable declaration?


    Thanks,

    Anvar

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    My guess is that they are identical, although I will admit I have never seen the second before.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by President
    And one more thing, where is the C++ language specification online?
    Draft versions are available online for free, if you know where to find them. The final version in PDF format is available for about 30 USD from the online ANSI store.

    Quote Originally Posted by President
    Does it say anything about these two variants of variable declaration?
    Definitely, since it defines the grammar. Unfortunately, I am not certain of what it says in this case. My tracing of the grammar rules leads me to conclude that the syntax is invalid, but considering that the online Comeau compiler agrees with g++ 3.4.5, I am probably wrong.
    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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The meaning of int(a); is context sensitive. If a hasn't been declared, a becomes an int, and the parentheses are discarded. If a is an expression, then int(a); is a cast operation whose result is discarded, and the statement will have no effect.

    I prefer context free statements to context sensitive ones, if I can help it, since they are that much easier to parse.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    int(a); after int a; only gives a compile error with multiple definitions.
    For the cast to work, it has to be assigned to something...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Tags for this Thread