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
79
votes
6 answers

Can we have recursive macros?

I want to know if we can have recursive macros in C/C++? If yes, please provide a sample example. Second thing: why am I not able to execute the below code? What is the mistake I am doing? Is it because of recursive macros? # define pr(n) ((n==1)? 1…
user1367292
  • 1,029
  • 2
  • 11
  • 13
79
votes
5 answers

How do I get the unix timestamp in C as an int?

I would like to get the current timestamp and print it out using fprintf.
Tim
  • 6,079
  • 8
  • 35
  • 41
79
votes
5 answers

Why are string literals l-value while all other literals are r-value?

C++03 5.1 Primary expressions §2 says: A literal is a primary expression. Its type depends on its form (2.13). A string literal is an lvalue; all other literals are rvalues. Similarly, C99 6.5.1 §4 says: A string literal is a primary expression.…
Alok Save
  • 202,538
  • 53
  • 430
  • 533
78
votes
9 answers

The real difference between "int" and "unsigned int"

int: The 32-bit int data type can hold integer values in the range of −2,147,483,648 to 2,147,483,647. You may also refer to this data type as signed int or signed. unsigned int : The 32-bit unsigned int data type can hold integer values in the…
Fabricio
  • 7,705
  • 9
  • 52
  • 87
78
votes
13 answers

How do you introduce unit testing into a large, legacy (C/C++) codebase?

We have a large, multi-platform application written in C. (with a small but growing amount of C++) It has evolved over the years with many features you would expect in a large C/C++ application: #ifdef hell Large files that make it hard to isolate…
mpontillo
  • 13,559
  • 7
  • 62
  • 90
78
votes
13 answers

Is there any practical use for a function that does nothing?

Would there be any use for a function that does nothing when run, i.e: void Nothing() {} Note, I am not talking about a function that waits for a certain amount of time, like sleep(), just something that takes as much time as the compiler /…
Epidem7c
  • 877
  • 1
  • 3
  • 7
78
votes
7 answers

Does const-correctness give the compiler more room for optimization?

I know that it improves readability and makes the program less error-prone, but how much does it improve the performance? And on a side note, what's the major difference between a reference and a const pointer? I would assume they're stored in the…
slartibartfast
  • 4,348
  • 5
  • 31
  • 46
78
votes
9 answers

Override a function call in C

I want to override certain function calls to various APIs for the sake of logging the calls, but I also might want to manipulate data before it is sent to the actual function. For example, say I use a function called getObjectName thousands of times…
dreamlax
  • 93,976
  • 29
  • 161
  • 209
78
votes
14 answers

How to iterate over a string in C?

Right now I'm trying this: #include int main(int argc, char *argv[]) { if (argc != 3) { printf("Usage: %s %s sourcecode input", argv[0], argv[1]); } else { char source[] = "This is an example."; int…
Vincent
  • 3,965
  • 2
  • 22
  • 30
78
votes
4 answers

error LNK2005: xxx already defined in MSVCRT.lib(MSVCR100.dll) C:\something\LIBCMT.lib(setlocal.obj)

I'm using DCMTK library for reading Dicom files (Image format used in medical image processing.) I'm having a problem in compiling this DCMTK source code. DCMTK uses some additional external libraries (zlib, tiff, libpng, libxml2, libiconv). I know…
volpack
  • 835
  • 1
  • 8
  • 7
78
votes
13 answers

Unusual usage of .h file in C

During reading article about filtering, I've found some strange using of .h file - use it for filling array of coefficients: #define N 100 // filter order float h[N] = { #include "f1.h" }; //insert coefficients of filter float x[N]; float…
artsin
  • 1,434
  • 2
  • 13
  • 29
78
votes
16 answers

Hiding members in a C struct

I've been reading about OOP in C but I never liked how you can't have private data members like you can in C++. But then it came to my mind that you could create 2 structures. One is defined in the header file and the other is defined in the source…
Marlon
  • 19,924
  • 12
  • 70
  • 101
78
votes
4 answers

How do I use setsockopt(SO_REUSEADDR)?

I am running my own http server on a raspberry pi. The problem is when I stop the program and restart it, the port is no longer available. Sometimes I get the same issue when receiving lots of requests. I want to use SO_REUSEADDR so that I can keep…
user3735849
  • 893
  • 1
  • 6
  • 6
78
votes
2 answers

How do I print a non-null-terminated string using printf?

How can I print a non-null-terminated string using printf, assuming that I know the length of the string at runtime?
Mike
  • 23,892
  • 18
  • 70
  • 90
78
votes
5 answers

Linux Kernel: System call hooking example

I'm trying to write some simple test code as a demonstration of hooking the system call table. "sys_call_table" is no longer exported in 2.6, so I'm just grabbing the address from the System.map file, and I can see it is correct (Looking through the…
Stephen
  • 4,176
  • 2
  • 24
  • 29