Skip to main content
2827 votes
30 answers
413k 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 *) ...
Patrick McDonald's user avatar
683 votes
6 answers
305k views

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

What is wrong with using feof() to control a read loop? For example: #include <stdio.h> #include <stdlib.h> int main(int argc, char **argv) { char *path = "stdin"; FILE ...
William Pursell's user avatar
682 votes
10 answers
102k 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?
Zolomon's user avatar
  • 9,937
925 votes
15 answers
107k views

Why are these constructs using pre and post-increment 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 ...
PiX's user avatar
  • 9,925
318 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 ...
Vinit Dhatrak's user avatar
140 votes
7 answers
160k views

scanf() leaves the newline character in the buffer

I have the following program: int main(int argc, char *argv[]) { int a, b; char c1, c2; printf("Enter something: "); scanf("%d", &a); // line 1 printf("...
ipkiss's user avatar
  • 13.7k
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?
Vamsi's user avatar
  • 6,063
1024 votes
11 answers
328k views

What is the strict aliasing rule?

When asking about common undefined behavior in C, people sometimes refer to the strict aliasing rule. What are they talking about?
Benoit's user avatar
  • 39.4k
398 votes
1 answer
696k 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 ...
855 votes
19 answers
483k 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? And how about the arguments? If int main() then return 1 or return 0?
Joel's user avatar
  • 15.6k
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[] = "...
Markus's user avatar
  • 3,641
371 votes
16 answers
605k views

Removing trailing newline character from fgets() input

I am trying to get some data from the user and send it to another function in gcc. The code is something like this. printf("Enter your Name: "); if (!(fgets(Name, sizeof Name, stdin) != NULL)) { ...
sfactor's user avatar
  • 13.1k
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?
Kevin's user avatar
  • 26.2k
401 votes
18 answers
568k views

How to find the size of an array (from a pointer pointing to the first element array)?

First off, here is some code: int main() { int days[] = {1,2,3,4,5}; int *ptr = days; printf("%u\n", sizeof(days)); printf("%u\n", sizeof(ptr)); return 0; } Is there a way to ...
jkidv's user avatar
  • 4,569
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 ...
user avatar

15 30 50 per page
1
2 3 4 5
3097