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

This is a discussion on Difference between "int a;" and "int(a);"? within the C++ Programming forums, part of the General Programming Boards category; Hello! What's the difference between declaring a variable in these ways: 1) int a; 2) int(a); Both compile fine on ...

  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
    Posts
    22,915
    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
    22,159
    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.
    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

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    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
    Posts
    22,915
    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


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