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

Append to GNU make variables via command line

I am using a GNU-make Makefile to build a C project with several targets (all, clean, and a few project specific targets). In the process of debugging, I would like to append some flags to a single build without permanently editing the Makefile…
Michael Koval
  • 8,207
  • 5
  • 42
  • 53
75
votes
11 answers

fixed length data types in C/C++

I've heard that size of data types such as int may vary across platforms. My first question is: can someone bring some example, what goes wrong, when program assumes an int is 4 bytes, but on a different platform it is say 2 bytes? Another question…
user2793162
75
votes
5 answers

htons() function in socket programing

I am new to socket programming and I am trying to understand the operation of htons(). I've read a few tutorials on the Internet like this and this one for instance. But I couldn't understand what htons() does exactly. I tried the following…
User123422
  • 889
  • 2
  • 11
  • 16
75
votes
6 answers

C and Python - different behaviour of the modulo (%) operation

I have found that the same mod operation produces different results depending on what language is being used. In Python: -1 % 10 produces 9 In C it produces -1 ! Which one is the right modulo? How to make mod operation in C to be the same like in…
psihodelia
  • 29,566
  • 35
  • 108
  • 157
75
votes
4 answers

How can a C compiler be written in C?

This question may stem from a misunderstanding of compilers on my part, but here goes... One can find the following statement in the preface to the first edition of K&R (page xi): The operating system, the C compiler, and essentially all UNIX…
jub0bs
  • 60,866
  • 25
  • 183
  • 186
75
votes
7 answers

Get the compiler options from a compiled executable?

It there a way to see what compiler and flags were used to create an executable file in *nix? I have an old version of my code compiled and I would like to see whether it was compiled with or without optimization. Google was not too helpful, but I'm…
Mosby
  • 1,291
  • 1
  • 10
  • 14
74
votes
5 answers

Is NULL always zero in C?

I was interviewing a guy for a mid-level software engineering position yesterday, and he mentioned that in C, NULL is not always zero and that he had seen implementations of C where NULL is not zero. I find this highly suspect, but I want to be…
chi42
  • 925
  • 1
  • 7
  • 8
74
votes
14 answers

Seeing expanded C macros

If I want to expand a C macro, what are some good ways to do that (besides tracing it manually)? For instance, GTK_WIDGET_SET_FLAGS, it uses a macro that uses a macro that uses a macro (or two) ... I want to just see it somehow expanded…
hasen
  • 161,647
  • 65
  • 194
  • 231
74
votes
3 answers

What is the purpose of epoll's edge triggered option?

From epoll's man page: epoll is a variant of poll(2) that can be used either as an edge-triggered or a level-triggered interface When would one use the edge triggered option? The man page gives an example that uses it, but I don't see why it is…
Dan
  • 12,409
  • 3
  • 50
  • 87
74
votes
5 answers

Why is strncpy insecure?

I am looking to find out why strncpy is considered insecure. Does anybody have any sort of documentation on this or examples of an exploit using it?
stimms
  • 42,945
  • 30
  • 96
  • 149
74
votes
7 answers

Is there an equivalent to WinAPI's MAX_PATH under linux/unix?

If I want to allocate a char array (in C) that is guaranteed to be large enough to hold any valid absolute path+filename, how big does it need to be. On Win32, there is the MAX_PATH define. What is the equivalent for Unix/linux?
AndrewR
  • 10,759
  • 10
  • 45
  • 56
74
votes
7 answers

How do I modify a pointer that has been passed into a function in C?

So, I have some code, kind of like the following, to add a struct to a list of structs: void barPush(BarList * list,Bar * bar) { // if there is no move to add, then we are done if (bar == NULL) return;//EMPTY_LIST; // allocate space for…
Paul Wicks
  • 62,960
  • 55
  • 119
  • 146
74
votes
4 answers

How can I include a C header that uses a C++ keyword as an identifier in C++?

I've been using C++ and compiling with clang++. I would like to include the header for an X11 program I am writing. Unfortunately this header uses explicit for some field names (such as line 727) and that is a keyword in C++. Is there…
CLearner
  • 767
  • 4
  • 10
74
votes
7 answers

How to copy directory from source tree to binary tree?

Copying directory from source tree to binary tree. For example: How to copy www to bin folder. work ├─bin └─src ├─doing │ └─www ├─include └─lib Thanks.
Jiang Bian
  • 1,933
  • 3
  • 16
  • 14
74
votes
5 answers

How does "sizeof" determine the size of an array?

How does C find the size of an array at runtime? Where is the information about the array size or bounds stored ?
Kazoom
  • 5,659
  • 16
  • 56
  • 69