Comprehensive C Language Cheatsheet for Programmers
Comprehensive C Language Cheatsheet for Programmers
This cheatsheet serves as a concise reference for the key concepts of the C programming language, derived from the Tutorialspoint C Language Cheatsheet.
1. Basic Structure of a C Program
- Every C program consists of functions, with
main()
being the entry point. - Syntax:
#include <stdio.h>
int main() {
// code goes here
return 0;
}
2. Data Types
C has several built-in data types:
- int: Integer type
- float: Floating-point type
- double: Double-precision floating-point type
- char: Character type
Example:
int age = 25;
float weight = 70.5;
char grade = 'A';
3. Variables
- Variables must be declared before use.
- Syntax for declaration:
data_type variable_name;
Example:
int count;
float temperature;
4. Control Structures
a. Conditional Statements
- if statement:
if (condition) {
// code to execute if condition is true
}
b. Loops
- for loop:
for (initialization; condition; increment) {
// code to execute
}
Example:
for (int i = 0; i < 5; i++) {
printf("%d\n", i); // prints numbers 0 to 4
}
5. Functions
- Functions are blocks of code designed to perform a particular task.
- Syntax:
return_type function_name(parameters) {
// code
return value; // optional
}
Example:
int add(int a, int b) {
return a + b;
}
6. Arrays
- An array is a collection of variables of the same type.
- Declaration:
data_type array_name[size];
Example:
int numbers[5]; // array of 5 integers
7. Pointers
- Pointers hold the memory address of another variable.
- Declaration:
data_type *pointer_name;
Example:
int a = 10;
int *p = &a; // p points to the address of a
8. Input and Output
- Use
printf()
for output andscanf()
for input.
Example:
int num;
printf("Enter a number: ");
scanf("%d", &num);
9. Comments
- Comments are used to explain code and are ignored by the compiler.
- Single-line comment:
// comment
- Multi-line comment:
/* comment */
Conclusion
This cheatsheet covers the essentials of C programming, including the structure of a program, data types, control structures, functions, arrays, pointers, and basic input/output operations. Understanding these concepts is crucial for beginners to start coding in C effectively.