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
468
votes
28 answers

What are the barriers to understanding pointers and what can be done to overcome them?

Why are pointers such a leading factor of confusion for many new, and even old, college level students in C or C++? Are there any tools or thought processes that helped you understand how pointers work at the variable, function, and beyond…
David McGraw
  • 5,157
  • 7
  • 36
  • 36
464
votes
17 answers

Difference between a Structure and a Union

Is there any good example to give the difference between a struct and a union? Basically I know that struct uses all the memory of its member and union uses the largest members memory space. Is there any other OS level difference?
gagneet
  • 35,729
  • 29
  • 78
  • 113
463
votes
17 answers

How to allocate aligned memory only using the standard library?

I just finished a test as part of a job interview, and one question stumped me, even using Google for reference. I'd like to see what the StackOverflow crew can do with it: The memset_16aligned function requires a 16-byte aligned pointer passed to…
JimDaniel
  • 12,513
  • 8
  • 61
  • 67
462
votes
14 answers

How do you format an unsigned long long int using printf?

#include int main() { unsigned long long int num = 285212672; //FYI: fits in 29 bits int normalInt = 5; printf("My number is %d bytes wide and its value is %ul. A normal number is %d.\n", sizeof(num), num, normalInt); …
andrewrk
  • 30,272
  • 27
  • 92
  • 113
461
votes
8 answers

How can I get a file's size in C?

How can I find out the size of a file I opened with an application written in C? I would like to know the size because I want to put the content of the loaded file into a string, which I allocate using malloc(). Just writing…
Nino
  • 5,261
  • 4
  • 22
  • 15
458
votes
23 answers

Is Fortran easier to optimize than C for heavy calculations?

From time to time I read that Fortran is or can be faster then C for heavy calculations. Is that really true? I must admit that I hardly know Fortran, but the Fortran code I have seen so far did not show that the language has features that C doesn't…
quinmars
  • 11,175
  • 8
  • 32
  • 41
452
votes
9 answers

What is the LD_PRELOAD trick?

I came across a reference to it recently on proggit and (as of now) it is not explained. I suspect this might be it, but I don't know for sure.
Hank Gay
  • 70,339
  • 36
  • 160
  • 222
452
votes
17 answers

Why use pointers?

I know this is a really basic question, but I've just started with some basic C++ programming after coding a few projects with high-level languages. Basically I have three questions: Why use pointers over normal variables? When and where should I…
Anonymous
449
votes
6 answers

How do I print the full value of a long string in gdb?

I want to print the full length of a C-string in GDB. By default it's being abbreviated, how do I force GDB to print the whole string?
John Carter
  • 53,924
  • 26
  • 111
  • 144
446
votes
10 answers

How do the likely/unlikely macros in the Linux kernel work and what is their benefit?

I've been digging through some parts of the Linux kernel, and found calls like this: if (unlikely(fd < 0)) { /* Do something */ } or if (likely(!err)) { /* Do something */ } I've found the definition of them: #define likely(x) …
terminus
  • 13,745
  • 8
  • 34
  • 37
445
votes
7 answers

How to printf "unsigned long" in C?

I can never understand how to print unsigned long datatype in C. Suppose unsigned_foo is an unsigned long, then I try: printf("%lu\n", unsigned_foo) printf("%du\n", unsigned_foo) printf("%ud\n", unsigned_foo) printf("%ll\n",…
bodacydo
  • 75,521
  • 93
  • 229
  • 319
441
votes
3 answers

Why do all the C files written by my lecturer start with a single # on the first line?

I'm going through some C course notes, and every C program source file begins with a single # on the first line of the program. Then there are blank lines, and following that other stuff followed by the main function. What is the reason for the…
The Main Man
  • 2,433
  • 2
  • 9
  • 8
433
votes
5 answers

How exactly does __attribute__((constructor)) work?

It seems pretty clear that it is supposed to set things up. When exactly does it run? Why are there two parentheses? Is __attribute__ a function? A macro? Syntax? Does this work in C? C++? Does the function it works with need to be static? When…
Casebash
  • 114,675
  • 90
  • 247
  • 350
424
votes
17 answers

How do I concatenate const/literal strings in C?

I'm working in C, and I have to concatenate a few things. Right now I have this: message = strcat("TEXT ", var); message2 = strcat(strcat("TEXT ", foo), strcat(" TEXT ", bar)); Now if you have experience in C I'm sure you realize that this gives…
The.Anti.9
  • 43,474
  • 48
  • 123
  • 161
423
votes
3 answers

Printing all global variables/local variables?

How can I print all global variables/local variables? Is that possible in gdb?
cpuer
  • 7,413
  • 14
  • 35
  • 39