Questions tagged [c99]

This tag is for questions regarding the International Standard ISO 9899:1999, aka "C99", with technical corrigenda, and for questions about code written in C99 (as opposed to K&R C, C89 or later C Standard revisions like the 2011 revision C11).

This tag is for questions regarding the International Standard ISO 9899:1999 , aka "C99", with technical corrigenda, and for questions about code written in C99 (as opposed to K&R C, C89 or later C Standard revisions like the 2011 revision C11).

Always use the tag for all your C questions, then complement it with the tags for questions that are specific to this version of the standard.

1900 questions
67
votes
5 answers

How does the compiler allocate memory without knowing the size at compile time?

I wrote a C program that accepts integer input from the user, that is used as the size of an integer array, and using that value it declares an array of given size, and I am confirming it by checking the size of the array. Code: #include…
Rahul
  • 975
  • 9
  • 25
64
votes
6 answers

Is there any reason not to use fixed width integer types (e.g. uint8_t)?

Assuming you're using a compiler that supports C99 (or even just stdint.h), is there any reason not to use fixed-width integer types such as uint8_t? One reason that I'm aware of is that it makes much more sense to use chars when dealing with…
DanielGibbs
  • 9,910
  • 11
  • 76
  • 121
63
votes
6 answers

Does the C preprocessor strip comments or expand macros first?

Consider this (horrible, terrible, no good, very bad) code structure: #define foo(x) // commented out debugging code // Misformatted to not obscure the point if (a) foo(a); bar(a); I've seen two compilers' preprocessors generate different results…
Phil Miller
  • 36,389
  • 13
  • 67
  • 90
61
votes
9 answers

What is the correct type for array indexes in C?

What type for array index in C99 should be used? It have to work on LP32, ILP32, ILP64, LP64, LLP64 and more. It doesn't have to be a C89 type. I have found 5 candidates: size_t ptrdiff_t intptr_t / uintptr_t int_fast*_t / uint_fast*_t int_least*_t…
Michas
  • 8,534
  • 6
  • 38
  • 62
59
votes
4 answers

What is the difference between intXX_t and int_fastXX_t?

I have recently discovered existence of standard fastest type, mainly int_fast32_t and int_fast64_t. I was always told that, for normal use on mainstream architecture, one should better use classical int & long which should always fit to the…
Coren
  • 5,517
  • 1
  • 21
  • 34
58
votes
9 answers

Smart pointers/safe memory management for C?

I, and I think many others, have had great success using smart pointers to wrap up unsafe memory operations in C++, using things like RAII, et cetera. However, wrapping memory management is easier to implement when you have destructors, classes,…
mannicken
  • 6,885
  • 4
  • 31
  • 38
57
votes
14 answers

Which functions in the C standard library commonly encourage bad practice?

This is inspired by this question and the comments on one particular answer in that I learnt that strncpy is not a very safe string handling function in C and that it pads zeros, until it reaches n, something I was unaware of. Specifically, to quote…
user257111
57
votes
5 answers

Does a[a[0]] = 1 produce undefined behavior?

Does this C99 code produce undefined behavior? #include int main() { int a[3] = {0, 0, 0}; a[a[0]] = 1; printf("a[0] = %d\n", a[0]); return 0; } In the statement a[a[0]] = 1; , a[0] is both read and modified. I looked n1124 draft…
Masaki Hara
  • 3,295
  • 21
  • 21
57
votes
4 answers

What are the incompatible differences between C(99) and C++(11)?

This question was triggered by replie(s) to a post by Herb Sutter where he explained MS's decision to not support/make a C99 compiler but just go with the C(99) features that are in the C++(11) standard anyway. One commenter replied: (...) C is…
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
55
votes
6 answers

Are prototypes required for all functions in C89, C90 or C99?

To be truly standards-compliant, must all functions in C (except for main) have a prototype, even if they are only used after their definition in the same translation unit?
Sydius
  • 13,567
  • 17
  • 59
  • 76
54
votes
3 answers

In C99, is f()+g() undefined or merely unspecified?

I used to think that in C99, even if the side-effects of functions f and g interfered, and although the expression f() + g() does not contain a sequence point, f and g would contain some, so the behavior would be unspecified: either f() would be…
Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
53
votes
7 answers

Anonymous union within struct not in c99?

here is very simplified code of problem I have: enum node_type { t_int, t_double }; struct int_node { int value; }; struct double_node { double value; }; struct node { enum node_type type; union { struct int_node…
Martin
  • 1,473
  • 2
  • 12
  • 20
53
votes
1 answer

Maximum size of size_t

I know in C return type of sizeof operator is size_t being unsigned integer type defined in . Which means max size of it should be 65535 as stated in C99 standard 7.18.3: limit of size_t SIZE_MAX 65535 However in gcc-4.8.2…
Sunil Bojanapally
  • 12,528
  • 4
  • 33
  • 46
52
votes
2 answers

Is there a document describing how Clang handles excess floating-point precision?

It is nearly impossible(*) to provide strict IEEE 754 semantics at reasonable cost when the only floating-point instructions one is allowed to used are the 387 ones. It is particularly hard when one wishes to keep the FPU working on the full 64-bit…
Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
51
votes
8 answers

How to wrap printf() into a function or macro?

This might sound like an interview question but is actually a practical problem. I am working with an embedded platform, and have available only the equivalents of those functions: printf() snprintf() Furthermore, the printf() implementation (and…
Vorac
  • 8,726
  • 11
  • 58
  • 101