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.

Filter by
Sorted by
Tagged with
2770 votes
29 answers
370k views

Do I cast the result of malloc?

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
673 votes
5 answers
288k 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
649 votes
9 answers
86k 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,409
910 votes
15 answers
99k 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,745
307 votes
13 answers
232k 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
131 votes
7 answers
141k 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.3k
490 votes
11 answers
102k 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,903
976 votes
11 answers
288k 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
  • 38k
400 votes
1 answer
593k 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 ...
835 votes
19 answers
450k 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.2k
338 votes
19 answers
100k 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,501
346 votes
15 answers
541k 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.6k
852 votes
13 answers
287k 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.2k
393 votes
17 answers
532k 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,489
1229 votes
19 answers
952k 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
251 votes
11 answers
633k views

How do I properly compare strings in C?

I am trying to get a program to let a user enter a word or character, store it, and then print it until the user types it again, exiting the program. My code looks like this: #include <stdio.h> ...
nmagerko's user avatar
  • 6,626
68 votes
5 answers
14k 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 a mysterious crash or "segmentation fault" when I copy/scan data to the address where an ...
102 votes
7 answers
63k views

Using fflush(stdin)

So a quick Google search for fflush(stdin) for clearing the input buffer reveals numerous websites warning against using it. And yet that's exactly how my CS professor taught the class to do it. How ...
wrongusername's user avatar
101 votes
2 answers
14k 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 ...
Lundin's user avatar
  • 196k
98 votes
7 answers
90k views

srand() — why call it only once?

This question is about a comment in this question Recommended way to initialize srand? The first comment says that srand() should be called only ONCE in an application. Why is it so?
Lipika Deka's user avatar
  • 3,784
579 votes
14 answers
392k 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 ...
user avatar
687 votes
10 answers
482k 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?
Crazy Chenz's user avatar
  • 12.7k
264 votes
9 answers
102k views

Is an array name a pointer?

Is an array's name a pointer in C? If not, what is the difference between an array's name and a pointer variable?
user avatar
1033 votes
27 answers
459k views

What is the difference between a definition and a declaration?

The meaning of both eludes me.
Maciek's user avatar
  • 19.5k
273 votes
6 answers
516k 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?
T.T.T.'s user avatar
  • 33.5k
1797 votes
20 answers
128k views

With arrays, why is it the case that a[5] == 5[a]?

As Joel points out in Stack Overflow podcast #34, in C Programming Language (aka: K & R), there is mention of this property of arrays in C: a[5] == 5[a] Joel says that it's because of pointer ...
Dinah's user avatar
  • 53k
394 votes
11 answers
174k 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 ...
1774 votes
23 answers
702k views

What is the difference between const int*, const int * const, and int const *?

I always mess up how to use const int*, const int * const, and int const * correctly. Is there a set of rules defining what you can and cannot do? I want to know all the do's and all don'ts in terms ...
user avatar
67 votes
10 answers
37k 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(...
Radek Simko's user avatar
  • 15.9k
3113 votes
27 answers
1.6m views

How to set, clear, and toggle a single bit

How can I set, clear, and toggle a bit?
JeffV's user avatar
  • 53.1k
211 votes
8 answers
56k views

What does the comma operator , do?

What does the , operator do in C?
lillq's user avatar
  • 14.8k
1484 votes
12 answers
983k 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 ...
3050 votes
30 answers
872k views

What is the difference between #include <filename> and #include "filename"?

What is the difference between using angle brackets and quotes in an include directive? #include <filename> #include "filename"
quest49's user avatar
  • 49.7k
240 votes
12 answers
58k views

How dangerous is it to access an array out of bounds?

How dangerous is accessing an array outside of its bounds (in C)? It can sometimes happen that I read from outside the array (I now understand I then access memory used by some other parts of my ...
ChrisD's user avatar
  • 2,417
151 votes
8 answers
38k 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? ...
LeopardSkinPillBoxHat's user avatar
2111 votes
17 answers
1.2m views

What is the effect of extern "C" in C++?

What exactly does putting extern "C" into C++ code do? For example: extern "C" { void foo(); }
Litherum's user avatar
  • 22.7k
351 votes
21 answers
40k 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"...
n. m. could be an AI's user avatar
95 votes
15 answers
48k views

Is it a good idea to typedef pointers?

I looked through some code and noticed that the convention was to turn pointer types like SomeStruct* into typedef SomeStruct* pSomeStruct; Is there any merit to this?
Unknown's user avatar
  • 46k
12 votes
1 answer
3k 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 ...
Stefano Borini's user avatar
1385 votes
25 answers
3.2m views

How do I determine the size of my array in C?

How do I determine the size of my array in C? That is, the number of elements the array can hold?
Mark Harrison's user avatar
134 votes
5 answers
41k views

Implicit type promotion rules

This post is meant to be used as a FAQ regarding implicit integer promotion in C, particularly implicit promotion caused by the usual arithmetic conversions and/or the integer promotions. Example 1) ...
Lundin's user avatar
  • 196k
22 votes
7 answers
27k views

fgets doesn't work after scanf [duplicate]

#include <stdio.h> #include <string.h> #include <ctype.h> void delspace(char *str); int main() { int i, loops; char s1[101], s2[101]; scanf("%d", &loops); ...
Vayn's user avatar
  • 2,517
175 votes
9 answers
174k 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?
atp's user avatar
  • 30.3k
38 votes
4 answers
17k 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; ...
Vikas Verma's user avatar
  • 3,636
319 votes
11 answers
472k 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 ...
Manu's user avatar
  • 5,544
105 votes
7 answers
17k views

(Why) 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 ...
user541686's user avatar
  • 205k
173 votes
7 answers
39k 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 ...
Joe Pineda's user avatar
  • 5,531
858 votes
17 answers
1.0m views

What is a segmentation fault?

What is a segmentation fault? Is it different in C and C++? How are segmentation faults and dangling pointers related?
Rajendra Uppal's user avatar
234 votes
6 answers
107k views

How come an array's address is equal to its value in C?

In the following bit of code, pointer values and pointer addresses differ as expected. But array values and addresses don't! How can this be? Output my_array = 0022FF00 &my_array = 0022FF00 ...
Alexandre's user avatar
  • 5,045
66 votes
5 answers
92k views

Changing address contained by pointer using function

If I've declared a pointer p as int *p; in main module, I can change the address contained by p by assigning p = &a; where a is another integer variable already declared. I now want to change the ...
pranphy's user avatar
  • 1,797

1
2 3 4 5
883