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.
##c Tag usage
When posting questions about C programming, please keep in mind that C compilers are slow to fully adopt new revisions of the C standard, and many C codebases in common use are still written against old revisions. Therefore, we assume you are asking about the oldest standardized version of the language (ISO 9899:1990, usually abbreviated "C89" for complicated historical reasons) unless you specifically mention you are working with a newer version. Please also keep this in mind when answering or commenting on questions tagged c.
Many C compilers' default behavior was tuned for code written a long time ago, to much looser tolerances than are now considered the minimum; if you are writing new C code, we strongly encourage you to turn on lots of optional "warnings" and treat them all as must-fix. With the widely used GCC and Clang, for example, a good tradeoff among standards conformance, bug detection, and real-world compatibility is -std=gnuxx -Wall -Wextra -Wpedantic
on the command line (where the xx
in -std=gnuxx
is a placeholder for the revision of the standard you want, for example -std=gnu11
for the 2011 revision). The stricter -std=cxx
modes are likely to expose bugs in system headers, so they should only be used if you are prepared to deal with that possibility.
Please make sure to include:
- Target system & compiler version
- Add kr-c, c90, c99, c11 or c17 in case your question is specific to one particular version of the standard (more info).
- Relevant flags/switches passed to the compiler, assembler or linker if applicable
- Verbatim copies of compiler warnings or errors if applicable. Please post them as text and not as screen shots.
- Whenever asking for debugging help, a complete but minimal program demonstrating the bug, that we can experiment with. Bugs in C code often have their root cause nowhere near the place flagged by the compiler or debugger as erroneous; if you only show us a fragment of your program, there's a good chance we won't be able to help.
###Is it C, C++ or both?
This tag is for questions related to C, not C++. In some cases, you may be working with both and applying both tags is entirely appropriate. However, please refrain from using both tags in an effort to help your question reach a wider audience. After all, C++ answers won't help you solve the problem in C, and good C answers rarely describe the best approach in C++.
##Frequently Asked Questions (FAQ)
Types and qualifiers
- What is the strict aliasing rule?
- Is char signed or unsigned by default?
- Implicit type promotion rules
- What is the difference between const int*, const int * const, and int const *?
Declaration and initialization
- What is the difference between a definition and a declaration?
- (Why) is using an uninitialized variable undefined behavior?
- How to initialize all members of an array to the same value?
Scope and storage duration
- Can a local variable's memory be accessed outside its scope?
- How do I use extern to share variables between source files?
- What does "static" mean in C?
Integer arithmetic
- Why is unsigned integer overflow defined behavior but signed integer overflow isn't?
- How do I detect unsigned integer overflow?
- What is the behavior of integer division?
Floating point arithmetic
- Why are floating point numbers inaccurate?
- C program to convert Fahrenheit to Celsius (common beginner FAQ: the presence of
5/9
integer division in a Fahrenheit to Celsius calculator) - Is the behaviour of casting a negative double to unsigned int defined in the C standard? Different behaviour on ARM vs. x86
Operators, precedence and order of evaluation
- Why are these constructs using pre and post-increment undefined behavior?
- What is the difference between ++i and i++?
- Is short-circuiting logical operators mandated? And evaluation order?
- With arrays, why is it the case that a[5] == 5[a]?
- How do I set, clear, and toggle a single bit?
- What are bitwise shift (bit-shift) operators and how do they work?
- What does the comma operator , do?
- Why doesn't a+++++b work? ("maximal munch rule")
Arrays
- What is array to pointer decay?
- Is an array name a pointer?
- Why isn't the size of an array parameter the same as within main?
- How dangerous is it to access an array out of bounds?
- Getting a stack overflow exception when declaring a large array
- Why does the order of the loops affect performance when iterating over a 2D array?
Pointers and null
- How to write C/C++ code correctly when null pointer is not all bits zero
- How to access a local variable from a different function using pointers?
- How to find the size of an array (from a pointer pointing to the first element array)?
- Why does int pointer '++' increment by 4 rather than 1?
- Pointer to pointer clarification
- Crash or "segmentation fault" when data is copied/scanned/read to an uninitialized pointer
- Assigning a pointer to an integer (without a cast, why is this not allowed?)
Function pointers
- How do function pointers in C work?
- Casting a function pointer to another type
- Why do function pointer definitions work with any number of ampersands '&' or asterisks '*'?
Strings
- How can I correctly assign a new string value?
- How do I properly compare strings in C?
- What is the difference between char s[] and char *s?
- Why do I get a segmentation fault when writing to a "char *s" initialized with a string literal, but not "char s[]"?
- Why are strlcpy and strlcat considered insecure?
- String literals: Where do they go?
- How to split a string into tokens in C? (how to use strtok)
Dynamic memory allocation
- Do I cast the result of malloc?
- Dynamic memory access only works inside function
- Correctly allocating multi-dimensional arrays
Structs and unions
- Why isn't sizeof for a struct equal to the sum of sizeof of each member?
- Partitioning struct into private and public sections? (private encapsulation with opaque type/opaque pointers)
The pre-processor and macros
- What is the difference between #include <filename> and #include "filename"?
- Why use apparently meaningless do-while and if-else statements in macros?
Standard compliance
- What should main() return in C and C++?
- Where do I find the current C or C++ standard documents?
- What is the difference between C, C99, ANSI C and GNU C?
Undefined, unspecified and implementation-defined behavior
- Undefined, unspecified and implementation-defined behavior
- Definitive List of Common Reasons for Segmentation Faults
- What is a bus error? Is it different from a segmentation fault?
The standard library
- Which functions from the standard library must (should) be avoided?
- How to read / parse input in C? The FAQ
- Removing trailing newline character from fgets() input
- Correct format specifier to print pointer or address?
- Why is “while( !feof(file) )” always wrong?
- Why is the gets function so dangerous that it should not be used?
- srand() — why call it only once?
- Using fflush(stdin)
- What will happen if '&' is not put in a 'scanf' statement?
Best practices and style concerns
- Why is the asterisk before the variable name, rather than after the type?
- "static const" vs "#define" vs "enum"
- Is it a good idea to typedef pointers?
###External resources
- The comp.lang.c FAQ has answers to many frequently asked C questions. For example, see The Clockwise/Spiral Rule for parsing C declarations.
- C Reference provides a reference to C language and standard library functions.
- What Every Computer Scientist Should Know About Floating-Point Arithmetic, by David Goldberg
- cdecl: C gibberish ↔ English, a site that translates C expressions to readable English.
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