C Language Cheatsheet 📝👨‍💻



C is a powerful and widely used programming language known for its efficiency, flexibility, and low-level system access. Developed in the early 1970s at Bell Labs, it has influenced many other programming languages and remains fundamental in the world of software development.

Hello World Program

#include <stdio.h>

int main() {
printf("Hello, World!\n");
return 0;
  • #include <stdio.h>: This line includes the standard input-output header file for input and output operations.

  • int main() { ... }: Every C program starts with the main function. It’s where the program begins its execution. int specifies that the function will return an integer.

  • printf("Hello, World!\n");: printf is used for formatted output. In this case, it prints “Hello, World!\n” to the console.

  • return 0;: Indicates that the program has terminated successfully.

Variables and Data Types

int age = 25;
float height = 5.8;
char grade = 'A';
  • int: Integer data type.
  • float: Floating-point data type.
  • char: Character data type.


#define PI 3.14
const int MAX_VALUE = 100;
  • #define: Used for defining constants.
  • const: Used for defining constant variables.

Input from User

int num;
printf("Enter a number: ");
scanf("%d", &num);
  • scanf("%d", &num);: Reads an integer from the user and stores it in the variable num.

Conditional Statements

if (condition) {
// Code if condition is true
} else {
// Code if condition is false

if-else: Used for decision making.


a. For Loop
for (int i = 0; i < 5; i++) {
// Code to be repeated
  • for(initialization; condition; increment): Used for iterating a block of code.
b. While Loop
while (condition) {
// Code to be repeated
  • while(condition): Continues executing as long as the condition is true.
c. Do-While Loop

do {
// Code to be repeated
} while (condition);

  • do-while: Similar to while, but ensures the code block is executed at least once.


int numbers[5] = {1, 2, 3, 4, 5};
  • int numbers[5]: Declares an integer array of size 5.
  • {1, 2, 3, 4, 5}: Initializes the array with these values.


int add(int a, int b) {
return a + b;

int add(int a, int b): Defines a function named add that takes two integer arguments and returns an integer.


int num = 10;
int *ptr = &num;

  • int *ptr: Declares a pointer to an integer.
  • &num: Gets the memory address of the variable num.


struct Person {
char name[50];
int age;

struct Person { ... }: Defines a structure named Person with two members: name and age.

File Handling

FILE *file = fopen(“example.txt”, “r”);
if (file != NULL) {
// File operations

  • FILE *file = fopen("example.txt", "r");: Opens a file named example.txt in read mode.

  • fclose(file);: Closes the file.

Dynamic Memory Allocation

int *ptr = (int *)malloc(sizeof(int));

  • malloc(sizeof(int)): Allocates memory for an integer.

Leave a Comment

Your email address will not be published. Required fields are marked *