C Cheatsheet

Prasun Das| July 1, 2022 at 5:56 AM | 8 minutes Read


C programming language was developed by Dennis Ritchie of Bell Labs in 1972 to improve on the already existing B programming language. Let’s take a quick glance at the important features of C to better understand its popularity.

Features -

  • Structured language
  • Portability
  • Code reusability and ability to extend and customise
  • General purpose programming language
  • Faster speed
  • Mid level programming language


A typical C program has the following structure :

Let’s see a C program to print a line of text :


In line 1, the header file stdio.h is called as it will enable us to do standard input and output functions in the program.

In line 2, int main() flags off the start of the execution of the program.

printf() is the standard output function while scanf() is the standard input function.

x is the variable which will store the value given by the user.

Note that %d is the placeholder corresponding to integer value.




A C keyword is a word having a predefined meaning to the C compiler. Also known as the reserved words, they can not be used as a variable name or constant name. There are only 32 keywords in C. 


A character denotes any alphabet, digit or special symbol used for representing information. Following are the valid numbers, alphabets and special symbols allowed in C -


  • C is a case sensitive language.
  • Every statement should end with a ;
  • Keywords can’t be used for any other purpose.
  • A variable can’t start with anything but an alphabet or underscore; no blank spaces allowed; no special symbol other than underscore.
  • Variable declaration is must before using it in the program.
  • All files should have the extension .c


Data types are basically declarations of variables which determine the size and the type of data associated with the variables.

Data types in C can further be classified into three categories -

Primary (primitive), User defined, Derived.

Refer the image below for the detailed classification :


The character combinations which consist of a backslash (/) followed by a letter or by a combination of digits are known as escape sequences. Generally the escape sequences are used for putting tabs, carriage returns, backspace, etc in the output stream.

A table of escape sequences and their purpose is given below:


Operators are specific symbols which help us to perform operations of logical and mathematical nature in the program.

 Operators are classified into:

  • Arithmetic
  • Relational
  • Logical
  • Bitwise
  • Assignment
  • Conditional
  • Special

Operators in C usually have the same utility and meaning as in C++.


Operator precedence determines the grouping of terms in an expression and decides how the expression is evaluated. Certain operators have higher precedence than others which implies that their intended operation is executed before the other.

The following table enlists the precedence of operators and their relative associativity -


A decision control structure is one which checks a particular condition and then decides whether a set of operations is to be carried out or not. C employs three major decision control structures namely - if statement, if else statement, switch statement.

if statement:

General form of if statement -

If the expression within if() holds true, the set of code gets executed or else it is just ignored during execution.

if else statement:

General form of if else statement -

Unlike the if statement, the if else statement can execute two different sets of code depending on the validity of the test expression. If the expression is true, one set of code will get executed and if the expression is false another set of code will get executed.

Nested if else:

General form -

When we write a complete if else construct within another if statement or the else statement it’s called nested if else structure.

else if statement:

General form of else if structure -

The expressions are evaluated in order; if an expression is true, the "statement" or "block of statement" associated with it is executed, and this terminates the whole chain. If not any condition holds true, the else block is executed by default.

switch statement:

General form-

The switch statement allows a variable to be tested for equality against a given list of values (cases). If no case matches the variable value the default case gets executed.

It is important to note that float expressions can not be tested using switch case statements.


Iterative statements facilitate programmers to execute any block of code lines repeatedly for a specific number of times as per the conditions imposed by the programmer. The popular iterative statements of C are discussed below,

while loop:

The code within the while loop gets executed as long as the condition being tested holds true. When the test condition returns false, the control moves to the first statement following the loop body.

General form:

do while loop:

The do while loop is an exit controlled loop unlike while loop. The code written within the loop gets executed first then the check condition is checked. This implies that the do while loop will execute at least once irrespective whether the test condition is true or false.

General form:

for loop:

The for loop is used for iterating a block of code as long as the specified condition allows it to.

General form:

Use of break and continue keyword:

The break keyword is used within loops to exit the loop to terminate the loop and exit it.

The continue keyword when used skips the current iteration of the loop and directly returns to the starting point of the loop.


Arrays are collections of multiple variables having the same data types. Note that the first element of an array is numbered 0. Also keep in mind that it’s essential to declare an array using its type and dimension before using it.

  • Array declaration:

data_type array_name[array_size];

Example: int marks[10];

The above line of code creates an array named “marks” with 10 elements of the integer data type.

We can also initialize an array while declaring it by:

int num[6] = { 2, 4, 12, 5, 45, 5 } ;

  • Accessing array elements:

int valueOfThirdElement = marks[2];

Note that marks[2] is the third element of the array and its value gets assigned to the respective variable.

  • Entering data into an array:

Let’s refer the below line of code to understand it -

The for loop causes the process of asking for and then receiving a student’s marks from the user to be repeated 30 times. The first time through the loop, i has a value 0, so the scanf() function will cause the value typed to be stored in the array element marks[0], which is the first element of the array. This whole process gets repeated until i becomes 29. 


Strings can be said to be an one dimensional array of characters. A string can be declared by -

char str_name[size];

We can then employ the scanf() function to take the input from the user. Keep in mind that the size of the string should not exceed the array dimension. Also, scanf() does not support multi word strings. To overcome this, use gets() function which allows multi word strings. puts() is used for displaying a single word string; use two puts() if the multi word string has two words and so on.


Here’s a list of some useful string functions provided by the string.h header file:


A block of code which can be called or used as and when required in a program is called a function. Functions make the program more robust and reusable. It is recommended to break a program into smaller units and use functions for each of them.

A function typically has: 

Return value – The value that the function returns at its end is the return value. This value must have the same data type as the one in function declaration. 

Parameters – These are the placeholders of the values passed on to them. 

Declaration – It specifies the name of the function, its return type, parameter list. 

When a function is called, a collection of statements are executed simultaneously.

Refer the below example for better understanding:

Important points to remember -

  • There’s no limit to the number of functions in a program, at least one function is necessary for the program.
  • If the program has one function , it must be the main() function.
  • Each function in the program is called in the sequence as specified in main().
  • After each function has done its job, the control returns back to the main().

Note: Use void type function when the function does not need to return a value.


Recursion is known as the technique of making a function call itself. This provides a way to break the complicated problems down into simple problems which are comparatively easier to solve.

Call by value recursion:

In the call by value method, the value of the actual parameters is first copied into the formal parameters. In other words, it can be said that the value of the variable is used in the function call in the call by value method. 

Call by reference recursion:


Here, in call by reference, the address of the variable is passed into the function call as the actual parameter. The value of the actual parameters can then be modified by changing the formal parameters since the address of the actual parameters is being passed.


A pointer is that variable which stores the memory address of another variable within it. It is created with the * operator. In C, the pointers enable us to manipulate the data in the computer’s memory itself. The pointer declaration is like -

<variable_type> *<name>;

Let’s see how we work with pointers by referring the below example

The address of variable c is assigned to the pc pointer; the variable c stores the value 5 in it. We can access the value stored in c by using *pc.


Math functions are those which allow us to perform several mathematical operations on numbers. Include the math.h header file to use them in the program.

sqrt() function returns the square root of the number;

pow(x,y) returns the value of x to the power y.

Other frequently used math functions include:


C structures(structs) are a way of grouping several related variables of similar or different data types in one place. Unlike arrays, it can handle variables with different data types.

You can create a structure by using the struct keyword and declare each of its members inside curly braces:

To access the structure elements, use the . operator.

Keep in mind that before the dot there must always be a structure variable; after the dot there must always be a structure element.


The most common use of file is via a file pointer.

FILE *filePointer;

The type FILE is not a basic type but defined in stdio.h

Opening a file - for creation and edit

Opening a file is performed using the fopen() function defined in the stdio.h header file.

The syntax for opening a file in standard I/O is:

For example,

Let's suppose the file newprogram.txt doesn't exist in the location E:\cprogram. The first function creates a new file named newprogram.txt and opens it for writing as per the mode 'w'.

The writing mode allows you to create and edit (overwrite) the contents of the file.

Now let's suppose the second binary file oldprogram.bin exists in the location E:\cprogram. The second function opens the existing file for reading in binary mode 'rb'.

Closing a File

The file (both text and binary) should be closed after reading/writing.

Closing a file is performed using the fclose() function.

Here, fptr is a file pointer associated with the file to be closed.

Reading and writing to a text file

For reading and writing to a text file, we use the functions fprintf() and fscanf().

They are just the file versions of printf() and scanf(). The only difference is that fprintf() and fscanf() expects a pointer to the structure FILE.

Example 1: Write to a text file

This program takes a number from the user and stores in the file program.txt.



After you compile and run this program, you can see a text file program.txt created in C drive of your computer. When you open the file, you can see the integer you entered.



#c#cheatsheet#fundamentals to learn
View Count:3.9k


Similar Articles

Busting myths about web3

WHAT IS WEB3?Web3 is the catch-all term for the vision of an upgraded version of the internet aimed at giving power back to the users. It is truly the......

Cover image


Operating Systems is one of the core subjects for a computer science student. As such a lot of important questions are asked on this subject in interv......

Cover image

Cloud Computing Cheatsheet

Getting started with cloud computing ?The words "cloud" and "web" will be interchangeable. What kind of business operates today without the internet? ...

Cover image

Software Engineering — SOFTWARE MAINTENANCE [Part-4]

SOFTWARE MAINTENANCE:Software Maintenance can be defined as the process of modifying a software product after its delivery. The main purpose of softwa...

Cover image

Software Engineering — Software Process Activities [Part - 3]

A quick summary upto what we did this far&nbsp; from this series:Software EngineeringSoftware engineering is a discipline of engineering concerned wit...

Cover image

Understanding Time Complexity with Interview Examples

Introduction:Understanding time complexity of a function is imperative for every programmer to execute a program in the most efficient manner. Time co...

Cover image

Software Engineering — Software Development lifecycle and software process(Part 2)

&nbsp;Software MethodologyA software methodology (which is also known as software process) is a set of related development Phases that leads to the pr...

Cover image

Software Engineering [Part 1] - Introduction

Software Engineering is a subject that requires when you are a software developer as well as when you are in your college preparing for a semester or ...

Cover image

SQL Interview Questions

Are you getting ready for your SQL developer job interview?You've come to the correct place.This tutorial will assist you in brushing up on your SQL s...

Cover image


Introduction: What is SQL?To understand SQL, we must first understand databases and database management systems (DBMS).Data is essentially a collectio...

Cover image

Computer Networking Cheatsheet (Last Minute Notes)

&nbsp;Networking: Importance in real life!Using a computer for work, as well as for personal use, ha...

Cover image

Last Minute Preparation Cheat Sheet for Quantitative Aptitude

FORMULA LIST:ALGEBRA :1.Sum of first n natural numbers = n(n+1)/22.Sum of the squares of first n nat...

Cover image

Database Management System Last Minute Notes [Part - 2]

ER-DiagramWhat are ER Models ?An Entity Relationship Diagram (ERD) is a visual representation of dif...

Cover image

Database Management System Last Minute Notes [Part - 1]

What is actually DBMS?A Database Management System (DBMS) is system software used to manage the orga...

Cover image

C Interview Questions — 2022

C is a general purpose high-level language most popular amongst coders, it is the most compatible, e...

Cover image

C++ STL Cheat Sheet

C++ programming language has templates which basically allows functions and classes to work with gen...

Cover image


C++ was conceived in the late 1970s to overcome the limitations of C. It&nbsp; is an extension of th...

Cover image

Python [Advanced] Interview Questions — 2022

Interview Questions are a great source to test your knowledge about any specific field. But remember...

Cover image

Basic Python [Core] Interview Questions for Freshers and Short Sample Answers — 2022

The most popular high-level, multipurpose programming language right now is Python.Python supports p...

Cover image

Python Cheat Sheet - Learn the basics of Python

IntroductionPython is a high-level programming language with dynamic binding and high-level inherent...

Cover image

Basic Java Interview Questions for Freshers and Short Sample Answers — 2022

Interview Questions are a great source to test your knowledge about any specific field. But remember...

Cover image

Java OOP Cheat Sheet — A Quick Guide to Object-Oriented Programming in Java

Object-Oriented Programming or better known as OOPs is one of the major pillars of Java that has lev...

Cover image

Learn Java: Basics to Advanced Concepts [Java Cheatsheet]

Java is a high-level programming language known for its robustness, object-oriented nature, enhanced...

Cover image