0% found this document useful (0 votes)
5 views

notes

This C Programming Cheat Sheet provides an overview of basic syntax, data types, control structures, functions, arrays, pointers, structures, memory management, and file handling. It includes code examples for variable declaration, comments, print/scan functions, conditional statements, loops, and memory operations. The document serves as a quick reference for essential C programming concepts and functionalities.

Uploaded by

fokake3451
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

notes

This C Programming Cheat Sheet provides an overview of basic syntax, data types, control structures, functions, arrays, pointers, structures, memory management, and file handling. It includes code examples for variable declaration, comments, print/scan functions, conditional statements, loops, and memory operations. The document serves as a quick reference for essential C programming concepts and functionalities.

Uploaded by

fokake3451
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

C Programming Cheat Sheet

1. Basic Syntax

• Variables: Declare variables using types like int, float, char, double, etc.

c
CopyEdit
int x = 10;
float y = 5.5;

• Comments:

c
CopyEdit
// Single line comment
/* Multi-line
comment */

• Print/Scan Functions:

c
CopyEdit
printf("Hello, World!\n"); // Printing to console
scanf("%d", &x); // Reading input

2. Data Types:

• int: Integer (e.g., 1, -4, 10)


• float: Floating-point number (e.g., 3.14, -5.7)
• char: Character (e.g., 'a', 'b', '1')
• double: Double precision floating-point (more precise than float)

3. Control Structures:

• if-else:

c
CopyEdit
if (x > 10) {
printf("Greater than 10\n");
} else {
printf("Less or equal to 10\n");
}

• switch-case:

c
CopyEdit
switch (x) {
case 1:
printf("One\n");
break;
case 2:
printf("Two\n");
break;
default:
printf("Default case\n");
}

• Loops:
o for loop:

c
CopyEdit
for (int i = 0; i < 10; i++) {
printf("%d ", i);
}

o while loop:

c
CopyEdit
while (x < 10) {
printf("%d ", x);
x++;
}

4. Functions:

• Defining a function:

c
CopyEdit
int add(int a, int b) {
return a + b;
}

• Function call:

c
CopyEdit
int result = add(5, 3);

5. Arrays:

• Declaration:

c
CopyEdit
int arr[5] = {1, 2, 3, 4, 5};

• Accessing:

c
CopyEdit
printf("%d", arr[2]); // Outputs: 3

6. Pointers:
• Declaration & Usage:

c
CopyEdit
int x = 10;
int *ptr = &x;
printf("%d", *ptr); // Outputs: 10

• Pointers and Arrays:

c
CopyEdit
int arr[] = {1, 2, 3};
int *p = arr;
printf("%d", *(p + 1)); // Outputs: 2

7. Structures:

• Definition:

c
CopyEdit
struct Person {
char name[50];
int age;
};

struct Person p1 = {"Alice", 30};

8. Memory Management:

• malloc() and free():

c
CopyEdit
int *ptr = (int *)malloc(sizeof(int) * 5);
free(ptr);

9. File Handling:

• Opening, reading, writing:

c
CopyEdit
FILE *file = fopen("file.txt", "r");
char ch = fgetc(file);
fclose(file);

You might also like