Skip to main content
The 2024 Developer Survey results are live! See the results

Questions tagged [c]

C is a general-purpose programming language used for system programming (OS and embedded), libraries, games, and cross-platform. This tag should be used with general questions concerning the C language, as defined in the ISO 9899 standard (the latest version, 9899:2018, unless otherwise specified — also tag version-specific requests with c89, c99, c11, etc.). C is distinct from C++, and it should not be combined with the C++ tag without a specific reason.

c
2798 votes
30 answers
393k 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
680 votes
6 answers
297k 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
661 votes
10 answers
93k 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,627
918 votes
15 answers
103k 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,845
315 votes
13 answers
244k 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
135 votes
7 answers
152k 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.5k
495 votes
11 answers
111k 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
  • 5,963
1001 votes
11 answers
305k 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
  • 38.8k
398 votes
1 answer
638k 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 ...
844 votes
19 answers
466k 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.4k
345 votes
20 answers
104k 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,571
358 votes
15 answers
576k 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
  • 12.9k
867 votes
13 answers
299k 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
  • 25.8k
398 votes
17 answers
552k 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,539
1261 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
2983