All Questions

1857
votes
27answers
151687 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) *...
398
votes
5answers
161047 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...
662
votes
13answers
53709 views

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

#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
344711 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...
495
votes
19answers
248911 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...
400
votes
8answers
35101 views

Undefined, unspecified and implementation-defined behavior

What is the difference between undefined, unspecified, and implementation-defined behavior in C and C++?
617
votes
8answers
146190 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?
248
votes
7answers
31799 views

What is array decaying?

What is decaying of an array? Is there any relation to array pointers?
148
votes
11answers
90864 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...
201
votes
16answers
49807 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