46,442 questions
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 *) ...
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 ...
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?
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 ...
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 ...
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("...
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?
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?
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?
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[] = "...
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)) {
...
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?
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 ...
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 ...