C Programming Cheat Sheet

Basic Syntax

ConceptSyntaxExample
Include Header#include <header>#include <stdio.h>
Main Functionint main() { ... }int main() { return 0; }
Print Statementprintf("text");printf("Hello World");
Input Statementscanf("%d", &var);scanf("%d", &num);
Comments// single line /* multi */// This is comment

Data Types

TypeSizeRangeExample
int4 bytes-2,147,483,648 to 2,147,483,647int age = 25;
char1 byte-128 to 127char grade = 'A';
float4 bytes6 decimal precisionfloat price = 19.99;
double8 bytes15 decimal precisiondouble pi = 3.14159;
void0 bytesNo valuevoid func();

Operators

CategoryOperatorExampleDescription
Arithmetic+ - * / %a + bAddition, Subtraction, Multiplication, Division, Modulus
Relational== != < > <= >=a == bComparison operators
Logical&& || !a && bAND, OR, NOT
Assignment= += -= *= /= %=a += bAssignment operators
Bitwise& | ^ ~ << >>a & bBitwise operations

Control Structures

StructureSyntaxExample
If Statementif(condition) { ... }if(x > 0) { printf("Positive"); }
If-Elseif(c) { ... } else { ... }if(x > 0) { printf("Pos"); } else { printf("Neg"); }
Switchswitch(var) { case x: ... break; }switch(day) { case 1: printf("Mon"); break; }
For Loopfor(init; cond; inc) { ... }for(i=0; i<10; i++) { printf("%d", i); }
While Loopwhile(condition) { ... }while(x > 0) { x--; }
Do-Whiledo { ... } while(condition);do { scanf("%d", &x); } while(x != 0);

Functions

ConceptSyntaxExample
Function Declarationreturn_type name(type param);int add(int a, int b);
Function Definitionreturn_type name(type param) { ... }int add(int a, int b) { return a+b; }
Function Callname(arg1, arg2);result = add(5, 3);
Recursionfunction calls itselfint fact(int n) { if(n<=1) return 1; else return n*fact(n-1); }

Arrays

ConceptSyntaxExample
Array Declarationtype name[size];int arr[10];
Array Initializationtype name[] = {a, b, c};int nums[] = {1, 2, 3};
Access Elementname[index]arr[0] = 5;
2D Arraytype name[row][col];int matrix[3][3];

Pointers

ConceptSyntaxExample
Pointer Declarationtype *name;int *ptr;
Address Operator&variableptr = &var;
Dereference Operator*pointervalue = *ptr;
Pointer Arithmeticptr++, ptr--, ptr+nptr++; // move to next address

Strings

FunctionDeclarationPurposeExample
strlenint strlen(char *str);Get string lengthlen = strlen("hello");
strcpychar *strcpy(char *dest, char *src);Copy stringstrcpy(dest, src);
strcatchar *strcat(char *dest, char *src);Concatenate stringsstrcat(str1, str2);
strcmpint strcmp(char *str1, char *str2);Compare stringsresult = strcmp(s1, s2);
getschar *gets(char *str);Read stringgets(buffer);
putsint puts(char *str);Print stringputs("Hello");

Memory Management

FunctionDeclarationPurposeExample
mallocvoid *malloc(size_t size);Allocate memoryptr = malloc(10 * sizeof(int));
callocvoid *calloc(size_t n, size_t size);Allocate and initialize to 0ptr = calloc(10, sizeof(int));
reallocvoid *realloc(void *ptr, size_t size);Resize allocated memoryptr = realloc(ptr, 20 * sizeof(int));
freevoid free(void *ptr);Free allocated memoryfree(ptr);

Preprocessor Directives

DirectiveUsageExampleDescription
#includeInclude header files#include <stdio.h>Includes header files
#defineDefine macros#define PI 3.14159Defines constants/macros
#ifdefConditional compilation#ifdef DEBUG ... #endifCompile conditionally
#ifndefCheck if not defined#ifndef HEADER_H ... #endifGuard against multiple inclusion
#if #else #endifConditional compilation#if x>5 ... #else ... #endifConditional compilation

Structures and Unions

ConceptSyntaxExample
Structurestruct name { ... };struct Student { int id; char name[20]; };
Structure Variablestruct name var;struct Student s1;
Access Membervar.members1.id = 10;
Unionunion name { ... };union Data { int i; float f; };
Typedeftypedef old new;typedef struct Student Std;

File Operations

FunctionDeclarationPurposeExample
fopenFILE *fopen(char *name, char *mode);Open filefp = fopen("file.txt", "r");
fcloseint fclose(FILE *fp);Close filefclose(fp);
freadsize_t fread(void *ptr, size_t size, size_t n, FILE *fp);Read from filefread(buffer, 1, 10, fp);
fwritesize_t fwrite(void *ptr, size_t size, size_t n, FILE *fp);Write to filefwrite(buffer, 1, 10, fp);
fprintfint fprintf(FILE *fp, char *format, ...);Formatted writefprintf(fp, "%d", num);
fscanfint fscanf(FILE *fp, char *format, ...);Formatted readfscanf(fp, "%d", &num);