C (programming language) facts for kids
![]() Logotype used on the cover of the first edition of The C Programming Language
|
|
Paradigm | Multi-paradigm: imperative (procedural), structured |
---|---|
Designed by | Dennis Ritchie |
Developer | ANSI X3J11 (ANSI C); ISO/IEC JTC 1 (Joint Technical Committee 1) / SC 22 (Subcommittee 22) / WG 14 (Working Group 14) (ISO C) |
First appeared | 1972 |
Stable release |
C23 / October 31, 2024
|
Preview release |
C2y (N3220) / February 21, 2024
|
Typing discipline | Static, weak, manifest, nominal |
OS | Cross-platform |
Filename extensions | .c, .h |
Major implementations | |
pcc, GCC, Clang, Intel C, C++Builder, Microsoft Visual C++, Watcom C | |
Dialects | |
Cyclone, Unified Parallel C, Split-C, Cilk, C* | |
Influenced by | |
B (BCPL, CPL), ALGOL 68, PL/I, Fortran | |
Influenced | |
Numerous: AMPL, AWK, csh, C++, C--, C#, Objective-C, D, Go, Java, JavaScript, JS++, Julia, Limbo, LPC, Perl, PHP, Pike, Processing, Python, Rust, Seed7, V (Vlang), Vala, Verilog (HDL), Nim, Zig | |
|
C is a powerful programming language created in the 1970s by Dennis Ritchie. It is still very popular and important today. C lets programmers control computer hardware very closely. This makes it great for building operating systems, device drivers, and other core computer programs. C works on all kinds of computers, from huge supercomputers to tiny embedded systems found in everyday devices.
C was developed at Bell Labs between 1972 and 1973. It was made to help build the Unix operating system. Over time, C became one of the most widely used programming languages. You can find C compilers (programs that turn C code into computer instructions) for almost every computer system. A famous book called The C Programming Language helped set the standard for C for many years. Since 1989, C has been officially standardized by groups like American National Standards Institute (ANSI) and International Organization for Standardization (ISO).
C is an imperative language, meaning it uses commands to tell the computer what to do step-by-step. It supports structured programming, which helps organize code. C was designed to be turned into machine instructions (the computer's direct language) very efficiently. This means C programs run fast and use little memory. Even though it works closely with hardware, C is also designed to work on many different types of computers without many changes.
C doesn't have some features found in newer languages, like object-oriented programming or automatic memory cleanup. However, you can add these features using special libraries (collections of code).
C has been one of the top programming languages for a long time. It is popular because it is fast, efficient, uses little memory, and is simple. C programs use much less energy than programs written in languages like Python or Java.
Contents
How C Works
C code has some special rules and features:
- You can write C code freely, not in a strict line-by-line way.
- Each command (statement) usually ends with a semicolon (`;`).
- Curly braces (`{ }`) are used to group commands together into blocks.
- All the main code is inside special blocks called functions.
- When you give information to a function, it's usually a copy (pass by value). You can also pass a "pointer" to change the original information.
- C has a small number of special words (keywords) that have specific meanings.
- It has ways to control the flow of your program, like `if` (for choices), `for`, `do`, `while` (for repeating actions), and `switch` (for multiple choices).
- C has many operators for math, logic, and other tasks, like `+`, `-`, `*`, `/`, `==` (for checking if two things are equal), and `&&` (for "and").
- You can declare a variable (a place to store data) inside a block, and it will only work within that block.
- A function can call itself (this is called recursion).
- Every variable in C has a specific type (like `int` for whole numbers or `char` for single letters).
- You can create your own data types using `typedef`.
- Arrays (lists of data) use square brackets, like `month[11]`.
- C lets you work directly with computer memory using pointers.
- It has a preprocessor that helps prepare your code before it's compiled. This includes adding other code files (`#include`) and defining shortcuts (`#define`).
- C programs can be built in parts (modules), making them easier to manage.
- The main C language is small, but it has a large standard library for common tasks like input/output (I/O) and math.
"Hello, World!" Example
The "Hello, World!" program is a classic example used to introduce new programmers to a language. It simply prints "hello, world" on the screen.
Here is a modern version of the "Hello, World!" program in C:
#include <stdio.h>
int main(void)
{
printf("hello, world\n");
}
Let's break down what this code does:
- `#include <stdio.h>`: This line tells the C preprocessor to include the `stdio.h` file. This file contains important information for input and output functions, like `printf`.
- `int main(void)`: This declares the main function, which is where your program starts running. `int` means it will return a whole number, and `void` means it doesn't take any special information when it starts.
- `{`: This curly brace marks the beginning of the code inside the `main` function.
- `printf("hello, world\n");`: This line calls the `printf` function. `printf` displays text on the screen. The text inside the quotes, `"hello, world\n"`, is what will be shown. The `\n` means "start a new line" after printing.
- `;`: This semicolon marks the end of the `printf` command.
- `}`: This curly brace marks the end of the `main` function.
History of C
C's history is closely linked to the Unix operating system. Unix was first written in a very basic computer language called assembly language.
From B to C
In the early 1970s, Ken Thompson wanted a better language for Unix. He created a simpler version of another language called BCPL and named it B. However, B was too slow and couldn't use all the features of the new computers they were using.
In 1971, Dennis Ritchie started improving B. He added new features, like a way to handle single characters. He called this "New B." As Thompson used New B to rewrite parts of Unix, his needs helped shape the language. By 1972, more advanced features were added, like pointers and different types of arrays. The language was then renamed C.
The C compiler and some tools made with C were included in early versions of Unix. By 1973, a large part of the Unix operating system was rewritten in C. This was a big deal because most operating systems at the time were written in assembly language.
K&R C
In 1978, Brian Kernighan and Dennis Ritchie published a book called The C Programming Language. This book, often called K&R, became the unofficial guide for the C language for many years. The version of C described in this book is known as "K&R C" or "C78."
K&R C introduced important features like a standard way to handle input and output (I/O) and new data types. Even after official standards came out, many programmers still wrote code in K&R C to make sure it would work on older systems.
Official C Standards
As C became more popular, different versions appeared. To make sure C programs worked the same everywhere, the American National Standards Institute (ANSI) started creating an official standard in 1983.
- ANSI C (C89/C90): In 1989, the first official C standard was released by ANSI. This version is often called "ANSI C" or "C89." In 1990, the International Organization for Standardization (ISO) adopted it, calling it "C90." So, C89 and C90 are basically the same. This standard added new features like function prototypes (which help check for errors when calling functions). Most modern C code is based on C89.
- C99: In 1999, a new standard called "C99" was published. It added more features, such as `inline` functions (which can make programs faster), new data types (like `long long int` for very large numbers), and a way to add comments using `//` (like in C++).
- C11: In 2011, the "C11" standard was released. It brought important updates like better support for Unicode (for different languages and symbols), atomic operations (for safe multi-tasking), and multi-threading (for programs that do many things at once).
- C17: Published in 2018, "C17" was a smaller update. It mainly fixed issues and clarified parts of the C11 standard, without adding major new features.
- C23: The latest major C standard, "C23," was published in October 2024. It includes new keywords and types, like `nullptr_t` (for a special "nothing" value) and `_BitInt(N)` (for numbers with a specific number of bits).
Embedded C
Sometimes, C is used for "embedded programming," which means writing code for small computer systems inside other devices (like in a washing machine or a car). For these special uses, C often needs extra features to handle specific hardware. In 2008, the C Standards Committee released a report to help standardize these features for embedded C.
How C Code is Structured
C has a formal set of rules for how you write code. You can add comments to your code to explain it. Comments start with `/*` and end with `*/`, or (since C99) they can start with `//` and go to the end of the line.
C source files contain declarations (where you tell the computer about types and variables) and function definitions (where you write the actual code). Blocks of code are put inside curly braces (`{ }`).
C uses "statements" to tell the computer what to do. The most common statement is an "expression statement," which is a calculation or action followed by a semicolon. C also has special statements to control the program's flow, like `if` for making decisions and `for` or `while` for repeating actions.
C's Data Types
C has different types of data it can work with. These include:
- Integers: Whole numbers (like 1, 5, -10). They can be different sizes (`short`, `int`, `long`, `long long`) and can be signed (positive or negative) or unsigned (only positive).
- Floating-point numbers: Numbers with decimal points (like 3.14, -0.5). These are `float` or `double`.
- Characters: Single letters or symbols (`char`).
- Boolean: (Since C99) A type that can only be `true` or `false`.
- Arrays: Lists of items of the same type.
- Pointers: Special variables that store memory addresses.
- Structures (`struct`): Ways to group different types of data together under one name.
- Unions (`union`): Similar to structures, but all members share the same memory location.
Pointers
Pointers are a very important part of C. A pointer is like a special variable that holds the memory address of another variable or a function. Think of it like a house number that tells you where to find something in the computer's memory.
You can use a pointer to "dereference" it, which means looking at or changing the data stored at that memory address. Pointers are used for many things in C:
- Working with text strings.
- Managing dynamic memory (memory that your program asks for while it's running).
- Building complex data structures like trees or linked lists.
- Passing functions as arguments to other functions.
A "null pointer" means it doesn't point to any valid location. This is useful for showing that something isn't there or that an error happened.
Using pointers carefully is important because they give you direct access to memory. If you use them incorrectly, it can cause problems in your program.
Arrays
Arrays in C are like lists that hold many items of the same type. For example, an array of `int` can hold many whole numbers. In older C versions, arrays had a fixed size set when you wrote the program. However, C99 added "variable-length arrays" that can change size while the program runs.
When you access an item in an array, like `myArray[0]`, C usually doesn't check if you are trying to go outside the array's boundaries. This means you need to be careful not to access memory that doesn't belong to your array, which can lead to errors.
C doesn't have a special way to declare "multi-dimensional arrays" (like a grid or table). Instead, you declare an array of arrays, which works the same way. These are often used for math problems involving matrices.
Memory Management
Managing computer memory is a key job for a programming language. C offers three main ways to handle memory for your program's data:
- Static Memory: This memory is set aside when your program is compiled. It stays available as long as your program is running.
- Automatic Memory: This memory is used for temporary data and is stored on the "stack." It is automatically freed up when the part of the code where it was declared finishes. This is easy to use, but the stack has limited space.
- Dynamic Memory: This memory is requested by your program while it is running, from a larger area called the "heap." You use functions like `malloc` to ask for it and `free` to release it when you're done. This is useful for data whose size isn't known until the program runs.
Most C programs use a mix of all three methods. Automatic and static memory are simpler because the computer manages them for you. Dynamic memory gives you more control but requires you to be careful to avoid "memory leaks" (when you forget to free memory) or using "dangling pointers" (pointers that point to memory that has already been freed).
C Libraries
C uses "libraries" to add more features. A library is a collection of functions and tools that you can use in your program. Each library usually has a "header file" (like `stdio.h`) that tells your program what functions are available.
The most important library is the C standard library. It comes with every C system and provides functions for common tasks like:
- Reading from and writing to files (`stdio.h`).
- Managing memory (`stdlib.h`).
- Doing math calculations (`math.h`).
- Working with text strings (`string.h`).
Many other libraries exist because C is so widely used. Often, parts of programs written in other languages (like Python or Java) will use C libraries for tasks that need to be very fast.
File Handling
Handling files and input/output (I/O) is done through libraries in C, not directly by the language itself. The standard library uses "streams" to handle data flow. A stream is like a flow of data that can come from or go to different devices, like your keyboard, screen, or a file on your hard drive. This system helps make I/O more efficient by temporarily storing data in a "buffer" before sending it.
Tools for C Programmers
Many tools help C programmers write better code and find mistakes:
- Lint: This tool checks your C code for common errors or questionable parts that might cause problems.
- Compilers with warnings: Modern C compilers can often warn you about code that looks valid but might be a mistake.
- Memory checking tools: Tools like Valgrind help find errors related to how your program uses memory, such as memory leaks.
Why C is Used
C is used for many important tasks, especially in "systems programming" (building the core parts of computers).
For System Programming
C is popular for building operating systems and embedded systems for several reasons:
- Direct Hardware Access: C lets you directly control computer hardware and memory. This is crucial for setting up system features.
- Simple to Run: C code doesn't need many special features to run, making it easy to start up a system.
- Fast and Efficient: C code translates very well into the computer's own instructions, so it runs quickly and uses few resources.
- Uses CPU Features: C can use many of the special features of a computer's processor.
- Handles Data Structures: It's easy to organize and work with complex data, like file systems, using C.
- Powerful Operators: C has many operators for math and logic, which helps process data effectively.
- Small Language: C is a relatively small language, making it easier to understand.
- Predictable Performance: C gives you direct control over memory, which helps programs run smoothly without unexpected pauses.
- Works with Other Code: C can easily work with code written in assembly language or other high-level languages.
- Large Ecosystem: C has many existing libraries, tools, and compilers, making it a common choice.
For Fast Libraries
C is great for creating parts of programs that need to be very fast. Many important math and science libraries, like the GNU Scientific Library, are written in C. Other languages, like Python, often use C for their high-performance parts.
Games
Many computer games, especially those needing top performance, have used C. For example, the classic game Doom (from 1993) was built using C.
As an Intermediate Language
Sometimes, other programming languages are first translated into C code, and then that C code is compiled. This helps make those languages work on many different types of computers.
Other Languages Built with C
Because C is so widely available and efficient, many compilers and interpreters for other popular programming languages are written in C. For example, the main versions of Python, Perl, Ruby, and PHP are all written in C.
Web Servers
Two of the most popular web servers, Apache HTTP Server and Nginx, are written in C. These servers handle requests from web browsers and deliver web content very quickly, thanks to C's close-to-hardware approach.
Limitations of C
Even though C is very popular and powerful, it has some challenges:
- Memory Errors: Managing memory with `malloc` and `free` can be tricky. If not done carefully, it can lead to "memory leaks" (wasted memory) or "dangling pointers" (pointers that point to invalid memory).
- Direct Memory Access: While powerful, direct memory access means it's possible to accidentally corrupt memory if there are programming errors.
- Weak Type Checking: C checks data types, but it's not as strict as some other languages. This means you can sometimes accidentally mix types, which can lead to unexpected results.
- Programmer's Responsibility: C compilers don't add many safety checks to the final program. This means the programmer has to be very careful to prevent errors like buffer overruns (when a program tries to write too much data into a small space).
- Optimization Challenges: Because pointers can point to different places, it's sometimes harder for C compilers to make the code as fast as possible compared to languages like Fortran.
- String Handling: Working with text strings in C can be complicated, often requiring manual memory management.
- No Built-in Object Orientation: C doesn't directly support object-oriented programming features like classes.
- Error Handling: C mainly uses "return codes" to show errors, which can be less convenient than "exception handling" found in other languages.
To help with these issues, special guidelines like MISRA C are used in some areas (like embedded systems) to write safer C code. Many modern C compilers also provide warnings to help programmers find potential bugs.
Languages Related to C
Many languages created after C were inspired by it and borrowed parts of its design. These include C++, C#, Java, JavaScript, Python, Perl, and PHP. Often, these languages use C's way of writing commands and expressions, but they have different ways of handling data and organizing large programs.
When object-oriented programming became popular, C++ and Objective-C were created as extensions of C to add these new features. Both were originally designed to be translated into C code first, and then compiled.
- C++: Originally called "C with Classes," C++ was designed by Bjarne Stroustrup. It adds stronger type checking and object-oriented features to C. C++ is almost a full superset of C, meaning most C code can also run as C++ code.
- Objective-C: This language adds object-oriented features to C, taking ideas from both C and Smalltalk. It is a strict superset of C, meaning all valid C code is also valid Objective-C code.
Other languages like Ch, Cilk, and Unified Parallel C are also very similar to C.
Images for kids
-
The cover of the book The C Programming Language, first edition, by Brian Kernighan and Dennis Ritchie
See also
In Spanish: C (lenguaje de programación) para niños