Search for notes by fellow students, in your own course and all over the country.

Browse our notes for titles which look like what you need, you can preview any of the notes via a sample of the contents. After you're happy these are the notes you're after simply pop them into your shopping cart.

My Basket

You have nothing in your shopping cart yet.

Title: C language
Description: Learn c from basics

Document Preview

Extracts from the notes are below, to see the PDF you'll receive please use the links above


Unit – 1

Unit - 1
Introduction
Computer Systems
Computer is the word which is derived from a Latin word Compute which is nothing but
calculate
...
Computer is an electronic device which can perform arithmetic and logical operations and
stores the data
...
A Computer is an electronic device that executes the instructions in a program or in other
words
...
A device that computes especially a programmable electronic machine that performs highspeed mathematical or logical operations and stores the data
...
The set of Programs is called the
Software



Data: It is collection of raw facts and figures



Information: data which has been massaged into a useful form that is the processed data
which is called as Information
...




Output: The information or result which is given by the computer
...


A computer system has Four Functions
...
Accepts Data (Accept)
2
...
Produces Output (Output)
4
...




Programs are the set of instructions that given to the computer
...
1

Unit – 1


Commands are special code or key words that the user input to perform a task , like They
may also chosen by clicking on a command button



User response is the user’s answer to the computer‘s question, such as choosing OK, YES or
NO or by typing in text
...
People also process data
...




The computers can’t think in the same way that people do
...


Output:
This is data that has been processed into useful form, now it is called as information
...

Categories of Output:
Text documents: including reports, letters etc
...

Main memory:
It is also called as Primary Storage
...
It‘s volatile, meaning that the power off erases all the data
...

Secondary Memory:
It is also called as Auxiliary Storage
...
It is
nonvolatile, meaning that turning the power off does not erase it
...




Reliability: Failures are usually due to human error, one way or another, Accuracy is there
...




Versatility: A computer is able to adapt or be adapted to many different functions or
activities
...
2

Unit – 1
Peripherals of a computer are:


Memory: RAM, Hard Disk, Cache Memory



C
...
U: Mother Board, Switched Mode Power Supply, Floppy Drive, CD-RW, Mother Board
Consists of CMOS, PCI, Processor, Heat Sink Fan



Input Devices: Keyboard, Mouse, Light Pen, Joy Stick, Scanner, Speech Input

Devices,

Touch Screen, Touch Pad
...
The most basic collection includes a
Computer CPU, a Monitor, a Keyboard, and a Mouse
...
The computer's CPU is actually a small electronic device inside the
case but the term is often used to refer to the whole collection of electronics inside the box
...
It
consists of a television picture tube that had been modified to accept the type of video signal created
by the computer's electronics
...
The picture quality leaves something to be desired
...
A
computer keyboard closely resembles a conventional typewriter keyboard with the addition of
numerous keys that are used specifically for computing functions
...
The mouse is another input device used to point at objects on the computer monitor and select
them
...

Floppy Disk Drive:
Once the most advanced of storage devices, floppy diskettes are normally used a temporary
storage containers or transportation media for data
...
44 MB of
computer data
...
Computer diskettes are not as reliable or fast as the internal storage drives on the
computer
...


C Programming Study Material

Page No
...
Most disks of
this kind are "Read only" meaning that the computer can retrieve information from the disk, but
cannot place information on it
...
A different kind of hardware mechanism is needed to employ this
innovation
...
Every component at some point
communicates through the motherboard, either by directly plugging into it or by communicating
through one of the motherboards ports
...
Its
purpose inside your PC is to provide a platform for all the other components and peripherals to talk
to each other
...
The primary function of the motherboard chipset is to direct this data to the correct
areas of the motherboard, and therefore the correct components
...
Motherboards
contain expansion slots such as the ISA, PCI, AGP and DIMM sockets
...

So the motherboard has a massive part to play in the workings of your PC
...
Its
best to buy a decent motherboard especially if you plan on buying extra's in the future
...
Below
is an illustration of what the inside of the hard disk drive may look like
...

SMPS:
It is a Switched Mode Power Supply
...
And
this is used to coverts the AC power to DC power and supply that DC power to all the components in
the Computer System
...
4

Unit – 1

Hardware Concepts
The computer system mainly consists of 2 important components
...
Central Processing Unit (CPU) or Processor
...
Random Access Memory (RAM)
Any program is required to executed, it has to load into RAM
...


A Simple Computer
The above systems are called as Von-Neumann computers, or Serial Computers
...

The following are the different ware’s available
...
Hardware: Materialistic thing which we can touch and feel
...
Software: Ideological things which we cannot perceive and feel
...
Firmware: It is a fixed and small program that internally controls various electronics
devices
...

4
...

5
...

Some Novice HW Concepts:
Processor:
The processors are rated as 2
...
, These numbers really convey the clock rate
at which the processor can work at most
...
That is in terms of operations per second
...

Computers use some elementary operations known as micro operations while executing the
user’s instruction
...



b value has to be fetched from RAM to CPU



c value has to be fetched from RAM to CPU



Plus operation has to be performed
...
5

Unit – 1
Binary System:
The information, which it is stored or manipulated by the computer memory it will be done in
binary mode
...
In order to run any
program, it has to be loaded into RAM
...
So this memory is also called as volatile memory
...
A Flip-Flop can be
considered as a bi-state device such as an electrical switch
...
If it is set, value ONE is assumed to be stored, otherwise ZERO is assumed to be
stored
...
A series of Flip-Flops in the processor is called as
register
...
If the number what
we like to store in the computer memory is not exact multiple of eight Flip-Flops, then ZERO
padding will be done to the left hand side of the number and then the number is stored in the
computer memory
...
Whatever we
store in this device cannot be modified or erased through software
...
This can be used to
store Power On Self Test (POST) software
...
This is called as POST software
...
If all the parts are working fine then the Operating
System (OS) such as Disk Operating System, Microsoft Windows, Unix etc
...
Then the users can utilize the computer
services
...
6

Unit – 1

Software Concepts
Operating System:
An operating system is an interface between the Computer System and User
...
The OS provides software to the user to utilize the
computer resources
...
, among the users
...

System Software:
The software related to operating systems, network drivers, device drivers, etc
...
Development of this type of software needs the knowledge of basics of physical
devices
...

Examples: Operating System, Device Drivers, Network Drivers, shells and loaders
Application Software:
The application programs are software that enables the end-user to perform specific,
productive tasks, such as word processing, image manipulation
...

Examples: Games, Text Editors, Image Editors, Spreadsheet packages
...
It is used to develop
the software that satisfies the user’s requirements
...
,
There are 3 types of programming languages
...

And the examples of this low level language are Machine Language, Assembly Language
...
And the
examples of these medium level languages are C, C++
...
The
examples of these high level languages are C, C++, Java, FORTRAN, COBOL, BASIC
...
Thus, programs written using processors Machine Language or Assembly language
will work only on that processor
...

High level language programs can work on variety of machines without many modifications
...


C Programming Study Material

Page No
...
They give freedom to directly interact with the parts of the computer system
...



It should have better readability



Understandability



Repair ability



Upgradeability



Conciseness



Portability

Translators:
The translators are used to convert the programming written in one level language to another
level language
...



Assemblers



Compilers



Converters



Interpreters

Assemblers:
An assembler takes Assembly Language Programs as input and converts them into Machine
Language
...
Because, assembly
language programs are also not understand by the processor
...

Compilers:
The compilers take high level language programs as input and convert them into machine
language
...
The examples of ‘C’ language compilers are Turbo C, Borland C, Sun C, Solaris C
...

Interpreters:
The interpreters also take high level language programs as input and convert them into
machine language
...
Usually, Interpreters are needed to identify run time errors, which
are also called as bugs
...
8

Unit – 1
Errors:
An error is a mistake done by anybody
...
So the errors during programming are classified as


Compile time errors (Syntactical Errors)



Run time errors (Bugs)



Computational errors

Compile time errors:
These errors are also called as Syntactical errors
...
Unless, we correct all the errors in our
program, it will not be converted into machine language
...

Examples of this type of errors:


In ‘C’ language all the executable statements are ended with semi colon
...




In JAVA and ‘C’ language the program ends with the following symbol “}”
...


Run time errors:
These errors are also called as Bugs
...
If the run time errors occur when the program execution then
the program will abnormally terminate
...

Example of this type of errors:


If programs violate the rules of the Operating System
...




Program contains an expression like1/0
...


Computational errors:
These errors are the ones, which will distort the final value of the program
...


Also because of mathematical equations, and operations on them may lead to

computational errors
...
333333
...
This is also
technically called as precision
...
Mistakes in the 40th decimal point may also lead to catastrophic results
...
9

Unit – 1

Problem Solving
Program Development Stages:
The typical program development involves
1
...
Preparing Flow Chart
3
...
Compiling and Executing
...
Algorithm can be defined as a
well-defined computational procedure that takes single value or a set of values as inputs and
produces a single or multiple values as output
...
A single problem may have multiple algorithms
...

In order to compare algorithms, we must have some criteria to measure the efficiency of our
algorithms
...
The time and space used by the algorithm are the
two main measures for the efficiency of the algorithm
...
This is because key operations are so defined that the time for the other operation is
much less than or at the most equal to the time for key operations
...

The complexity of an algorithm is a function which gives the running time and / or storage
space requirement of the algorithm in terms of the size of the input data
...

Example: Algorithm for finding the maximum of three numbers
1
...
If a is greater than b and c then print a as maximum
...

So as told above, algorithms are written in normal language
...
So that algorithm is independent of the programming language
...

Pseudo-code:
Pseudo-code is a simple instruction language to write any code
...
The main difference b/w an algorithm and a pseudo code is that, algorithm is written as
steps using natural language (English) whereas pseudo code consists of both natural language and
mathematical notations
...
10

Unit – 1

Flowchart
The pictorial representation or a diagrammatic representation of an Algorithm is called as
Flowchart
...
The operation to be performed is
written in the box
...

Uses of Flowchart:
1
...

2
...

3
...

The following graphic symbols are used while preparing the flow chart
...
To find the maximum of three numbers

C Programming Study Material

Page No
...
To print the Fibonacci series for 1 to n value
...
Here the algorithm and flowchart is common for all the programming languages
...
So here we
will see how the coding will done using ‘C’ Language
...
Some editors are line-oriented, while others are characteroriented (full-screen editors)
...
UNIX operating system has ed, vi editors and TURBO C gives an menu driven interface
with many options
...
Program files are saved with the extension
...
c, add
...
There are several such IDEs available in the market targeted towards different
operating systems
...
And we can use the common editors like notepad, word pad in
Microsoft Windows operating systems
...

Here we can create or edit files
...


C Programming Study Material

Page No
...
Use
Notepad for basic text editing
...

Using Turbo ‘C’ Editor:
Turbo ‘C’ editor is an IDE (Integrated Development Environment)
...
Using this we can create the program, compile the program and
after that we can execute the program also
...
The
menus like File, Edit, Run, Compile, Options, Help etc
...

Compilation and Execution:
After writing the program using any of the editors we have to save the file
...
Turbo C makes an object file with extension
...
After that when we are executing the file Turbo C makes executable file with extension

...

Program Development and Execution using Turbo C:
Assuming that you are using a Turbo C compiler here are the steps that you need to follow to
compile and execute your first C program


Open Command Prompt in Microsoft Windows Operating System
...
Then the command prompt will be
opened
...
That will be possible using change directory option
...

Then you can come to C:\>
...
The compiler TC
...
For that you need to give C:\> cd tc/bin in the command prompt
...




Select New from the File menu
...




Save the program using F2 under a proper name (say Program1
...




Use Alt+F9 to compile the program
...




Use Alt + F5 to view the output
...
EXE) on the disk
...
If we copy this file to another
machine we can execute it there without being required to recompile it
...


C Programming Study Material

Page No
...
The various steps involved in the program development are as follows:
Editing Phase
In the editing phase, the C program is entered into a file through a text editor
...
Modern
compiler vendors provide Integrated Development Environment (IDE)
...
Corrections in the program at later stages are done through these editors
...

Compilation Phase
This phase is carried out by a program called as compiler
...
The compilation phase cannot proceed successfully until and unless the
source code is error-free
...
The error-free source code is translated into object code and stored in a separate file
with an extension ‘
...

Linking Phase
In this phase, the linker links all the files and functions with the object code under execution
...

Execution Phase
In this phase, the executable object code is loaded into the memory and the program
execution begins
...
The errors may be run-time errors and logical errors
...
14

Unit – 1

Computer Languages
A programming language is an artificial language designed to express computations that can
be performed by a machine, particularly a computer
...

Many programming languages have some form of written specification of their syntax (form)
and semantics (meaning)
...
For example, the
C programming language is specified by an ISO Standard
...

The earliest programming languages predate the invention of the computer, and were used to
direct the behavior of machines
...
Most programming
languages describe computation in an imperative style, i
...
, as a sequence of commands, although
some languages, such as those that support functional programming or logic programming, use
alternative forms of description
...
Machine code may be regarded as a primitive
programming language or as the lowest-level representation of a compiled and/or assembled
computer program
...
Machine code is sometimes called native code when referring to
platform-dependent parts of language features or libraries
...

Symbolic Language:
Most organisms communicate, but humans are unique in communicating via symbolic
language
...
We think and behave in ways that are quite odd compared to other species because
of the way that language has defined us
...

The programming language that uses symbols for expressing operations and operands is
called as Symbolic Language
...
The
symbolic language manipulates symbols rather than numbers
...
In comparison to low-level programming languages, it may use natural

C Programming Study Material

Page No
...
Such languages hide the
details of CPU operations such as memory access models and management of scope
...
A
high-level language isolates the execution semantics of computer architecture from the specification
of the program, making the process of developing a program simpler and more understandable with
respect to a low-level language
...
The examples for the high-level languages are C, C++, Java, FORTRAN,
COBOL etc
...
In that type the program then save it
after that we need to compile the program then execute the program
...
For that you need to follow the
below steps
...
For that click on the start
button and click on Run… then write cmd in the text box
...




Then come to C:\>
...
We can give cd\
...
Now start the compiler
...
EXE is usually present
in C:\TC\BIN directory
...

So now C compiler will start
...




Type the program
...
That looks like this

C Programming Study Material

Page No
...
For example in the above program we forget to give
semicolon “;” after the printf statement
...
That looks like
this

So in the above picture it shows that statement missing in function main
...

Compiling:
After saving the file we need to compile the compile
...
Then the file will be compiled by the compiler if there are no syntactical
errors in the program
...
obj file
...
obj file
...
See the following picture
...
17

Unit – 1
Now after compiling the file Welcome
...
obj
...
For example,
in the above program we given a ‘printf’ function, then the linker links our program, object code
with the object code of the printf function
...

For example, if a user uses a ‘printf’ function in his her program, the linker links the user programs,
object code with the object code of the printf function
...
Linking can be done at compile time, at load time (by
loaders) and also at run time (by application programs)
Executing:
After compiling and linking the file we will get
...
Then execute the file
...
So to run or execute the file you need
to press Ctrl+F9
...
It will be shown
in the below picture
...
exe file
...
To see the output we need to
press Alt + F5
...


C Programming Study Material

Page No
...
Although it was
originally intended to run under UNIX, there has been a great interest in running it under the MSDOS operating system on the IBM PC and compatibles
...

C is not the best beginning language because it is somewhat puzzling in nature
...
There seems to be no limit to the flexibility available
...
Portability means that C programs written for a
computer with a particular kind of processor say Intel can be executed on computers with different
processors such as Motorola, Sun Sparc, or IBM with little or no modification
...
An Operating System is
software that controls the various functions of a computer
...

Behind the Name – ‘C’ Language:
BCPL (Basic Combined Programming Language) is a computer programming language
designed by Martin Richards of the University of Cambridge in 1966
...
It is almost extinct, as it was replaced with the C language
...
The first version of UNIX was written in the low-level PDP-7 assembler language
...

Then Dennis Ritchie developed the Language 'C' due to the arrival of PDP-11 computer
...


C Language Usages:
‘C’s ability to communicate directly with hardware makes it a powerful choice for system
programmers
...

Additionally, even compilers and interpreters for other languages such as FORTRAN, Pascal, and
BASIC are written in C
...
It is
also used to develop any kind of application, including complex business ones
...
19

Unit – 1


Embedded Systems



Systems Programming



Artificial Intelligence



Industrial Automation



Computer Graphics



Space Research



Image Processing



Game Programming

‘C’ Language is a Structure Programming Language:
C is a structured programming language, which means that it allows you to develop programs
using well-defined control structures, and provides modularity (breaking the task into multiple sub
tasks that are simple enough to understand and to reuse)
...

Advantages of ‘C’ Language:
The ‘C’ Language is a very powerful language
...
And the other advantages of the ‘C’ language are


C is simple
...
Keywords are words that have
special meaning in C language
...




C enables easy communication with computer hardware making it easy to write system
programs such as compilers and interpreters
...




'C' has very good facilities for creating tables of constant data within the source file
...
All the heavy work is done by
explicit library function calls
...




'C' compilers have an optimization phase which can be invoked if desired
...
The code will be just as good as that
produced by an assembly code programmer of normal skill real guru programmers can do
only slightly better
...
20

Unit – 1

Structure of C Program
The C language program should start with main() function
...
The below one is the structure of the C language
program
...
These lines are always preceded by a hash sign (#)
...
Example of this
preprocessor directive is #include ...

These preprocessor directives extend only across a single line of code
...
No semicolon (;) is expected at
the end of a preprocessor directive
...

The variables which we are declaring before the main function are called as global variables
...

The function main is very important, and must appear once, and only once in every C
program
...
Following the main
program name is a pair of parentheses which are an indication to the compiler that this is a function
...
The
actual program statements go between the two braces and in this case, there are no statements
because the program does absolutely nothing
...
21

Unit – 1
The variables which we are declaring in the function are called as local variables
...
The variables can be used within the function in which
it is declared
...
One of them should be main function
...
We can write any number of user defined functions in the
program
...
" A token
is source-program text that the compiler does not break down into component elements
...
Punctuation characters
such as brackets ([ ]), braces ({ }), parentheses ( ( ) ), and commas (,) are also tokens
...

An object, sometimes called a variable, is a location in storage, and its interpretation depends on two
main attributes: its storage class and its type
...
Two rules must be kept in mind when naming
identifiers
...
Using INDEX for a variable name is not the
same as using index and neither of them is the same as using InDeX for a variable name
...




Don’t give the space in between the identifier
...
Example: don’t give as student no, you have to declare as student_no
...
50;

C Programming Study Material

Page No
...
These are data types
...

A C programmer has to use appropriate data type as per his requirement
...


Integer

int

2
...


Floating Point

float

4
...


Void

void

The size and range of each data type is given in the table below
DATA TYPE

RANGE OF VALUES

char

-128 to 127

int

-32768 to +32767

float

3
...
4 e+38

double

1
...
7 e+308

Integer Type:
Integers are whole numbers with a machine dependent range of values
...
All of these data types have signed and unsigned
forms
...
Unsigned numbers are always
positive and consume all the bits for the magnitude of the number
...

Floating Point Types:
Floating point number represents a real number with 6 digits precision
...
When the accuracy of the floating point number is
insufficient, we can use the double to define the number
...
To extend the precision further we can use long double which consumes 80 bits of
memory space
...
23

Unit – 1
Void Type:
Using void data type, we can specify the type of a function
...

Character Type:
A single character can be defined as a defined as a character type of data
...
The qualifier signed or unsigned can be explicitly applied
to char
...

Size and Range of Data Types on 16 bit machine
...
4 e-38 to 3
...
7e-308 to 1
...
4 e-4932 to 3
...
24

Unit – 1

Constant and Variables
Instructions in C language are formed using syntax and keywords
...
Any instruction that mismatches with C language Syntax generates
an error while compiling the program
...
Keywords as special words which are exclusively used by C language, each keyword has
its own meaning and relevance hence, Keywords should not be used either as Variable or Constant
names
...

1
...
Digits
3
...
White Spaces
White Spaces are ignored by the compiler until they are a part of string constant
...

C Character-Set Table:
Letters

Digits

Upper Case A to Z

0 to 9

Lower Case a to z
Keywords:
Every word in C language is a keyword or an identifier
...
They are specifically used by the compiler for its own purpose and they
serve as building blocks of a c program
...


...
int


...
break


...
long


...
case


...
register


...
char


...
return


...
const


...
short


...
continue


...
signed


...
default


...
size of


...
do


...
static


...
25

Unit – 1
White Space:
1
...
Horizontal Tab
3
...
New Line
5
...
Comma

&


...



...
Caret

;


...
Asterisk

:


...
Minus Sign

?


...
Plus Sign

'


...
Opening Angle (Less than sign)

"


...
Closing Angle (Greater than sign)

!


...
Left Parenthesis

|


...
Right Parenthesis

/


...
Left Bracket

\


...
Right Bracket

~


...
Left Brace

-


...
Right Bracket

$


...
Number Sign

%
...



...
We
can do this by using the qualifier const at the time of initialization
...
Always, const type has to be declared and initialized at the
same time
...
The declaration does
the following things
...
Tells the compiler the variables name
...
Specifies what type of data the variable will hold
...
And ask the computer system to allocate memory to that variable
C Programming Study Material

Page No
...
vn;
Where v1, v2, v3 are variable names
...
A declaration
statement must end with a semicolon
...




A variable name should not be a keyword
...
Variable name should not start with a number
...




The underscore character is also permitted in identifiers
...
Some compilers
allow variable names whose length could be up to 247 characters
...




No special symbol other than an underscore can be used in a variable name
...


User defined type declaration:
In C language a user can define an identifier that represents an existing data type
...
The general syntax is
typedef type identifier;
Here type represents existing data type and ‘identifier’ refers to the ‘row’ name given to the
data type
...
They can be later used to declare
variables as follows:
salary dept1, dept2;
average section1, section2;

C Programming Study Material

Page No
...

The second type of user defined datatype is enumerated data type which is defined as follows
...
Value n};
The identifier is a user defined enumerated datatype which can be used to declare variables
that have one of the values enclosed within the braces
...

enum identifier V1, V2, V3, ……… Vn
The enumerated variables V1, V2, …
...
value n
Examples:
enum day {Monday, Tuesday, …
...
The general form of a symbolic constant is
# define symbolic_name value of constant
Valid examples of constant definitions are:
# define marks 100
# define total 50
# define pi 3
...
It is a standard practice to place them at the beginning of the program
...
Both
these functions definition is available in stdio
...
The printf function is used to print the data on the
console
...
Whatever message we wanted to be displayed on the screen is to be
included in between two double quotes and send to printf
...


C Programming Study Material

Page No
...
This function
requires two things to be specified
...
A format specifier which is designed according to our output requirement
...

2
...

The scanf() function is used to read data from the keyboard
...

1
...
This is specified with
a format string according to the rules given in the following table
...

2
...
That is, we are required to
supply memory locations where the data has to stored
...
If we want to read data into variable v, we have to say &v
...
So we have to supply address of the
variable after the format specifier
...


%f

Print float value

%g

Print using %e or %f whichever is smaller

%o

Print actual value

%s

Print a string

%x

Print a hexadecimal integer (Unsigned) using lower case a – f

%X

Print a hexadecimal integer (Unsigned) using upper case A – F

%a

Print a unsigned integer
...


C Programming Study Material

Page No
...
There are two ways to give
comments in C language
...



Single line comments can give using // symbol
...


Examples:

This is the comment

void main() // this is the main function

/* this is used for the multi
line comments in the c language */

The /*
...
These are essential since it enhances the readability and understandability of the
program
...

Though comments are not necessary, it is a good practice to begin a program with a comment
indicating the purpose of the program, its author and the date on which the program was written
...
For example, a comment can
be written before the statement, after the statement or within the statement
...
Thus we can type this
text in small case, capital or a combination
...

Comments cannot be nested
...


C Programming Study Material

Page No
...
Operators are used in C language program to operate on data
and variables
...
Arithmetic operators
2
...
Logical Operators
4
...
Increments and Decrement Operators
6
...
Bitwise Operators

Arithmetic Operators
All the basic arithmetic operations can be carried out in C
...
Both unary and binary operations are available in C language
...
And binary operator needs two operands such as, the numbers 6 and 9 when
operated by binary + will have the value 6 + 9 as 15
...
The modulus operator is a special operator in C
language which evaluates the remainder of the operands after division
...
31

Unit – 1
Example Program:
#include ...
h
void main() //tell the compiler the start of the program
{
int num1, num2, sum, sub, mul, div, mod; //declaration of variables
printf(“ Please Enter Two Numbers :: “); //asking user to enter no’s
scanf (“%d %d”, &num1, &num2); //inputs the operands
sum = num1+num2; //addition of numbers and storing in sum
...

printf(“\n The difference is = %d”, sub); //display the output
mul = num1*num2; //multiplication of numbers and storing in mul
...

printf(“\n The division is = %d”, div); //display the output
mod = num1%num2; //modulus of numbers and storing in mod
...
0
The modulus is = 0

Integer Arithmetic:
When an arithmetic operation is performed on two whole numbers or integers than such an
operation is called as integer arithmetic
...
Let x = 27 and y = 5
be 2 integer numbers
...

x + y = 32
x – y = 22
x * y = 115
x%y=2
x/y=5
C Programming Study Material

Page No
...

Floating point arithmetic:
When an arithmetic operation is preformed on two real numbers or fraction numbers such an
operation is called floating point arithmetic
...
The remainder operator is not applicable for floating point arithmetic
operands
...
0 and y = 4
...
0
x – y = 10
...
0
x / y = 3
...
If anyone operand is of
real type then the result will always be real thus 15/10
...
5
...
This is when the relational operator comes into picture
...
That is they need two operands
...
C supports the following relational operators
...
It is required to compare the marks of 2 students,
salary of 2 persons then we can use the relational operators
...

exp1 relational operator exp2

C Programming Study Material

Page No
...
Given below is a list of examples of relational expressions and evaluated
values
...
5 <= 25 TRUE
-65 > 0 FALSE
10 < 7 + 5 TRUE
Relational expressions are used in decision making statements of C language such as if, while
and for statements to decide the course of action of a running program
...
OF STUDENTS PASSED ARE :: %d",np);
}
In the above program we are reading students marks from the user and finding out number of
students passed in that particular subject
...
We
are comparing every student marks with 40 marks and if it is true then it will return 1 that means
true
...
So the np is nothing
but the no
...

Output:
ENTER FIVE STUDENT MARKS :: 45 67 28 34 56
THE NO
...


B

A && B A || B

T

T

T

T

&&

A
T

Operator Meaning

F

F

T

F

T

F

T

F

F

F

F

Logical AND

||

Logical OR

!

Logical NOT

C Programming Study Material

Page No
...
If both the expressions to the left and to the right of the logical operator is true then
the whole compound expression is true
...
e
...

Logical OR (||)
The logical OR is used to combine 2 expressions or the condition evaluates to true if any one
of the 2 expressions is true
...
It
evaluates to true if a is less than either m or n and when a is less than both m and n
...
In other words it just reverses the value of the
expression
...
And this not operator is a unary
operator
...

For example
!A
F

F

greater than or equal to y
...

Example

Statement with simple

Statement with

x=a+b

assignment operator
a=a+1

shorthand operator
a += 1

a=a–1

a -= 1

addition, C has a set of shorthand

a = a * (n+1)

a *= (n+1)

assignment operators of the form
...


C Programming Study Material

In

Page No
...
The
operator oper = is known as shorthand assignment operator
...

Output
ENTER VALUES OF a, b, c, d
5578
a = 48

Increment / Decrement Operators
C provides two unusual operators for incrementing and decrementing variables
...
We have
frequently used ++ to increment by 1
...

Operators

Postfix

Prefix

Increment

i++

++i

Decreemnt

i--

--i

x = x+1; is the same as ++x;
and

x = x–1;

is the same as x––;

Unary increment / decrement operators are applicable to integer type of variables only (which
includes int, char, long) but not to constants or expressions
...

C Programming Study Material

Page No
...

Example Program:
#include ...


{
int i=10, j, k;
j = i++;

First i value is incremented by one and then this
modified value is assigned to k
...

This is a ternary operator
...
This operator can be used in either of the following
styles
...
(expr) ? expr1 : expr2;
2
...
Where
as in the second style, expr1 is evaluated if expr is true and its value is assigned to var
...

Note: In C language any value if it is positive or negative is logically considered as true
...
So true means numerical value is 1 and false is 0
...
37

Unit – 1

Here we used both conditional operator and logical operators also
...
And in that expression if that is true then the first printf() statement will
execute, that means the LEAP YEAR will be displayed on console
...


Output:
ENTER A YEAR 1999
NOT A LEAP YEAR
ENTER A YEAR 2000
LEAP YEAR

COMMA Operator:
You can combine multiple expressions in a single expression using the comma operator
...

Output
k = 5
...
Here the expression is evaluated from left to
right, that is, i = 4 is evaluated first then j = 5 is evaluated
...


C Programming Study Material

Page No
...

Arithmetic Expressions:
In C every expression evaluates to a value that is every expression results in some value of a
certain type that can be assigned to a variable
...

Algebraic Expression

C Expression

axb–c

a*b–c

(m + n) (x + y)

(m + n) * (x + y)

(ab / c)

a*b/c

3x2 +2x + 1

3*x*x+2*x+1

(x / y) + c

x/y+c

Evaluation of Expressions:
Expressions are evaluated using an assignment statement of the form
Variable = expression;
Variable is any valid C variable name
...
All variables
used

in

the

expression

must

be

assigned

values

before

evaluation

is

attempted
...

main ()
{
float a, b, c x, y, z; a = 9; b = 12; c = 3;
x = a – b / 3 + c * 2 – 1;
y = a – b / (3 + c) * (2 – 1);
z = a – ( b / (3 + c) * 2) – 1;
printf (“x = %f \n”,x);
printf (“y = %f \n”,y);
printf (“z = %f \n”,z);
}
C Programming Study Material

Page No
...
00
y = 7
...
00

Type Conversions
Some times it is required to convert the one data type to another data type
...
Type conversion occurs when the expression has data of mixed data types, for
example, converting an integer value into a float value, or assigning the value of the expression to a
variable with different data types
...
C
automatically converts any intermediate values to the proper type so that the expression can be
evaluated without losing any significance
...
If the operands are of
different types the lower type is automatically converted to the higher type before the operation
proceeds
...

In type conversion, the data type is promoted from lower to higher because converting higher
to lower involves loss of precision and value
...
Integer types are lower than floating-point types
...
Signed types are lower than unsigned types
...
Short whole-number types are lower than longer types
...
The hierarchy of data types is as follows: double, float, long, int, short, char
...
If one operand is long double, the other will be converted to long double and result will be
long double
...
If one operand is double, the other will be converted to double and result will be double
...
If one operand is float, the other will be converted to float and result will be float
...
If one of the operand is unsigned long int, the other will be converted into unsigned long int
and result will be unsigned long int
...
If one operand is long int and other is unsigned int then

C Programming Study Material

Page No
...
If unsigned int can be converted to long int, then unsigned int operand will be
converted as such and the result will be long int
...
Else both operands will be converted to unsigned long int and the result will be
unsigned long int
...
If one of the operand is long int, the other will be converted to long int and the result will be
long int
...
If one operand is unsigned int the other will be converted to unsigned int and the result will
be unsigned int
...

Consider for example the calculation of number of female and male students in a class
Ratio = female_Students / male_Students
Since if female_students and male_students are declared as integers, the decimal part will be
rounded off and its ratio will represent a wrong figure
...

Ratio = (float) female_students / male_students
The operator float converts the female_students to floating point for the purpose of evaluation
of the expression
...
The process of such a local conversion is
known as explicit conversion or casting a value
...


Conditional expressions
A conditional expression contains the expression with condition
...

var = (expr) ? expr1 : expr2;
The expression expr1 is evaluated first
...
Otherwise expr3 is evaluated, and that
is the value
...
Thus to set z to the maximum of a and b, z =
(a > b) ? a : b; /* z = max(a, b) */
It should be noted that the conditional expression is indeed an expression, and it can be used
wherever any other expression can be
...


C Programming Study Material

Page No
...
:: %d",max);
}

Output:
ENTER TWO NUMBERS :: 4 6
THE MAX NO
...
In that first a
compare with b and if a is maximum that is a is greater than b then, a value that is the value
contained by a is assigned to max variable
...

This is the program to find out the maximum of two numbers only
...


Example Program:
main()
{
int a,b,c,max;
printf(" ENTER THREE NUMBERS :: ");

Conditional Expression

scanf("%d %d %d",&a,&b,&c);
max=((a>b)&&(a>c)?a:(b>c)?b:c);
printf(" THE MAX NO
...
:: 6

C Programming Study Material

Page No
...
There are two distinct priority levels of arithmetic operators in C
...




If parentheses are nested, the evaluation begins with the innermost sub expression
...




The associability rule is applied when two or more operators of the same precedence level
appear in the sub expression
...
When
Parenthesis is used, the expressions within parenthesis assume highest priority
...
The precedence is used to determine
how an expression involving more than one operator is evaluated
...

The operators of same precedence are evaluated from right to left or from left to right
depending on the level
...
The table given
below gives the precedence of each operator
...

2

Unary

Logical negation (NOT) R → L

~

Bitwise 1’s complement Right -> Left

+

Unary plus

-

Unary minus

++

Pre or post increment

--

Pre or post decrement

&

Address

*

Indirection

Size of
C Programming Study Material

!

Size of operant in bytes
Page No
...
*
→*

4

Member Access

L→R

Not Equal to

L→R

L→R
L→R
L→R

L→R

9

Bitwise AND

&

Bitwise AND

L→R

10

Bitwise XOR

^

Bitwise XOR

L→R

11

Bitwise OR

|

Bitwise OR

L→R

12

Logical AND

&&

Logical AND

L→R

14

Conditional

?:

Ternary Operator

R→L

15

Assignment

=

Assignment

R→L

*=

Assign product

%=

Assign reminder

/=

Assign quotient

+=

Assign sum

-=

Assign difference

&=

Assign bitwise AND

^=

Assign bitwise XOR

|=

Assign bitwise OR

<<=

Assign left shift

>>=

Assign right shift

,

Evaluate

16

Comma

C Programming Study Material

L→R

Page No
...
We haven’t attempted to look within these data
types to see how they are constructed out of individual bits, and how these bits can be manipulated
...
This is because, the programming languages are byte
oriented, whereas hardware tends to be bit oriented
...
These permit the
programmer to access and manipulate individual bits within a piece of data
...
They are only int and char but not float and double
...
Binary
operators take two arguments, while unary operators only take one
...
On older microprocessors, bitwise operations are slightly faster than addition
and subtraction operations and usually significantly faster than multiplication and division
operations
...

Bitwise operators interpret operands as strings of bits
...
These bit strings are then interpreted according to data type
...
The various Bitwise Operators available in C are shown below
...
They are logical operators, shift operators,
rotation operation and mask operations
...


C Programming Study Material

Page No
...
The bitwise-AND operator compares each bit of its first operand to the
corresponding bit of its second operand
...

Otherwise, the corresponding result bit is set to 0
...

Again, this operator must not be confused with its Boolean "logical and", which treats its operands as
Boolean values, and is written "&&" (two ampersands)
...
And it
compares each bit of its first operand to the corresponding bit of its second operand
...
Otherwise, the corresponding result bit is set to 0
...
Again, this operator must not

be confused with its Boolean "logical or", which treats its operands as Boolean values, and is written
"||" (two pipes)
...
46

Unit – 1
Bitwise XOR Operator:
A bitwise exclusive or takes two bit patterns of equal length and performs the logical XOR
operation on each pair of corresponding bits
...
The bitwise-exclusive-OR operator compares each bit of its first
operand to the corresponding bit of its second operand
...
Otherwise, the corresponding result bit is set to 0
...
Assembly
language programmers sometimes use the XOR operation as a short-cut to set the value of a register
to zero
...

Bitwise NOT Operator:
The bitwise NOT, or complement, is a unary operation that performs logical negation on each
bit, forming the ones' complement of the given binary value
...
It is a unary operator
...

x

~x

0

1

1

0

For example:
(NOT) ~

0111(decimal 7)
= 1000 (decimal 8)

In many programming languages, the bitwise NOT operator is "~" (tilde)
...
The "logical not" is not a bitwise operation
...
47

Unit – 1
Example Program:
The below program illustrates the all logical bit wise operators
...
e
...
e
...
e
...
e
...
e
...
e
...
48

Unit – 1

Bitwise Shift Operators
The bitwise shift operators take two arguments
...
And there are two types of bitwise shift operators
...
Left shift is denoting with “<<” and right shift is denoting with “>>”
...
And in the right most
bit 0 will be added
...


Logical Left Shift
For example:
c3 = c1 << 2;
This is a left-shift operation
...
c1 indicates the operand
that should be an expression returning a whole number
...

It is evaluated as follows: if the c1 value is 4
...
Since a vacuum is created on the right
side, it is filled with 0s to get 0001 0000
...


Right Shift Operator:
In this right shift the bits are shifted to right from the original position
...
This operation shown in below figure
...
49

Unit – 1
For example:
c3 = c1 >> 2;
This is a right-shift operation
...
c1 indicates the
operand that should be an expression returning a whole number
...

It is evaluated as follows: if c value is 4
...
The left most bits are replaced with
zeros
...

Example Program:
The following program illustrates the bitwise shift operators
...
50

Unit – 1

Bit Rotation
Another form of shift is the circular shift or bit rotation
...
The value that is shifted in on the right
during a left-shift is whatever value was shifted out on the left, and vice versa
...

In Circular Shift also there are two types
...

Left Circular Shift or Rotate Left:
In this the bits are shifted towards left from the original position
...
The operation is shown in the
following figure
...
And whatever bit is there
in the right most position that bit will be added to the left most position
...


Right Circular Shift or Rotate Right

C Programming Study Material

Page No
...
First one is if we want to fix the value
of any position as 1 or 0 or to know the value of any position in the bit pattern
...
The AND operator can be used to check the status of the bits of
this attribute byte
...
A hidden file is
one, which is never shown in the directory, even though it exists on the disk
...
If 1 then
it is ON, if 0 then it is OFF
...
And here we don’t know the
original bit pattern
...
That we can do using AND operator by using the bit pattern 11111111
...

So if we do like this, even though the user don’t know the original bit pattern also he will get

the result and know that the third bit if zero that is OFF
...
If the first operand
happens to be 00000111, then to switch OFF bit number 1, our AND mask bit pattern should be
11111101
...
Therefore, irrespective of whether the first bit is ON or OFF previously, it is switched
OFF
...


C Programming Study Material

Page No
Title: C language
Description: Learn c from basics