0% found this document useful (0 votes)
3 views122 pages

C Programming Notes

A programming language is a set of instructions used by developers to communicate with computers, enabling the creation of applications and software. Programming languages are categorized into low-level, high-level, and middle-level types, each serving different purposes and offering various advantages. The C language, developed by Dennis Ritchie in 1972, is a general-purpose, procedure-oriented language known for its efficiency and portability, widely used in system software and operating systems.

Uploaded by

samywuwa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views122 pages

C Programming Notes

A programming language is a set of instructions used by developers to communicate with computers, enabling the creation of applications and software. Programming languages are categorized into low-level, high-level, and middle-level types, each serving different purposes and offering various advantages. The C language, developed by Dennis Ritchie in 1972, is a general-purpose, procedure-oriented language known for its efficiency and portability, widely used in system software and operating systems.

Uploaded by

samywuwa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Programming Language

As we know, to communicate with a person, we need a specific language, similarly to


communicate with computers, programmers also need a language is called
Programming language.

Before learning the programming language, let's understand what is language?

What is Language?
Language is a mode of communication that is used to share ideas, opinions with each
other. For example, if we want to teach someone, we need a language that is
understandable by both communicators.

What is a Programming Language?


A programming language is a computer language that is used by programmers
(developers) to communicate with computers. It is a set of instructions written in any
specific language ( C, C++, Java, Python) to perform a specific task.

A programming language is mainly used to develop desktop applications, websites,


and mobile applications.

Types of programming language


1. Low-level programming language
Low-level language is machine-dependent (0s and 1s) programming language. The
processor runs low- level programs directly without the need of a compiler or
interpreter, so the programs written in low-level language can be run very fast.

Low-level language is further divided into two parts -

i. Machine Language

Machine language is a type of low-level programming language. It is also called


as machine code or object code. Machine language is easier to read because it is
normally displayed in binary or hexadecimal form (base 16) form. It does not require a
translator to convert the programs because computers directly understand the machine
language programs.
The advantage of machine language is that it helps the programmer to execute the
programs faster than the high-level programming language.

ii. Assembly Language

Assembly language (ASM) is also a type of low-level programming language that is


designed for specific processors. It represents the set of instructions in a symbolic and
human-understandable form. It uses an assembler to convert the assembly language
to machine language.

The advantage of assembly language is that it requires less memory and less execution
time to execute a program.

2. High-level programming language


High-level programming language (HLL) is designed for developing user-friendly
software programs and websites. This programming language requires a compiler or
interpreter to translate the program into machine language (execute the program).

The main advantage of a high-level language is that it is easy to read, write, and
maintain.

High-level programming language includes Python, Java, JavaScript, PHP, C#, C++,
Objective C, Cobol, Perl, Pascal, LISP, FORTRAN, and Swift programming language.

A high-level language is further divided into three parts -

i. Procedural Oriented programming language

Procedural Oriented Programming (POP) language is derived from structured


programming and based upon the procedure call concept. It divides a program into
small procedures called routines or functions.

Procedural Oriented programming language is used by a software programmer to


create a program that can be accomplished by using a programming editor like IDE,
Adobe Dreamweaver, or Microsoft Visual Studio.

The advantage of POP language is that it helps programmers to easily track the
program flow and code can be reused in different parts of the program.

Example: C, FORTRAN, Basic, Pascal, etc.


ii. Object-Oriented Programming language

Object-Oriented Programming (OOP) language is based upon the objects. In


this programming language, programs are divided into small parts called objects. It
is used to implement real-world entities like inheritance, polymorphism, abstraction, etc
in the program to makes the program resusable, efficient, and easy-to-use.

The main advantage of object-oriented programming is that OOP is faster and easier to
execute, maintain, modify, as well as debug.

Example: C++, Java, Python, C#, etc.

iii. Natural language

Natural language is a part of human languages such as English, Russian, German, and
Japanese. It is used by machines to understand, manipulate, and interpret human's
language. It is used by developers to perform tasks such as translation, automatic
summarization, Named Entity Recognition (NER), relationship extraction, and
topic segmentation.

The main advantage of natural language is that it helps users to ask questions in any
subject and directly respond within seconds.

3. Middle-level programming language


Middle-level programming language lies between the low-level programming
language and high-level programming language. It is also known as the intermediate
programming language and pseudo-language.

A middle-level programming language's advantages are that it supports the features of


high-level programming, it is a user-friendly language, and closely related to machine
language and human language.

Example: C, C++, language


C Language
C language is a general-purpose, procedure oriented programming language, developed by
Dennis Ritchie in 1972 at AT & T’S Bell Telephone Laboratories, US. Dennis Ritchie
created it to developed Unix Operating System.
If you want to become a Software Engineer, then it very important to have knowledge of
some Programming Languages, because you cannot become a software engineer without
learning any programming language.

What is C language?
 C Language is a general-purpose, programming language. Which was created by Dennis
Ritchie in 1972 at AT & T’S Bell Telephone Laboratories. Dennis Ritchie wanted to
create an operating system named Unix Operating System. to develop it, the C language
was developed.
 The special thing is that we can do low-level programming with the help of C language,
due to this feature, C programming language is used to make system software such as
Operating system, Device Driver, Compiler, etc.
 C Language covered the Basic features ( Variable, Data Types, Array, String, Function,
Structure, Pointer, Loop, etc.) of all other programming languages, due to which C
language is called Mother Language of all other programming languages.

What is the History of C language?


 Before becoming of C Language, in 1966, a person named Martin Richard created a
Programming Language named BCPL (Basic Combined Programming Language). It was
created at AT & T’S Bell Telephone Laboratories, US, by the combining of basic features
of all programming languages.
 BCPL was not suitable for making large software, as well as coding in low-level style.
 Ken Thompson, who worked at AT & T’S Bell Telephone Laboratories along with
Martin Richard, created the B language in 1969 by improving BCPL.
 Actually, Ken Thompson wanted to create an operating system that needed a good
programming language to build and that language was B Language. After creating B
language, he created Unix Operating System.
 Dennis Ritchie also worked at AT & T’S Bell Telephone Laboratories, which created C
Language in 1972, and told Ken Thompson, if we build the Unix Operating System with
the help of C Language, then we can add more features to it. The biggest feature was the
operating system being portable.

Features of C Language
 C is a simple and easy programming language.
 There are commands/instructions like English in C language which are very easy for a
programmer to read, understand, and code.
 C is a Procedure Oriented Programming Language.
 C is a very powerful and case sensitive programming language.
 C Language is a Compiler based dynamic programming language.
 C language is a middle level language due to which both low level and high level
programming can be done.
 The C language is the most commonly used programming language in developing
operating systems and embedded systems.
 C language is a very portable and powerful programming language.
 C language is a Syntax Based Language.
 C is a general purpose programming language that covers basic features of all other
languages.

Applications of C Language
If you are learning any programming language and you do not know what you can make by
learning that language, then you will not be able to do anything by learning that language.
In such a situation, you should know what you are going to do by learning that language, so
let’s know what you can make with the help of C language.
 You can make a good operating system using C Language like- Windows, Linux, Mac.
because the major part of this operating system is written in C language.
 With the help of C Language you can create a Compiler which converts programming
language code into machine language.
 With this, you can create a Text Editor.
 Utility Software can be created.
 Can create software with database management like – Oracle, Mysql, etc.
 Device drivers can be created.
What is the History of C Language?
 In earlier times, every programming language was created to fulfill some purpose, such
as Forton (Formula Translator) was created to make Scientific and Mathematical
Applications. Similarly, COBOL (Common Business Oriented Language) was created to
make business applications.
 In similar way, C Language was also originally created to make the Unix operating
system. C Language was developed by Dennis Ritchie in 1972 at Bell Laboratories of
AT&T (American Telephone & Telegraph).
 Dennis Ritchie is said to be the inventor of the C language, but the history of C language
is quite interesting.
 in 1960 Dennis Ritchie and some other employees working on a project called Multics, at
Bell Laboratories of AT&T (American Telephone & Telegraph).
 The aim of this project was to create an operating system for a large computer on which
thousands of people could work simultaneously.
 But AT&T Bell Laboratories closed this project after a few years because this project
could not make money for them. After the closure of the project, all the employees of this
project got involved in the work of another project.
 Ken Thompson, who was also working on Project Multics, created a file system. He used
the knowledge gained from the Multics project to further improve this file system.
 When Ken Thompson completed the work of that system, he named the system Unix, the
entire code of this system was written in Assembly Language.
 Ken Thompson created Unix with the help of B Language, B Language was created by
Ken Thompson in 1969 by improving a little more the language BCPL which was created
by Martin Richards, so Ken Thompson named his language in honor of Martin Richards.
 BCPL was created in 1966 by Martin Richards to combine the basic features of all other
programming languages of the time. Which further improvised by Ken Thompson for his
use and created the B language.
 The biggest drawback of B Language was that there was no concept of “Data Types”
(everything was expressed in machine language) and B Language did not provide the
functionality of “Structure”.
 Dennis Ritchie created C Language in 1972, removing all these drawbacks of B
Language. Dennis Ritchie has added some more features in C language along with
removing the drawback of B Language.
 C Language was a powerful mixture of high-level functionality and operating system
building features. Therefore, many components of Unix were re-written in the C
language.
 Kernighan and Ritchie wrote a book called, “The C Programming Language, 1st edition”
(Kernighan said that he has no contribution in the design of C language. It has been made
entirely by Dennis Ritchie, but he ” Is the author of famous programs like Hello World
”and other Unix programs).
 ANSI (American National Standards Institute) published A standard of C language in
1989 that was called ANSI C or “C89” due to this published standard.
 This standard was approved by the ISO (International Organization for Standardization)
in 1990, after that, it was called “C90”. In 1999, ISO published another standard of C
language for the internationalization of C. after that C was called “C99”.
 In 2011, another standard was published for C, after that, it was named “C11” and in
2018, another standard of C was published that is called “C18”. C18 is the latest standard
in the C programming language.

Features of C Language
These are some special features of C Language, due to which C language has made its place
in the hearts of programmers for the last 50 years.
1. Portability
2. Powerful Programming Language
3. Simple Programming Language
4. Structured Oriented Language
5. Compiler Based
6. Syntax Based Language
7. Efficient use of pointers
8. Middle level language
9. Case sensitive
10. Modularity
1. Portability
C programs are very portable means that C programs can be run easily on different machines
or PCs with little change or no change. Compiler and Preprocessor make it possible to run on
different PCs.
Read about compiler and preprocessor from here – Compiler and Preprocessor.

2. Powerful Programming Language


C programming language is a very fast and efficient programming language because it
uses Data Types, Function, and Control Statements.

3. Simple Programming Language


C language has commands like English, which makes it very easy for programmers to
understand the code and write programs.

4. Structured Oriented Language


C Language is a structured oriented programming language. Structured Oriented
Programming Language reduces the complexity of the code which leads to a lot of clarity in
the programs.

5. Compiler Based
C language is a compiler based programming language. Programs of C language cannot be
executed or run without compiling.

6. Syntax Based Language


C language is a Syntax based programming language. Syntax Based Programming Language
is the language which treats rules and regulation strictly like C, C ++, Java, etc. If a
programming language does not strictly follow rules and regulations, then such programming
language is loosely called Syntax-based programming language such as HTML.

7. Efficient Use of Pointers


A pointer is a variable that points or holds the address of another variable, using Pointer
greatly increases the performance of programs because the direct memory address is accessed
in it. The pointer is used quite well in C language.
8. Middle Level Language
C language is a Middle Level Programming Language, using that low level programming can
be done and high level programming can also be done due to which both application software
and system software can be made easily with the help of C language.

9. Case Sensitive
C language is a Case Sensitive Programming Language. We understand the meaning of Case
Sensitive by an example, if we write such printf in C language and write PRINTF in another
place, then in C language, both are different things. This is a special feature of C Language.
If the words written in Lower Case and Upper Case in a programming language have
different meanings, then such programming language is called Case Sensitive Programming
Language like – C, C ++, JAVA, .NET Programming Language is Case Sensitive
Programming Language and such Programming language in which there is no difference
between the words written in Lower Case and Upper Case is called Case Insensitive
Programming Language. HTML, SQL Language is a case insensitive language.

10. Modularity
In C language, we can divide our code into blocks so that it is easier to read and write the
code. It is a technique to design software in which a whole program is divided into several
functions or blocks.

Application of C Language
 C language is used to create Computer Application Software like Databases,
spreadsheets, etc.
 Embedded software is used in writing.
 C language is used to create system software such as Operating System
 To create applications related to Graphics such as Computer and Mobile games |
 Unix kernel has been fully developed in C language.
 C Language is used to create Network Devices and Device drivers.
 C language is used to make a compiler. The compiler converts the high level code into
low level code or machine code.
Basic Structure of C Program
Whenever we create a program in C language, we can divide that program into six different
sections. This section is as follows:
1. Documentation (Documentation Section)
2. Preprocessor Statements (Link Section)
3. Definition Section
4. Global Declarations Section
5. Main functions section
6. User-Defined Functions or Sub Program Section
In C language, all these six sections together make up the Basic Structure of C Program. Now
let’s learn in detail about all these sections -:

1. Documentation (Documentation Section)


Programmers write comments in the Documentation section to describe the program. The
compiler ignores the comments and does not print them on the screen. Comments are used
only to describe that program.
In the comments, programmer writes the name of the program, the author name which is
making the program, and other information like – the date of the program, its purpose, etc. all
These are written under the Documentation Section.
Example -:
/* File Name -: Hello.c
Author Name -: Jeetu Sahu Founder of CsTutorialpoint
Date -: 05/05/2021
Description -: A Program to find Odd or Even Number
*/

2. Preprocessor Statements (Link Section)


Within the Link Section, we declare all the Header Files that are used in our program. From
the link section, we instruct the compiler to link those header files from the system libraries,
which we have declared in the link section in our program.
Example -:
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <math.h>
In addition to all these Header Files in the Link Section, there are a lot of Header Files which
we can link in our program if needed.

3. Definition Section
The definition of Symbolic Constant is defined in this section, so this section is called
Definition Section. Macros are used in this section.
Example -:
#define PI 3.14
4. Global Declarations Section
Within the Global Declarations Section section, we declare such variables which we can use
anywhere in our program, and that variable is called Global Variables, we can use these
variables in any function.
In the Global Declaration section, we also declare functions that we want to use anywhere in
our program, and such functions are called Global Function.
Example -:
int area (int x); //global function
int n; // global Variable

5. Main functions section


Whenever we create a program in C language, there is one main() function in that program.
The main () function starts with curly brackets and also ends with curly brackets. In the main
() function, we write our statements.
The code we write inside the main() function consists of two parts, one Declaration Part and
the other Execution Part. In the Declaration Part, we declare the variables that we have to use
in the Execution Part, let’s understand this with an example.
Example -:
int main (void)
{
int n = 15; // Declaration Part
printf ("n = %d", n); // Execution Part
return (0);
}

6. User-Defined Functions or Sub Program Section


Declare all User-Defined Functions under this section.
Example -:
int sum (int x, int y)
{
return x + y;
}

A Simple Program to the Find Area of Circle in C Language


/* File Name -: area.c
Author Name -: CsTutorialpoint
Date -: 27/12/2020
Description -: A Program to find Area of Circle In C Language
*/
#include<stdio.h> // Link Section
#define PI 3.14 // Definition Section
float r; // Global Declarations Section
float areaofcircle(float); // Global Declarations Section
int main(void) // Main functions Section
{
float aoc; // Declaration Part
printf("Enter the radius of circle"); // Execution Part
scanf("%f",&r);
aoc=areaofcircle(r);
printf("area of circle is %f",aoc);
return (0);
}
float areaofcircle(float R) // User-Defined Functions or Sub Program Section
{
return (R*R*PI) ;
}
Tokens in C language
 When we write a program in the C language, then there are some statements in the
program and every statement has some words and every word has some meaning. In easy
words, we divide the program into many small units, the smallest unit of these programs
is called Tokens.
 Token is the smallest unit of C language that is most commonly used to build a program
in C language.
 Let’s understand this with an example -:
 when we write a sentence in English, then Sentence is made up of many words and every
Word has some meaning. Just like we cannot create a Sentence without using Word. In
the same way in C language we cannot create a program without using tokens because
tokens are the smallest unit of C language which is very important to build a program.

Types of Tokens In C
There are six types of tokens –
 Keywords
 Identifiers
 Constants
 Strings
 Special Symbols
 Operators
Let’s learn one by one about all these types of C Tokens in detail.

1. Keywords
When we write a program in C language, then there are some words in the program and every
word has some meaning, and the word which is already defined is called Keyword or
Reserved Word or Predefined Word.
Because the keywords are predefined or reserved, so they cannot be used as the name of a
variable. They have their own different significance and specialty.
If we use a keyword as the name of a Variable, then it means, we are changing the meaning
of the keyword which is not allowed in the C language.
Each keyword has its own meaning, and its meaning cannot be changed. The compiler is
already aware of the meaning of all keywords, so the compiler does not need to tell the
meaning of these keywords separately.
C language supports total 32 keywords, which are given below. We write the keywords in the
lowercase letter.
auto double int struct
break else long switch
case enum register typedef
char extern return union
const short float unsigned
continue for signed void
default goto sizeof volatile
do if static while

2. Identifiers
You will see many things in your daily life such as car, mobile, home, your friends like
Ramesh, Suresh, etc. You may know all of them by a different name. Just think once, if they
are not named, how will you talk about them?
So it is very important to have their name so that we can uniquely identify everyone and that
name we call Identifiers.

Identifiers are used to name Variable, Function, Array, Structure, etc. In C language,
Identifiers are user-defined. User-defined means the user determines what will be the name of
Variable, Function, Array, Structure, etc.
The names contain only uppercase letters, lowercase letters, underscore, and numbers. But
the two identifiers cannot have the same name. Also, we cannot use keywords as identifiers.

Some rules for making identifiers:


 The first letter of the identifier will be the alphabet, and the underscore.
 Only uppercase letters, lowercase letters, underscore, and numbers can be used in this.
 In the Identifier name white space is not allowed.
 A keyword cannot be used as an identifier.
 Identifiers may not exceed 31 characters in length.
 Whatever Identifiers you make should be Meaningful.
 Identifiers’ names cannot begin with numbers.

3. String
String is an Array of character that ends with a Null character (\ 0). The Null character (\ 0)
indicates that the string is over. String is always written with double quotes (” “). The size of
the string depends on the number of characters present in it.
We can define a string in different ways -:
 char string [20] = {‘C’, ‘s’, T’, ‘u’, ‘t’, ‘o’, r ‘, i’, ‘a’, ‘l’, p ‘, o’, ‘i’, ‘n’, ‘t’, ‘\ 0’};
 char string [20] = “CsTutorialpoint”;
 char string [] = “CsTutorialpoint”;
In the above examples, the compiler will allocate 20 bytes of memory in the string, in which
String is represented as a character. In the second example also, the compiler will allocate 20
bytes of memory to the string in RAM, and in the third example, the compiler will allocate
memory to the string in RAM at run time.

4. Operator
You must have read about a lot of operators in maths. For example, like 2 + 4 in it, 2 and 4
are called operands or data, and + we are called operators. So the operator performs the basic
operation and the operator needs some data or operands for the operation. Without operands,
the operator cannot do any of his work.
In C language, there are some similar operators, with the help of which we perform some
operations. Depending on the number of operands the operator can be divided into two parts:

1. Unary Operators
2. Binary Operators

#1. Unary Operators


The operators who need only one operand to perform the operation are called Unary
Operators. For example, in the increment and decrement operator, only one of the operands is
required to perform the operation. Increment operator (++), decrement operator (-) and sizeof,
(type) * etc. are Unary Operator.

#2. Binary Operators


Operators who need more than one operand to perform an operation are called Binary
Operators.
Binary Operators are of the following types:
 Arithmetic operators
 Relational Operators
 Logical Operators
 Assignment Operators
 Conditional Operators
 Bitwise operators
Click here to read in detail about Operators ? Operators In C

5. Constant
Constants are those whose value does not change. Once the value is declared, it cannot be
changed. Constant’s value is fixed, this is also called Literals.
We can declare Constant in two ways -:
 Using const keyword
 Using #define pre-processor

Types of Constant
Constant Example
Integer constant 4, 111, 110, 34, etc.
Floating-point constant 65.6, 67.8, 11.2, 2.0 etc.
Octal constant 011, 088, 022, etc.
Hexadecimal constant 0x1a, 0x4b, 0x6b, etc.
Character constant ‘a’, ‘b’, ‘c’, etc.
String constant “C”, “C++”, “Python”, “Java”, “.net”, etc.

6. Special Character
Some special symbols are used in C language which has their own special meaning and
cannot be used for any other purpose.
The following Special Symbols are used in the C language which has their own special
meaning and cannot be used for any other purpose.
Brackets [] -: Opening and Closing brackets are used in references to the Array element, It
also indicate single and multidimensional subscripts.
Parentheses () -: parenthesis () is used in function calls and function declaration in C
language.
Curly Braces {} -: Curly Braces {} is used to start and end the code. Curly Braces {} tells the
compiler that the code starts where it is and ends with it.
Comma (,) -: Comma (,) is used to separate multiple statements such as when separating
parameters in a function call, when printing the value of more than one variable by printf().
To isolate those variables, etc.
Semicolon (;) -: The semicolon (;) is used when a statement in the program has ended. the
semicolon tells the compiler that the statement in the program has ended.
Assignment operator (=) -: It is used to assign value to a variable in C language.
Pre-processor (#) -: This is used for preprocessor directives. This symbol tells the pre-
processor, that the header file is to be used in this line of the program.

What Is Variable In C Language


Variable is the name of memory location, where we store data or information. we can easily
change and update the variable several times, during the execution of the program. Variables
are a type of identifier that we use to store data.

Let us understand the variable better by this one example -:

Whenever we want to store any information or data in our computer or laptop, then we store
it in the memory of our computer. The operating system provides a complex address, in
which data is stored in memory, which is difficult for normal users like us to remember.

In such a situation, the operating system also provides some features to the normal user so
that he can decide the name of the place in which he wants to keep that data and if he creates
a folder, then he Can keep the data or information inside that folder. so that, if he needs that
data or information further, then he can easily find and retrieve it.

Similarly, when we create a program in the C language, and if we want to store any data in
that program, then we store that data or information in memory And the place where we have
stored that data or information, we give a name of that place. and that place name is called a
variable, we give memory location a name so that data can be easily found later.

In simple words, a Variable is the name of the place in memory where we store any data
or information, so that, if the information is needed then it can easily find and use it.

Syntax to Declare a Variable


Data_type Variable_name

Example of declaring a variable :


char my_name = "CsTutorialpoint";
In this example, char is a data type and my_name is the name of the variable in which
CsTutorialpoint is being stored. Apart from char data type, we can store any kind of data
using any other data type. To know more about data type, read from here What is Data types
So friends here, you have understood what is a variable, why variable is used, and what is the
basic syntax of a variable. Now let’s learn what is the rule for declaring a variable?

Rules for Naming a Variable


 Variable names can only use alphabets, numbers, and underscores.
 The first letter of the variable’s name will be either a letter or an underscore.
 Variable names cannot begin with a number.
 The variable name should be Meaningful.
 Blank and free space cannot be used in the name of a variable.
 A keyword cannot be used as a variable name.
 In C language, if you write a variable in lowercase letter and the same variable in
uppercase letter, then both of them are different variables in terms of C language
because C language is a case sensitive language.

Following are the examples of valid variable names in a ‘C’ program:


Name_is
_nameis
_Nameis14
Name or name
Example -:
int number_is = 410;

Following are the examples of invalid variable names in a ‘C’ program:


Name@Jeetu
Your Name
1Nameis

Declaring, Defining, and Initializing a variable

Variable Declaration -:
When we create a variable to store data in C language, first of all we have to declare that
variable and this is called declaration.
Through variable declaration, we tell the compiler that:
 What is the name of the variable
 Which type of data is going to be stored in the variable.
 How much space in memory will be needed to store data to the variable so that the
compiler can allocate memory to that variable correctly.
 By declaring the variable, we tell the compiler that we are going to use this name and
type in the variable in our program.

Syntax of Declaring a variable


data_type variable_name;

Example -:
int num;
num = 78;
In this example int is a data type that tells the compiler that the integer type value is going to
be stored in this variable and num is the name of the variable in which we are storing 78 .
We can declare the variable using the extern keyword, although it is optional.

Example -:
extern int a, b, c;
Variable Definition -:
When we declare a variable in our program and tell the compiler that this data type and name
of variable is going to be used in our program, it is called variable definition. Variable
declaration and variable definition are related to each other.

Example -:
int a;
float b, c;

Initializing a variable -:
When we store a value in the declared variable, it is called initialization.

Example -:
int num;
num = 100;
In this example, we store 100 values in a variable named num, by num = 100.
We can also initialize a variable in a single line by a single statement.

Example -:
int num = 96;

Types of Variables in C
In C language, Variables are of the following types:
1. Local variable
2. Global variable

1. Local variable
A variable that we declare inside a function or block is called a local variable.
The scope of the local variable is only up to that block or function, we cannot use such
variable in any other function.
Declaration of such variables has to be done at the beginning and the local variable needs to
be initialized before use.
The scope of a local variable is only up to the block in which that declare is, we cannot use
such variable in any other block or function.

Example -:
#include <stdio.h>
void fun()
{
int x = 150; // local variable
}
int main()
{
fun();
}
In this example, the scope of x variable is only up to the fun () function, we cannot use it in
any other function except the fun () function.

2. Global variable
Global variables are variables that are declared outside the function or block. We can use the
global variable in any other block or function. The value of this variable can be used and
changed in any function.
It is necessary to declare this variable before using it.

Example -:
#include <stdio.h>
int x = 10; //global variable
void fun1()
{
printf("%d \n" , x);
}
void fun2()
{
printf("%d \n" , x);
}
int main()
{
fun1();
fun2();
return 0;
}

Output -:
10
10
In the above example, we have declared the x variable outside the function or block and used
this x variable with both fun1 () and fun2 () functions because it is a global variable that we
can use with any function.

What is Data Types in C Language?


 In C language, Whenever we create a variable to store any data or information, then while
declaring that variable, we also need to declare which type of data that variable is going
to store.
 These data types can be int, char, float, double, and by looking at these data types, it is
known which type of value is going to be stored in the variable.
 We store Integer value in variables, created with int data type, and in variables created
with char data type we store character type of data, and in variables created by float data
type we store floating point values.
 When declaring a variable, we declare its type because when the compiler converts our
source code into machine code then the compiler will allocate some memory in ram for
that variable according to that data type.
 So we declare the data type of variable to tell the compiler, which type of data is going to
be stored in that variable. so that the compiler allocates memory in ram for that variable
correctly according to data types.
 When declaring a variable, its type is also declared. so that the compiler allocates as much
memory to the variable as it needs. If we don’t do this then memory loss may be more.
 In easy words, Data Types tells what kind of data we are going to store in a variable.
 Let’s understand data types with an example -:
Example of Data Types in C
// Example of Data Types in C
#include <stdio.h>
int main ()
{
int x = 14;
char ch = 'a';
float f1 = 25.7;
printf ("x = %d, ch = %c, f1 = %f", x, ch, f1);
return (0);
}

Output -:
x = 14, ch = a, f1 = 25.700001

Types of Data Types in C Language


There are mainly three types of data types in C language -:
1. Pre-defined data types
2. Derived data types
3. User-defined data type

1. Pre-defined Data Types


Such data types are called Pre-defined Data Types which are already defined and don’t need
to be defined separately, such as int, char, float, double, void, etc. These data types come in
Basic Data Types.
These data types, don’t mean to tell the compiler separately because they are already defined.
And the data types that are also keywords is called primitive data types.
List of Predefined Data Types
Pre-defined Data Types Examples
Integer Type int, long int, short int, unsigned int
Character Type char
Floating Point Data Type Float , double
List of Predefined Data Types
Integer Type
 The “int” keyword is used to create a variable of integer type.
 We store numeric values in the variable of Integer Type.
 Variables that are created of “int” data type can store 2 byte, 4 byte, and 8-byte data.
But this thing depends on the compiler.
 Some compilers store 2-byte data in a variable made of int data type, while some store
4-byte data.
 int (2 byte) can store values from -32,768 to +32,767.
 int (4 byte) can store values from -2,147,483,648 to + 2,147,483,647.
 If you want to store more data value in variables then you can create a variable using
the keyword “long int”. With the help of this keyword, the variable can store more
amount of data than the normal variable.
 The ” %d ” Format Specifier is used to print the value of a variable made of int data
type.

Syntax -:
int x = 5;
long int y;

Example
#include <stdio.h>
int main ()
{
int x = 5;
long int y;
printf ("Enter a Number \n");
scanf ("%d", &y);
printf ("value of x = %d, y = %d", x, y);

return 0;
}
Output -:
Enter a Number
10
value of x = 5, y = 10

Character Type
 We store character type data in a variable made with char data type.
 The “char” keyword is used to create a variable of character type.
 A variable created by the keyword “char” stores a single character.
 Variables made with Character Type have a size of 1 byte.
 The %c Format Specifier is used to print the value of a variable made of char data
type.

Syntax
char ch = 'A';
char ch2 = 'a';

Example
#include <stdio.h>
int main ()
{

char ch = 'A';
char ch2;
printf ("Enter a Character \n");
scanf ("%c", &ch2);
printf ("value of ch = %c, ch2 = %c", ch, ch2);

return 0;
}

Output -:
Enter a Character
B
value of ch = A, ch2 = B

Floating Point Data Type


Floating point data type consists of two types -:
 Float
 Double

Float
 It is used to store decimal points values.
 The “float” keyword is used to create a variable of floating point type.
 The variable made from Float Type has a size of 4 bytes.
 The %f Format Specifier is used to print the value of a variable made of Float data
type.

Example of Float Data Type


#include <stdio.h>
int main ()
{
float x = 5.50;
float y;
printf ("Enter a decimal Number \n");
scanf ("%f", & y);
printf ("sum of x and y is %f", x + y);

return 0;
}

Output -:
Enter a decimal number
10.06
Sum of x and y is 15.560000

Double
 It is also used to store data with decimal points like float data type.
 The “double” keyword is used to create a variable of double data type.
 A variable made of double type has a size of 8 byte.
 The %lf Format Specifier is used to print the value of a variable made of double data
type.

Example of Double Data Type


#include <stdio.h>
int main ()
{
float x = 679999999.454;
double y = 679999999.454;

printf ("float x = %f and double y = %lf", x, y);

return 0;
}

Output
float x = 680000000.000000 and double y = 679999999.454000
Friends, all the data types that you have known about above are called basic data types or pre-
defined data types. Let’s now learn about Derived Data Types.
2. Derived Data Types
Derived Data Types are data types that contain grouping of variables.
Derived Data Types is as follows:
 Array
 Function
 Pointer
We will learn about these Derived Data Types in our further post details.

3. User-Defined Data Types


Data Types that are not already defined and which have to be defined separately are called
User-Defined Data Types. Such as – Structure, Enumerator, union.
Operators in C Language
Operators are kinds of symbols that help us to do specific mathematical and logical
computations. In other words, we can say that an operator operates operands.

For example, see the statement given below:


z = x + y;

Here, the “+” operator is known as Addition Operator and the ‘a’ and ‘b’ are operands. The
Addition Operator tells the compiler to be added both operands ‘a’ and ‘b’.
If you are learning a programming language and do not have the concept of operators, then
that programming language is incomplete because operators are the foundation of any
programming language and C language is incomplete without operators.

Operators are used to manipulate data and variables in a program. In C language, we will read
about different type operators.

Let’s see how many types of operators are there in C Language?

Types of Operators in C Language


Operators in C Language are of the following types:
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise operators
5. Assignment Operators
6. Misc Operators or Other Operator

Types of Operators Operator Examples


Arithmetic Operators +, -, *, /, %, ++, —
Relational Operators ==, >= , <= ,
Logical Operators &&, ||, !
Bitwise Operator &, |, ^, ~, >>, <<
Assignment Operators =, +=, -+, *=, %=, &=
Misc Operators or Other Operator sizeof(), ‘ , ‘ , ? :

1. Arithmetic Operators
Arithmetic Operators are used to perform arithmetic operations such as Addition,
Subtraction, Multiplication, Division, etc.
The following operators are covered under Arithmetic Operators -: (+, -, *, /,%, post-
increment, pre-increment, post-decrement, pre-decrement).
There are two types of Arithmetic Operators.
1. Unary Operators
2. Binary Operators

#1) Unary Operators


Operators who perform mathematical operations on a single operand are called Unary
Operators.

#2) Binary Operators:


Operators who perform mathematical operations on two operands are called Binary Operator.

2. Relational Operators
Relational operators are used to compare between two operands, such as to determine which
of the two operands is larger and which is smaller, to determine whether one of the operands
is equal to another of the operands.
Relational Operators provide the result as True or False. Some examples of Relational
Operators are: (==,> =, <=)

3. Logical Operators
Logical Operators are used to combine two and more conditions. The result always gets
Boolean value by Logical Operators which means the result is given as True or False and we
consider True as 1 and False as 0.
For Example -: In C language Logical Operators (&&) returns true in the result when both
conditions are satisfied and if both conditions are not satisfied then it returns False in the
result.
Examples of Logical Operators are: (&&, ||,!) |
4. Bitwise Operators
Bitwise Operators are used to perform operations in the bit level between operands. Bitwise
Operators first convert the operands to Bit and then perform the operation.
To perform fast processing of Mathematical operations like addition, subtraction,
multiplication, etc is done at Bit-level by Bitwise Operators.
Examples of Bitwise Operators are: – (&, |, ^, ~, >>, <<)

5. Assignment Operators
Assignment Operators are used to assign a value to a variable. The operator on the left side of
the Assignment Operators is the variable and the operator on the right side is the value that
has to be stored in the left side operands (variable).
In Assignment Operators, the left side will be a variable, and the right side should also be the
value of the same type because we can assign the same type of value in a variable.
If the operands on both sides are of different types then that value cannot be assigned in the
variable and if we do this then there is a risk of data loss.

Some examples of Assignment Operators are: (=, + =, – +, * =,% =, & =)

6. Misc Operators or Other Operator


Apart from the above operators, there are some other operators available in the C language
which are used to perform some specific tasks. Some of them are given below:

sizeof operator
The sizeof operator is heavily used in the C/C++ programming language. It is a unary
operator run in a compile-time that can be used to calculate the size of operands.
The result of sizeof operator is an unsigned integral type which is usually denoted by size_t.
Basically, the sizeof operator is used to calculate the size of a variable.

Comma operator
Comma Operator is a binary operator that evaluates the first operand and provides the result,
then evaluates the second operator and then returns its result. The Comma Operator has the
lowest precedence in any C operator. The comma acts as both an operator and a separator.
Conditional Operator
Is Conditional Operator something like this Expression1? Expression2: Expression3. First of
all, Expression1 is seen and then if Expression1 is true then Expression2 is executed, and if
Expression1 is false. Then Expression3 is executed.

What is Arithmetic Operators In C


Arithmetic operators in C language are used to perform Arithmetic/ Mathematical operations
such as Addition, Subtraction, Multiplication, Division, etc.
Arithmetic Operators are of two types -:
1. Unary Operators
2. Binary Operators

Unary Operators In C
Operators who need only one operand to perform the operation are called Unary Operators.
Examples of this operators are -: ‘+ +’, ‘- -‘, here we call the “+ +” operator as the Increment
Operator and the “- -” operator we call the Decrement Operator.

# 1) Increment Operator In C
Increment Operators are operators that, when used with a variable, then it increases the value
of that variable by one.

For Example -: Suppose we have created a variable x by int data type and store 5 values in it.
Now if we use Increment Operator in variable x then the value of x variable will increase
from 5 to 6.

Let’s understand this by making a program.


#include <stdio.h>
int main ()
{
int x = 5;
printf("Before using increment operator the value of x is: %d", x);
++x;
printf("\n After using increment operator the value of x is: %d", x);
return 0;
}

Output -:
Before using increment operator the value of x is: 5
After using increment operator the value of x is: 6

 We can use the Increment Operator in two ways, one before the Variable name and
the other after the Variable name.
 When we use increment operator before a variable name then it is called pre
increment operator and when it is used after a variable name then it is called post
increment operator.
 While using the increment operator both ways, the value of the variable increases by
one,
 But when we use pre increment and post increment operator at a time where more
than one operator is being used, then the result of both types of increment operator
can be different there.

Let’s understand this with an example.

First we will create a program using the pre increment operator, then the second one using the
post increment operator, and finally, we will compare the result/output of these two.

Example 1 – Using Pre Increment Operator


#include <stdio.h>
int main ()
{
int x = 5;
int y;

y = ++x; // pre increment operator


printf ("\n Pre Increment Operator Result -: Value is: x = %d, y = %d", x, y);
return 0;
}
Output -:
Pre Increment Operator Result -: Value is: x = 6, y = 6
Example 2 – Using Post Increment Operator
#include <stdio.h>
int main ()
{
int x = 5;
int y;

y = x++; // post increment operator

printf ("\n Post Increment Operator Result -: Value is: x = %d, y = %d", x, y);
return 0;
}
Output -:
Post Increment Operator Result -: Value is: x = 6, y = 5
# 2) Decrement Operator In C
Decrement operator is an operator that, when used with a variable, then it reduce the value of
that variable by one. We can also use Decrement Operator as an increment operator in two
ways, one before the variable name and the other after the variable name.
When we use Decrement Operator before variable name then it is called Pre Decrement
Operator and when we use it after variable name then it is called Post Decrement Operator.

Example Program -: Pre Decrement Operator In C Language


#include <stdio.h>
int main ()
{
int x = 5;
int y;

y = --x; // pre Decrement Operator


printf ("\n Pre Decrement Operator Result -: Value is: x = %d, y = %d", x, y);
return 0;
}
Output -:
Pre Decrement Operator Result -: Value is: x = 4, y = 4
Example Program -: Post Decrement Operator Example
#include <stdio.h>
int main ()
{
int x = 5;
int y;

y = x--; // post Decrement Operator


printf ("\n Post Decrement Operator Result -: Value is: x = %d, y = %d", x, y);

return 0;
}
Output -:
Post Decrement Operator Result -: Value is: x = 4, y = 5

Binary Operators In C
The operators who require two operands to perform the operation are called Binary
Operators.
Examples of this Operators are: (+, -, *, /,%). here “+” Is called an Addition Operator, “-” is
called Subtraction Operator. “*” Is called a Multiplication Operator. “/” Is called Division
Operator. “%” Is called Modulus Operator or Modulo Operator.

Let’s now learn about all these operators one by one -:


Addition Operator (+) -: It works by connecting two operands Example -: a + b.
Subtraction Operator (-) -: It works by subtracting two operands Example -: a-b
Multiplication Operator (*) -: It works by multiplying two operands Example -: a * b
Division Operator (/) -: It works to David from the first operand to the second operand.
Example -: a / b
Modulus Operator (%) -: Returns the remainder when the second operands are divided by
the first example -: a% b
Example
#include <stdio.h>
int main ()
{
int a, b, c;

printf ("Enter two number \n");


scanf ("%d %d", &a, &b);

c = a+b;
printf ("Addition of a + b is: %d", c);

c = a-b;
printf ("\n Subtraction of a-b is: %d", c);

c = a*b;
printf ("\n Multiplication of a*b is: %d", c);

c = a/b;
printf ("\n Division of a/b is: %d", c);

c = a%b;
printf ("\n Remainder of is: %d", c);

return 0;
}

Output -:
Enter two number
10
5
Addition of a + b is: 15
Subtraction of a-b is: 5
Multiplication of a*b is: 50
Division of a/b is: 2
Remainder of is: 0

Relational Operators in C
Relational operators are used to make comparisons between any two operands such as -: One
operand is equal to another operand or not, to know which of the two operands is bigger and
which is smaller Etc.
Relational Operators returns the result as True and False.

Examples of Relational operators are:


1. Equal to operator (==)
2. Not equal to operator (! =)
3. Greater than operator (>)
4. Less than operator (<)
5. Greater than or equal to operator (> =)
6. Less than or equal to operator (<=)

1. Equal to operator (==) -:


This operator checks between given operands whether the operands are equal or not. If both
operands are equal then the Equal to operator returns true in the result and if both the
operands are not equal then False returns.
Let’s understand this with a program -:
#include <stdio.h>
int main ()
{
int a, b;
printf ("Enter Two Number \n");
scanf ("%d %d", &a, &b);

if (a==b)
{
printf("\n Both Are Equal");
}
else
{
printf("\n Both Are Not Equal");
}

return 0;
}
Output -:
Enter Two Number
10
20
Both Are Not Equal

2. Not equal to operator (! =)


This operator also checks between two given operands, whether the Operands are equal or
not, if both operands are equal then the not equal to operator gives the result returns False and
if both the operators are not equal then it returns true. This is the special thing of not equal to
operator.
Let’s understand this with a program -:
#include <stdio.h>
int main ()
{
int a, b;
printf ("Enter Two Number \ n");
scanf ("%d %d", &a, &b);

if (a!=b)
{
printf ("\n Both Are Equal");
}
else
{
printf ("\n Both Are Not Equal");
}

return 0;
}
Output -:
Enter Two Number
15
15
Both Are Not Equal
In this example, both the operands are equal, but Not equal to operator (! =) Returned false
which led to both Are Not Equal in the result.

3. Greater than operator (>)


Greater than operator (>) checks from the given operands whether the first operands are
greater than the second operands?. If the first operand is larger then it returns true in the result
and if the first operand is not greater than the second operand then it returns False in the
result.
Example -:
#include <stdio.h>
int main ()
{
int A, B;
printf ("Enter Two Number \ n");
scanf ("%d %d", &A, &B);

if (A!=B)
{
printf ("\n A is Greater Than B");
}
else
{
printf ("\n A is Smaller Than B");
}
return 0;
}
Output -:
Enter Two Number
20
15
A is greater than B

4. Less than operator (<)


Less than operator (<) checks from the given operands whether the first operand is smaller
than the second operand? If the first operand is small, then it returns true in the result and if
the first operand is not smaller, then the second operand returns False in the result.
Example -:
#include <stdio.h>
int main ()
{
int A, B;
printf ("Enter Two Number \ n");
scanf ("%d %d", &A, &B);

if (A!=B)
{
printf ("\n A is Less Than B");
}
else
{
printf ("\n A is Greater Than B");
}

return 0;
}
Output -:
Enter Two Number
10
20
A is Less Than B

5. Greater than or equal to operator (> =)


This operator checks from the given operands whether the first operand is greater or equal
than the second operands. If the first operand is larger or equal then it returns true in the
result and if the first operand is not greater or equal to the second operand, then it returns
False in the result.
Example -:
#include <stdio.h>
int main ()
{
int A, B = 5;
printf ("Enter A Number \ n");
scanf ("%d", &A);

if (A>=B)
{
printf ("\n A is Greater Than Or Equal to B");
}
else
{
printf ("\n A is Not Greater Than Or Equal to B");
}

return 0;
}
Output -:
Enter A Number
5
A is Greater Than Or Equal to B
6. Less than or equal to operator (<=)
This operator checks from the given operands whether the first operand is small or equal to
the second operand. If the first operand is small or equal, then it returns true in the result and
if the first operand is not smaller or equal to the second operand, then it returns False.
Example -:
#include <stdio.h>
int main ()
{
int A = 3, B = 5;

if (A<=B)
{
printf ("\n A is Less Than Or Equal to B");
}
else
{
printf ("\n A is Not Less Than Or Equal to B");
}

return 0;
}
Output -:
A is Less Than Or Equal to B

Logical Operators In C
Logical Operators are used to combine two and more conditions. The result always gets
Boolean value by Logical Operators, which means the result is given as True or False and we
consider True as 1 and False as 0.
For Example -: In C language Logical Operators (&&) returns true in the result when both
conditions are satisfied and if both conditions are not satisfied then it returns False in the
result.
Examples of Logical Operators are : (&&, ||,!) In this, “&&” is called AND Operator, ” || ” is
called OR Operator. ” ! ” is called NOT Operator.
Let’s now learn about all these operators one by one.

Logical AND operator in C Language


The Logical AND operator (&&) operator is used to check any two conditions.
This operator checks both the given conditions whether those conditions are correct or not. If
both the conditions are true then the ‘&&’ operator returns true in the result and if the
conditions are not true then it returns false.
Let’s understand the same thing in the form of a table.
Operand 1 Operand 2 Result
true true true
true false false
false true false
false false false
Let’s see an example:
int a = 10, b = 6;
Suppose we have the following logical expression :
(a == 10) && (b <10)
Both the conditions a == 10 and b <10 are true in the above expression, so the Logical AND
operator will return 1 or True in the result.
Let’s see some more examples of Logical AND operator
Expression Intermediate Expression Result
(a==4) && (b==2) false && false => false 0
(a>100) && (b<10) false && true => false 0
a && b true && true => true 1
a && 0 true && false => false 0

Let’s understand this by an example:


int a = 10;
(a == 21)&&(a++);
In the above expression, a == 21 is false, so operand a++ is not checked even if that second
operand is true or false. Whatever the second operand, direct results are given.
#include <stdio.h>
void main ()
{
int a = 10, result;

printf ("Initial value of a is: %d \n", a);

// result of the logical expression is stored in result


result = (a == 20) && (a++);

printf ("Final value of a is: %d \n", a);


printf ("Result of logical expression is: %d \n", result);

Output -:
Initial value of a is: 10
Final value of a is: 10
Result of logical expression is: 0

You saw in this example, a == 20 is wrong in the expression but the operand a++ was correct,
its result should be 11, but the compiler did not check it because the first operand was wrong
and that is why the compiler after the first operand was wrong Did not run a++ at all.

Logical OR operator in C Language


Out of both the given conditions, this operator returns true in the result if any one of the
conditions is true and if both the conditions are not true then it returns false.
Operand 1 Operand 2 Result
true true true
true false true
false true true
false false false
Let’s see an example
int a = 10, b = 9;
Suppose we have the following logical expression:
(a <12) || (b <5)
The first condition in the above expression, a <12 is correct, so the Logical or operator will
not even check the second condition, b <5 which is false and will return 1/True directly in the
result.
Logical or operator returns true in the result when a single condition is true and in this
example only the first condition was true, so it returned the result true without checking the
second condition.

Let’s see some more examples of Logical Or operator.


Expression Intermediate Expression Result
(a==4) || (b==2) false || false => false 0
(a>10) || (b<10) false || true => true 1
a || b true || true => true 1
a || 12.12 true || true => true 1

Let’s understand the same thing with an example below.


#include <stdio.h>

int main ()
{
int a = 10, result;

printf ("Initial value of a = %d \n", a);

// result of the logical expression is stored in result


result = (a == 10) || (a--);

printf ("Final value of a = %d \n", a);


printf ("Result of logical expression = %d \n", result);
return 0;
}
Output -:
Initial value of a = 10
Final value of a = 10
Result of logical expression = 1

Logical NOT operator in C Language


Logical NOT operator (!) Returns Boolean type results. If the condition is true then the NOT
operator (!) Makes it false and returns false in the result, and if the condition is false then the
NOT operator (!) Returns it in the result by making it true.
Operand Result
true false
false true
Let’s understand this better with the help of a program -:
int a = 11, b = 7;
logical expression
! (a> 5)
In this example (a> 5) is correct but NOT operator (!) Returns False in the result because it is
the Behavior.

Example -:
#include <stdio.h>
int main ()
{
int a = 110, result;
printf ("Initial value of a = %d \n", a);

// result of the logical expression is stored in result


result = (a>10);

printf ("Is a>10 : %d \n", result);


printf ("After Applying not operator \n");
printf ("Is a>10 : %d \n", !result);

// Signal to operating system everything works fine


return 0;
}
Output -:
Initial value of a = 110
Is a>10 : 1
After applying not operator
Is a>10 : 0

Assignment Operators In C
Assignment operators is a binary operator which is used to assign values in a variable, with
its right and left sides being a one-one operand. The operand on the left side is variable in
which the value is assigned and the right side operands can contain any of the constant,
variable, and expression.

Example -:
x = 18 // right operand is a constant

y = x // right operand is a variable

z = 1*12+x // right operand is an expression


The Assignment operator is a lower priority operator. its priority has much lower than the rest
of the other operators. Its priority is more than just the comma operator. The priority of all
other operators is more than the assignment operator.

We can assign the same value to multiple variables simultaneously by the assignment
operator.
x = y = z = 100
Here x, y, and z are initialized to 100.
In C language, the assignment operator can be divided into two categories.
1. Simple assignment operator
2. Compound assignment operators

1. Simple Assignment Operator In C


This operator is used to assign left-side values to the right-side operands, simple assignment
operators are represented by (=).
Syntax
Variable = constant_variable_anyExpration
Example -:
x = 10 // right operand is a constant

y = x // right operand is a variable

z = 1*15+x // right operand is an expression

2. Compound Assignment Operators In C


Compound Assignment Operators use the old value of a variable to calculate its new value
and reassign the value obtained from the calculation to the same variable.
Examples of compound assignment operators are: (Example: + =, – =, * =, / =,% =, & =, ^ =)
Look at these two statements:
x = 100;
x = x + 5;
Here in this example, adding 5 to the x variable in the second statement is again being
assigned to the x variable.
Compound Assignment Operators provide us with the C language to perform such operation
even more effecient and in less time.

Syntax of Compound Assignment Operators


variable op = expression
Here op can be any arithmetic operators (+, -, *, /,%).
Example -:
x = 100;
x+= 5;
The above statement is equivalent to the following depending on the function:

List of Assignment Operators In C


Operators Example/Description
= sum = 101;101 is assigned to variable sum
+= sum += 101; This is same as sum = sum + 101
-= sum -= 101; This is same as sum = sum – 101
*= sum *= 101; This is same as sum = sum * 101
/= sum /= 101; This is same as sum = sum/101
%= sum %= 101; This is same as sum = sum % 101
&= sum&=101; This is same as sum = sum & 101
^= sum ^= 101; This is same as sum = sum ^ 101
List of Assignment Operators In C

Conditional Operator in C
Conditional Operator is a ternary operator as it works on three operands. The Conditional
Operator behaves like an if-else statement.
With the help of Conditional Operator, we can also do all the work done by if-else.
With the conditional operator, we can write code using very few lines.
Conditional statements are represented by two symbols, namely, ‘? Is represented by ‘and’: ‘.
(?:)

Syntax of Conditional Operator In C


Expression1? expression2: expression3;

Look at this syntax, if the condition of Expression1 is True, then Expression2 runs or else
Expression3 runs.
Example – Conditional Operator In C
We are going to create such a program with the help of conditional operator.
In which we will say the user to enter his age. Then they will check the age entered by them
and tell them whether the user is eligible for voting or not.
If the user is eligible for voting, then we will print “eligible for voting” in the output,
otherwise, we will print “not eligible for voting”.
#include <stdio.h>
int main ()
{
int age; // variable declaration

printf("Enter your age \n");


scanf("%d", &age); // taking user input for age variable
// conditional operator
(age>=18)? (printf ("eligible for voting")) :( printf ("not eligible for voting"));
return 0;
}
Run Code
See the above example, in that we asked the user to enter his age and (age> = 18), check
whether the age of the user is 18 or 18+.
Let’s see, if the age of the user was less than 18, what would have come in the output and if it
was more than 18, then what would have come in the output?
Input 1-:
Enter your age
24
eligible for voting
The age of the user is more than 18, so “eligible for voting” was printed in the output.
Input 2 -:
Enter your age
14
Not eligible for voting
The age of the user is less than 18, so “Not eligible for voting” was printed in the output.
What is Control Statements in C
 Such statements by which we determine the flow of a program are called Control
Statements or Decision control statements.
 In any programming language, various tasks need to be done depending on the
condition and to do such tasks we use Control Statements or Decision-making
statements.
 For example, consider an online website, when you enter the wrong ID or password it
displays an error page and when you enter the correct credential it displays a welcome
page.
 So there should be a logic that checks the condition (id and password) and if the
condition is correct then it displays the welcome page otherwise it displays an error
page.
 When we create a program in C language and execute that program, the statements
inside the program are executed in the same order in which they are written.
 But sometimes we want a statement in the program to be executed on a particular
condition or else that statement should not be executed.
 Also, under certain circumstances we want to execute any statements, again and
again, we use Control Statements or Condition statements for all of them.
 Control Statements help us control the flow of the program. Through Control
Statements we determine how the program will move from one statement to another.
 In C Programming language we have different types of decision control statements.

Types of Control Statement in C


In C language we have the following decision control statements
 If statements
 Loop Statements
 Switch statement
 Conditional Operator Statement
 Goto Statement
If statements
If statement is one of the powerful conditional statements in C language. The If statement is
always used with a condition.
The condition is checked before executing any statement inside the body of if statement.

Syntax
if (condition)
{
Statements;
}
if statements are of 3 types:
 If..else
 Nested if esle
 Else if ladder

if else Statement
In this decision control statement, we have two blocks of statements. One block is inside if
and the other block is inside else.
If, If condition is true, then the statement inside the if block is executed, otherwise the
statement inside the else block is executed.

Syntax
if (Condition)
{
True block of statements
}
Else
{
False block of statements
}
else if ladder
else if ladder control statement is also like if-else statement but there is a slight difference in
it that inside else block one or more if-else statement conditions are present.

Syntax
if (condition1)
{
// These statements would execute if the condition1 is true
}
else if (condition2)
{
// These statements would execute if the condition2 is true
}
else if (condition3)
{
// These statements would execute if the condition3 is true
}
.
.
else
{
// These statements would execute if all the conditions return false.
}

Nested if else
When inside the if statement or else statement one or more if else statement, then it is called
Nested

Syntax
if (condition) {
// Nested if else inside the body of "if"
if (condition2) {
// Statements inside the body of nested "if"
}
else {
// Statements inside the body of nested "else"
}
}
else {
// Statements inside the body of "else"
}

Loop Statements
When we want to repeatedly run a particular statement until a particular condition is met,
then in that case we use Loop Statements.
There are three types of loop statements in C language:
 While loop
 Do while loop
 For loop

While loop
In the while loop, the first condition is checked and if the condition is true only then the
statement inside the while loop is run until the statement inside the while loop runs as long as
the condition of the while loop is true. as soon as the condition is False, the control is moved
out of the while loop, and another statement is executed.

Syntax
while (Condition)
{
Statements;
}

Do while loop
The do-while loop is also similar to the while loop, but there is a difference in that the while
loop first checks the condition, and the statements inside the while loop are run only when the
condition is true whereas, in the do-while loop the condition is true Or False, the statement
inside it runs once.

Syntax
Do
{
// statements inside the loop
}
While (condition);

For loop
In For Loop, we write both the initialization and control condition of the variable together
inside the parentheses “()”. If the condition of For Loop is true, then the statement inside it is
run, or else the statement is not run.

Syntax
for (initialization statement; condition)
{
// statements inside the loop
}
Switch statement
When we have a lot of conditions and we have to run a statement matching that condition,
then we use the switch case statement. with the help of Switch Case Statement We can do all
the work done by if-else
Syntax
Switch (expression)
{
Case label1:
Statement (S);
Break;
Case label2:
Statement (S);
Break;
Case label3;
Statement (s);
Break;
….
Case labelN:
Statement (s);
Break;
Default:
Statement (s);
}

Goto Statement
The goto statement is also known as the jump control statement, it is used to transfer the
control of the program from one block to another. The goto keyword is used when declaring
the goto statement.

Syntax
goto labelname;
Labelname;
In the syntax above, goto is a keyword used to transfer control to labelname. labelname is a
variable name. goto will transfer the control of the program to labelname and the statements
after labelname will be executed.

if statement in C
 When we want to execute a statement inside a block only when the given condition is
true, then we use the if statement.
 if Statement is a very simple Decision Making Control Statement which decides on
the basis of the given condition whether the statement inside the if block will run or
not.
 If the condition is true then the statement inside the if block is run and if the condition
is false then the statement inside the if block is not run.
 In C language, when we write one or more statements by covering them with a curly
bracket {}, then it is called a block.
 The condition in parenthesis () of if is checked as true or false. If the condition is true
then it is considered to be a non-zero value and if the condition is false then it is
considered zero.
Syntax -:
if (condition)
{
// Statements to execute if
// condition is true
}

Example
If (5>2) // Here () is called parenthesis in which 5> 2 is an expression or condition.
{
printf ("five is greater than two");
}
Output -:
five is greater than two

If else Statement In C

The if-else statement is an extended form of the If statement. In this, we execute the
statement of the block containing else, if the if condition is false.
The if-else statement is used when we have to perform two operations on the same condition.
Meaning that when we want to run the statement of another block when a condition is true
and the statement of another block should run when the condition is false.

Syntax of If else Statement In C -:


if (Condition)
{
// code to be executed if condition is true
}
else
{
// code to be executed if condition is false
}
Let us understand this by creating a program.

Example -:
Here we will create a program in which we will enter the user’s age and based on the input
entered by the user, we will check whether the user is eligible for voting?
If the user’s age is more than 18, then we will run a statement inside the if block and if the
user is less than 18, then we will run a statement inside the else block.
// c program to check user are eligible or not for voting
#include <stdio.h>
int main ()
{
int age;
printf ("Enter Your Age \n");
scanf ("%d", &age);

if (age>=18)
{
/* The codes in this block will only execute when
The above condition (age> = 18) is true.
*/

printf ("You are eligible for vote");


}
else
{
/* The codes in this block will only execute when
The above condition (age> = 18) will be False.
*/

printf("You are not eligible for vote");


}
return 0;
}

In this example, we take input from the user and check whether he is more than 18 years old.
Based on the input given by the user, its output will be something like this.
Output -:
Enter Your Age
21
You are eligible for vote

Nested if-else statement in C

A nested if-else statement is also similar to an if-else statement, but the slight difference is
that in a nested if-else statement, we use many more if-else statements within the if-else
statement.
When we use an if-else statement or if statement inside an if block or inside an else block
then, it is called a Nested if-else statement.
The nested if-else statement is used when we have a lot of conditions, which we have to
execute a statement only after checking.
Let us understand these things by making a program.

Example -:
Below we are going to create a program in which we will take three numbers of inputs from
the user and based on the input given by that user, we will check which of those numbers is
bigger. We will finally print the value of whatever number will be bigger.
#include <stdio.h>
int main ()
{
int x, y, z;
printf ("Enter three number \n");
scanf ("%d %d %d", &x, &y, &z);

if (x>y) // first condition


{// The code of this block will run when the first condition is true
if (x>z) // second condition
printf ("Greater value is: %d", x);
else
printf ("Greater value is: %d", z);
}
else
{ // If the first condition is False, then the code of this block will run.

if (y>z)
printf ("Greater value is: %d", y);
else
printf ("Greater value is: %d", z);
}
return 0;
}
Run Code Live
You can run the live demo of this program by clicking on the Run Code Live button above.
In this program, we input three values from the user via scanf (). Then we first checked if x>y
in the parenthesis of if the value of x is greater than y?
There can be two points here, either the value of x will be greater than y or smaller.
If the value of x is larger, then we will enter the inside of the if block and run the statement
inside it, and if the value of x is smaller, then we will enter the inside else block and run the
statement inside it.
Both these things depend on the input given by the user, so let’s see what the output of this
program is.
Output -:
Enter three number
12
6
10
Greater value is: 12

Now let’s know about the else if ladder statement And how it is used.
else if ladder statement in C
When we use an if statement or if-else statement inside an else block then, it is called else if
ladder statement.
else if ladder statement is used when we have many conditions in else statement which we
have to execute a statement only after checking.

Syntax -:
if (condition1)
{
// code to be executed if condition1 is true
}
else if (condition2) {
// code to be executed if condition2 is true
}
else if (condition3) {
// code to be executed if condition3 is true
}
.
.
.
else {
// code to be executed if all the conditions are false
}
Let us understand the else if ladder statement by creating a program

Example -:
Below we are going to create a program in which we will input two values from the user,
then compare the input given by the user to each other.
Are the two values equal?
Which of the two is smaller and which is bigger.
#include <stdio.h>
int main ()
{
int num1, num2;
printf ("Input the value of var1:");
scanf ("%d", &num1);

printf ("Input the value of var2:");


scanf ("%d", &num2);

if (num1 == num2)
{
printf ("num1 is equal to num2 \n");
}
else if (num1>num2)
{
printf ("num1 is greater than num2 \n");
}
else
{
printf ("num2 is greater than num1 \n");
}
return 0;
}
Output -:
Input the value of num1: 10
Input the value of num2: 20
num2 is greater than num1

What is Loop In C
 When we want to run a particular part of a program or a block multiple times, then we
use Loop Statements.
 Meaning that when we want to run any particular statement of our program repeatedly
till a particular condition is true, then we use Loop Statements.
Let’s understand these things with this one example.
Example -: If we want to print “Welcome To CsTutorialpoint” 10 times, then one way we can
print “Welcome To CsTutorialpoint” by writing 10 printf () in the program. Another way can
be that we use Loop Statements.

If we use the previous method then the program will be something like this:
#include <stdio.h>
Void main ()
{
printf ("Welcome To CsTutorialpoint \n");
printf ("Welcome To CsTutorialpoint \n");
printf ("Welcome To CsTutorialpoint \n");
printf ("Welcome To CsTutorialpoint \n");
printf ("Welcome To CsTutorialpoint \n");
printf ("Welcome To CsTutorialpoint \n");
printf ("Welcome To CsTutorialpoint \n");
printf ("Welcome To CsTutorialpoint \n");
printf ("Welcome To CsTutorialpoint \n");
printf ("Welcome To CsTutorialpoint \n");

Output -:
Welcome To CsTutorialpoint
Welcome To CsTutorialpoint
Welcome To CsTutorialpoint
Welcome To CsTutorialpoint
Welcome To CsTutorialpoint
Welcome To CsTutorialpoint
Welcome To CsTutorialpoint
Welcome To CsTutorialpoint
Welcome To CsTutorialpoint
Welcome To CsTutorialpoint
If we want to print “Welcome To CsTutorialpoint” 10 times using Loop Statements, then for
this we have to write a Loop Statement in which we will give condition and secondly we
have to write a printf () Statement by which we will print “Welcome To CsTutorialpoint”.
#include <stdio.h>
int main ()
{
int i = 1; // initialization expression

while (i<=10) // Loop Condition - Test Expression


{
printf ("Welcome ToCsTutorialpoint \n");

i++; // update expression


}
return 0;
}
Output -:
Welcome To CsTutorialpoint
Welcome To CsTutorialpoint
Welcome To CsTutorialpoint
Welcome To CsTutorialpoint
Welcome To CsTutorialpoint
Welcome To CsTutorialpoint
Welcome To CsTutorialpoint
Welcome To CsTutorialpoint
Welcome To CsTutorialpoint
Welcome To CsTutorialpoint

Types of Loop in C
In C language, we can use loop in three ways.
1. While loop
2. Do while loop
3. For loop
1. While Loop
 While Loop is used when we do not already know how often to run the loop.
 In While Loop, we write the condition in parenthesis “()” after the while keyword. If
the condition is true then the control enters the body of the while Loop and runs the
statements inside the while loop.
 As the control comes to the end of While Loop, the control again goes to While Loop
and the condition is checked again and if the condition is true this time, then the
statement inside While Loop runs again.
 This action continues till the condition of the while Loop does not become False. We
also use the increment (++) and increment (-) operators to change the condition of
while loop.
 While Loop is also called the entry control loop because the condition is checked
before the control reaches the block of the while loop, and if the condition is true then
the control enters the body of the while Loop and runs the statement inside it.
While Loop’s syntax is something like this:

Syntax
while (condition)
{
// code to be executed
statements;
statements;
.
.
}

2. Do while loop
Do while loop is used when we want to execute the statement inside the Do while loop block
once.
We finally check the condition in the Do while loop and if the condition is true then the
control again goes to the Do while loop and the statement inside it start executing and if the
condition is False then the control is removed from the do while loop and another statement is
executed.
Syntax -:
Do
{
// code to be executed
statements;
statements;
statements;
.
.
} while (condition);

3. For Loop

For Loop is used when we have to run a particular statement repeatedly until a particular
condition is correct.
In the case of for Loop, we already know how many times the loop will run, so for Loop, is
also called a pre-tested loop or open-ended loop.
In for loop, we write both the initialization and control condition of the variable together
inside the parentheses “()”. If the condition of For Loop is true, then the statement inside it is
run, or else the statement is not run.
Syntax -:
for (initial value; condition; incrementation or decrementation)
{
statements;
}

For Loop in C
 For Loop is an entry-controlled loop In which the condition inside For Loop is
checked before executing the statement And if the condition is true only then the
statement inside For Loop is executed or run.
 In C language, For Loop is used to execute a particular statement or block repeatedly
until a particular condition is true.
 In For Loop, we write both the initialization and control condition of the variable
together inside the parentheses “()”. If the condition of For Loop is true, then the
statement inside it is run or else the statement is not run.

Syntax of For Loop in C


for (initialization; condition test; update expression)
{
// body of the loop
// Statements to be executed repeatedly
}

Initialization -: Here we initialize or store a value in a variable. Initialization is performed


only once in the for loop. example: int i = 1;

Condition Test: – The condition is written in this section every time we have to run a loop.
The condition can be any Boolean expression. For example: i <= 10;
Once the loop is run, the condition is checked again and if the condition returns true this time,
then the loop runs again and if the condition is false then the control comes out of the loop.

Update Expression -: Here the increment or decrement operator is used to update the
Expression. for example: i ++;
Let’s understand the for loop better by this one example.

Example of For Loop


Here we are going to make a program to print “Hello World” 10 times with the help of For
loop.
// C program to illustrate for loop
#include <stdio.h>
int main ()
{
int i;

// i = 1; Initialization
// i<= 10 Condition Test
// i++ Update Expression

for (i = 1; i<=10; i++)


{
printf ("Hello World \n");
}

return 0;
}
Run Code
Let’s see how this program will execute?
How Program Work?
1. The first variable will be the declare.
2. The control goes to the for loop and store 1 value in the i variable
3. After that the condition i <= 10 will be checked.
4. The stipulated condition is correct, so the statement inside the for loop will execute
once. Which will print once Hello World on the screen.
5. When the statement of the body of the for loop is executed once, then the control will
go to the Update Expression where the value of i will be incremented due to i ++
being written.
6. After incrementing the value of i, the control will again go to section i <= 10 in the
condition of the for loop and check the condition again.
7. If this time the condition is true, then the statements inside the for loop will be
executed again and the control will again go to Update Express.
8. Where the value of i will be incremented again due to i ++ being written.
9. After that the control will again go to section i <= 10 for the condition of the for loop
and check the condition again.
10. Here the action will continue until the condition with i <= 10 becomes false.
11. When the condition with i <= 10 is checked 11 times, then the condition will be
wrong because by then the value of i will be 11 so the control loop will be out.
12. Before coming out of the control loop, “Hello World” must have been printed 10
times.
The output of this program will be
Output -:
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World

What is While Loop In C


 While loop is a way to repeatedly run a particular statement or block in a program to a
particular condition.
 While loop is also called pre-tested loop or entry-controlled loop. Because while loop
has first condition check as if statement, after that if condition is true then the control
enters into the body of the loop (which is covered by the curly bracket {}) and runs
the statements inside it
 As soon as the control comes to the end of the body of the while loop, the control
again goes to the while loop and the condition is checked again. If this time the
condition is correct, then the statement of the body of the while loop runs.
 This action lasts until the condition of the while loop becomes incorrect. As soon as
the condition of the while loop is wrong, the control comes out of the while loop and
starts running the statements after the while loop.
 While loop is the easiest structure Loop Statement in C language.
 While loop is used in C language when we do not know how many times the
statements inside the body of the loop have to run.
 In the body of the while loop (which is covered by the curly bracket {})
 If there is only one statement, then even if it is not covered with a curly bracket, it
works.
 But if there is more than one statement in the body of the while loop, then it has to be
covered with curly bracket {}.
 I suggest you should always cover the while loop with the curly bracket even if it
contains a single statement. This makes the code easier to read.

Syntax of While Loop


while (test_expression)
{
// statements

update_expression;
}

While loop has two parts -:


 Test expression
 Update Expression

Test Expression -:
In this expression, we check the condition. If the condition is true then we enter inside the
body of the while loop and execute the statement inside it, and if the condition is false then
we do not enter inside the while loop.

Example -:
include <stdio.h>
#include <conio.h>
int main ()
{
int num = 1; // initializing the variable
while (num<=10) // test_expression
{
printf ("%d \n", num);
num++; // update_expression;
}
return 0;
}
In the example above (num <= 10) there is a test_expression.
Update Expression -:
When we enter inside the body of the while loop, we try to change the condition of the while
loop using the update expression. In Update Expression we use increment or increment
operator with variable.
Example -:
while (num<=10) // test_expression
{
printf ("%d \n", num);
num++; // update_expression;
}
See the example program we created above, in it, we have written num ++, this is an update
expression incrementing the value of num variable.
To understand the while loop more, let us now create a program.
E
xample Program of While Loop
We are going to create a program in which we will print the values from 1 to 10.
#include <stdio.h>
int main ()
{
int i = 1;

while (i<=10)
{
printf ("%d \n", i);
i++;
}
return 0;
}
Run Code
In this program we have int i = 1; I did a variable declare named i and stored 1 in it.
Then we put a condition in the parenthesis of the while loop by i<= 10. Since this condition is
correct, the control enters the body of the while loop and runs the statement inside it.
i++ in the body of the while loop; Is a statement with the help of which we are increasing the
value of the i variable.
Thereby comes a time in which the value of i variable becomes larger than 10 and i<= 10
condition becomes false. The control then exits from the while loop.
The output of this program will be something like
Output -:
1
2
3
4
5
6
7
8
9
10
Now let us know about Do While Loop

What is Do While Loop in C


 Do while loop is also like a while loop, But the difference in them is that the condition
is checked first in the while loop and the statements inside the while loop are run only
when the condition is correct.
 While the condition in the do-while loop is true or false, the statement inside it only
runs once because in the do-while loop the condition is checked in the end of the
block.
 So if the condition is true then the block of do while loop is executed again and if the
condition is false then the block of do while loop is not executed and the control exits
from the do while loop.
Note – the condition is true or False, in the Do While loop, the statement inside it runs once.

Syntax of Do While loop


Do
{
// statements inside the loop
}
While (condition);

Note – After writing While (condition) in Do While loop, it is necessary to apply semicolon
in the end, whereas it was not in the while loop.
The Do While loop is also called the post-tested loop or exit controlled loop because in the
Do While loop, the condition is checked at the end of the block

Let’s understand the Do While loop by making a program and understand it better.

Example Program of Do While Loop


// C program to illustrate do-while loop
#include <stdio.h>
int main ()
{
int i = 1; // Initialization expression

do {
// loop body
printf ("Hello World \n");

i++; // Update expression


} while (i<=1); // Test expression or do-while loop condition

return 0;
}

Look at this example program above, in that we first created the i variable and stored 2 values
in it.
Then we created a body of do-while loop inside which we used the printf () function to print
“Hello World”.
After that we have i++; By increasing the value of i, due to which the value of i will increase
to 2.
At the end of the body of the do-while loop, we have written the condition i<= 1, which will
check if the condition is false because the value of the i variable has increased due to which
i<= 1 is not true.
Because of which the condition will be false, the control will come out of the do-while loop.
If you want to try this program by running , then you can try it by clicking on the demo
button below.
Live Demo
The output of this program will be something like this.
Output -:
Hello World

What is Switch Statement In C


 Switch Statement is a Decision Making Control Statement that we use when we have
many options and we need to execute only one of those options.
 Switch Statement is an alternative to a larger if else statement. With the help of
Switch Case Statement We can do all the work done by if-else statement
 We cannot evaluate the expression as true or False in the parentheses () of Switch
Statement. We match that expression with constant values written after the Case
keyword in the body of the Switch Statement and if the expression matches any
constant value, we execute the code after that case.

Syntax of switch case statement


The syntax of the switch case statement is something like this:
switch (variable or an integer expression)
{
case constant_1 // Code to be executed if expression == constant_1
break;
case constant_2: // Code to be executed if expression == constant_2
break;
case constant_3: // Code to be executed if expression == constant_3
break;
default: // the default case is optional
// Code to be executed if none of the cases match
;
}

To understand the Switch statement in C language, let us see a simple example.


Example of Switch Statement In C
#include <stdio.h>
void main ()
{
int number = 0;

printf ("Enter a number: \n");


scanf ("%d", &number);

switch (number)
{
case 10:
printf ("number is equals to 10");
break;
case 50:
printf ("number is equal to 50");
break;
case 100:
printf ("number is equal to 100");
break;
default:
printf ("number is not equal to 10, 50 or 100");

}
Run Code Live
Output -:
Enter a number
10
number is equals to 10

What is Break Statement In C


 break is a keyword that is used in C language with an if statement. The break
statement is used to bring the control of the program out of the loop and switch.
 Suppose we are creating a program in which we are going to search a particular
number out of 1000 numbers. If we get that number in the 10th search, then why
should we do the remaining 990 searches? Since we found our number only in the
10th search, in such a situation, we would like to end the loop by not doing the rest of
the searches.
 In C language, if we want to end a loop before its entire condition is running, we use a
break statement for this.
Let’s understand these things through a program.

Example Program -:
#include <stdio.h>
int main ()
{
int i;

for (i = 1; i<10; i++)


{
if (i == 5)
{
break; // breaks out of the for loop
}
printf ("Value of i = %d \n", i);
}
// signal to operating system everything works fine
return 0;
}
Run Code
Output -:
Value of i = 1
Value of i = 2
Value of i = 3
Value of i = 4

Continue Statement C
 Continue Statement is a loop control statement that we use in C language when we
want to run the program from next iteration by skipping a particular line or code when
a particular condition becomes true.
 Suppose you are playing a game and in order to cross a level in that game you need
some particular point within a particular time.
 If you have achieved that point before time, then the game starts at the next level.
 In the same way, when we have some special condition in our program, we want to
start the program with the next iteration, then we have to use the continue statement.
 The continue statement is slightly opposite to the break statement. It does not take the
control of the program out of the entire loop, but takes the control of the program to
the beginning of that loop and runs the loop with the next iteration.
 The continue statement is mostly used inside a loop statement with an if condition.

Syntax of Continue Statement In C


// loop statements
continue;
// some lines of the code which is to be skipped

Example: Continue Statement inside for loop


#include <stdio.h>
int main ()
{
for (int j = 0; j <= 10; j ++)
{
if (j == 5)
{
/* The continue statement is encountered when
the value of j is equal to 5.
*/
continue;
}
/* The printf () statement will not run when j == 5 because
continue statement has run before and
The control has reached the beginning of the loop.
*/
printf ("% d \n", j);
}
return 0;
}
Run Code
Output -:
0
1
2
3
4
6
7
8
9
10
Look at the output of the program, it does not print 5 because the continue statement is run
when the j == 5 condition is true so that the control of the program returns to the loop again
and the program starts with the next iteration.

You might also like