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
79
votes
9 answers

Performance of built-in types : char vs short vs int vs. float vs. double

Seeing Alexandre C's reply in the other topic, I'm curious to know that if there is any performance difference with the built-in types: char vs short vs int vs. float vs. double. Usually we don't consider such performance difference (if any) in…
Nawaz
  • 353,942
  • 115
  • 666
  • 851
79
votes
12 answers

Does using heap memory (malloc/new) create a non-deterministic program?

I started developing software for real-time systems a few months ago in C for space applications, and also for microcontrollers with C++. There's a rule of thumb in such systems that one should never create heap objects (so no malloc/new), because…
The Quantum Physicist
  • 24,987
  • 19
  • 103
  • 189
79
votes
3 answers

What are the implications of the linux __user macro?

I was hoping someone could explain the nuances of the __user macro used in the linux kernel source. First of all, the macro: # define __user __attribute__((noderef, address_space(1))) Now, after some googling I read that this macro allows…
Mr. Shickadance
  • 5,283
  • 9
  • 45
  • 61
79
votes
7 answers

Why is the ternary operator used to define 1 and 0 in a macro?

I'm using an SDK for an embedded project. In this source code I found some code which at least I found peculiar. In many places in the SDK there is source code in this format: #define ATCI_IS_LOWER( alpha_char ) ( ( (alpha_char >= ATCI_char_a) &&…
Viktor S
  • 761
  • 1
  • 5
  • 13
79
votes
2 answers

How to compile executable for Windows with GCC with Linux Subsystem?

Windows 10 Anniversary Update includes the Linux Subsystem for Ubuntu. I installed gcc with sudo apt-get install gcc. I wrote some simple C code for testing purposes: #include int main(void){ printf("Hello\n"); return 0; } And…
Mikhail
  • 11,067
  • 7
  • 28
  • 53
79
votes
10 answers

Can I assume the size of long int is always 4 bytes?

Is it always true that long int (which as far as I understand is a synonym for long) is 4 bytes? Can I rely on that? If not, could it be true for a POSIX based OS?
Elimination
  • 2,619
  • 4
  • 22
  • 38
79
votes
7 answers

How to compile .c file with OpenSSL includes?

I am trying to compile a small .c file that has the following includes: #include #include #include #include In the same folder where I have the .c file I have a /openssl with all…
jahmax
  • 8,181
  • 7
  • 26
  • 25
79
votes
12 answers

Is declaration of variables expensive?

While coding in C, I came across the below situation. int function () { if (!somecondition) return false; internalStructure *str1; internalStructure *str2; char *dataPointer; float xyz; /* do something here with the above local…
Whoami
  • 13,930
  • 19
  • 84
  • 140
79
votes
14 answers

If a 32-bit integer overflows, can we use a 40-bit structure instead of a 64-bit long one?

If, say, a 32-bit integer is overflowing, instead of upgrading int to long, can we make use of some 40-bit type if we need a range only within 240, so that we save 24 (64-40) bits for every integer? If so, how? I have to deal with billions and space…
Aragon
  • 1,551
  • 1
  • 15
  • 25
79
votes
16 answers

Examples of good gotos in C or C++

In this thread, we look at examples of good uses of goto in C or C++. It's inspired by an answer which people voted up because they thought I was joking. Summary (label changed from original to make intent even clearer): infinite_loop: // code…
fizzer
  • 13,551
  • 9
  • 39
  • 61
79
votes
6 answers

Mixing extern and const

Can I mix extern and const, as extern const? If yes, does the const qualifier impose it's reign only within the scope it's declared in or should it exactly match the declaration of the translational unit it's declared in? I.e. can I declare say…
legends2k
  • 31,634
  • 25
  • 118
  • 222
79
votes
1 answer

Do I need to keep a file open after calling mmap on it?

I have a program that maps quite a few (100's) of sizable files 10-100MB each. I need them all mapped at the same time. At the moment I am calling open followed by mmap at the beginning of the program, followed by munmap and close at the end. Often…
camelccc
  • 2,847
  • 8
  • 26
  • 52
79
votes
12 answers

Difference between "system" and "exec" in Linux?

What is the difference between system and exec family commands? Especially I want to know which one of them creates child process to work?
Kamil
  • 799
  • 1
  • 5
  • 3
79
votes
4 answers

Declaring and checking/comparing (bitmask-)enums in Objective-C

You know in Cocoa there is this thing, for example you can create a UIView and do: view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; I have a custom UIView with multiple states, which I have defined in an…
thibaultcha
  • 1,320
  • 2
  • 15
  • 27
79
votes
3 answers

PDF specifications for coders: Adobe or ISO?

I want to code an application that can read and decode a pdf document; now where I'm supposed to get the specs for this fileformat ? The PDF format is standardized from the ISO group but it's not clear to me where is the most reliable source for…
user1824407
  • 4,401
  • 4
  • 15
  • 20