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 absent a rational 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


359290 questions
9401
votes
26 answers

What is the "-->" operator in C/C++?

After reading Hidden Features and Dark Corners of C++/STL on comp.lang.c++.moderated, I was completely surprised that the following snippet compiled and worked in both Visual Studio 2008 and G++ 4.4. Here's the code: #include int main() { …
GManNickG
  • 459,504
  • 50
  • 465
  • 534
3117
votes
10 answers

Improve INSERT-per-second performance of SQLite

Optimizing SQLite is tricky. Bulk-insert performance of a C application can vary from 85 inserts per second to over 96,000 inserts per second! Background: We are using SQLite as part of a desktop application. We have large amounts of configuration…
Mike Willekes
  • 5,570
  • 9
  • 30
  • 33
2767
votes
27 answers

How do you set, clear, and toggle a single bit?

How do you set, clear, and toggle a bit?
JeffV
  • 47,302
  • 31
  • 96
  • 120
2602
votes
30 answers

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

In the C and C++ programming languages, what is the difference between using angle brackets and using quotes in an include statement, as follows? #include #include "filename"
quest49
  • 42,302
  • 4
  • 18
  • 14
2554
votes
29 answers

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(int) * length); rather than: int *sieve = (int *) malloc(sizeof(int) * length); Why would this be the…
Patrick McDonald
  • 59,808
  • 14
  • 95
  • 115
2095
votes
4 answers

What does the ??!??! operator do in C?

I saw a line of C that looked like this: !ErrorHasOccured() ??!??! HandleError(); It compiled correctly and seems to run ok. It seems like it's checking if an error has occurred, and if it has, it handles it. But I'm not really sure what it's…
Peter Olson
  • 121,487
  • 47
  • 188
  • 235
1825
votes
16 answers

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
  • 19,691
  • 3
  • 20
  • 27
1733
votes
5 answers

What is ":-!!" in C code?

I bumped into this strange macro code in /usr/include/linux/kernel.h: /* Force a compilation error if condition is true, but also produce a result (of value 0 and type size_t), so the expression can be used e.g. in a structure initializer (or…
chmurli
  • 14,332
  • 3
  • 13
  • 12
1685
votes
17 answers

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 arithmetic but I still don't understand. Why does a[5] ==…
Dinah
  • 48,876
  • 29
  • 126
  • 149
1515
votes
23 answers

Compiling an application for use in highly radioactive environments

We are compiling an embedded C++ application that is deployed in a shielded device in an environment bombarded with ionizing radiation. We are using GCC and cross-compiling for ARM. When deployed, our application generates some erroneous data and…
rook
  • 62,960
  • 36
  • 149
  • 231
1505
votes
21 answers

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 of assignments, passing to the functions, etc.
ultraman
1334
votes
11 answers

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 quick dive-in to the subject.
Yuval Adam
  • 149,388
  • 85
  • 287
  • 384
1245
votes
19 answers

What does "static" mean in C?

I've seen the word static used in different places in C code; is this like a static function/class in C# (where the implementation is shared across objects)?
David
1152
votes
22 answers

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
  • 267,774
  • 112
  • 308
  • 434
1087
votes
17 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
1
2 3
99 100