Constants having types in C
Constants having types in C
..
Last edited by Perica on Tue Dec 05, 2006 9:36 pm, edited 1 time in total.
Re:Constants having types in C
The term is "integer suffix", or "floating suffix". It's been in there since K&R, well before C++ came into existence IIRC.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)?
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.)Why does this requirement exist in the language at all?
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.
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....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").
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.
Every good solution is obvious once you've found it.
Re:Constants having types in C
..
Last edited by Perica on Tue Dec 05, 2006 9:36 pm, edited 1 time in total.
Re:Constants having types in C
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].
Every good solution is obvious once you've found it.
Re:Constants having types in C
..
Last edited by Perica on Tue Dec 05, 2006 9:37 pm, edited 1 time in total.
Re:Constants having types in C
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.Perica wrote: By the way: is C99 just a short name for the C or C++ standards (or both :-\)?
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++...
Every good solution is obvious once you've found it.
Re:Constants having types in C
..
Last edited by Perica on Tue Dec 05, 2006 9:37 pm, edited 1 time in total.
- Colonel Kernel
- Member
- Posts: 1437
- Joined: Tue Oct 17, 2006 6:06 pm
- Location: Vancouver, BC, Canada
- Contact:
Re:Constants having types in C
Although I'm not familiar with it, I've seen Comeau mentioned a lot on comp.lang.c++.moderated.Which compiler is it? OpenWatcom? The Intel C/C++ Compiler? ... ?
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.Concerning GCC and Visual C++: do they just have minor implementation errors, or something major?
Top three reasons why my OS project died:
- Too much overtime at work
- Got married
- My brain got stuck in an infinite loop while trying to design the memory manager
Re:Constants having types in C
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.
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.
Every good solution is obvious once you've found it.
Re:Constants having types in C
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!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.
Note, this makes the user count one higher, probably one in total right now.