2

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?

  • Implicit ints are generally frowned upon (at least by me anyway) – Paul R Jul 13 '10 at 15:31
  • 2
    @Paul Implicit int doesn't happen in this case. –  Jul 13 '10 at 15:33
  • @Neil: I'm assuming he's actually compiling C rather than C++ as tagged (otherwise his compiler is somewhat broken) – Paul R Jul 13 '10 at 15:36
  • I just have to point out ... int = integer / int != integral though it would be interesting if someone could figure out a way to store integrals as a primitive data type :D – Shaded Jul 13 '10 at 15:42
  • I am compiling C++ I am using Borland C++ Builder –  Jul 13 '10 at 15:43
  • 3
    @Changeling: time to get a decent compiler then. – Paul R Jul 13 '10 at 15:56
  • 1
    @Shaded: Integral has more than one meaning. Any type that holds an integer value is called an integral type. – GManNickG Jul 13 '10 at 16:04
  • @GMan: hmm so it is... I thought it was just a typo. Well I guess I've learned something new today! – Shaded Jul 13 '10 at 16:14

4 Answers4

8

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
  • 348,265
  • 75
  • 913
  • 977
  • @Changeling: Are you using C or C++? There's a difference. – James McNellis Jul 13 '10 at 15:33
  • I have this in a header file and there is no typedef: `const MAX_RCV_TIMEOUT_COUNTER = 500/ML_RCV_THREAD_DELAY;` –  Jul 13 '10 at 15:33
  • 8
    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 Jul 13 '10 at 15:33
  • @Changeling Where is "here"? Which compiler? –  Jul 13 '10 at 15:33
  • int is default (see my post above). – Mister Mystère Jul 13 '10 at 15:34
  • @Mister: In C90, yes. Not in C++. – James McNellis Jul 13 '10 at 15:34
  • @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 Jul 13 '10 at 15:38
  • @Neil: Borland C++ Builder 5.. yes it is old I know but I don't have the luxury of moving to a newer compiler. –  Jul 13 '10 at 15:47
  • @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 Jul 13 '10 at 15:48
  • @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 Jul 13 '10 at 15:54
  • 2
    @Changeling: gcc is up-to-date and free - why stick with an old, unsupported and non-standard compiler ? – Paul R Jul 13 '10 at 15:56
  • @Paul: Does gcc compile .dfm files from Borland C++ Builder 5 Delphi projects and support Quick Report? –  Jul 13 '10 at 15:59
  • 1
    @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 Jul 13 '10 at 16:03
  • @Changeling: maybe not, but it compiles C++ correctly, which your "Borland C++ Builder" evidently fails to do. – Paul R Jul 13 '10 at 16:03
  • @James: thanks for the further clarification - a great example of pragmatism. ;-) – Paul R Jul 13 '10 at 16:05
  • @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 :) –  Jul 13 '10 at 16:07
  • 2
    @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? – deft_code Jul 13 '10 at 16:30
  • @Caspin: Two Software Engineers working on independent projects makes one :) –  Jul 13 '10 at 16:39
2

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)
Nordic Mainframe
  • 28,058
  • 10
  • 66
  • 83
1
const variable = 10;

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

Gustavo V
  • 152
  • 1
  • 1
  • 4
0

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
  • 952
  • 2
  • 16
  • 39
  • 2
    The question is tagged C++. K&R C has (or at least should have) nothing to do with it. – Tyler McHenry Jul 13 '10 at 15:34
  • 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 Jul 13 '10 at 15:41
  • 2
    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 Jul 13 '10 at 15:45
  • @Tyler: I cranked up the warning level to ALL and it does not complain –  Jul 13 '10 at 15:56