C Programming language Cheatsheet

by anupmaurya
0 comment

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 languageC 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; }
Code language: C/AL (cal)
  • #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];
Code language: CSS (css)


datatype *pointername;
Code language: C/AL (cal)


struct structure_name { member definition; member definition; ... member definition; } [one or more structure variables]; struct structure_name variable name;
Code language: C/AL (cal)

3. Enumeration Data types

enum name{constant1, constant2, constant3, ....... };
Code language: C/AL (cal)

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;
Code language: C/AL (cal)


int x = 10; // declaring int variable and assigning value 10 to it char grade = 'A'; // declaring char variable and assigning value A to it
Code language: C/AL (cal)


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];
Code language: C/AL (cal)


int a[5] = {1,2,3,4,5};
Code language: C/AL (cal)

Two dimensional array:

data-type array-name[size][size];
Code language: C/AL (cal)


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


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
Code language: JavaScript (javascript)


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";
Code language: JavaScript (javascript)


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

const datatype <constant-name> = <constant-value>;
Code language: HTML, XML (xml)
#define <constant-name> <constant-value>
Code language: HTML, XML (xml)

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

if(conditional-expression) { //code }
Code language: JavaScript (javascript)

2. If-else

if(conditional-expression) { //code } else { //code }
Code language: JavaScript (javascript)

3. If-else-if ladder

if(conditional-expression-1) { //code } else if(conditional-expression-2) { //code } else if(conditional-expression-3) { //code } .... else { //code }
Code language: PHP (php)

4. Switch

switch(conditional-expression){ case value1: //code break; //optional case value2: //code break; //optional ... default: //code to be executed when all the above cases are not matched; }
Code language: C/AL (cal)


1. For

for(Initialization; Condition; Increment/decrement){ //code }
Code language: C/AL (cal)

2. While

while(condition){ //code }
Code language: C/AL (cal)

3. Do-While

do{ //code } while(condition);
Code language: C/AL (cal)


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){ //code } // calling a function function_name (parameters)
Code language: C/AL (cal)


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
Code language: C/AL (cal)


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
Code language: C/AL (cal)


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
Code language: C/AL (cal)

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 fclose(fptr);
Code language: C/AL (cal)
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