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

how to make an application thread safe?

I thought thread safe, in particular, means it must satisfy the need for multiple threads to access the same shared data. But, it seems this definition is not enough. Can anyone please list out the things to be done or taken care of to make an…
ashmish2
  • 2,885
  • 8
  • 40
  • 54
75
votes
13 answers

Launch Failed. Binary not found. CDT on Eclipse Helios

I'm using Eclipse Helios on Ubuntu 10.04, and I'm trying to install CDT plugin on it. I download it from here here. And then I go to Install New Software and select the zip file (I don't extract it, just select the zip file). And its ok, it…
rogcg
  • 10,451
  • 20
  • 91
  • 133
75
votes
10 answers

Why not concatenate C source files before compilation?

I come from a scripting background and the preprocessor in C has always seemed ugly to me. None the less I have embraced it as I learn to write small C programs. I am only really using the preprocessor for including the standard libraries and header…
user3420382
75
votes
1 answer

Why define \0 as the first element of a char array in C?

When I read BlueZ source code, I often see char arrays defined like this: // bluez/android/sco-msg.h static const char BLUEZ_SCO_SK_PATH[] = "\0bluez_sco_socket"; What good is it to define the first element as \0?
user1923105
  • 713
  • 5
  • 9
75
votes
2 answers

g++ linker: force static linking if static library exists?

I've a program which links to many libraries. g++, by default, prefers to link to shared libraries, even if the corresponding archive exists. How can I change this preference to prefer static archives over dynamic libraries, if a static archive…
kumar
  • 2,696
  • 3
  • 26
  • 34
75
votes
9 answers

Faster way to zero memory than with memset?

I learned that memset(ptr, 0, nbytes) is really fast, but is there a faster way (at least on x86)? I assume that memset uses mov, however when zeroing memory most compilers use xor as it's faster, correct? edit1: Wrong, as GregS pointed out that…
maep
  • 1,318
  • 1
  • 12
  • 15
75
votes
1 answer

Definitive List of Common Reasons for Segmentation Faults

NOTE: We have a lot of segfault questions, with largely the same answers, so I'm trying to collapse them into a canonical question like we have for undefined reference. Although we have a question covering what a segmentation fault is, it…
CodeMouse92
  • 6,840
  • 14
  • 73
  • 130
75
votes
11 answers

What is the purpose of ";" at the end of for loop?

I found the following code: int func_prim (int zahl) { int count; if (zahl < 0) return -1; for (count = 2; zahl % count != 0 && zahl >= count; count++); if (count == zahl) return 1; return 0; } The point of…
Wien95
  • 796
  • 6
  • 6
75
votes
4 answers

Can argc overflow?

I was wandering in SO and saw this question. Then I started wondering if I can overflow argc. Standard says that argv[argc] must be a null pointer but this will be false if argc overflow. (I wrote a small C program and a python script to test it but…
Bora M. Alper
  • 3,538
  • 1
  • 24
  • 35
75
votes
5 answers

What kind of optimization does const offer in C/C++?

I know that where possible you should use the const keyword when passing parameters around by reference or by pointer for readability reasons. Is there any optimizations that the compiler can do if I specify that an argument is constant? There could…
UnTraDe
  • 3,747
  • 10
  • 36
  • 60
75
votes
3 answers

Getting a weird percent sign in printf output in terminal with C

I have this printf statement at the end of my program: printf("%d", total_candies); total_candies is an int, and while I expect everything to work correctly, along with the actual number, I'm getting a weird percent sign at the end. Can anyone…
rounak
  • 9,217
  • 3
  • 42
  • 59
75
votes
5 answers

Do temp variables slow down my program?

Suppose I have the following C code: int i = 5; int j = 10; int result = i + j; If I'm looping over this many times, would it be faster to use int result = 5 + 10? I often create temporary variables to make my code more readable, for example, if…
Elliot Gorokhovsky
  • 3,610
  • 2
  • 31
  • 56
75
votes
4 answers

Loop with a zero execution time

Is it possible to have a loop which has a zero execution time? I would think that even an empty loop should have an execution time since there is an overhead associated with it.
SudarakaR
  • 937
  • 6
  • 6
75
votes
7 answers

Get IP address of an interface on Linux

How can I get the IPv4 address of an interface on Linux from C code? For example, I'd like to get the IP address (if any) assigned to eth0.
leeeroy
  • 11,216
  • 17
  • 52
  • 54
75
votes
2 answers

Linux shared memory: shmget() vs mmap()?

In this thread the OP is suggested to use mmap() instead of shmget() to get shared memory in Linux. I visited this page and this page to get some documentation, but the second one gives an obscure example regarding mmap(). Being almost a newbie, and…
BowPark
  • 1,340
  • 2
  • 21
  • 31