PPS T Notes - 1
PPS T Notes - 1
PPS T Notes - 1
SOLVING (18ESCS114)
What is C?
Why do we use C?
Who invented C?
C Programming Language was invented in the year 1972 by Dennis Ritchie (Dennis MacAlistair
Ritchie). He was an American Computer Scientist worked at Bell Labs as a researcher along
with Ken Thompson. He was born on 9th September 1941 and lived till 12th October 2011.
Following are the applications and software used to create and execute C programs.
1. Turbo C
2. Turbo C++
3. GNU C
4. Code Blocks
5. Net Beans
Introduction to Computer Systems
What is a Computer?
The computer is an electronic device which operates under the control of instructions stored
in its memory. A computer can take data from the user through input devices (Input), process
the user given data (Processing), produces the result to the user through output devices
(Output)and stores data (Information) for future use. A Computer can be defined as follows...
The Computer is an electronic device which operates under the control of instructions
stored in its memory and it takes the data from the user, a process that data gives the result
1. Hardware
2. Software
3. User
Here the user interacts with the software, and the software makes the computer hardware
touch and feel every hardware of the computer. All hardware components perform any task
1. Input Devices - These are the parts through which a user can give the data to the
computer.
2. Output Devices - These are the physical components of a computer through which the
3. Storage Devices - These are the physical components of a computer in which the data
can be stored.
4. Devices Drives - Using drives, user can read and write data on to the storage devices
6. Other Devices - Other than the above hardware components, a computer also
Input Devices
Computer input devices are the physical components of the computer which are used to give
the data given by the user to the computer. Using input devices the user can give the data to
the computer.
Example
Output Devices
Computer output devices are the physical components of the computer which are used to
give the computer result to the User. Using output devices, the user can see the computer-
generated result.
Example
Storage Devices
Computer storage devices are the physical components of the computer which are used to
Example
Device Drives
Computer Device drives are the physical components of the computer which are used to read
Computer Cables
In a Computer, various cables are used to make connections among the various hardware
Example
Other Devices
CPU (Processor)
SMPS
Motherboard
The application interacts with the operating system, and the operating system makes
hardware components to work according to the user given instructions. The hardware
components send the result back to the operating system, then the operating system
forwards the same to the application and the application shows the result to the user.
By using input devices, the user interacts with the application and the application uses output
devices to show the result. All input and output devices work according to the instructions
Computing Environments
What is Computing Environment?
When we want to solve a problem using a computer, the computer makes use of various
devices which work together to solve that problem. There may be a various number of ways
to solve a problem. We use the various number of computer devices arranged in different
ways to solve different problems. The arrangement of computer devices to solve a problem
is as follows.
Computing Environment is a collection of computers which are used to process and
complete program resides on the stand-alone machine and executed from the same
machine. Laptops, mobile devices, printers, scanners and the computer systems we use at
home, office are the examples for the personal computing environment.
can perform multiple operations at a time by using a multitasking operating system. Here the
processor time is divided among different tasks and this is called “Time-sharing”. For example,
a user can listen to music while writing something in a text editor. Windows 95 and later
versions of Windows OS, iOS and Linux operating systems are the examples for this
computing environment.
These both machines will exchange the information through an application. Here Client is a
normal computer like PC, Tablet, Mobile, etc., and Server is a powerful computer which stores
huge data and manages the huge amount of file and emails, etc., In this environment, client
requests for data and server provides data to the client. In the client-server environment, the
communication between client and server is performed using HTTP (Hyper Text Transfer
Protocol).
Distributed Computing Environment
In the distributed computing environment, the complete functionality of the software is not
on a single computer but is distributed among multiple computers. Here we use a method of
two or more computers. These computers communicate with each other over a network to
perform the complete task. In a distributed computing environment, the data is distributed
among different systems and that data is logically related to each other.
Grid computing is a collection of computers from different locations. All these computers
work for a common problem. A grid can be described as a distributed collection of a large
Computer Languages
What is Computer Language?
Generally, we use languages like English, Hindi, etc., to make communication between two
persons. That means when we want to make communication between two persons we need
a language through which persons can express their feelings. Similarly, when we want to
make communication between user and computer or between two or more computers we
need a language through which user can give information to the computer and vice versa.
When a user wants to give any instruction to the computer the user needs a specific language
The user interacts with the computer using programs and that programs are created using
Computer languages are the languages through which the user can communicate with the
Every computer programming language contains a set of predefined words and a set of rules
Over the years, computer languages have been evolved from Low-Level to High-Level
Languages. In the earliest days of computers, only Binary Language was used to write
language are written in the form of binary numbers 1's & 0's. A computer can directly
understand the binary language. Machine language is also known as the Machine Code.
As the CPU directly understands the binary language instructions, it does not require any
translator. CPU directly starts executing the binary language instructions and takes very less
time to execute the instructions as it does not require any translation. Low-level language is
Advantages
• Low-level language instructions require very less time for their execution.
Disadvantages
• In low-level language, there is more chance for errors and it is very difficult to find
Middle-level language is a computer language in which the instructions are created using
symbols such as letters, digits and special characters. Assembly language is an example of
Binary code instructions in low-level language are replaced with mnemonics and operands in
translator which takes assembly code as input and produces machine code as output. That
Advantages
low-level language.
Disadvantages
machine-dependent.
High-Level Language
A high-level language is a computer language which can be understood by the users. The
high-level language is very similar to human languages and has a set of grammar rules that
are used to make instructions more easily. Every high-level language has a set of predefined
words known as Keywords and a set of rules known as Syntax to create instructions. The high-
level language is easier to understand for the users but the computer cannot understand it.
Languages like COBOL, FORTRAN, BASIC, C, C++, JAVA, etc., are examples of high-level
English to write program instructions. These instructions are converted to low-level language
Advantages
• Writing instructions in a high-level language is easier.
• The programs created using high-level language runs on different machines with little
change or no change.
Disadvantages
The following figure provides a few key points related to computer languages.
From the above figure, we can observe the following key points...
• The programming languages like C, C++, Java, etc., are written in High-level language
written using a high-level language like English. But, the computer cannot understand the
high-level language. It can understand only low-level language. So, the program written in the
understandable for the computer. This conversion is performed using either Interpreter or
Compiler.
Popular programming languages like C, C++, Java, etc., use the compiler to convert high-level
converts high-level language instructions into low-level language instructions. Generally, the
compiler performs two things, first it verifies the program errors, if errors are found, it returns
a list of errors otherwise it converts the complete code into the low-level language.
To create and execute C programs in the Windows Operating System, we need to install Turbo
C software. We use the following steps to create and execute C programs in Windows OS…
source code, we use any text editor to write the program instructions. The instructions written
in the source code must follow the C programming language rules. The following steps are
• Select Run
The compilation is the process of converting high-level language instructions into low-level
language instructions. We use the shortcut key Alt + F9 to compile a C program in Turbo C.
The compilation is the process of converting high-level language instructions into low-level
language instructions.
Whenever we press Alt + F9, the source file is going to be submitted to the Compiler. On
receiving a source file, the compiler first checks for the Errors. If there are any Errors then
compiler returns List of Errors, if there are no errors then the source code is converted
into object code and stores it as a file with .obj extension. Then the object code is given to
the Linker. The Linker combines both the object code and specified header file code and
The processor can understand this .exe file content so that it can perform the task specified
is submitted to the CPU. On receiving .exe file, CPU performs the task according to the
instruction written in the file. The result generated from the execution is placed in a window
After running the program, the result is placed into User Screen. Just we need to open the
User Screen to check the result of the program execution. We use the shortcut key Alt + F5 to
The file which contains c program instructions in a high-level language is said to be source
code. Every c program source file is saved with .c extension, for example, Sample.c.
Whenever we press Alt + F9 the source file is submitted to the compiler. Compiler checks for
the errors, if there are any errors, it returns a list of errors, otherwise generates object code
in a file with name Sample.obj and submit it to the linker. The linker combines the code from
specified header file into an object file and generates executable file as Sample.exe. With this
When we press Ctrl + F9 the executable file is submitted to the CPU. Then CPU performs the
task according to the instructions written in that program and place the result into
UserScreen.
Then we press Alt + F5 to open UserScreen and check the result of the program.
Important Points
• The compiler converts complete program at a time from high-level language to low-
level language.
• Input to the compiler is .c file and output from the compiler is .exe file, but it also
• The compiler converts the file only if there are no errors in the source code.
Overall Process
• Type the program in C editor and save with .c extension (Press F2 to save).
• If there are errors, correct the errors and recompile the program.
of steps. These steps are called phases in program development. The program development
life cycle is a set of steps or phases that are used to develop a program in any programming
language.
Generally, the program development life cycle contains 6 phases, they are as follows….
• Problem Definition
• Problem Analysis
• Algorithm Development
• Maintenance
1. Problem Definition
In this phase, we define the problem statement and we decide the boundaries of the problem.
In this phase we need to understand the problem statement, what is our requirement, what
should be the output of the problem solution. These are defined in this first phase of the
2. Problem Analysis
In phase 2, we determine the requirements like variables, functions, etc. to solve the problem.
That means we gather the required resources to solve the problem defined in the problem
3. Algorithm Development
During this phase, we develop a step by step procedure to solve the problem using the
specification given in the previous phase. This phase is very important for program
This phase uses a programming language to write or implement the actual programming
instructions for the steps defined in the previous phase. In this phase, we construct the actual
program. That means we write the program to solve the given problem using programming
During this phase, we check whether the code written in the previous step is solving the
specified problem or not. That means we test the program whether it is solving the problem
for various input data values or not. We also test whether it is providing the desired output
or not.
6. Maintenance
During this phase, the program is actively used by the users. If any enhancements found in
this phase, all the phases are to be repeated to make the enhancements. That means in this
phase, the solution (program) is used by the end-user. If the user encounters any problem or
wants any enhancement, then we need to repeat all the phases from the starting, so that the
C Background
C is a structured programming language. It is also known as function orientated programming
language. C programming language was developed in the year of 1972 by Dennis Ritchie at
In the year of 1968, research was started by Dennis Ritchie on programming languages like
BCPL, CPL. The main aim of his research was to develop a new language to create an OS called
UNIX. After four years of research, a new programming language was created with solutions
for drawbacks in languages like BCPL & CPL. In the year of 1972, the new language was
The name 'c' was selected from the sequence of previous language ‘B’ (BCPL) because most
The first outcome of the c language was the UNIX operating system. The initial UNIX OS was
The founder of the ‘C’ language, Dennis Ritchie is known as “Father of C” and also “Father of
UNIX”.
The c programming language is very popular because it is reliable, simple and easy to use and
The CPL was invented by Martin Richards at the University of Cambridge in the early of 1960s.
The BCPL was invented by Martin Richards at the University of Cambridge in the year of 1966.
It was a popular programming language at that time. BCPL allows the user, direct access to
3. B Language
B language is derived from BCPL. It was introduced in the year of 1969 by Ken
Thompson and Dennis Ritchie at Bell Laboratory, USA. The B language is similar to BCPL.
4. C Language
C language is derived from the B language. It was introduced in the year of 1972 by Dennis
Ritchie at Bell Laboratory, USA. The C language was mainly developed to create an operating
system called UNIX. The name C is given based on the previous language B and BCPL. Ninety
percent of the UNIX operating system code is written in C language. During the 1970s, the C
organizations began creating their version of C language for their respective projects. So, C
language has got many variants at that time. Later it was standardized.
5. ANSI C (C89)
In the year of 1983, the ANSI (American National Standards Institute) formed a committee to
frame standard specifications for the C language. In the year of 1989, this committee
introduced a standard version of C with the name "ANSI C" with standard library files. The
6. C90
In the year of 1990, the ANSI C was got ISO (International Organization for Standardization)
standardization with the inclusion of a few new features like new library files, new processor
commands. And it was also added with keywords const, volatile and signed, etc... ISO
standardized ANSI C as ISO/IEC 9899:1990. This version is called as C90 in short form.
7. C99
In the year of 1995, many new features were added to the C90 to create a new version of it.
This new version of C was got ISO standardization in the year of 1999 with the name ISO/IEC
9899:1999. In the short form, it is called as C99. Later C99 became the official standard version
of C.
C Program Basics
C is a structured programming language. Every c program and its statements must be in a
This section is used to provide a small description of the program. The comment lines are
simply ignored by the compiler, that means they are not executed. In C, there are two types
of comments.
1. Single Line Comments: Single line comment begins with // symbol. We can write any
2. Multiple Lines Comments: Multiple lines comment begins with /* symbol and ends
with */. We can write any number of multiple lines comments in a program.
In a C program, the comment lines are optional. Based on the requirement, we write
comments. All the comment lines in a C program just provide the guidelines to understand
Preprocessing commands are used to include header files and to define constants. We use
the #include statement to include the header file into our program. We use
a #define statement to define a constant. The preprocessing statements are used according
to the requirements. If we don't need any header file, then no need to write #include
statement. If we don't need any constant, then no need to write a #define statement.
The global declaration is used to define the global variables, which are common for all the
functions after its declaration. We also use the global declaration to declare functions. This
Every C program must write this statement. This statement (main) specifies the starting point
of the C program execution. Here, main is a user-defined method which tells the compiler
that this is the starting point of the program execution. Here, int is a data type of a value that
is going to return to the Operating System after completing the main method execution. If we
The open brace indicates the beginning of the block which belongs to the main method. In C
In this section, we declare the variables and functions that are local to the function or block
in which they are declared. The variables which are declared in this section are valid only
result, calculations, etc., All the statements in this section are written according to the
requirements.
The close brace indicates the end of the block which belongs to the main method. In C
This is the place where we implement the user-defined functions. The user-defined function
implementation can also be performed before the main method. In this case, the user-
defined function need not be declared. Directly it can be implemented, but it must be before
the main method. In a program, we can define as many user-defined functions as we want.
2. Every C program must contain exactly one main method (Starting point of the program
execution).
5. For every open brace ({), there must be respective closing brace (}).
C Character Set
As every language contains a set of characters used to construct words, statements, etc., C
language also has a set of characters which include alphabets, digits, and special symbols. C
Every C program contains statements. These statements are constructed using words and
these words are constructed using characters from C character set. C language character set
1. Alphabets
2. Digits
3. Special Symbols
Alphabets
C language supports all the alphabets from the English language. Lower and upper case
Digits
C language supports 10 digits which are used to construct numerical values in C language.
Digits - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Special Symbols
C language supports a rich set of special symbols that include symbols to perform
mathematical operations, to check conditions, white spaces, backspaces, and other special
symbols.
Special Symbols - ~ @ # $ % ^ & * ( ) _ - + = { } [ ] ; : ' " / ? . > , < \ | tab newline space NULL bell
Every character in C language has its equivalent ASCII (American Standard Code for
C Tokens
Every C program is a collection of instructions and every instruction is a collection of some
individual units. Every smallest individual unit of a c program is called token. Every instruction
in a c program is a collection of tokens. Tokens are used to construct c programs and they are
1. Keywords
2. Identifiers
3. Operators
4. Special Symbols
5. Constants
6. Strings
7. Data values
C Keywords
As every language has words to construct statements, C programming also has words with a
specific meaning which are used to construct c program instructions. In the C programming
language, keywords are special words with predefined meaning. Keywords are also known as
In the C programming language, there are 32 keywords. All the 32 keywords have their
Keywords are the reserved words with predefined meaning which already known to the
compiler
Properties of Keywords
1. All the keywords in C programming language are defined as lowercase letters so they
2. Every keyword has a specific meaning, users can not change that meaning.
3. Keywords can not be used as user-defined names like variable, functions, arrays,
pointers, etc...
The following table specifies all the 32 keywords with their meaning...
C Identifiers
In C programming language, programmers can specify their name to a variable, array, pointer,
function, etc... An identifier is a collection of characters which acts as the name of variable,
function, array, pointer, structure, etc... In other words, an identifier can be defined as the
user-defined name to identify an entity uniquely in the c programming language that name
may be of the variable name, function name, array name, pointer name, structure name or a
label.
The identifier is a user-defined name of an entity to identify it uniquely during the program
execution
Example
int marks;
char studentName[30];
2. An identifier should not start with a numerical value. It can start with a letter or an
underscore.
3. We should not use any special symbols in between the identifier even whitespace.
5. There is no limit for the length of an identifier. However, the compiler considers the
The following are the commonly used rules for creating identifiers for better programming...
3. We start every identifier with a lowercase letter. If an identifier contains more than
one word then the first word starts with a lowercase letter and second word onwards
first letter is used as an UPPERCASE letter. We can also use an underscore to separate
C data types
Data used in c program is classified into different types based on its properties. In the C
programming language, a data type can be defined as a set of values with similar
characteristics. All the values in a data type have the same properties.
Data types in the c programming language are used to specify what kind of value can be
stored in a variable. The memory size and type of the value of a variable are determined by
the variable data type. In a c program, each variable or constant or array must have a data
type and this data type specifies how much memory is to be allocated and what type of values
are to be stored in that variable or constant or array. The formal definition of a data type is
as follows...
The Data type is a set of value with predefined characteristics. data types are used to
The primary data types in the C programming language are the basic data types. All the
primary data types are already defined in the system. Primary data types are also called as
Built-In data types. The following are the primary data types in c programming language...
The integer data type is a set of whole numbers. Every integer value does not have the decimal
value. We use the keyword "int" to represent integer data type in c. We use the keyword int
to declare the variables and to specify the return type of a function. The integer data type is
used with different type modifiers like short, long, signed and unsigned. The following table
value must contain the decimal value. The floating-point data type has two variants...
• float
• double
We use the keyword "float" to represent floating-point data type and "double" to represent
double data type in c. Both float and double are similar but they differ in the number of
decimal places. The float value contains 6 decimal places whereas double value contains 15
or 19 decimal places. The following table provides complete details about floating-point data
types.
The character data type is a set of characters enclosed in single quotations. The following
The following table provides complete information about all the data types in c programming
language...
void data type
The void data type means nothing or no value. Generally, the void is used to specify a function
which does not return any value. We also use the void data type to specify empty parameters
of a function.
each integer constant is given a name. The keyword "enum" is used to define the enumerated
data type.
Derived data types are user-defined data types. The derived data types are also called as user-
defined data types or secondary data types. In the c programming language, the derived data
• Arrays
• Structures
• Unions
• Enumeration
C Variables
Variables in a c programming language are the named memory locations where the user can
store different values of the same datatype during the program execution. That means a
variable is a name given to a memory location in which we can store different values of the
same data type. In other words, a variable can be defined as a storage container to hold values
of the same datatype during the program execution. The formal definition of a data type is as
follows...
Variable is a name given to a memory location where we can store different values of the
Every variable in c programming language must be declared in the declaration section before
it is used. Every variable must have a datatype that determines the range and type of values
A variable name may contain letters, digits and underscore symbol. The following are the
3. A variable name should not contain any special symbols except underscore(_).
4. A variable name can be of any length but compiler considers only the first 31
Declaration of Variable
Declaration of a variable tells the compiler to allocate the required amount of memory with
the specified variable name and allows only specified datatype values into that memory
location. In C programming language, the declaration can be performed either before the
function as global variables or inside any block or function. But it must be at the beginning of
block or function.
Declaration Syntax:
datatype variableName;
Example
int number;
The above declaration tells to the compiler that allocates 2 bytes of memory with the
name number and allows only integer values into that memory location.
C Constants
In C programming language, a constant is similar to the variable but the constant hold only
one value during the program execution. That means, once a value is assigned to the
constant, that value can't be changed during the program execution. Once the value is
assigned to the constant, it is fixed throughout the program. A constant can be defined as
follows...
A constant is a named memory location which holds only one value throughout the
program execution.
In C programming language, a constant can be of any data type like integer, floating-point,
Integer constants
decimal integer value is specified as direct integer value whereas octal integer value is
An integer constant can also be unsigned type of integer constant or long type of integer
constant. Unsigned integer constant value is suffixed with 'u' and long integer constant value
is suffixed with 'l' whereas unsigned long integer constant value is suffixed with 'ul'.
Example
contain the exponent part. When a floating-point constant is represented in exponent form,
Example
Character Constants
Example
'A'
'2'
'+'
In the C programming language, there are some predefined character constants called escape
sequences. Every escape sequence has its own special functionality and every escape
sequence is prefixed with '\' symbol. These escape sequences are used in output function
called 'printf()'.
String Constants
A string constant is a collection of characters, digits, special symbols and escape sequences
"This is btechppsclass"
We can define string constant using multiple lines as follows...
We can also define string constant by separating it with white space as follows...
Creating constants in C
We create a constant of any datatype using 'const' keyword. To create a constant, we prefix
The general syntax for creating constant using 'const' keyword is as follows...
OR
Example
const int x = 10 ;
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
int i = 9 ;
const int x = 10 ;
i = 15 ;
x = 100 ; // creates an error
printf("i = %d\nx = %d", i, x ) ;
The above program gives an error because we are trying to change the constant variable
value (x = 100).
using this preprocessor directive it must be defined at the beginning of the program (because
all the preprocessor directives must be written before the global declaration).
We use the following syntax to create constant using '#define' preprocessor directive...
Example
#define PI 3.14
Example Program
#include<stdio.h>
#include<conio.h>
#defien PI 3.14
void main(){
int r, area ;
area = PI * (r * r) ;
C Storage Classes
In C programming language, storage classes are used to define things like storage
location (whether RAM or REGISTER), scope, lifetime and the default value of a variable.
memory (RAM) or CPU Registers. The allocation of memory depends on storage classes.
In C programming language, there are FOUR storage classes and they are as follows...
The default storage class of all local variables (variables declared inside block or function) is
auto storage class. Variable of auto storage class has the following properties...
Property Description
Keyword Auto
Life time Till the control remains within the block in which variable is define
Example Program 1
#include<stdio.h>
#include<conio.h>
int main(){
int i;
auto char c;
float f;
printf("i = %d\tc = %c\tf = %f",i,c,f);
return 0;
}
Example Program 2
#include<stdio.h>
#include<conio.h>
int main(){
int a=10;
{
int a=20;
printf("%d",a);
}
printf(" %d",a);
return 0;
}
Example Program 3
#include<stdio.h>
#include<conio.h>
int main(){
{
int a=20;
printf("%d",a);
}
printf(" %d",a); //a is not visible here
return 0;
}
The default storage class of all global varibles (variables declared outside function) is external
storage class. Variable of external storage class has the following properties...
Property Description
Keyword Extern
Life time As long as the program’s execution does not comes to end
Example Program 1
#include<stdio.h>
#include<conio.h>
Example Program 2
#include<stdio.h>
#include<conio.h>
end of the program. The static variable allows to initialize only once and can be modified any
number of times. Variable of static storage class has the following properties...
Property Description
Keyword Static
Life time The value of the persists between different function calls (i.e., Initializatio
Example Program 1
#include<stdio.h>
#include<conio.h>
static int a;
int main(){
printf("%d",a);
return 0;
}
Example Program 2
#include<stdio.h>
#include<conio.h>
allocated in CPU Registers. The memory of the register variable is allocated in CPU Register
but not in Computer memory (RAM). The register variables enable faster accessibility
compared to other storage class variables. As the number of registers inside the CPU is very
less we can use very less number of register variables. Variable of register storage class has
Property Description
Keyword Register
Life time Till the control remains within the block in which variable is define
Example Program 1
#include<stdio.h>
#include<conio.h>
int main(){
register int a,b;
scanf("%d%d",&a,&b);
printf("%d %d",a,b);
}
Register register CPU Register Garbage Local to the block in which the
Value variable has defined
C Output Functions
C programming language provides built-in functions to perform output operation. The output
operations are used to display data on user screen (output screen) or printer or any file. The
1. printf()
2. putchar()
3. puts()
4. fprintf()
printf() function
The printf() function is used to print string or data values or a combination of string and data
values on the output screen (User screen). The printf() function is built-in function defined in
a header file called "stdio.h". When we want to use printf() function in our program we need
to include the respective header file (stdio.h) using the #include statement. The printf()
Syntax:
printf("message to be display!!!");
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
Output:
In the above example program, we used the printf() function to print a string on to the output
screen.
The printf() function is also used to display data values. When we want to display data values
Syntax:
printf("format string",variableName);
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
int i = 10;
float x = 5.5;
printf("%d %f",i, x);
Output:
In the above example program, we used the printf() function to print data values of variables
i and x on to the output screen. Here i is a an integer variable so we have used format string
The printf() function can also be used to display string along with data values.
Syntax:
void main(){
int i = 10;
float x = 5.5;
printf("Integer value = %d, float value = %f",i, x);
Output:
In the above program, we are displaying string along with data values.
Every function in the C programming language must have a return value. The printf() function
also have an integer as a return value. The printf() function returns an integer value equivalent
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
int i;
i = printf("btechsmartclass");
printf(" is %d number of characters.",i);
Output:
characters. So it returns integer value 15 to the variable "i". The value of "i" is printed in the
Generally, when we write multiple printf() statements the result is displayed in a single line
because the printf() function displays the output in a single line. Consider the following
example program...
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
printf("Welcome to ");
printf("btechsmartclass ");
printf("the perfect website for learning");
Output:
In the above program, there are 3 printf() statements written in different lines but the output
called escape sequences. Escape sequences are special characters with special functionality
used in printf() function to format the output according to the user requirement. In the C
\a Beep sound
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
printf("Welcome to\n");
printf("btechsmartclass\n");
printf("the perfect website for learning");
}
Output:
putchar() function
The putchar() function is used to display a single character on the output screen. The putchar()
functions prints the character which is passed as a parameter to it and returns the same
character as a return value. This function is used to print only a single character. To print
multiple characters we need to write multiple times or use a looping statement. Consider the
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
char ch = 'A';
putchar(ch);
Output:
puts() function
The puts() function is used to display a string on the output screen. The puts() functions prints
a string or sequence of characters till the newline. Consider the following example program...
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
char name[30];
printf("\nEnter your favourite website: ");
gets(name);
puts(name);
}
Output:
fprintf() function
The fprintf() function is used with the concept of files. The fprintf() function is used to print a
line into the file. When you want to use fprintf() function the file must be opened in writting
mode.
C Input Functions
C programming language provides built-in functions to perform input operations. The input
operations are used to read user values (input) from the keyboard. The c programming
1. scanf()
2. getchar()
3. getch()
4. gets()
5. fscanf()
scanf() function
The scanf() function is used to read multiple data values of different data types from the
keyboard. The scanf() function is built-in function defined in a header file called "stdio.h".
When we want to use scanf() function in our program, we need to include the respective
header file (stdio.h) using #include statement. The scanf() function has the following syntax...
Syntax:
scanf("format strings",&variableNames);
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
int i;
printf("\nEnter any integer value: ");
scanf("%d",&i);
printf("\nYou have entered %d number",i);
Output:
In the above example program, we used the scanf() function to read an integer value from
The scanf function also used to read multiple data values of different or the same data types.
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
int i;
float x;
printf("\nEnter one integer followed by one float value : ");
scanf("%d%f",&i, &x);
printf("\ninteger = %d, float = %f",i, x);
Output:
In the above example program, we used the scanf() function to read one integer value and
one float value from the keyboard. Here 'i' is an integer variable so we have used format string
%d, and 'x' is a float variable so we have used format string %f.
The scanf() function returns an integer value equal to the total number of input values read
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
int i,a,b;
float x;
printf("\nEnter two integers and one float : ");
i = scanf("%d%d%f",&a, &b, &x);
printf("\nTotal inputs read : %d",i);
Output:
getchar() function
The getchar() function is used to read a character from the keyboard and return it to the
program. This function is used to read a single character. To read multiple characters we need
to write multiple times or use a looping statement. Consider the following example program...
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
char ch;
printf("\nEnter any character : ");
ch = getchar();
printf("\nYou have entered : %c\n",ch);
Output:
getch() function
The getch() function is similar to getchar function. The getch() function is used to read a
character from the keyboard and return it to the program. This function is used to read a
single character. To read multiple characters we need to write multiple times or use a looping
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
char ch;
printf("\nEnter any character : ");
ch = getch();
printf("\nYou have entered : %c",ch);
}
Output:
gets() function
The gets() function is used to read a line of string and stores it into a character array. The
gets() function reads a line of string or sequence of characters till a newline symbol enters.
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
char name[30];
printf("\nEnter your favourite website: ");
gets(name);
printf("%s",name);
}
Output:
fscanf() function
The fscanf() function is used with the concept of files. The fscanf() function is used to read
data values from a file. When you want to use fscanf() function the file must be opened in
reading mode.
C Operators
An operator is a symbol used to perform arithmetic and logical operations in a program. That
means an operator is a special symbol that tells the compiler to perform mathematical or
logical operations. C programming language supports a rich set of operators that are
classified as follows.
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
5. Assignment Operators
6. Bitwise Operators
7. Conditional Operator
8. Special Operators
The arithmetic operators are the symbols that are used to perform basic mathematical
operations like addition, subtraction, multiplication, division and percentage modulo. The
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
When it is used with numerical values, it performs mathematical addition and when it is used
⇒ The remainder of the division operator is used with integer data type only.
The relational operators are the symbols that are used to compare two values. That means
the relational operators are used to check the relationship between two values. Every
relational operator has two results TRUE or FALSE. In simple words, the relational operators
are used to define conditions in a program. The following table provides information about
relational operators.
Operator Meaning
< Returns TRUE if the first value is smaller than second value otherwise returns FA
> Returns TRUE if the first value is larger than second value otherwise returns FAL
<= Returns TRUE if the first value is smaller than or equal to second value otherwise
>= Returns TRUE if the first value is larger than or equal to second value otherwise
!= Returns TRUE if both values are not equal otherwise returns FALSE
&& Logical AND - Returns TRUE if all conditions are TRUE otherwise returns FALSE
|| Logical OR - Returns FALSE if all conditions are FALSE otherwise returns TRUE
! Logical NOT - Returns TRUE if condition is FLASE and returns FALSE if it is TRUE
⇒ Logical AND - Returns TRUE only if all conditions are TRUE, if any of the conditions is FALSE
⇒ Logical OR - Returns FALSE only if all conditions are FALSE, if any of the conditions is TRUE
The increment and decrement operators are called unary operators because both need only
one operand. The increment operators adds one to the existing value of the operand and the
decrement operator subtracts one from the existing value of the operand. The following table
Operator Meaning
The increment and decrement operators are used infront of the operand (++a) or after the
Pre-Increment or Pre-Decrement
In the case of pre-increment, the value of the variable is increased by one before the
expression evaluation. In the case of pre-decrement, the value of the variable is decreased by
one before the expression evaluation. That means, when we use pre-increment or pre-
decrement, first the value of the variable is incremented or decremented by one, then the
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
int i = 5,j;
j = ++i; // Pre-Increment
Output:
Post-Increment or Post-Decrement
In the case of post-increment, the value of the variable is increased by one after the
expression evaluation. In the case of post-decrement, the value of the variable is decreased
by one after the expression evaluation. That means, when we use post-increment or post-
decrement, first the expression is evaluated with existing value, then the value of the variable
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
int i = 5,j;
j = i++; // Post-Increment
Output:
The assignment operators are used to assign right-hand side value (Rvalue) to the left-hand
side variable (Lvalue). The assignment operator is used in different variants along with
arithmetic operators. The following table describes all the assignment operators in the C
programming language.
Operator Meaning
+= Add both left and right-hand side values and store the result into left-hand side
-= Subtract right-hand side value from left-hand side variable value and store the r
into left-hand side variable
*= Multiply right-hand side value with left-hand side variable value and store the re
into left-hand side variable
/= Divide left-hand side variable value with right-hand side variable value and store
into the left-hand side variable
Operator Meaning
%= Divide left-hand side variable value with right-hand side variable value and store
into the left-hand side variable
The bitwise operators are used to perform bit-level operations in the c programming
language. When we use the bitwise operators, the operations are performed based on the
binary values. The following table describes all the bitwise operators in the C programming
language.
Operator Meaning
& the result of Bitwise AND is 1 if all the bits are 1 otherwise it is 0
^ the result of Bitwise XOR is 0 if all the bits are same otherwise it is 1
<< the Bitwise left shift operator shifts all the bits to the left by the specified numbe
>> the Bitwise right shift operator shifts all the bits to the right by the specified num
Conditional Operator (?:)
The conditional operator is also called a ternary operator because it requires three operands.
This operator is used for decision making. In this operator, first we verify a condition, then we
perform one operation out of the two operations based on the condition result. If the
condition is TRUE the first option is performed, if the condition is FALSE the second option is
Example
A = (10<15)?100:200; A value is 100
sizeof operator
This operator is used to find the size of the memory (in bytes) allocated for a variable. This
sizeof(variableName);
Example
sizeof(A); ⇒the result is 2 if A is an integer
This operator is used to separate variables while they are declaring, separate the expressions
C Expressions
What is an expression?
In any programming language, if we want to perform any calculation or to frame any condition
etc., we use a set of symbols to perform the task. These set of symbols makes an expression.
In the above definition, an operator is a symbol that performs tasks like arithmetic
Operands are the values on which the operators perform the task. Here operand can be a
Expression Types in C
In the C programming language, expressions are divided into THREE types. They are as
follows...
1. Infix Expression
2. Postfix Expression
3. Prefix Expression
Infix Expression
The expression in which the operator is used between operands is called infix expression.
Example
Postfix Expression
The expression in which the operator is used after operands is called postfix expression.
Example
Prefix Expression
The expression in which the operator is used before operands is called a prefix expression.
Example
C Operator Precedence and
Associativity
What is Operator Precedence?
In c programming language every operator has precedence (priority). When there is more
than one operator in an expression the operator with higher precedence is evaluated first
multiple operators with equal precedence, we use associativity to determine the order of
In c programming language the operator precedence and associativity are as shown in the
following table.
1 () function call
[] array reference
-> structure member access
. structure member access
Precedence Operator Operator Meaning
2 ! negation
~ 1's complement
+ Unary plus
- Unary minus
++ increment operator
-- decrement operator
& address of operator
* pointer
sizeof returns size of a variable
(type) type conversion
3 * multiplication
/ division
% remainder
4 + addition
- subtraction
7 == equal to
!= not equal to
9 ^ bitwise EXCLUSIVE OR
10 | bitwise OR
Precedence Operator Operator Meaning
12 || logical OR
13 ?: conditional operator
14 = assignment
*= assign multiplication
/= assign division
%= assign remainder
+= assign addition
-= assign subtraction
&= assign bitwise AND
^= assign bitwise XOR
|= assign bitwise OR
<<= assign left shift
>>= assign right shift
15 , separator
In the above table, the operator precedence decreases from top to bottom and increases
C Expression Evaluation
In the C programming language, an expression is evaluated based on the operator
precedence and associativity. When there are multiple operators in an expression, they are
evaluated according to their precedence and associativity. The operator with higher
precedence is evaluated first and the operator with the least precedence is evaluated last.
that expression.
expression...
10 + 4 * 3 / 2
In the above expression, there are three operators +, * and /. Among these three operators,
both multiplication and division have the same higher precedence and addition has lower
precedence. So, according to the operator precedence both multiplication and division are
evaluated first and then the addition is evaluated. As multiplication and division have the
same precedence they are evaluated based on the associativity. Here, the associativity of
multiplication and division is left to right. So, multiplication is performed first, then division
and finally addition. So, the above expression is evaluated in the order of * / and +. It is
evaluated as follows...
4 * 3 ====> 12
12 / 2 ===> 6
10 + 6 ===> 16
different data types. When the expression contains similar datatype values then it is evaluated
without any problem. But if the expression contains two or more different datatype values
then they must be converted to the single datatype of destination datatype. Here, the
destination is the location where the final result of that expression is stored. For example, the
multiplication of an integer data value with the float data value and storing the result into a
float variable. In this case, the integer value must be converted to float value so that the final
follows...
1. Type Conversion
2. Type Casting
Type Conversion
The type conversion is the process of converting a data value from one data type to another
data type automatically by the compiler. Sometimes type conversion is also called implicit
type conversion. The implicit type conversion is automatically performed by the compiler.
For example, in c programming language, when we assign an integer value to a float variable
the integer value automatically gets converted to float value by adding decimal value 0. And
when a float value is assigned to an integer variable the float value automatically gets
converted to an integer value by removing the decimal value. To understand more about type
int i = 10 ;
float x = 15.5 ;
char ch = 'A' ;
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
int i = 95 ;
float x = 90.99 ;
char ch = 'A' ;
i = x ;
printf("i value is %d\n",i);
x = i ;
printf("x value is %f\n",x);
i = ch ;
printf("i value is %d\n",i);
Output:
In the above program, we assign i = x, i.e., float variable value is assigned to the integer
variable. Here, the compiler automatically converts the float value (90.99) into integer value
(90) by removing the decimal part of the float value (90.99) and then it is assigned to variable i.
Similarly, when we assign x = i, the integer value (90) gets converted to float value (90.000000)
Typecasting
Typecasting is also called an explicit type conversion. Compiler converts data from one data
type to another data type implicitly. When compiler converts implicitly, there may be a data
loss. In such a case, we convert the data from one data type to another data type using explicit
type conversion. To perform this we use the unary cast operator. To convert data from one
type to another type we specify the target data type in parenthesis as a prefix to the data
(TargetDatatype) DataValue
Example
float average ;
perform totalMarks / maxMarks the result is a float value, but the destination (average)
datatype is a float. So we use type casting to convert totalMarks and maxMarks into float data
type.