C Programming Language Tutorial
Master the C programming language from basic syntax to advanced concepts with practical examples, exercises, and projects.
Step-by-Step
Beginner friendly approach
Practical Examples
100+ code examples
Real Projects
Build portfolio projects
Introduction to C Programming
C is a general-purpose, procedural programming language developed by Dennis Ritchie at Bell Labs in the early 1970s. It is one of the most widely used programming languages of all time and forms the basis for many other languages like C++, Java, and C#.
History of C
- Developed by Dennis Ritchie between 1969 and 1973
- Originally designed for UNIX operating system
- ANSI standardized in 1989 (C89)
- ISO standardized in 1990 (C90)
- Latest standard is C18 (2018)
Why Learn C?
- Foundation for other programming languages
- Used in system programming and embedded systems
- High performance and efficiency
- Teaches fundamental programming concepts
- Widely used in operating systems and compilers
First C Program
Every C program must have a main() function. Here's the traditional "Hello, World!" program:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
Basic Syntax and Structure
C programs are composed of various tokens which can be keywords, identifiers, constants, string literals, and symbols.
| Component | Description | Example |
|---|---|---|
| Preprocessor Directives | Instructions for the preprocessor | #include <stdio.h> |
| Functions | Blocks of code that perform tasks | int main() { ... } |
| Variables | Named storage locations | int count = 10; |
| Statements | Instructions that perform actions | printf("Hello"); |
| Comments | Explanatory text ignored by compiler | // Single line comment |
Variables and Data Types
Variables are used to store data in memory. Each variable has a specific type, which determines the size and layout of the variable's memory.
#include <stdio.h>
int main() {
// Variable declaration and initialization
int age = 25;
float salary = 45000.50;
double pi = 3.1415926535;
char grade = 'A';
_Bool isEmployed = 1;
// Display values
printf("Age: %d\n", age);
printf("Salary: %.2f\n", salary);
printf("Grade: %c\n", grade);
printf("Is Employed: %d\n", isEmployed);
return 0;
}
C Data Types Reference
| Data Type | Keyword | Size (Bytes) | Range | Format Specifier |
|---|---|---|---|---|
| char | char |
1 | -128 to 127 or 0 to 255 | %c |
| unsigned char | unsigned char |
1 | 0 to 255 | %c |
| short | short or short int |
2 | -32,768 to 32,767 | %hd |
| unsigned short | unsigned short |
2 | 0 to 65,535 | %hu |
| int | int |
4 | -2,147,483,648 to 2,147,483,647 | %d or %i |
| unsigned int | unsigned int |
4 | 0 to 4,294,967,295 | %u |
| long | long or long int |
4 or 8 | -2,147,483,648 to 2,147,483,647 (4 bytes) -9.22×10¹⁸ to 9.22×10¹⁸ (8 bytes) |
%ld |
| unsigned long | unsigned long |
4 or 8 | 0 to 4,294,967,295 (4 bytes) 0 to 1.84×10¹⁹ (8 bytes) |
%lu |
| long long | long long |
8 | -9.22×10¹⁸ to 9.22×10¹⁸ | %lld |
| unsigned long long | unsigned long long |
8 | 0 to 1.84×10¹⁹ | %llu |
| float | float |
4 | ±1.2×10⁻³⁸ to ±3.4×10³⁸ (6-7 decimal digits precision) | %f |
| double | double |
8 | ±2.3×10⁻³⁰⁸ to ±1.7×10³⁰⁸ (15-16 decimal digits precision) | %lf |
| long double | long double |
10, 12, or 16 | ±3.4×10⁻⁴⁹³² to ±1.1×10⁴⁹³² (19-20 decimal digits precision) | %Lf |
| void | void |
N/A | No value | N/A |
| _Bool | _Bool (C99) |
1 | 0 (false) or 1 (true) | %d |
Type Modifiers
signed- Can be positive or negative (default for integers)unsigned- Only positive valuesshort- Smaller storage (at least 16 bits)long- Larger storage (at least 32 bits)long long- Very large storage (at least 64 bits)
Important Notes
- Size may vary by compiler and platform
- Use
sizeof()operator to check size on your system charcan be signed or unsigned by default (compiler dependent)intis usually the "natural" size for the processor- Always initialize variables before use
Key Takeaways
- C is a procedural, general-purpose programming language developed in the 1970s
- Every C program must have a
main()function - Variables must be declared before use with appropriate data types
- C has multiple data types with specific sizes and ranges
- Use meaningful variable names, proper indentation, and comments
- Data type sizes may vary by platform - use
sizeof()to check