46,441 questions
2825
votes
30
answers
414k
views
Should I cast the result of malloc (in C)?
In this question, someone suggested in a comment that I should not cast the result of malloc. i.e., I should do this:
int *sieve = malloc(sizeof(*sieve) * length);
rather than:
int *sieve = (int *) ...
682
votes
10
answers
103k
views
Undefined, unspecified and implementation-defined behavior
What is undefined behavior (UB) in C and C++? What about unspecified behavior and implementation-defined behavior? What is the difference between them?
319
votes
13
answers
253k
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 this warning:
(.text+0x34): warning: the `gets' function is dangerous and should not be used.
I remember this has ...
504
votes
11
answers
120k
views
What is array-to-pointer conversion aka. decay?
What is array-to-pointer conversion aka. decay? Is there any relation to array pointers?
398
votes
1
answer
698k
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 ...
352
votes
21
answers
107k
views
Why do I get a segmentation fault when writing to a "char *s" initialized with a string literal, but not "char s[]"?
The following code receives seg fault on line 2:
char *str = "string";
str[0] = 'z'; // could be also written as *str = 'z'
printf("%s\n", str);
While this works perfectly well:
char str[] = "...
878
votes
13
answers
310k
views
Why isn't sizeof for a struct equal to the sum of sizeof of each member?
Why does the sizeof operator return a size larger for a structure than the total sizes of the structure's members?
1280
votes
19
answers
1.0m
views
How do I use extern to share variables between source files?
I know that global variables in C sometimes have the extern keyword. What is an extern variable? What is the declaration like? What is its scope?
This is related to sharing variables across source ...
69
votes
5
answers
15k
views
Crash or 'segmentation fault' when data is copied/scanned/read to an uninitialized pointer
This question is meant to be used as reference for all frequently asked questions of the nature:
Why do I get unexpected behavior, usually a mysterious crash or 'segmentation fault,' when I assign/...
109
votes
3
answers
18k
views
Correctly allocating multi-dimensional arrays
The intent of this question is to provide a reference about how to correctly allocate multi-dimensional arrays dynamically in C. This is a topic often misunderstood and poorly explained even in some C ...
592
votes
14
answers
400k
views
What is the difference between char s[] and char *s?
In C, one can use a string literal in a declaration like this:
char s[] = "hello";
or like this:
char *s = "hello";
So what is the difference? I want to know what actually happens in terms of ...
709
votes
10
answers
542k
views
Why does printf not flush after the call unless a newline is in the format string?
Why does printf not flush after the call unless a newline is in the format string? Is this POSIX behavior? How might I have printf immediately flush every time?
294
votes
6
answers
590k
views
What is the behavior of integer division?
For example,
int result;
result = 125/100;
or
result = 43/100;
Will result always be the floor of the division? What is the defined behavior?
393
votes
11
answers
200k
views
Where do I find the current C or C++ standard documents?
For many questions the answer seems to be found in "the standard". However, where do we find that? Preferably online.
Googling can sometimes feel futile, again especially for the C standards, since ...
68
votes
10
answers
38k
views
How to access a local variable from a different function using pointers?
May I have any access to a local variable in a different function? If so, how?
void replaceNumberAndPrint(int array[3]) {
printf("%i\n", array[1]);
printf("%i\n", array[1]);
}
int * getArray(...
3173
votes
28
answers
1.7m
views
How to set, clear, and toggle a single bit
How can I set, clear, and toggle a bit?
1542
votes
12
answers
1.1m
views
How do function pointers in C work?
I had some experience lately with function pointers in C.
So going on with the tradition of answering your own questions, I decided to make a small summary of the very basics, for those who need a ...
360
votes
20
answers
46k
views
Why should I always enable compiler warnings?
I often hear that when compiling C and C++ programs I should "always enable compiler warnings". Why is this necessary? How do I do that?
Sometimes I also hear that I should "treat warnings as errors"...
16
votes
1
answer
5k
views
Why does clang produce inefficient asm with -O0 (for this simple floating point sum)?
I am disassembling this code on llvm clang Apple LLVM version 8.0.0 (clang-800.0.42.1):
int main() {
float a=0.151234;
float b=0.2;
float c=a+b;
printf("%f", c);
}
I compiled with no -...
155
votes
8
answers
42k
views
When and why will a compiler initialise memory to 0xCD, 0xDD, etc. on malloc/free/new/delete?
I know that the compiler will sometimes initialize memory with certain patterns such as 0xCD and 0xDD. What I want to know is when and why this happens.
When
Is this specific to the compiler used?
...
40
votes
4
answers
18k
views
What is the effect of trailing white space in a scanf() format string?
What is difference between scanf("%d") and scanf("%d ") in this code, where the difference is the trailing blank in the format string?
#include <stdio.h>
int main(void)
{
int i, j;
...
107
votes
7
answers
20k
views
When is using an uninitialized variable undefined behavior?
If I have:
unsigned int x;
x -= x;
it's clear that x should be zero after this expression, but everywhere I look, they say the behavior of this code is undefined, not merely the value of x (until ...
340
votes
11
answers
543k
views
Structure padding and packing
Consider:
struct mystruct_A
{
char a;
int b;
char c;
} x;
struct mystruct_B
{
int b;
char a;
} y;
The sizes of the structures are 12 and 8 respectively.
Are these structures padded ...
180
votes
9
answers
186k
views
What happens to a declared, uninitialized variable in C? Does it have a value?
If in C I write:
int num;
Before I assign anything to num, is the value of num indeterminate?
178
votes
7
answers
42k
views
Is short-circuiting logical operators mandated? And evaluation order?
Does the ANSI standard mandate the logical operators to be short-circuited, in either C or C++?
I'm confused for I recall the K&R book saying your code shouldn't depend on these operations being ...