Skip to main content
Filter by
Sorted by
Tagged with
2825 votes
30 answers
414k views

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 *) ...
Patrick McDonald's user avatar
682 votes
10 answers
103k views

What is undefined behavior (UB) in C and C++? What about unspecified behavior and implementation-defined behavior? What is the difference between them?
Zolomon's user avatar
  • 9,937
319 votes
13 answers
253k views

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 ...
Vinit Dhatrak's user avatar
504 votes
11 answers
120k views

What is array-to-pointer conversion aka. decay? Is there any relation to array pointers?
Vamsi's user avatar
  • 6,063
398 votes
1 answer
698k views

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

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[] = "...
Markus's user avatar
  • 3,641
878 votes
13 answers
310k views

Why does the sizeof operator return a size larger for a structure than the total sizes of the structure's members?
Kevin's user avatar
  • 26.2k
1280 votes
19 answers
1.0m views

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 ...
user avatar
69 votes
5 answers
15k views

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

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 ...
Lundin's user avatar
  • 220k
592 votes
14 answers
400k views

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 ...
user avatar
709 votes
10 answers
542k views

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?
Crazy Chenz's user avatar
  • 13.3k
294 votes
6 answers
590k views

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?
T.T.T.'s user avatar
  • 34.8k
393 votes
11 answers
200k views

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

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(...
Radek Simko's user avatar
  • 16.2k
3173 votes
28 answers
1.7m views

How can I set, clear, and toggle a bit?
JeffV's user avatar
  • 54.8k
1542 votes
12 answers
1.1m views

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

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"...
n. m. could be an AI's user avatar
16 votes
1 answer
5k views

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 -...
Stefano Borini's user avatar
155 votes
8 answers
42k views

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? ...
LeopardSkinPillBoxHat's user avatar
40 votes
4 answers
18k views

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; ...
Vikas Verma's user avatar
  • 3,746
107 votes
7 answers
20k views

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 ...
user541686's user avatar
  • 212k
340 votes
11 answers
543k views

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 ...
Manu's user avatar
  • 5,834
180 votes
9 answers
186k views

If in C I write: int num; Before I assign anything to num, is the value of num indeterminate?
rxmnnxfpvg's user avatar
  • 31.2k
178 votes
7 answers
42k views

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 ...
Joe Pineda's user avatar
  • 5,711

1
2 3 4 5
929