Home Cheatsheet C Programming language Cheatsheet

C Programming language Cheatsheet

by anupmaurya
109 minutes read

The C programming language is a computer programming language that was developed to do system programming for the operating system UNIX and is an imperative programming language. C was developed in the early 1970s by Ken Thompson and Dennis Ritchie at Bell Labs.


  • scanf(“%d”, &x) — read value into the variable x from input stream
  • Printf(“%d”,x) — printf value to the output stream
  • gets(str) — reads a line from input stream into a variable

Sample C program

#include <stdio.h>    
int main(){    
printf("Hello World!!");    
return 0;   
  • #include is a keyword which is used to include the library file <stdio.h>.
  • <stdio.h> library file is used to read the data from terminal and to display the data on terminal. It has several in-built functions like printf(), scanf() etc.
  • main() function is the entry point of any C program.
  • printf and scanf are inbuilt library functions which are used for input and output in C language. They are defined in stdio.h header file.
  • return 0 is used to terminate the main() function and returns the value 0
  • // — single line comment
  • /* comments */ — Multi line comment

Data types

Basicint, char, float, double
Derivedarray, pointer, structure, union

1. Basic Data types

Data typeDescriptionRangeMemory SizeFormat specifier
intused to store whole numbers-32,768 to 32,7672 bytes%d
short intused to store whole numbers-32,768 to 32,7672 bytes%hd
long intused to store whole numbers-2,147,483,648 to 2,147,483,6474 bytes%li
floatused to store fractional numbers6 to 7 decimal digits4 bytes%f
doubleused to store fractional numbers15 decimal digits8 bytes%lf
charused to store a single characterone character1 bytes%c

2. Derived Data types


data-type array-name[size];


datatype *pointername;


struct structure_name {

   member definition;
   member definition;
   member definition;
} [one or more structure variables]; 

struct structure_name variable name;

3. Enumeration Data types

enum name{constant1, constant2, constant3, ....... };

4. Void Data types

Void specifies that there is no return value. Generally used when function returns no value, pointer with type as void represents the address of an object but not it’s type.



data-type variable-name = value;


int x = 10; // declaring int variable and assigning value 10 to it
char grade = 'A'; // declaring char variable and assigning value A to it


Integer Literal- decimal255
Integer Literal- octal0377
Integer Literal- hexadecimal0xFF
Float point Literal53.0f, 79.02
Character literals‘a’, ‘1’
String literals“OneCompiler”, “Foo”

Escape sequences

Escape sequenceDescription
\nNew line
\rCarriage Return
?Question mark
\tHorizontal tab
\vVertical tab
\fForm feed
Single quotation
Double quotation
\0Null character
\bBack space


One dimentional Array:

data-type array-name[size];


int a[5] = {1,2,3,4,5};

Two dimensional array:

data-type array-name[size][size];


int a[2][3] = {


Operator typeDescription
Arithmetic Operator+ , – , * , / , %
comparision Operator< , > , <= , >=, != , ==
Bitwise Operator& , ^ , |
Logical Operator&& , `
Assignment Operator= , += , -= , *= , /= , %=, <<=, >>=, &=, ^=, `
Ternary Operator? :
sizeof operatorsizeof()

Keywords(reserved words)

auto         double      int        struct
break        else        long       switch
case         enum        register   typedef
char         extern      return     union
const        float       short      unsigned
continue     for         signed     void
default      goto        sizeof     volatile
do           if          static     while


Identifiers are user defined names for variables, functions and arrays.


  • They must be less than or equal to 31 characters.
  • No special characters.
  • Must start with a letter or under score.
  • Can contain letters, digits, or underscore only.


Strings are an array of characters ended with null character. Enclosed in double quotes.

char str[]="onecompiler";


Constants are the fixed values. They can be declared in two ways as shown below:

const datatype <constant-name> = <constant-value>;
#define <constant-name> <constant-value>

Special characters

  • {} : specifies start and end of code blocks
  • [] : used for arrays
  • () : used for functions
  • , : used to seperate variables, constants etc
  • * : used for pointers
  • # : used as a macro processor.

Conditional Statements

1. If


2. If-else

} else {

3. If-else-if ladder

} else if(conditional-expression-2) {
} else if(conditional-expression-3) {
else {

4. Switch

case value1:    
 break;  //optional  
case value2:    
 break;  //optional  
 //code to be executed when all the above cases are not matched;    


1. For

for(Initialization; Condition; Increment/decrement){  

2. While


3. Do-While

} while(condition); 


Function is a sub-routine which contains set of statements.

// declaring a function
return_type function_name(parameters);

// defining a function
return_type function_name(parameters){  

// calling a function
function_name (parameters)


Pointer is a variable which holds the memory information(address) of another variable of same data type.

datatype *pointername;


int x = 10, *ptr;

/*ptr = x; // Error because ptr is adress and x is value
*ptr = &x;  // Error because x is adress and ptr is value */

ptr = &x; // valid because &x and ptr are addresses
*ptr = x; // valid because both x and *ptr values 


Structure is a user-defined data type where it allows you to combine data of different data types.

struct structure_name {

   member definition;
   member definition;
   member definition;
} [one or more structure variables]; 

struct structure_name variable name; //declaring structure variables


Union is a user-defined datatype similar to structs which allows to store different data types in the same memory location. In Unnions, one member can contain a value at any given time.

union union_name {
   member definition;
   member definition;
   member definition;
} [one or more union variables];  

union union_name variable name; // Declaring Union Variables

File handling

File operations like create, update, read, and deleting files which are stored on the local file system can be performed in C.

FILE *fptr; //declaring a pointer of type File
fptr = fopen("filename",mode); //opening a file
fscanf(fptr, "format specifier", data); //read a file
fprintf(fptr, "format specifier", data); //write a file
rOpens for reading.
rbOpens for reading in binary mode.
r+Opens for both reading and writing.
wOpens for writing.
wbOpens for writing in binary mode.
aOpens for append.
abOpens for append in binary mode.
w+Opens for both reading and writing.
wb+Opens for both reading and writing in binary mode.
rb+Opens for both reading and writing in binary mode.
a+Opens for both reading and appending.
ab+Opens for both reading and appending in binary mode.

Check out topic-wise tutorial C Programming Tutorial

If you have quires and suggestions put them in the comments.Hope this C Programming language Cheat-sheet useful to you, Share & Help Other to learn 🙂

You may also like

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.