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
77
votes
9 answers

How to get the number of CPUs in Linux using C?

Is there an API to get the number of CPUs available in Linux? I mean, without using /proc/cpuinfo or any other sys-node file... I've found this implementation using sched.h: int GetCPUCount() { cpu_set_t cs; CPU_ZERO(&cs); sched_getaffinity(0,…
Treviño
  • 2,999
  • 3
  • 28
  • 23
77
votes
7 answers

How to write a while loop with the C preprocessor?

I am asking this question from an educational/hacking point of view, (I wouldn't really want to code like this). Is it possible to implement a while loop only using C preprocessor directives. I understand that macros cannot be expanded recursively,…
Tarski
  • 5,360
  • 4
  • 38
  • 47
77
votes
16 answers

faster alternative to memcpy?

I have a function that is doing memcpy, but it's taking up an enormous amount of cycles. Is there a faster alternative/approach than using memcpy to move a piece of memory?
Tony Stark
  • 24,588
  • 41
  • 96
  • 113
77
votes
3 answers

Is a^a or a-a undefined behaviour if a is not initialized?

Consider this program: #include int main(void) { unsigned int a; printf("%u %u\n", a^a, a-a); return 0; } Is it undefined behaviour? On the face of it, a is an uninitialized variable. So that points to undefined behaviour.…
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
77
votes
7 answers

Poor memcpy Performance on Linux

We have recently purchased some new servers and are experiencing poor memcpy() performance. The memcpy() performance is 3x slower on the servers compared to our laptops. Server Specs Chassis and Mobo: SUPER MICRO 1027GR-TRF CPU: 2x Intel Xeon…
nick
  • 513
  • 1
  • 8
  • 12
77
votes
16 answers

How to get the real and total length of char * (char array)?

For a char [], I can easily get its length by: char a[] = "aaaaa"; int length = sizeof(a)/sizeof(char); // length=6 However, I cannot do like this to get the length of a char * by: char *a = new char[10]; int length =…
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
77
votes
10 answers

Array-size macro that rejects pointers

The standard array-size macro that is often taught is #define ARRAYSIZE(arr) (sizeof(arr) / sizeof(arr[0])) or some equivalent formation. However, this kind of thing silently succeeds when a pointer is passed in, and gives results that can seem…
nneonneo
  • 171,345
  • 36
  • 312
  • 383
77
votes
10 answers

How can I find the header files of the C programming language in Linux?

When I write C programs in Linux, and then compile them using gcc, I am always curious about where those header files are. For example, where stdio.h is. More generally, where is stdbool.h? What I want to know is not only where it is, but also how…
Yishu Fang
  • 9,448
  • 21
  • 65
  • 102
77
votes
16 answers

How can I find the number of elements in an array?

I have an int array and I need to find the number of elements in it. I know it has something to do with sizeof but I'm not sure how to use it exactly.
AndrewP
  • 1,057
  • 2
  • 8
  • 9
76
votes
4 answers

Returning from a void function

Which is more correct way to return from function: void function() { // blah some code } OR void function() { // blah some code return; } Rationale for second way: It expresses developer intentions more clearly. It helps detecting function…
Agnius Vasiliauskas
  • 10,935
  • 5
  • 50
  • 70
76
votes
13 answers

How can I break out of two nested for loops in Objective-C?

I have two for loops nested like this: for(...) { for(...) { } } I know that there is a break statement. But I am confused about if it breaks both loops or just the one in which it was called? I need to break both ones as soon as I see…
Thanks
  • 40,109
  • 71
  • 208
  • 322
76
votes
7 answers

When to use const char * and when to use const char []

I know they are different, I know how they are different and I read all questions I could find regarding char* vs char[] But all those answers never tell when they should be used. So my question is: When do you use const char *text = "text"; and…
rve
  • 5,897
  • 3
  • 40
  • 64
76
votes
5 answers

Why can a string be assigned to a char* pointer, but not to a char[] array?

Can someone explain why this works with the pointer: char * str1; str1 = "Hello1"; str1 = "new string"; // but not this char str2 [] = "hello"; str2 = "four"; // or this char str3 []; str3 = "hello"; str3 = "hello";
nick
  • 1,147
  • 1
  • 11
  • 12
76
votes
3 answers

How to do an specific action when a certain breakpoint is hit in GDB?

I am looking for a way to do some action when a particular break point hits in gdb. Basically I have some memleak in my program. When malloc and free function hits, I need to enter into the function (step) and collect some basic information like…
Thangaraj
  • 3,038
  • 7
  • 40
  • 43
76
votes
5 answers

How do you check if a directory exists on Windows in C?

Question In a Windows C application I want to validate a parameter passed into a function to ensure that the specified path exists.* How do you check if a directory exists on Windows in C? *I understand that you can get into race conditions where…
Zach Burlingame
  • 13,476
  • 14
  • 56
  • 65