108
1. Basic Syntax
Structure of a C Program:
#include int main() { // Your code here return 0; }
2. Data Types
Type | Description | Example |
---|---|---|
int |
Integer type | int a = 10; |
float |
Floating point number | float f = 3.14; |
double |
Double precision floating point | double d = 9.81; |
char |
Character type | char c = 'A'; |
void |
No return type | void |
3. Input/Output
Print to Console:
printf("Hello, World!\n");
Scan Input:
int age; scanf("%d", &age);
4. Operators
Operator | Description | Example |
---|---|---|
+ |
Addition | a + b |
- |
Subtraction | a - b |
* |
Multiplication | a * b |
/ |
Division | a / b |
% |
Modulus | a % b |
++ |
Increment | a++ |
-- |
Decrement | a-- |
5. Control Structures
Conditional Statements
if-else Statement:
if (condition) { // code } else { // code }
switch Statement:
switch (expression) { case 1: // code break; case 2: // code break; default: // code }
Loops
for Loop:
for (int i = 0; i < 10; i++) { // code }
while Loop:
while (condition) { // code }
do-while Loop:
do { // code } while (condition);
6. Functions
Function Declaration & Definition:
int add(int a, int b) { return a + b; } Function Call:
int result = add(10, 20);
Passing by Reference:
void updateValue(int *x) { *x = 100; }
7. Arrays
One-dimensional Array:
int arr[5] = {1, 2, 3, 4, 5};
Multi-dimensional Array:
int matrix[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
8. Pointers
Declaration:
int *ptr;
Pointer Initialization:
int a = 10; int *ptr = &a;
Dereferencing a Pointer:
int value = *ptr; // value is 10
9. Strings
String Declaration:
char str[] = "Hello"; String Functions (from ): strlen(str): Get length of a string. strcpy(dest, src): Copy a string. strcat(str1, str2): Concatenate strings. strcmp(str1, str2): Compare two strings.
10. Structs
Define a Structure:
struct Person { char name[50]; int age; };
Accessing Struct Members:
struct Person person; strcpy(person.name, "John"); person.age = 25;
11. Dynamic Memory Allocation
Memory Allocation (using ):
malloc(): Allocate memory.
free(): Deallocate memory.
int *ptr = (int*)malloc(sizeof(int) * 5); // Allocate memory for 5 integers free(ptr); // Free allocated memory
12. File I/O
Opening a File:
FILE *fptr; fptr = fopen("file.txt", "r");
Writing to a File:
FILE *fptr = fopen("file.txt", "w"); fprintf(fptr, "Writing to a file"); fclose(fptr);
Reading from a File:
char buffer[100]; FILE *fptr = fopen("file.txt", "r"); fgets(buffer, 100, fptr); fclose(fptr);
13. Preprocessor Directives
Include Header Files:
#include
Macros:
#define PI 3.14
Conditional Compilation:
#ifdef DEBUG // code to compile if DEBUG is defined #endif
14. Commonly Used Library Functions
Function | Description | Library |
---|---|---|
printf() |
Prints to the console | <stdio.h> |
scanf() |
Reads input from the user | <stdio.h> |
malloc() |
Allocates dynamic memory | <stdlib.h> |
free() |
Frees allocated memory | <stdlib.h> |
strlen() |
Gets the length of a string | <string.h> |
strcpy() |
Copies one string to another | <string.h> |
strcmp() |
Compares two strings | <string.h> |
fopen() |
Opens a file | <stdio.h> |
fclose() |
Closes a file | <stdio.h> |
fgets() |
Reads a line from a file | <stdio.h> |
15. Common Escape Sequences
Escape Sequence | Description |
---|---|
\n |
Newline |
\t |
Tab |
\\ |
Backslash |
\" |
Double Quote |
\' |
Single Quote |
16. Comments
Single-line comment:
// This is a comment
Multi-line comment:
/* This is a multi-line comment */
17. Example Program
A simple program to find the sum of two numbers:
#include int main() { int num1, num2, sum; // Input two numbers printf("Enter two numbers: "); scanf("%d %d", &num1, &num2); // Calculate the sum sum = num1 + num2; // Output the result printf("The sum is: %d\n", sum); return 0; }
This cheat sheet provides a quick reference to basic concepts and syntax in the C programming language.