All Questions

1883
votes
27answers
154517 views

Do I cast the result of malloc?

In this question, someone suggested in a comment that I should not cast the result of malloc, i.e. int *sieve = malloc(sizeof(int) * length); rather than: int *sieve = (int *) malloc(sizeof(int) *...
409
votes
5answers
163380 views

Why is “while ( !feof (file) )” always wrong?

I've seen people trying to read files like this in a lot of posts lately. Code #include <stdio.h> #include <stdlib.h> int main(int argc, char **argv) { char * path = argc > 1 ? a...
671
votes
13answers
54348 views

Why are these constructs (using ++) undefined behavior in C?

#include <stdio.h> int main(void) { int i = 0; i = i++ + ++i; printf("%d\n", i); // 3 i = 1; i = (i++); printf("%d\n", i); // 2 Should be 1, no ? volatile int u = 0; u...
401
votes
1answers
352660 views

The Definitive C Book Guide and List

This question attempts to collect a community-maintained list of quality books on the C programming language, targeted at various skill levels. C is a complex programming language that is difficult t...
504
votes
19answers
252204 views

What should main() return in C and C++?

What is the correct (most efficient) way to define the main() function in C and C++ — int main() or void main() — and why? If int main() then return 1 or return 0? There are numerous duplicate...
405
votes
8answers
35990 views

Undefined, unspecified and implementation-defined behavior

What is the difference between undefined, unspecified, and implementation-defined behavior in C and C++?
621
votes
9answers
147971 views

What is the strict aliasing rule?

When asking about common undefined behavior in C, souls more enlightened than I referred to the strict aliasing rule. What are they talking about?
249
votes
7answers
32468 views

What is array decaying?

What is decaying of an array? Is there any relation to array pointers?
151
votes
11answers
92763 views

Why is the gets function so dangerous that it should not be used?

When I try to compile C code that uses the gets() function with GCC, I get a warning: (.text+0x34): warning: the `gets' function is dangerous and should not be used. I remember this has someth...
204
votes
16answers
50431 views

Why do I get a segmentation fault when writing to a string initialized with "char *s" but not "char s[]"?

The following code receives seg fault on line 2: char *str = "string"; str[0] = 'z'; printf("%s", str); While this works perfectly well: char str[] = "string"; str[0] = 'z'; printf("%s...

Previous Next