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
76
votes
19 answers

How do I get a specific range of numbers from rand()?

srand(time(null)); printf("%d", rand()); Gives a high-range random number (0-32000ish), but I only need about 0-63 or 0-127, though I'm not sure how to go about it. Any help?
akway
  • 1,738
  • 4
  • 21
  • 20
76
votes
8 answers

How to compile makefile using MinGW?

I'm new to this. Just wanted to ask how to compile a makefile. I am using MinGW compiler in C language. Do I have to save all my files in MinGW\bin? because right now my files are in a different directory. Appreciate the help.
AAA
  • 811
  • 2
  • 7
  • 7
76
votes
4 answers

What is the behavior of printing NULL with printf's %s specifier?

Came across an interesting interview question: test 1: printf("test %s\n", NULL); printf("test %s\n", NULL); prints: test (null) test (null) test 2: printf("%s\n", NULL); printf("%s\n", NULL); prints Segmentation fault (core dumped) Though this…
Deepanjan Mazumdar
  • 1,447
  • 3
  • 13
  • 20
76
votes
8 answers

What is the Cost of an L1 Cache Miss?

Edit: For reference purposes (if anyone stumbles across this question), Igor Ostrovsky wrote a great post about cache misses. It discusses several different issues and shows example numbers. End Edit I did some testing and…
Mark Wilkins
  • 40,729
  • 5
  • 57
  • 110
76
votes
7 answers

Passing an array by reference in C?

How can I pass an array of structs by reference in C? As an example: struct Coordinate { int X; int Y; }; SomeMethod(Coordinate *Coordinates[]){ //Do Something with the array } int main(){ Coordinate Coordinates[10]; …
Hannoun Yassir
  • 20,583
  • 23
  • 77
  • 112
76
votes
9 answers

Difference between array type and array allocated with malloc

Today I was helping a friend of mine with some C code, and I've found some strange behavior that I couldn't explain him why it was happening. We had TSV file with a list of integers, with an int each line. The first line was the number of lines the…
Jorge Leitao
  • 19,085
  • 19
  • 85
  • 121
75
votes
9 answers

Check glibc version for a particular gcc compiler

I have two gcc compilers installed on my system, one is gcc 4.1.2 (default) and the other is gcc 4.4.4. How can I check the libc version used by gcc 4.4.4, because /lib/libc.so.6 shows the glibc used by gcc 4.1.2, since it is the default compiler.
MetallicPriest
  • 29,191
  • 52
  • 200
  • 356
75
votes
2 answers

Difference between _Bool and bool types in C?

Can anyone explain me what is the difference between the _Bool and bool data type in C? For example: _Bool x = 1; bool y = true; printf("%d", x); printf("%d", y);
pr1m3x
  • 1,947
  • 6
  • 21
  • 19
75
votes
6 answers

Global variables in header file

I have a 2 modules (.c files) and one .h header file: file1.c: #include #include "global.h" int main() { i = 100; printf("%d\n",i); foo(); return 0; } file2.c #include #include "global.h" void foo() { i =…
Bruce
  • 33,927
  • 76
  • 174
  • 262
75
votes
6 answers

Can a C macro definition refer to other macros?

What I'm trying to figure out is if something such as this (written in C): #define FOO 15 #define BAR 23 #define MEH (FOO / BAR) is allowed? I would want the preprocessor to replace every instance of MEH with (15 / 23) but I'm not so sure that…
llakais
  • 1,577
  • 1
  • 12
  • 18
75
votes
2 answers

C/C++ global vs static global

Possible Duplicate: Static vs global I'm confused about the differences between global and static global variables. If static means that this variable is global only for the same file then why in two different files same name cause a name…
MoonBun
  • 4,322
  • 3
  • 37
  • 69
75
votes
4 answers

Passing an ellipsis to another variadic function

I have approximately 30 variadic functions. Each one accepts a path as the final argument, e.g.: bool do_foo(struct *f, int q, const char *fmt, ...) In each function, I have to check that the expanded format is less then or equal to a certain size.…
Tim Post
  • 33,371
  • 15
  • 110
  • 174
75
votes
5 answers

How to define relative paths in Visual Studio Project?

I have a library and a console application that uses a library. The library has a folder with source and header files. My project is in a child/inner directory but that library directory that I want to include is in a parent/upper directory. My…
Ali Ahmed
  • 1,749
  • 6
  • 20
  • 29
75
votes
4 answers

why does long long 2147483647 + 1 = -2147483648?

Why doesn't this code print same number? : long long a, b; a = 2147483647 + 1; b = 2147483648; printf("%lld\n", a); printf("%lld\n", b); I know that int variable's maximum number is 2147483647 because int variable is 4 byte. But as I know, long…
Hoseong Jeon
  • 1,240
  • 2
  • 12
  • 20
75
votes
7 answers

Why bit endianness is an issue in bitfields?

Any portable code that uses bitfields seems to distinguish between little- and big-endian platforms. See the declaration of struct iphdr in linux kernel for an example of such code. I fail to understand why bit endianness is an issue at all. As far…
Leonid99
  • 1,227
  • 2
  • 12
  • 10