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
73
votes
4 answers

Why would one use MACRO+0 !=0

In my current codebase I see this following pattern: #if SOMETHING_SUPPORTED+0 != 0 ... #endif Unfortunately this is a very old codebase and nobody knows how and why it started. I think it started in C and it was slowly converted into C with…
Mircea Ispas
  • 20,260
  • 32
  • 123
  • 211
73
votes
4 answers

What do 1.#INF00, -1.#IND00 and -1.#IND mean?

I'm messing around with some C code using floats, and I'm getting 1.#INF00, -1.#IND00 and -1.#IND when I try to print floats in the screen. What does those values mean? I believe that 1.#INF00 means positive infinity, but what about -1.#IND00 and…
Hoffmann
  • 14,369
  • 16
  • 76
  • 91
73
votes
5 answers

Intrusive lists

I've not been able to find too much information about them online. What are they and when are they typically used? Thanks.
Konrad
  • 39,751
  • 32
  • 78
  • 114
73
votes
3 answers

What defines an opaque type in C, and when are they necessary and/or useful?

I've seen the concept of 'opaque types' thrown around a bit but I really haven't found a succinct answer as to what defines an opaque type in C and more importantly what problems they allow us to solve with their existence. Thanks
SiegeX
  • 135,741
  • 24
  • 144
  • 154
73
votes
4 answers

Why is memory allocation on heap MUCH slower than on stack?

I have been told this many times. But I don't know WHY...What extra cost is involved when allocating memory from heap? Is it hardware related? Is it related to CPU cycles? So many guesses but no exact answers...Could someone give me some…
smwikipedia
  • 61,609
  • 92
  • 309
  • 482
73
votes
7 answers

Why does open() create my file with the wrong permissions?

I am trying to read some text from a file and write it to another using open(), read() and write(). This is my open() for the file-to-write-to (I want to create a new file and write into it): fOut = open ("test-1", O_RDWR | O_CREAT | O_SYNC); This…
Chaitanya
  • 751
  • 1
  • 6
  • 4
73
votes
3 answers

Setting std=c99 flag in GCC

I was wondering if there were any files in which I could set the -std=c99 flag, so that I would not have to set it for every compilation. I am using GCC 4.4 on Ubuntu.
Fatmarik
  • 969
  • 2
  • 9
  • 11
73
votes
3 answers

How do you read a segfault kernel log message

This can be a very simple question, I'm am attempting to debug an application which generates the following segfault error in the kern.log kernel: myapp[15514]: segfault at 794ef0 ip 080513b sp 794ef0 error 6 in myapp[8048000+24000] Here are my…
Sullenx
  • 731
  • 1
  • 6
  • 3
73
votes
5 answers

Effect of using a comma instead of a semi-colon in C and C++

I've noticed on a number of occasions when refactoring various pieces of C and C++ code that a comma is used rather than a semi-colon to seperate statements. Something like this; int a = 0, b = 0; a = 5, b = 5; Where I would have expected int a =…
SmacL
  • 22,555
  • 12
  • 95
  • 149
73
votes
8 answers

Returning function pointer type

Often I find the need to write functions which return function pointers. Whenever I do, the basic format I use is: typedef int (*function_type)(int,int); function_type getFunc() { function_type test; test /* = ...*/; return…
user545199
73
votes
9 answers

Rules for using the restrict keyword in C?

I'm trying to understand when and when not to use the restrict keyword in C and in what situations it provides a tangible benefit. After reading, "Demystifying The Restrict Keyword", ( which provides some rules of thumb on usage ), I get the…
Robert S. Barnes
  • 39,711
  • 30
  • 131
  • 179
73
votes
10 answers

How are exceptions implemented under the hood?

Just about everyone uses them, but many, including me simply take it for granted that they just work. I am looking for high-quality material. Languages I use are: Java, C, C#, Python, C++, so these are of most interest to me. Now, C++ is probably a…
Hamish Grubijan
  • 10,562
  • 23
  • 99
  • 147
73
votes
7 answers

Best way to check if a character array is empty

Which is the most reliable way to check if a character array is empty? char text[50]; if(strlen(text) == 0) {} or if(text[0] == '\0') {} or do i need to do memset(text, 0, sizeof(text)); if(strlen(text) == 0) {} Whats the most efficient way…
ZPS
  • 1,566
  • 4
  • 16
  • 20
73
votes
6 answers

Do I need to compile the header files in a C program?

Sometimes I see someone compile a C program like this: gcc -o hello hello.c hello.h As I know, we just need to put the header files into the C program like: #include "somefile" and compile the C program: gcc -o hello hello.c. When do we need to…
yuliu
  • 913
  • 1
  • 6
  • 8
73
votes
5 answers

practical examples use dup or dup2

I know what dup / dup2 does, but I have no idea when it would be used. Any practical examples? Thanks.
pierrotlefou
  • 39,805
  • 37
  • 135
  • 175
1 2 3
99
100