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
74
votes
6 answers

Does C or C++ guarantee array < array + SIZE?

Suppose you have an array: int array[SIZE]; or int *array = new(int[SIZE]); Does C or C++ guarantee that array < array + SIZE, and if so where? I understand that regardless of the language spec, many operating systems guarantee this property by…
user3188445
  • 4,062
  • 16
  • 26
74
votes
6 answers

.o files vs .a files

What is the difference between these two file types. I see that my C++ app links against both types during the construction of the executable. How to build .a files? links, references, and especially examples, are highly appreciated.
Sasha
74
votes
9 answers

How to get the Enum Index value in C#

In C, enums, internally equates to an integer. Therefore we can treat data types of enum as integer also. How to achieve the same with C#?
Shamim Hafiz - MSFT
  • 21,454
  • 43
  • 116
  • 176
74
votes
7 answers

Why do you need to recompile C/C++ for each OS?

This is more of a theoretical question than anything. I'm a Comp sci major with a huge interest in low level programming. I love finding out how things work under the hood. My specialization is compiler design. Anyway, as I'm working on my first…
Nassim Assaf
  • 773
  • 5
  • 5
74
votes
5 answers

The difference of int8_t, int_least8_t and int_fast8_t?

What is the difference between the int types int8_t, int_least8_t and int_fast8_t?
NebulaFox
  • 7,813
  • 9
  • 47
  • 65
74
votes
5 answers

segfault only when NOT using debugger

I have a multithreaded C program, which consistently generates a segmentation fault at a specific point in the program. When I run it with gdb, no fault is shown. Can you think of any reason why the fault might occur only when not using the…
Benubird
  • 18,551
  • 27
  • 90
  • 141
74
votes
9 answers

Why does "noreturn" function return?

I read this question about noreturn attribute, which is used for functions that don't return to the caller. Then I have made a program in C. #include #include noreturn void func() { printf("noreturn…
msc
  • 33,420
  • 29
  • 119
  • 214
74
votes
7 answers

What is the purpose of the h and hh modifiers for printf?

Aside from %hn and %hhn (where the h or hh specifies the size of the pointed-to object), what is the point of the h and hh modifiers for printf format specifiers? Due to default promotions which are required by the standard to be applied for…
74
votes
12 answers

Pointers to pointers vs. normal pointers

The purpose of a pointer is to save the address of a specific variable. Then the memory structure of following code should look like: int a = 5; int *b = &a; ...... memory address ...... value a ... 0x000002 ................... 5 b…
user42298
  • 1,831
  • 3
  • 19
  • 30
74
votes
21 answers

What is the fastest way to swap values in C?

I want to swap two integers, and I want to know which of these two implementations will be faster: The obvious way with a temp variable: void swap(int* a, int* b) { int temp = *a; *a = *b; *b = temp; } Or the xor version that I'm sure…
JawnV6
  • 1,367
  • 1
  • 12
  • 11
74
votes
2 answers

Return value optimization and copy elision in C

Some people are not aware that it's possible to pass and return structs by value in C. My question is about the compiler making unnecessary copies when returning structs in C. Do C compilers such as GCC use Return value optimization(RVO)…
Z boson
  • 32,619
  • 11
  • 123
  • 226
74
votes
7 answers

Where is `%p` useful with printf?

After all, both these statements do the same thing... int a = 10; int *b = &a; printf("%p\n",b); printf("%08X\n",b); For example (with different addresses): 0012FEE0 0012FEE0 It is trivial to format the pointer as desired with %x, so is there some…
Moeb
  • 10,527
  • 31
  • 84
  • 110
74
votes
4 answers

What is the type of string literals in C and C++?

What is the type of string literal in C? Is it char * or const char * or const char * const? What about C++?
missingfaktor
  • 90,905
  • 62
  • 285
  • 365
74
votes
11 answers

What is the simplest standard conform way to produce a Segfault in C?

I think the question says it all. An example covering most standards from C89 to C11 would be helpful. I though of this one, but I guess it is just undefined behaviour: #include int main( int argc, char* argv[] ) { const char *s =…
math
  • 8,514
  • 10
  • 53
  • 61
74
votes
14 answers

Is it possible to store the address of a label in a variable and use goto to jump to it?

I know everyone hates gotos. In my code, for reasons I have considered and am comfortable with, they provide an effective solution (ie I'm not looking for "don't do that" as an answer, I understand your reservations, and understand why I am using…
Joshua Cheek
  • 30,436
  • 16
  • 74
  • 83