tags:

views:

137

answers:

4

What is the difference between:

const variable = 10;

and

const int variable = 10;

Does variable, per the standard, get interpreted as an integral type when no type is defined?

+8  A: 

const variable = 10 is not valid C++, while const int variable = 10; is.

The only time (that I can think of) that const variable = 10 would be valid is if you had a type named variable and you had a function with an unnamed parameter of that type, taking a default argument:

typedef int variable;
void foo(const variable = 10);
James McNellis
Hmm, works here! :)
0A0D
@Changeling: Are you using C or C++? There's a difference.
James McNellis
I have this in a header file and there is no typedef: `const MAX_RCV_TIMEOUT_COUNTER = 500/ML_RCV_THREAD_DELAY;`
0A0D
C++ - compiler specific?
0A0D
Then you have a C++ compiler that is silently accepting code that is actually very old, outdated C. If your C++ compiler does not at least emit a warning for `const variable = 10`, then you need to either crank up your warning level by quite a bit, or get a new compiler. This is a sign that you are likely missing lots of other potentially helpful warnings and errors.
Tyler McHenry
@Changeling Where is "here"? Which compiler?
anon
int is default (see my post above).
Mister Mystère
@Mister: In C90, yes. Not in C++.
James McNellis
@James: implicit ints are still allowed in C99 - you should get a warning though with `gcc -std=c99` (i.e. even without `-Wall`)
Paul R
@Neil: Borland C++ Builder 5.. yes it is old I know but I don't have the luxury of moving to a newer compiler.
0A0D
@Paul: I'm not so sure. From C99 §6.7.2/2: "At least one type specifier shall be given in the declaration specifiers in each declaration, and in the specifier-qualifier list in each struct declaration and type name." That wasn't present in C90. Comeau in C99 mode rejects typeless variable declarations with an error.
James McNellis
@James: according to http://en.wikipedia.org/wiki/C99 (far from authoritative, I know !): "C99 is, for the most part, backward compatible with C89 but is stricter in some ways.In particular, a declaration that lacks a type specifier no longer has int implicitly assumed. The C standards committee decided that it was of more value for compilers to diagnose inadvertent omission of the type specifier than to silently process legacy code that relied on implicit int. In practice, compilers are likely to display a warning while compiling, assume int and continue translating the program."
Paul R
@Changeling: gcc is up-to-date and free - why stick with an old, unsupported and non-standard compiler ?
Paul R
@Paul: Does gcc compile .dfm files from Borland C++ Builder 5 Delphi projects and support Quick Report?
0A0D
@Paul: Ok. We're both right :-) Implicit int was removed, and a compiler must issue a diagnostic, however, "After issuing the diagnostic, an implementation may choose to assume an implicit int and continue to translate the program in order to support existing source code that exploits this feature" (C99 rationale, same section).
James McNellis
@Changeling: maybe not, but it compiles C++ correctly, which your "Borland C++ Builder" evidently fails to do.
Paul R
@James: thanks for the further clarification - a great example of pragmatism. ;-)
Paul R
@Paul: In a perfect world, I would have more than two Software Engineers working in a Company as large as mine but alas I have to take what the world gives me :)
0A0D
@changling. I'm a trench digger. My preferred tool is a stick. I know it's slower and digs inferior trenches but I can't upgrade to a shovel. Our defined process requires us to pound the sides of the trench with the stick to make the sides more flush. How would I be able to pound the sides flush with a shovel? A team of two software engineers is the definition of agile. So it's not policy holding you back. You should never be too busy cutting the log to stop and "Sharpen the Saw". So you can't be too busy. Is it fear of change?
caspin
@Caspin: Two Software Engineers working on independent projects makes one :)
0A0D
A: 

With no strict rules (K&R C etc. Edit : i.e. old C), int is the type by default. It certainly does not mean the variable has no type, and it does not have anything to do with const.

Mister Mystère
Tyler McHenry
Many newbies mix up C and C++... And, more seriously, older C++ is based on older C (the one I'm talking about) : if his compiler is not up-to-date, it silently insert 'int' between const and variable.Edit : Apparently, even in C99 implicit ints are allowed... But in C++ it has to be old.
Mister Mystère
Right, but the C++ standard says that implicit ints are *not* allowed. If the compiler, when building C++ code, doesn't even emit a warning about that, something is wrong with the compiler or the way it's being invoked.
Tyler McHenry
@Tyler: I cranked up the warning level to ALL and it does not complain
0A0D
+1  A: 
const variable = 10;

won't compile in almost all new moderns C++ compilers.

Gustavo V
+2  A: 

It means that x is implicitly declared an int. This is not allowed in C++, but in C and to maintain compatibility with C headers or pre-ISO C++ code, a lot of contemporary C++compilers still support this as an option.

My GCC 4.4 compiler here groks "const x=3;" when feed -fms-extensions on the command line (the manual says, that it turns on a couple of lamps which are required to understand MFC code)

UPDATE: I've checked it with VS-2005, you can have implicit int if you use

#pragma warning(disable:4430)
Luther Blissett