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 (pronounced "See", like the letter C) is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the UNIX operating system. Its design provides constructs that map efficiently to typical machine instructions, and therefore it found lasting use in applications that had formerly been coded in assembly language. It is a highly efficient procedural programming language and has an emphasis on functions whereas modern object-oriented programming languages tend to emphasize data.

The C programming language was based on the earlier programming languages B, BCPL, and CPL.

The C language and its optional library are standardized as ISO/IEC 9899, the current version being ISO/IEC 9899:2018 (C17). A draft version N2176 is available for free.

Although C was designed for implementing system software, it is also widely used for developing portable application software.

C is one of the most widely used programming languages of all time and there are very few computer architectures for which a C compiler does not exist. C has greatly influenced many other popular programming languages, most notably C++, which began as an extension to C. Other languages that have been greatly influenced by C are C#, Objective-C and Java.


Design

C is an imperative (procedural) systems implementation language. It was designed to be compiled using a relatively straightforward compiler, to provide low-level access to memory, to provide language constructs that map efficiently to machine instructions, and to require minimal run-time support. C was, therefore, useful for many applications that had formerly been coded in assembly language.

Despite its low-level capabilities, the language was designed to encourage cross-platform programming. A standards-compliant and portably written C program can be compiled for a very wide variety of computer platforms and operating systems with very few changes to its source code. The language has become available on a very wide range of platforms, from embedded microcontrollers to supercomputers.


Tag usage

When posting questions about C programming, please make sure to include:

  • Target system and compiler information. This includes the compiler name, version and settings used to compile.
  • In case your question is about compiler errors/warnings, please quote those errors/warnings in the question. Also clarify which line the compiler error refers to.
  • If your question is specific to one particular version of the the language, add or . Pre-standard, historical questions should be tagged .
  • Unless the question explicitly mentions which version of the C standard that is used, it is assumed that the current version is used. That is, whichever version of ISO 9899 that ISO currently lists as active. Please have this in mind when answering or commenting on questions tagged .

Using and together

C and C++ are two distinct and often incompatible languages. Avoid using both tags in the same question unless you have good reasons.

A question should be tagged with only, if:

  • It contains pure C, with no trace of C++, or questions with code that could be either language.
  • The code is compiled with a C compiler.

A question should be tagged with only, if:

  • It contains code with any C++ features. Even though the code may be "C style".
  • The code is compiled with a C++ compiler.

A question should be tagged with both and if it is about:

  • Specific differences between C and C++.
  • Compatibility or porting code between C and C++.
  • C++ code that uses C libraries (for example code using extern "C").

Editing and moderation guidelines for posts with both and tags:

To edit/re-tag/moderate questions with both tags, it is recommended that you have full edit privileges and either a gold or a gold badge.

If you encounter a post with both tags, edit/re-tag it if needed according to the above rules. If you can tell the language by reading the posted code, simply edit tags accordingly. Avoid prompting the user "is it C or C++?" in comments unless the question is truly unclear.

One example of an unclear question is when the user explicitly claims that they are programming in C, but posts code or compiler messages for C++. If so, prompt for clarification and close-vote as unclear.

"Either C or C++ is fine" opinions from the OP is a strong indication of a poor or unclear question. Answers may be very different depending on language picked. Prompt for clarification, close as unclear/too broad until the OP has clarified this.

Be careful about re-tagging questions once there are answers posted, particularly if there are already both C and C++ answers posted. In such cases, the tags should be left alone, since changing them would make posted answers invalid.

Answers with C++ code to a C question that has never been tagged should be deleted as off-topic. Please check the question edit history before flagging/deleting such answers, to verify that the question never had the C++ tag.


Books about C

There are many, many books of varying quality about how to use C. See the question Definitive C Book Guide and List.

Note that this question is controversial; it would not be accepted on modern Stack Overflow, but it is a useful historical artifact that is still being maintained.


Frequently Asked Questions (FAQ)

Types and qualifiers

Declaration and initialization

Scope and storage duration

Integer arithmetic

Floating-point arithmetic

Operators, precedence and order of evaluation

Loops

Arrays

Pointers and null

Function pointers

Strings

Dynamic memory allocation

Structs and unions

The preprocessor and macros

Standard compliance

Undefined, unspecified and implementation-defined behavior

The standard library

Best practices and style concerns


External resources


Hello World program in C

#include <stdio.h>

int main(void)
{
    printf("hello, world\n");
    return 0;
}

Chat Room

Chat about C with other Stack Overflow users


Online compilers


399079 questions
1226
votes
19 answers

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 files, but how does that work precisely? Where do I…
shilpa
1157
votes
26 answers

How to initialize all members of an array to the same value?

I have a large array in C (not C++ if that makes a difference). I want to initialize all members of the same value. I could swear I once knew a simple way to do this. I could use memset() in my case, but isn't there a way to do this that is built…
Matt
  • 84,419
  • 25
  • 57
  • 67
1146
votes
20 answers

What is the difference between ++i and i++?

In C, what is the difference between using ++i and i++, and which should be used in the incrementation block of a for loop?
The.Anti.9
  • 43,474
  • 48
  • 123
  • 161
1105
votes
5 answers

Why does the C preprocessor interpret the word "linux" as the constant "1"?

Why does the C preprocessor in GCC interpret the word linux (small letters) as the constant 1? test.c: #include int main(void) { int linux = 5; return 0; } Result of $ gcc -E test.c (stop after the preprocessing…
ahmedaly50
  • 7,765
  • 3
  • 12
  • 7
1030
votes
27 answers

What is the difference between a definition and a declaration?

The meaning of both eludes me.
Maciek
  • 19,435
  • 18
  • 63
  • 87
1015
votes
12 answers

typedef struct vs struct definitions

I'm a beginner in C programming, but I was wondering what's the difference between using typedef when defining a structure versus not using typedef. It seems to me like there's really no difference, they accomplish the same goal. struct myStruct{ …
user69514
  • 26,935
  • 59
  • 154
  • 188
997
votes
4 answers

Obfuscated C Code Contest 2006. Please explain sykes2.c

How does this C program work? main(_){_^448&&main(-~_);putchar(--_%64?32|-~7[__TIME__-_/8%8][">'txiZ^(~z?"-48]>>";;;====~$::199"[_*2&8|_/64]/(_&2?1:8)%8&1:10);} It compiles as it is (tested on gcc 4.6.3). It prints the time when compiled. On my…
corny
  • 7,824
  • 3
  • 14
  • 20
974
votes
11 answers

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
  • 37,894
  • 24
  • 81
  • 116
952
votes
31 answers

Unit Testing C Code

I worked on an embedded system this summer written in straight C. It was an existing project that the company I work for had taken over. I have become quite accustomed to writing unit tests in Java using JUnit but was at a loss as to the best way…
Paul Osborne
  • 5,024
  • 6
  • 24
  • 20
931
votes
14 answers

Difference between malloc and calloc?

What is the difference between doing: ptr = malloc(MAXELEMS * sizeof(char *)); And: ptr = calloc(MAXELEMS, sizeof(char*)); When is it a good idea to use calloc over malloc or vice versa?
user105033
  • 18,800
  • 19
  • 58
  • 69
924
votes
9 answers

Why use apparently meaningless do-while and if-else statements in macros?

In many C/C++ macros I'm seeing the code of the macro wrapped in what seems like a meaningless do while loop. Here are examples. #define FOO(X) do { f(X); g(X); } while (0) #define FOO(X) if (1) { f(X); g(X); } else I can't see what the do while…
jfm3
  • 36,964
  • 10
  • 32
  • 35
906
votes
15 answers

Why are these constructs using pre and post-increment undefined behavior?

#include 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 = u++ + ++u; printf("%d\n", u); // 1 u = 1; …
PiX
  • 9,705
  • 4
  • 19
  • 11
857
votes
18 answers

Using Boolean values in C

C doesn't have any built-in Boolean types. What's the best way to use them in C?
neuromancer
  • 53,769
  • 78
  • 166
  • 223
856
votes
17 answers

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
  • 19,218
  • 15
  • 59
  • 57
851
votes
13 answers

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
  • 25,207
  • 17
  • 54
  • 57