C is a general-purpose computer programming language used for operating systems, libraries, games and other high performance work. This tag should be used with general questions concerning the C language, as defined in the ISO 9899:2011 standard. If applicable, include a version-specific tag such as ...

1914
votes
27answers
158k 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.int *sieve=malloc(sizeof(int) * length);rather than:int *sieve=(int *) malloc(sizeof(int) * ...
417
votes
5answers
166k views

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

I've seen people trying to read files like this in a lot of posts lately.Code#include <stdio.h>#include <stdlib.h>int main(int argc, char **argv){char * path=argc > 1 ? ...
682
votes
13answers
55k views

Why are these constructs (using ++) undefined behavior in C?

#include <stdio.h>int main(void){int i=0;i=i++ + ++i;printf("%d\n", i); // 3i=1;i=(i++);printf("%d\n", i); // 2 Should be 1, no ?volatile int u=0;u ...
401
votes
1answer
361k 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 ...
515
votes
19answers
257k 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? If int main() then return 1 or return 0?There are numerous duplicates of ...
411
votes
8answers
37k views

Undefined, unspecified and implementation-defined behavior

What is the difference between undefined, unspecified, and implementation-defined behavior in C and C++?
630
votes
9answers
150k views

What is the strict aliasing rule?

When asking about common undefined behavior in C, souls more enlightened than I referred to the strict aliasing rule.What are they talking about?
255
votes
7answers
33k views

What is array decaying?

What is decaying of an array? Is there any relation to array pointers?
207
votes
16answers
52k views

Why do I get a segmentation fault when writing to a string initialized with “char *s” but not “char s[]”?

The following code receives seg fault on line 2:char *str="string";str[0]='z';printf("%s", str);While this works perfectly well:char str[]="string";str[0]='z';printf("%s",...
154
votes
12answers
96k 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 a warning: (.text+0x34): warning: the `gets' function is dangerous and should not be used.I remember this has ...
510
votes
11answers
132k 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?
763
votes
15answers
530k 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 ...
228
votes
15answers
267k views

How to find the 'sizeof' (a pointer pointing to an 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 ...
657
votes
24answers
247k views

What is the difference between a definition and a declaration?

The meaning of both eludes me.
420
votes
13answers
255k 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 ...

153050per page
angop.ao, elkhabar.com, noa.al, afghanpaper.com, bbc.com, time.com, cdc.gov, nih.gov, xnxx.com, github.com,