Page 1 of 1

Constants having types in C

Posted: Sun Jan 16, 2005 9:13 pm
by Perica
..

Re:Constants having types in C

Posted: Mon Jan 17, 2005 1:34 am
by Solar
Perica wrote: What is the point of appending an: L, f, etcetera; to constants in C (which I call "constant data-typing" ... I don't know the official term for it)?
The term is "integer suffix", or "floating suffix". It's been in there since K&R, well before C++ came into existence IIRC.
Why does this requirement exist in the language at all?
First off, you seldom have any need for it. If you provide a constant without suffix, the compiler just choses an appropriate type. (The smallest of [tt]int[/tt], [tt]long int[/tt] or - in C99 - [tt]long long int[/tt] that can hold the value, or [tt]double[/tt] for floating point values.)

Sometimes, however, you would like to have e.g. a -1 represented as [tt]long int[/tt], or a [tt]FLOAT_MAX[/tt] that is actually a [tt]float[/tt] instead of a [tt]double[/tt], and that's where the suffixes come into play.

C is a type-safe language, if not as safe as C++. And in a type-safe language, you need primitives to set the type correctly, so that you can run the compiler in its strictest error-checking mode without hundreds of "normal" type warnings getting into the way.
...this curiosity popped up when writing some C code that uses the type definitions in the stdint.h header of the standard C library - I just find it a neusance having to use the UINT*_C and INT*_C macros all over the place, in order to maintain compiler compatibility - all because of this "constant data-typing").
The [tt]UINTN_C[/tt] and [tt]INTN_C[/tt] macros expand a given constant to type uint_leastN_t and int_leastN_t, respectively. If you are using those, you seem to have very specific requirements for the types of your data, or you would just work with the basic [tt]int[/tt] type. I think that answers your question in itself.

Bottom line, suffixes are helpful if you have to take care of exact types. If you don't care, you don't have to use them.

Re:Constants having types in C

Posted: Wed Jan 19, 2005 8:45 am
by Perica
..

Re:Constants having types in C

Posted: Thu Jan 20, 2005 3:27 am
by Solar
Use [tt]gcc --std=c99[/tt]... prior to C99, <stdint.h>, uint64_t and the "LL" suffix weren't part of the standard, and heavens only know what GCC makes of it when you omit the [tt]--std=c99[/tt]. ;-)

Re:Constants having types in C

Posted: Sat Jan 22, 2005 5:34 am
by Perica
..

Re:Constants having types in C

Posted: Sat Jan 22, 2005 1:54 pm
by Solar
Perica wrote: By the way: is C99 just a short name for the C or C++ standards (or both :-\)?
ANSI C (X3.159-1989) was ratified in 1989 and published in 1990, followed up by the (identical) ISO standard ISO/IEC 9899:1990. This (including technical corrigenda) is what GCC does with --std=c89.

The standard was amended in 1995, which is known as AMD1 and done when you tell GCC --std=iso9899:199409.

The latest edition of the standard is ISO/IEC 9899:1999, or C99 for short, which GCC can be told to implement with --std=c99 or --std=iso9899:1999.

C++ had only one standard released so far, in 1998. But heads up, a new edition of that is coming - and there's only one compiler that correctly implements the 1998 standard, and it ain't either GCC or Microsoft VC++... ;-)

Re:Constants having types in C

Posted: Sun Jan 23, 2005 8:34 pm
by Perica
..

Re:Constants having types in C

Posted: Mon Jan 24, 2005 12:06 am
by Colonel Kernel
Which compiler is it? OpenWatcom? The Intel C/C++ Compiler? ... ?
Although I'm not familiar with it, I've seen Comeau mentioned a lot on comp.lang.c++.moderated.
Concerning GCC and Visual C++: do they just have minor implementation errors, or something major?
I can't speak to GCC's conformance, but here is some info about VC++ 7.1's conformance, straight from the horse's mouth.

Re:Constants having types in C

Posted: Mon Jan 24, 2005 1:36 am
by Solar
Several minor flaws aside, the Edison Design Group (EDG) have the only compiler core that implements template exports. (For all other compilers, you have to write templates in header files, breaking the rule that headers should provide declarations only.)

Interesting enough, they were also the ones most adamantly opposing this feature in the standards committee, since it's a ***** to implement. But once it was decided, they implemented it, while the rest went along with a "nobody needs this anyway" - which might be because no-one even knows it should be possible in the first line. ;)

Re:Constants having types in C

Posted: Mon Jan 24, 2005 9:09 am
by Candy
Solar wrote: Interesting enough, they were also the ones most adamantly opposing this feature in the standards committee, since it's a ***** to implement. But once it was decided, they implemented it, while the rest went along with a "nobody needs this anyway" - which might be because no-one even knows it should be possible in the first line. ;)
If I'm not mistaking (checking it right now) this is EXACTLY what I thought C++ didn't have and that I really did want. Thank you! :)

Note, this makes the user count one higher, probably one in total right now.