C Cheat Sheet & Quick Reference
C Cheat Sheet & Quick Reference
C
C quick reference cheat sheet that provides basic syntax and methods.
Ads by
Send feedback
AI Excel Course Just ₹99
Why this ad?
# Getting Started
hello.c
#include <stdio.h>
int main(void) {
printf("Hello World!\n");
return 0;
}
$ ./hello
Variables
https://quickref.me/c 1/22
09/01/2025, 23:55 C Cheat Sheet & Quick Reference
int x = 5;
int y = 6;
int sum = x + y; // add variables to sum
Constants
Best Practices
Comment
// this is a comment
printf("Hello World!"); // Can comment anywhere in file
Print text
https://quickref.me/c 2/22
09/01/2025, 23:55 C Cheat Sheet & Quick Reference
printf("a=%hX, b=%X, c=%lX\n", a, b, c);
// output => a=56 b=5CB c=1DAB83
Control the number of spaces
output result
20 345 700
56720 9999 20098
233 205 1
34 0 23
In %-9d, d means to output in 10 base, 9 means to occupy at least 9 characters width, and the width is not enough to fill with spaces, -
means left alignment
Strings
access string
modify string
printf("%s", greetings);
// prints "Jello World!"
printf("%s", greetings);
// print "Hell!"
NOTE: String literals might be stored in read-only section of memory. Modifying a string literal invokes undefined behavior. You can't
modify it.!
C does not have a String type, use char type and create an array of characters
https://quickref.me/c 3/22
09/01/2025, 23:55 C Cheat Sheet & Quick Reference
Condition
Ternary operator
Switch
int day = 4;
switch (day) {
case 3: printf("Wednesday"); break;
case 4: printf("Thursday"); break;
default:
printf("Weekend!");
}
// output -> "Thursday" (day 4)
While Loop
int i = 0;
while (i < 5) {
printf("%d\n", i);
i++;
}
NOTE: Don't forget to increment the variable used in the condition, otherwise the loop will never end and become an "infinite loop"!
Do/While Loop
int i = 0;
do {
printf("%d\n", i);
i++;
} while (i < 5);
For Loop
https://quickref.me/c 4/22
09/01/2025, 23:55 C Cheat Sheet & Quick Reference
Break out of the loop Break/Continue
int i = 0;
i++;
}
int i = 0;
if (i == 4) {
continue;
}
printf("%d\n", i);
}
Arrays
printf("%d", myNumbers[0]);
// output 25
printf("%d", myNumbers[0]);
https://quickref.me/c 5/22
09/01/2025, 23:55 C Cheat Sheet & Quick Reference
// add element
myNumbers[0] = 25;
myNumbers[1] = 50;
myNumbers[2] = 75;
myNumbers[3] = 100;
Enumeration Enum
enum week a, b, c;
enum week { Mon = 1, Tues, Wed, Thurs, Fri, Sat, Sun } a, b, c;
With an enumeration variable, you can assign the value in the list to it
scanf("%d", &day);
switch(day) {
case Mon: puts("Monday"); break;
case Tues: puts("Tuesday"); break;
case Wed: puts("Wednesday"); break;
case Thursday: puts("Thursday"); break;
default: puts("Error!");
}
User input
// Create an integer variable to store the number we got from the user
int myNum;
https://quickref.me/c 6/22
09/01/2025, 23:55 C Cheat Sheet & Quick Reference
// Output the number entered by the user
// create a string
char firstName[30];
// Ask the user to enter some text
printf("Enter your name: \n");
// get and save the text
scanf("%s", &firstName);
// output text
printf("Hello %s.", firstName);
memory address
printf("%p", &myAge);
// Output: 0x7ffe5367e044
create pointer
pointer variable
Dereference
# Operators
Arithmetic Operators
https://quickref.me/c 7/22
09/01/2025, 23:55 C Cheat Sheet & Quick Reference
+ Add x + y
- Subtract x - y
* Multiply x * y
/ Divide x / y
% Modulo x % y
++ Increment ++x
Assignment operator
x=5 x=5
x += 3 x=x+3
x -= 3 x=x-3
x *= 3 x=x*3
x /= 3 x=x/3
x %= 3 x=x%3
x &= 3 x=x&3
x |= 3 x=x|3
x ^= 3 x=x^3
x >>= 3 x = x >> 3
x <<= 3 x = x << 3
Comparison Operators
int x = 5;
int y = 3;
== equals x == y
!= not equal to x != y
Logical Operators
https://quickref.me/c 8/22
09/01/2025, 23:55 C Cheat Sheet & Quick Reference
&& and logical returns true if both statements are true x < 5 && x < 10
Operator Examples
Bitwise operators
& Bitwise AND operation, "AND" operation by binary digits (A & B) will get 12 which is 0000 1100
| Bitwise OR operator, "or" operation by binary digit (A | B) will get 61 which is 0011 1101
^ XOR operator, perform "XOR" operation by binary digits (A ^ B) will get 49 which is 0011 0001
~ Inversion operator, perform "inversion" operation by binary bit (~A) will get -61 which is 1100 0011
<< binary left shift operator A << 2 will get 240 which is 1111 0000
>> binary right shift operator A >> 2 will get 15 which is 0000 1111
# Data Types
Basic data types
https://quickref.me/c 9/22
09/01/2025, 23:55 C Cheat Sheet & Quick Reference
Data types
// create variables
int myNum = 5; // integer
float myFloatNum = 5.99; // floating point number
char myLetter = 'D'; // string
// High precision floating point data or numbers
double myDouble = 3.2325467;
// print output variables
printf("%d\n", myNum);
printf("%f\n", myFloatNum);
printf("%c\n", myLetter);
printf("%lf\n", myDouble);
void no type
%d or %i int integer
%c char character
int myNum = 5;
float myFloatNum = 5.99; // floating point number
char myLetter = 'D'; // string
// print output variables
https://quickref.me/c 10/22
09/01/2025, 23:55 C Cheat Sheet & Quick Reference
printf("%d\n", myNum);
printf("%f\n", myFloatNum);
printf("%c\n", myLetter);
# C Preprocessor
Preprocessor Directives
#pragma Issue special commands to the compiler using the standardized method
Predefined macros
__DATE__ The current date, a character constant in the format "MMM DD YYYY"
__LINE__ This will contain the current line number, a decimal constant
__STDC__ Defined as 1 when the compiler compiles against the ANSI standard
ANSI C defines a number of macros that you can use, but you cannot directly modify these predefined macros
#include <stdio.h>
int main() {
printf("File :%s\n", __FILE__);
printf("Date :%s\n", __DATE__);
printf("Time :%s\n", __TIME__);
printf("Line :%d\n", __LINE__);
https://quickref.me/c 11/22
09/01/2025, 23:55 C Cheat Sheet & Quick Reference
printf("ANSI :%d\n", __STDC__);
}
#define message_for(a, b) \
printf(#a " and " #b ": We love you!\n")
If the macro is too long to fit on a single line, use the macro continuation operator \
#include <stdio.h>
#define message_for(a, b) \
printf(#a " and " #b ": We love you!\n")
int main(void) {
message_for(Carole, Debra);
return 0;
}
When the above code is compiled and executed, it produces the following result:
When you need to convert a macro parameter to a string constant, use the string constant operator #
#include <stdio.h>
int main(void) {
int token34 = 40;
tokenpaster(34);
return 0;
}
defined() operator
#include <stdio.h>
int main(void) {
printf("Here is the message: %s\n", MESSAGE);
return 0;
}
Parameterized macros
int square(int x) {
return x * x;
https://quickref.me/c 12/22
09/01/2025, 23:55 C Cheat Sheet & Quick Reference
}
No spaces are allowed between the macro name and the opening parenthesis
#include <stdio.h>
#define MAX(x,y) ( (x) > (y) ? (x) : (y) )
int main(void) {
printf("Max between 20 and 10 is %d\n", MAX(10, 20));
return 0;
}
# C Function
Function declaration and definition
int main(void) {
printf("Hello World!");
return 0;
}
Declaration declares the function name, return type and parameters (if any)
// function declaration
void myFunction();
// main method
int main() {
myFunction(); // --> call the function
return 0;
}
Call function
// create function
void myFunction() {
printf("Good evening!");
}
https://quickref.me/c 13/22
09/01/2025, 23:55 C Cheat Sheet & Quick Reference
int main() {
myFunction(); // call the function
myFunction(); // can be called multiple times
return 0;
}
// Output -> "Good evening!"
// Output -> "Good evening!"
Function parameters
int main() {
myFunction("Liam");
myFunction("Jenny");
return 0;
}
// Hello Liam
// Hello Jenny
Multiple parameters
return 0;
}
// Hi Liam you are 3 years old.
// Hi Jenny you are 14 years old.
Return value
int myFunction(int x) {
return 5 + x;
}
int main() {
printf("Result: %d", myFunction(3));
return 0;
}
// output 8 (5 + 3)
two parameters
int main() {
printf("Result: %d", myFunction(5, 3));
// store the result in a variable
int result = myFunction(5, 3);
printf("Result = %d", result);
https://quickref.me/c 14/22
09/01/2025, 23:55 C Cheat Sheet & Quick Reference
return 0;
}
// result: 8 (5 + 3)
// result = 8 (5 + 3)
Recursive example
int main() {
int result = sum(10);
printf("%d", result);
return 0;
}
int sum(int k) {
if (k > 0) {
return k + sum(k -1);
} else {
return 0;
}
}
Mathematical functions
#include <math.h>
void main(void) {
printf("%f", sqrt(16)); // square root
printf("%f", ceil(1.4)); // round up (round)
printf("%f", floor(1.4)); // round up (round)
printf("%f", pow(4, 3)); // x(4) to the power of y(3)
}
cos(x) cosine
# C Structures
Create structure
https://quickref.me/c 15/22
09/01/2025, 23:55 C Cheat Sheet & Quick Reference
char myLetter; // member (char variable)
}; // end the structure with a semicolon
struct myStructure {
int myNum;
char myLetter;
};
int main() {
struct myStructure s1;
return 0;
}
Strings in the structure
struct myStructure {
int myNum;
char myLetter;
char myString[30]; // String
};
int main() {
struct myStructure s1;
strcpy(s1. myString, "Some text");
// print value
printf("my string: %s", s1.myString);
return 0;
}
int main() {
// Create a structure variable called myStructure called s1
struct myStructure s1;
// Assign values
to the members of s1
s1.myNum = 13;
s1.myLetter = 'B';
return 0;
}
https://quickref.me/c 16/22
09/01/2025, 23:55 C Cheat Sheet & Quick Reference
s2.myNum = 20;
Copy structure
struct myStructure s1 = {
13, 'B', "Some text"
};
Modify value
# file processing
https://quickref.me/c 17/22
09/01/2025, 23:55 C Cheat Sheet & Quick Reference
File processing function
There are many functions in the C library to open/read/write/search and close files
r+ Open a text file in read-write mode, allowing reading and writing of the file
w+ Open a text file in read-write mode, allowing reading and writing of the file
a+ Open a text file in read-write mode, allowing reading and writing of the file
#include <stdio.h>
void main() {
FILE *fp;
char ch;
fp = fopen("file_handle.c", "r");
while (1) {
ch = fgetc(fp);
if (ch == EOF)
break;
https://quickref.me/c 18/22
09/01/2025, 23:55 C Cheat Sheet & Quick Reference
printf("%c", ch);
}
fclose(fp);
}
After performing all operations on the file, the file must be closed with fclose()
#include <stdio.h>
void main() {
FILE *fp;
fp = fopen("file.txt", "w"); // open the file
#include <stdio.h>
void main() {
FILE *fp;
#include <stdio.h>
void main() {
FILE *fp;
fp = fopen("file1.txt", "w"); // open the file
fputc('a',fp); // write a single character to the file
fclose(fp); // close the file
}
#include <stdio.h>
#include <conio.h>
void main() {
FILE *fp;
char c;
clrscr();
fp = fopen("myfile.txt", "r");
https://quickref.me/c 19/22
09/01/2025, 23:55 C Cheat Sheet & Quick Reference
}
fclose(fp);
getch();
}
#include<stdio.h>
#include<conio.h>
void main() {
FILE *fp;
clrscr();
fp = fopen("myfile2.txt","w");
fputs("hello c programming",fp);
fclose(fp);
getch();
}
#include<stdio.h>
#include<conio.h>
void main() {
FILE *fp;
char text[300];
clrscr();
fp = fopen("myfile2.txt", "r");
printf("%s", fgets(text, 200, fp));
fclose(fp);
getch();
}
fseek()
#include <stdio.h>
void main(void) {
FILE *fp;
fp = fopen("myfile.txt","w+");
fputs("This is Book", fp);
rewind()
https://quickref.me/c 20/22
09/01/2025, 23:55 C Cheat Sheet & Quick Reference
#include <stdio.h>
#include <conio.h>
void main() {
FILE *fp;
char c;
clrscr();
fp = fopen("file.txt", "r");
getch();
}
// output
ftell()
#include <stdio.h>
#include <conio.h>
void main () {
FILE *fp;
int length;
clrscr();
fp = fopen("file.txt", "r");
fseek(fp, 0, SEEK_END);
length = ftell(fp); // return current position
fclose(fp);
getch();
}
// output
// file size: 18 bytes
https://quickref.me/c 21/22