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
78
votes
3 answers

How was the first C compiler written?

Is it true that the first C compiler was written in C itself? Then, how was it executed and compiled? Or, was this compiler written in assembly language?
Jaskaran S.P
  • 1,055
  • 2
  • 14
  • 15
78
votes
10 answers

DEBUG macros in C++

I just encountered a DEBUG macro in C that I really like #ifdef DEBUG_BUILD # define DEBUG(x) fprintf(stderr, x) #else # define DEBUG(x) do {} while (0) #endif I'm guessing a C++ analogue would be :- #ifdef DEBUG_BUILD # define DEBUG(x) cerr <<…
user277465
78
votes
1 answer

Flushing buffers in C

Should fflush() not be used to flush a buffer even if it is an output stream? What is it useful for? How do we flush a buffer in general?
mrk
  • 3,061
  • 1
  • 29
  • 34
78
votes
5 answers

typeof operator in C

Is typeof in C, really an operator? I'm thinking because there is no polymorphism in C, that there is nothing to do at run-time. That is, the answer to typeof is known at compile-time. (I can't think of a use of typeof that would not be known at…
Doug
  • 2,783
  • 6
  • 33
  • 37
78
votes
1 answer

Best C/C++ Network Library

I haven't done work in C/C++ for a little bit and was just wondering what people's favorite cross platform libraries are to use. I'm looking for something that is a good quick and dirty library as well as a library that is a little more robust. …
Hortitude
  • 13,638
  • 16
  • 58
  • 72
78
votes
8 answers

Is there a one-line function that generates a triangle wave?

In a similar way that modulo generates a sawtooth wave. It doesn't have to be continuous. here is what i mean: int m = 10; int x = 0; int i = 0; while (i < m*3) { printf("%d ", x); x++; x = x % m; i++; } generates a sequence 0..9,…
willc2
  • 38,991
  • 25
  • 88
  • 99
78
votes
3 answers

"strlen(s1) - strlen(s2)" is never less than zero

I am currently writing a C program that requires frequent comparisons of string lengths so I wrote the following helper function: int strlonger(char *s1, char *s2) { return strlen(s1) - strlen(s2) > 0; } I have noticed that the function returns…
Adrian Monk
  • 1,061
  • 9
  • 17
78
votes
3 answers

What does the registerNatives() method do?

In java, what does the private static method registerNatives() of the Object class do?
Hubris
  • 1,842
  • 2
  • 18
  • 26
77
votes
7 answers

Strings and character with printf

I was confused with usage of %c and %s in the following C program: #include void main() { char name[] = "siva"; printf("%s\n", name); printf("%c\n", *name); } Output: siva s Why we need to use pointer to display a…
Aspire
  • 921
  • 1
  • 7
  • 15
77
votes
7 answers

Where to find "gmp.h"?

I am installing a library, and got this error message: xxxx@ubuntu$ make (cd num; make all) make[1]: Entering directory `/home/xxxx/num' make[1]: Nothing to be done for `all'. make[1]: Leaving directory `/home/xxxx/num' (cd itv; make all) make[1]:…
SoftTimur
  • 5,630
  • 38
  • 140
  • 292
77
votes
15 answers

What is the reason for using a double pointer when adding a node in a linked list?

The two code examples below both add a node at the top of a linked list. But whereas the first code example uses a double pointer the second code example uses a single pointer code example 1: struct node* push(struct node **head, int data) { …
a6h
  • 773
  • 2
  • 6
  • 5
77
votes
15 answers

When is an integer<->pointer cast actually correct?

The common folklore says that: The type system exists for a reason. Integers and pointers are distinct types, casting between them is a malpractice in the majority of cases, may indicate a design error and should be avoided. Even when such a cast…
Kos
  • 70,399
  • 25
  • 169
  • 233
77
votes
8 answers

Rerouting stdin and stdout from C

I want to reopen the stdin and stdout (and perhaps stderr while I'm at it) filehandles, so that future calls to printf() or putchar() or puts() will go to a file, and future calls to getc() and such will come from a file. 1) I don't want to…
Chris Lutz
  • 73,191
  • 16
  • 130
  • 183
77
votes
8 answers

Error: Address already in use while binding socket with address but the port number is shown free by `netstat`

I tried to bind my socket(server socket) at port number 8000. It worked and did the job for me. At the end of the code I close the socket as well. The very next instant I run my code again and it shows me that the address is already in use. I have…
Durin
  • 2,070
  • 5
  • 23
  • 37
77
votes
5 answers

Left shifting with a negative shift count

What exactly happens here? a << -5 Obviously it doesn't right shift. But the book I'm reading states: On one machine, this expression actually does a left shift of 27 bits My question is; why? What causes a left shift of 27 bits? And what exactly…
Ishq
  • 1,189
  • 1
  • 8
  • 10