Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Regulation – 2018

18CS23 –C programming and Problem Solving

MODULE-1

COMPUTER GENERATIONS
1. First Generation
2. Second Generation
3. Third Generation
4. Fourth Generation
5. Fifth Generation

1. First Generation:
 The period of first generation computer is 1946-1959. And it is based on Vacuum
tube.
 The first generation computers used vacuum tubes (Electric bulbs) as the basic
components for memory and CPU (Central Processing Unit).
 These tubes produced a lot of heat and while doing the installations the tubes will
fuse frequently. So it is very expensive.
 The Batch processing operating system was used.
 It uses Punch cards, paper tape, and magnetic tape as input and output devices.
 It uses machine code as the programming language.
2. Second Generation:
 The period of second generation computer is 1959-1965. And it is based on
transistor.
 It was cheaper, consumed less power, more compact in size, more reliable and faster
than the first generation machines made of vacuum tubes.
 In this generation, magnetic cores were used as the primary memory and magnetic
tape and magnetic disks as secondary storage devices.
 The computers used batch processing and multiprogramming operating system.
 In this generation, assembly language and high-level programming languages like
FORTRAN, COBOL were used.
3. Third Generation:
 The period of Third generation computer is 1965-1971. And it is based on
Integrated Circuit. A single IC has many transistors, resistors, and capacitors along
with the associated circuitry.
 The IC was invented by Jack Kilby. This development made computers smaller in
size, reliable, and efficient.
 It uses remote processing, time-sharing, and multiprogramming operating system.
 It uses High-level languages (FORTRAN-II TO IV, COBOL, PASCAL PL/1, BASIC,
ALGOL-68 etc.).
4. Fourth Generation:
 The period of fourth generation computer is 1971-1980. And it is based on Very
Large Scale Integrated (VLSI) Microprocessor.
 VLSI circuits having about 5000 transistors and other circuit elements with their
associated circuits on a single chip.

Prepared By : c.valarmathi CSE Page 1


Regulation – 2018
18CS23 –C programming and Problem Solving

 Fourth generation computers became more powerful, compact, reliable, and


affordable.
 In this generation, time sharing, real time networks, distributed operating system
were used.
 All the high-level languages like C, C++, DBASE etc., were used in this generation.
5. Fifth Generation:
 The period of Fifth generation computer is 1980 onwards. And it is based on Ultra
Large Scale Integration (ULSI) microprocessor based. This Microprocessor contains
ten million electronic components.
 This generation is based on parallel processing hardware and AI (Artificial
Intelligence) software.
 All the high-level languages like C and C++, Java, .Net etc., are used in this
generation.

COMPUTER TYPES
There are 4 types of computers:
1. Micro computer
2. Mini computer
3. Mainframe computer
4. Super computer

1. Micro computer:
 Desktop computers, laptops, tablets and smart phones are called micro computers.
 These computers are cheaper than other types of computers.
 The micro computers are specially designed for general usage like entertainment,
education and work purposes.
 Example: Laptops, PCs, Smartphones.
2. Mini computer:
 Mini computers are used by small organizations and firms. The mini computers are
called as “midrange computers”.
 These computers are not designed for a single user. One department of large
company or organizations use mini computers for specific purposes
 Example: K-202, Texas Instrument TI-990, SDS-92, IBM Midrange Computers.
3. Mainframe Computer:
 Mainframe is very large in size and is an expensive computer.
 It is capable of supporting hundreds or even thousands of users simultaneously.
 Mainframe executes many programs concurrently and supports many simultaneous
executions of programs.
 Example: IBM4300, Hitachi’s Z800.
4. Super Computer:
 Supercomputers are one of the fastest computers currently available.
 Supercomputers are very expensive and are employed for specialized applications
such as data processing.

Prepared By : c.valarmathi CSE Page 2


Regulation – 2018
18CS23 –C programming and Problem Solving

 NASA uses super computers for launching space shuttles, controlling them and for
space exploration purpose.

COMPONENTS OF A COMPUTER

Input Unit:
 This unit contains devices with the help of which we enter data into the computer.
 This unit creates a link between the user and the computer.
 The input devices translate the information into a form understandable by the
computer.

Central Processing Unit:


 CPU is considered as the brain of the computer. CPU performs all types of data
processing operations.
 It stores data, intermediate results, and instructions (program). It controls the
operation of all parts of the computer.
 CPU itself has the following three components −
1. ALU (Arithmetic Logic Unit)
2. Memory Unit
3. Control Unit
1. Arithmetic Logic Unit (ALU)
 Arithmetic Section: Function of arithmetic section is to perform arithmetic
operations like addition, subtraction, multiplication, and division. All
complex operations are done by making repetitive use of the above
operations.
 Logic Section: Function of logic section is to perform logic operations such as
comparing, selecting, matching, and merging of data.
2. Memory Unit:
 This unit can store instructions, data, and intermediate results. This unit
supplies information to other units of the computer when needed.

Prepared By : c.valarmathi CSE Page 3


Regulation – 2018
18CS23 –C programming and Problem Solving

 It is also known as internal storage unit or the main memory or the primary
storage or Random Access Memory (RAM).
 Primary memory and secondary memory are two types of memories in the
computer.
 All inputs and outputs are transmitted through the main memory.

3. Control Unit:
 This unit controls the operations of all parts of the computer but does not
carry out any actual data processing operations.
 It is responsible for controlling the transfer of data and instructions among
other units of a computer.
 It obtains the instructions from the memory, interprets them, and directs the
operation of the computer.
 It communicates with Input/Output devices for transfer of data or results
from storage.
 It does not process or store data.

Output Unit:
 The output unit consists of devices with the help of which we get the information
from the computer.
 This unit is a link between the computer and the users.
 Output devices translate the computer's output into a form understandable by the
users.
SOFTWARE AND ITS TYPES
Definition:
Software is a set of programs, which is designed to perform a well-defined function. A
program is a sequence of instructions written to solve a particular problem.
There are two types of software
i) System Software
ii) Application Software

i) System Software:
The system software is a collection of programs designed to operate, control and extend
the processing capabilities of the computer itself. System software is generally prepared by
the computer manufactures. These software products comprise of programs written in
low-level languages, which interact with the hardware at a very basic level. System
software serves as the interface between the hardware and the end users.
Example: Operating System, compilers, Interpreter and Assemblers.
ii) Application Software:
Application software products are designed to satisfy a particular need of a particular
environment. All software applications developed in Software Company can come under
the application software. Application software may consist of single or collection of
programs.

Prepared By : c.valarmathi CSE Page 4


Regulation – 2018
18CS23 –C programming and Problem Solving

Example: Student Record Software, Library Management Software.

INPUT DEVICES AND ITS TYPES:


Input devices are used to give input to the systems. Input devices are,

 Keyboard
 Mouse
 Joy Stick
 Light pen
 Track Ball
 Track Ball

Keyboard:
Keyboard is the most common and very popular input device which helps to input data to
the computer. Keyboards are of two sizes 84 keys or 101/102 keys, but now keyboards
with 104 keys or 108 keys are also available for Windows and Internet.

S.No Keys & Description

Typing Keys
1
These keys include the letter keys (A-Z) and digit keys (09) which
generally give the same layout as that of typewriters.

Numeric Keypad
It is used to enter the numeric data or cursor movement. Generally, it
2
consists of a set of 17 keys that are laid out in the same configuration
used by most adding machines and calculators.

Function Keys
The twelve function keys are present on the keyboard which are
3
arranged in a row at the top of the keyboard. Each function key has a
unique meaning and is used for some specific purpose.

Control keys
4 These keys provide cursor and screen control. It includes four
directional arrow keys. Control keys also include Home, End, Insert,

Prepared By : c.valarmathi CSE Page 5


Regulation – 2018
18CS23 –C programming and Problem Solving

Delete, Page Up, Page Down, Control(Ctrl), Alternate(Alt),


Escape(Esc).

Special Purpose Keys


5 Keyboard also contains some special purpose keys such as Enter,
Shift, Caps Lock, Num Lock, Space bar, Tab, and Print Screen.

Mouse:
Mouse is the most popular pointing device. It is a very famous cursor-control device
having a small palm size box with a round ball at its base, which senses the movement of
the mouse and sends corresponding signals to the CPU when the mouse buttons are
pressed.
Generally, it has two buttons called the left and the right button and a wheel is present
between the buttons. A mouse can be used to control the position of the cursor on the
screen, but it cannot be used to enter text into the computer.
Joystick
Joystick is also a pointing device, which is used to move the cursor position on a monitor
screen. It is a stick having a spherical ball at its both lower and upper ends. The lower
spherical ball moves in a socket. The joystick can be moved in all four directions.

Light Pen
Light pen is a pointing device similar to a pen. It is used to select a displayed menu item or
draw pictures on the monitor screen. It consists of a photocell and an optical system
placed in a small tube. When the tip of a light pen is moved over the monitor screen and
the pen button is pressed, its photocell sensing element detects the screen location and
sends the corresponding signal to the CPU.

Track Ball
Track ball is an input device that is mostly used in notebook or laptop computer, instead
of a mouse. This is a ball which is half inserted and by moving fingers on the ball, the
pointer can be moved. Since the whole device is not moved, a track ball requires less space
than a mouse. A track ball comes in various shapes like a ball, a button, or a square.

Scanner
Scanner is an input device, which works more like a photocopy machine. It is used when
some information is available on paper and it is to be transferred to the hard disk of the

Prepared By : c.valarmathi CSE Page 6


Regulation – 2018
18CS23 –C programming and Problem Solving

computer for further manipulation. Scanner captures images from the source which are
then converted into a digital form that can be stored on the disk. These images can be
edited before they are printed.

OPERATING SYSTEM
The Operating System is a program with the following features −
 An operating system is a program that acts as an interface between the software and
the computer hardware.
 It is an integrated set of specialized programs used to manage overall resources and
operations of the computer.
 It is specialized software that controls and monitors the execution of all other programs
that reside in the computer, including application programs and other system software.
Objectives of Operating System

The objectives of the operating system are −


 To make the computer system convenient to use in an efficient manner.
 To hide the details of the hardware resources from the users.
 To provide users a convenient interface to use the computer system.
 To act as an intermediary between the hardware and its users, making it easier for the
users to access and use other resources.
 To manage the resources of a computer system.

Characteristics of Operating System


Here is a list of some of the most prominent characteristic features of Operating Systems −
 Memory Management − Keeps track of the primary memory, i.e. what part of it is in
use by whom, what part is not in use, etc. and allocates the memory when a process or
program requests it.
 Processor Management − Allocates the processor (CPU) to a process and deallocates
the processor when it is no longer required.
 Device Management − Keeps track of all the devices. This is also called I/O controller
that decides which process gets the device, when, and for how much time.
 File Management − Allocates and de-allocates the resources and decides who gets the
resources.
 Security − Prevents unauthorized access to programs and data by means of passwords
and other similar techniques.
 Job Accounting − Keeps track of time and resources used by various jobs and/or users.
 Control over System Performance − Records delays between the request for a service
and from the system.
 Interaction with the Operators − Interaction may take place via the console of the
computer in the form of instructions. The Operating System acknowledges the same, does
the corresponding action, and informs the operation by a display screen.

Prepared By : c.valarmathi CSE Page 7


Regulation – 2018
18CS23 –C programming and Problem Solving

 Error-detecting Aids − Production of dumps, traces, error messages, and other


debugging and error-detecting methods.
 Coordination between Other Software and Users − Coordination and assignment of
compilers, interpreters, assemblers, and other software to the various users of the
computer systems.

COMPUTER MEMORY
 A memory is just like a human brain. It is used to store data and instructions.
 Computer memory is the storage space in the computer, where data is to be
processed and instructions required for processing are stored.
 The memory is divided into large number of small parts called cells.
 Each location or cell has a unique address.
 For example, if the computer has 64k words, then this memory unit has 64 * 1024 =
65536 memory locations. The address of these locations varies from 0 to 65535.
 There are 3 types of memory
1. Cache Memory
2. Primary Memory
3. Secondary Memory

1. Cache Memory
 Cache memory is a very high speed semiconductor memory which can speed up the
CPU.
 It acts as a buffer between the CPU and the main memory.
 It is used to hold those parts of data and program which are most frequently used by
the CPU.
 The parts of data and programs are transferred from the disk to cache memory by
the operating system, from where the CPU can access them.
 Advantages:
1. Cache memory is faster than main memory.
2. It consumes less access time as compared to main memory.
3. It stores the program that can be executed within a short period of time.
4. It stores data for temporary use.
 Disadvantages:
1. Cache memory has limited capacity.
2. It is very expensive.
2. Primary Memory:
 Primary memory holds only those data and instructions on which the computer is
currently working.
 It has a limited capacity and data is lost when power is switched off.
 It is generally made up of semiconductor device.
 These memories are not as fast as registers.

Prepared By : c.valarmathi CSE Page 8


Regulation – 2018
18CS23 –C programming and Problem Solving

 The data and instruction required to be processed resides in the main memory.
 It is divided into two subcategories RAM and ROM.
3. Secondary Memory:
 This type of memory is also known as external memory or non-volatile. It is slower
than the main memory.
 These are used for storing data/information permanently.
 CPU directly does not access these memories, instead they are accessed via input-
output routines.
 The contents of secondary memories are first transferred to the main memory, and
then the CPU can access it.
 Example; disk, CD-ROM, DVD, etc.

INTRODUCTION to C LANGUAGE
C Language was designed and developed by Dennis Ritchie at BELL Laboratories in 1972. C
programming language is derived from earlier languages B, BCPL (Basic Combined
Programming Language) and CPL (Combined Programming Language).

ALGORITHM
An algorithm is a step by step procedure to solve a given problem. The good algorithm
should contain the following
 Finite number of steps.
 Each step should be clear and meaningful.
 The input and output should be defined precisely.
 An algorithm should not have computer code. ( C program)

Example1: Write an algorithm to multiply 2 numbers entered by user.


Step 1: Start
Step 2: Declare variables num1, num2 and mul.
Step 3: Read values of num1 and num2.
Step 4: Multiply num1 & num2 and assign the result to mul.
mul = num1+num2
Step 5: Display mul
Step 6: Stop

Example 2: Write an algorithm to find area and perimeter of a rectangle.


Step 1: Start
Step 2: Declare variables length, breadth, area and perimeter.
Step 3: Read values of length and breadth.
Step 4: Multiply length & breadth and assign the result to area.
area= length*breadth
Step5: Compute the perimeter using the formula
perimeter= 2* (length+breadth)

Prepared By : c.valarmathi CSE Page 9


Regulation – 2018
18CS23 –C programming and Problem Solving

Step6: Display area and perimeter.


Step7: stop.

Example 3: Write an algorithm to compute simple interest.


Step 1: Start
Step 2: Declare variables p, r, t and I.
Step 3: Read values of p, r and t.
Step 4: Multiply p, r & t and assign the result to I.
I=p*r*t
Step5: Display I.
Step7: stop.

Example 4: Write an algorithm to find sum and average of given three numbers.
Step 1: Start
Step 2: Declare variables a, b, c, sum and avg.
Step 3: Read values of a, b and c.
Step 4: Find the sum of a, b, c.
sum= a+b+c
Step 5: Find the average of a, b, c.
avg= sum/3;
Step 6: Display sum, avg.
Step 7: Stop.

Example 5: Write an algorithm to find the largest of three numbers.


Step 1: Start
Step 2: Declare variables a, b, c.
Step 3: Read values of a, b and c.
Step 4: Comparison
if ((a>b) and (a>c)) then
display a is greatest.
else if ((b>a) and (b>c)) then
display b is greatest.
else
display c is greatest.
Step 5: Stop.

Example 6: Write an algorithm to swap the two variables using third variable.
Step 1: Start
Step 2: Declare variables a and b.
Step 3: Read values of a and b.
Step 4: Exchange a & b
temp=a
a=b
b=temp

Prepared By : c.valarmathi CSE Page 10


Regulation – 2018
18CS23 –C programming and Problem Solving

Step 5: Display a and b.


Step 6: Stop.

THE STRUCTURE OF THE C PROGRAM

Documentation Section
Link Section
Definition Section
Global Declaration Section
main () function section
{
Declaration part;
Executable part;
}
Subprogram section
Function 1
Function 2
.
.
.
.
Function n
(i) Documentation Section
Documentation section consists of set of comment lines. This comment lines
indicate name of the program, author and other details.
(ii) Link Section
The Like section provides instruction to the compiler to link functions from the
system library such as using the #include directive.
(iii) Definition Section
The definition section defines all symbolic constants such as using the #define
directive.
(iv) Global declaration section
There are some variables that are used in more than one function such variables are
called global variables and are declared in the global declaration section. This section also
declares all the user-defined functions.
(v) Main() function Section
Every C program must have one main function section. This section contains two
parts:
a) Declaration Part:
The declaration part declares all the variables used in the executable part.

Prepared By : c.valarmathi CSE Page 11


Regulation – 2018
18CS23 –C programming and Problem Solving

b) Executable part:
There is at least one statement in the executable part. These two parts must
appear between the opening and closing braces. The program execution begins
at the opening brace and ends at the closing brace. The closing brace of the main
function is the logical end of the program. All the statements in the declaration
and executable part end with a semicolon.
(vi) Subprogram section:
If the program is a multi function program then the subprogram section contains all
the user defined functions that are called in the main() function. User defined functions are
generally placed immediately after the main () function, although they may appear in any
order.

Example:
//Documentation section
/* Program Name: Sample Program
Author:aaa
*/
//Link section
#include <stdio.h>
// Definition Section
#define MAX_ARRAY_LENGTH 20
// Global declaration section
int max_input_length=15;
// Declaring user defined function
float addNumbers(float a, float b);
//Main() function section
int main()
{
int localvariable=50; //Declaration part
clrscr();
//Executable part
printf (“Max array length is %d\n”, MAX_ARRAY_LENGTH);
printf (“Max input length is %d\n”, max_input_length);
printf(Sum of 5.5 and 8.5=%f\n”, addNumbers(5.5,8.5));
return 0;
}
//defining an user defined function which has been declared.
float addNumbers(float a, float b)
{
return (a+b);
}
ALPHABET
The various categories of characters are used while writing a program is called alphabet.
The alphabet may be letter or digit or any special symbol. There are 96 symbols in C.

Prepared By : c.valarmathi CSE Page 12


Regulation – 2018
18CS23 –C programming and Problem Solving

Example:
 Letters A-X, a-z, both upper and lower
 Digits 0-9
 Symbols such as + - * / %
 White spaces

C TOKENS
A token is a smallest or basic unit of a C program. One or more characters are grouped in
sequence to form meaningful words. These meaningful words are called tokens. C tokens
are classified as follows:
(i) Keywords
(ii) Identifiers
(iii) Constants
(iv) Operators
(v) Special symbols

(i) Keywords:
The words which have predefined meaning in C language are called keyword. And
this keywords are reserved for specific purpose in C language. They are also called
reserved words.
Example:
int, float, if, else, etc…

(ii) Identifiers:
Identifiers are the names given to program elements such as variables, constants,
function names, array names, etc.. It consists of sequence of one or more letters or digits
along with “_” (underscore). Rules to be followed to frame an identifier are:
 The first character in the identifier should be a letter or “_” (underscore) and can be
followed by any number of letters or digits or underscores.
 No extra symbols are allowed other than letters, digits and “_”.
 The length of an identifier can be maximum of 31 characters for external names.
Such as function names and global variables.
 The length of an identifier can be maximum of 63 characters for internal names such
as local variables.
 Keywords cannot be used as identifiers.
 Identifiers are case sensitive.

Example:
n, add, sum, sub

(iii) Constants:

Prepared By : c.valarmathi CSE Page 13


Regulation – 2018
18CS23 –C programming and Problem Solving

Constants are values that will not change during execution of the program.
Constants are classified as
(a) Integer Constants
(b) Floating point constants
(c) Character constants
(d) String constants
(e) Enumeration constants

(a) Integer constants:


Integer constants are numeric values without any fractional part or exponential
part. The integer constants are classified into 3 types.
 Decimal constants
The decimal constants can be formed using the numbers 0,1,2,3,4,5,6,7,8,9.
Example: 55, -66
 Octal constants
The octal constants can be formed using the numbers 0, 1,2,3,4,5,6,7.
Example: 055,022
 Hexadecimal constants
The hexadecimal constants can be formed using the numbers
0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F.
Example: 0021, 034A, 056B

(b) Floating point constants:


Floating point constants are numeric values that have either fractional form or
exponent form.
Example 1: 5.5, -6.6, +7.7

Example 2: 5.5 e3 [i.e: 5.5 x 103], 6.6 e-4 [i.e: 6.6 x 10-4]

(c) Character constants:


Character constants contain only one character enclosed within a pair of single
quote marks. Example: ‘J’, ‘S’

(d) String Constants:


String constants contain zero or more characters which are enclosed within a pair of
double quote marks.
Example:
“ “  NULL string constants.
“ “  String constants of 5 white space.
“J”  String constants has single character
“SSCE”  String constants has one word.
“welcome to ssce”  String constants has one sentence.

Prepared By : c.valarmathi CSE Page 14


Regulation – 2018
18CS23 –C programming and Problem Solving

(e) Enumeration constants:


The collection of named integer contants defined using the keyword “enum’ are
called enumeration constants.

Example:
enum days{sun, mon, tue, wed, thur, fri, sat};

Where
 enum is keyword.
 Days is variable name
 sun, mon, tue, wed, thur, fri, sat are enumeration constants having value
0,1,2,3,4,5,6 respectively by default.
(iv) Operators
Operators are symbols which perform the operation such as addition, substraction, etc
between values or variables.
Example:
+,_,*,%,/

(v) Special symbols


The special symbols are used in c program for specific reasons. There following special
symbols are used in c program.
a. Braces{}
The blocks of code written in the program between braces.
b. Parentheses()
The parentheses are used to indicate function calls and function parameters.
c. Brackets[]
The brackets are used as array element reference.

DECLARATION

What is variable?
Variable is an identifier whose value can be changed during execution of the
program. When we declare variable the memory location will be allocated based on data
type of that particular variable. We can store and manipulate values in that memory
location.
Declaration of the variable:
The declaration tells to the C compiler
- What is the name of the variable?
- What is the data type of the variable?
Initialization of the variable:
The variables can be initialized when it has been declared.
Syntax:
data type v1=value;

Prepared By : c.valarmathi CSE Page 15


Regulation – 2018
18CS23 –C programming and Problem Solving

Example:
int m=10;

ASSIGNMENT STATEMENT
Assignment statement is used to assign the value for a given variable at any point of time in
the given program.
Syntax:
variable name= value (or) expression;

Example:
c=30;
c=a+b;
What is multiple assignment statement?
Multiple assignment statement is used to assign single value to more than one
variable at anytime and in a single statement.
Example:
a=b=30;
d=c=a+b;

DATATYPES
C data types are defined as the data storage format. The variable can store a data to
perform a specific operation. Data types are used to define a variable before to use in a
program. There are four data types in C language. They are,

Types Data Types

A. Basic data types int, char, float, double

B. Enumeration data type enum

C. Derived data type pointer, array, structure, union

D. Void data type void

A. Basic data types in c language:


A.1. Integer data type:
Integer data type allows a variable to store numeric values. “int” keyword is used to refer
integer data type. The storage size of int data type is 2 or 4 or 8 byte.
A.2. Character data type:
Character data type allows a variable to store only one character. Storage size of character
data type is 1. We can store only one character using character data type. “char” keyword is
used to refer character data type.

Prepared By : c.valarmathi CSE Page 16


Regulation – 2018
18CS23 –C programming and Problem Solving

A.3. Floating point data type:


Floating point data type consists of 2 types. They are,
a. float
b. double
a. FLOAT:
Float data type allows a variable to store decimal values. Storage size of float data type is
4. This also varies depend upon the processor in the CPU. We can use up-to 6 digits after
decimal using float data type.
b. DOUBLE:
Double data type is also same as float data type which allows up-to 10 digits after decimal.
The range for double data type is from 1E–37 to 1E+37.
Modifiers in c language:
The amount of memory space to be allocated for a variable is derived by modifiers.
Modifiers are prefixed with basic data types to modify (either increase or decrease) the
amount of storage space allocated to a variable. For example, storage space for int data type
is 4 byte for 32 bit processor. We can increase the range by using long int which is 8
byte. We can decrease the range by using short int which is 2 byte.
There are 5 modifiers available in C language. They are,
1. short
2. long
3. signed
4. unsigned
5. long long
Below table gives the detail about the storage size of each C basic data type in 16 bit
processor. Please keep in mind that storage size and range for int and float datatype will
vary depend on the CPU processor (8, 16, 32 and 64 bit)

C Data types / storage


Size Range

char / 1 –127 to 127

int / 2 –32,767 to 32,767

1E–37 to 1E+37 with six digits of


float / 4 precision

1E–37 to 1E+37 with ten digits of


double / 8 precision

1E–37 to 1E+37 with ten digits of


long double / 10 precision

long int / 4 –2,147,483,647 to 2,147,483,647

Prepared By : c.valarmathi CSE Page 17


Regulation – 2018
18CS23 –C programming and Problem Solving

short int / 2 –32,767 to 32,767

unsigned short int / 2 0 to 65,535

signed short int / 2 –32,767 to 32,767

long long int / 8 –(2power(63) –1) to 2(power)63 –1

signed long int / 4 –2,147,483,647 to 2,147,483,647

unsigned long int / 4 0 to 4,294,967,295

unsigned long long int /


8 2(power)64 –1

B. Enumeration data type in c language:


Enumeration data type consists of named integer constants as a list. It start with 0 (zero)
by default and value is incremented by 1 for the sequential identifiers in the list.
Enum syntax in C:
enum identifier [optional{ enumerator-list }];
Enum example in C:
enum month { Jan, Feb, Mar }; or
/* Jan, Feb and Mar variables will be assigned to 0, 1 and 2 respectively by default */
C. Derived data type in C language:
Array, pointer, structure and union are called derived data type in C language.
D. Void data type in C language:
Void is an empty data type that has no value. This can be used in functions and pointers.

OPERATORS
An operator is a symbol which helps the user to command the computer to do a certain
mathematical or logical manipulations.
1. Arithmetic operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Increments and Decrement Operators
6. Conditional Operators
7. Bitwise Operators
8. Special Operators

1. Arithmetic Operators
The operators which are used to perform arithmetic operation such as addition,
subtraction, multiplication, division and modulus operation are called arithmetic operators.
The operator and meaning of the operator is shown below:

Prepared By : c.valarmathi CSE Page 18


Regulation – 2018
18CS23 –C programming and Problem Solving

+ addition
- subtraction
* multiplication
/ division
% modulus
Example:
c=a+b;
d=e-f;

The arithmetic operator is classified into three groups:


A. Integer Arithmetic
When an arithmetic operation is performed on two whole numbers or integers then
this operation is called as integer arithmetic. It always gives an integer as the result.
Example:
Let x = 32 and y = 5 be TWO integer numbers then
x + y = 37
x – y = 27
B. Floating point arithmetic
When an arithmetic operation is performed on two real numbers or fraction
numbers then this operation is called floating point arithmetic.
Example:
Let x = 24.0 and y = 4.0 be TWO real numbers then
x + y = 28.0
x – y = 20.0
C. Mixed mode arithmetic
If the arithmetic operation is performed on ONE real number and ONE integer
number then it is called as mixed mode arithmetic. The result of the mixed mode
arithmetic operation is always real number.
Example:
Let x=55 and y=10.0 then
x/y => 55/10.0 = 5.5

2. Relational Operators
Relational Operators are used to compare the relationship between operands and bring out
a decision.

Operators Example/Description

> x > y (x is greater than y)

< x < y (x is less than y)

>= x >= y (x is greater than or equal to y)

Prepared By : c.valarmathi CSE Page 19


Regulation – 2018
18CS23 –C programming and Problem Solving

<= x <= y (x is less than or equal to y)

== x == y (x is equal to y)

!= x != y (x is not equal to y)

3. Logical Operators
 These operators are used to perform logical operations on the given expressions and
produce the results.
 There are 3 logical operators in C language. They are, logical AND (&&), logical OR (||)
and logical NOT (!).
Operators Example/Description

&& (logical (x>5)&&(y<5)


AND) It returns true when both conditions are true

(x>=10)||(y>=10)
|| (logical It returns true when at-least one of the condition is
OR) true

!((x>5)&&(y<5))
It reverses the state of the operand “((x>5) &&
(y<5))”
! (logical If “((x>5) && (y<5))” is true, logical NOT operator
NOT) makes it false

4. Assignment Operators
In C programs, values for the variables are assigned using assignment operators.
For example, if the value “20” is to be assigned for the variable “sum”, it can be assigned
as “sum = 20;”
There are 2 categories of assignment operators in C language. They are,

1. Simple assignment operator (Example: =)


2. Compound assignment operators (Example: +=, -=, *=, /=, %=, &=, ^=)

Operators Example/Description

sum = 10;
= 10 is assigned to variable sum

sum += 10;
+= This is same as sum = sum + 10

Prepared By : c.valarmathi CSE Page 20


Regulation – 2018
18CS23 –C programming and Problem Solving

sum -= 10;
-= This is same as sum = sum – 10

sum *= 10;
*= This is same as sum = sum * 10

sum /= 10;
/= This is same as sum = sum / 10

sum %= 10;
%= This is same as sum = sum % 10

sum&=10;
&= This is same as sum = sum & 10

sum ^= 10;
^= This is same as sum = sum ^ 10

5. Increments and Decrement Operators


Increment operators are used to increase the value of the variable by one and decrement
operators are used to decrease the value of the variable by one in C programs.
Syntax:
Increment operator: ++var_name; (or) var_name++;
Decrement operator: – -var_name; (or) var_name – -;
Example:
Increment operator : ++ i ; i ++ ;
Decrement operator : – – i ; i – – ;

6. Conditional Operators
Conditional operators return one value if condition is true and returns another value is
condition is false. This operator is also called as ternary operator.
Syntax : (Condition? true_value: false_value);
Example: (A > 500 ? 0 : 1);
In above example, if A is greater than 500, 0 is returned else 1 is returned. This is equal to if
else conditional statements.

7. Bitwise Operators
These operators are used to perform bit operations. First Decimal values are converted
into binary values which are the sequence of bits and bit wise operators work on these bits.
Bit wise operators in C language are

 & – Bitwise AND


 | – Bitwise OR
 ~ – Bitwise NOT

Prepared By : c.valarmathi CSE Page 21


Regulation – 2018
18CS23 –C programming and Problem Solving

 ^ – XOR
 << – Left Shift
 >> – Right Shift

Example:
Consider x=40 and y=80. Binary forms of these values are given below.
x = 00101000
y= 01010000
All bit wise operations for x and y are given below.
 x&y = 00000000 (binary) = 0 (decimal)
 x|y = 01111000 (binary) = 120 (decimal)
 ~x = 11111111111111111111111111
11111111111111111111111111111111010111 = -41 (decimal)
 x^y = 01111000 (binary) = 120 (decimal)
 x << 1 = 01010000 (binary) = 80 (decimal)
 x >> 1 = 00010100 (binary) = 20 (decimal)

8. Special Operators
Below are some of the special operators that the C programming language offers.
Operators Description

This is used to get the address of the


variable.
& Example : &a will give address of a.

This is used as pointer to a variable.


Example : * a where, * is pointer to
* the variable a.

This gives the size of the variable.


Sizeof () Example : size of (char) will give us 1.

C EXPRESSIONS
 A sequence of operand and operators that reduces to a single value after evaluation
is called expression.
 There are 5 types of expression in C language:
1. Arithmetic Expression
a. Single Mode Arithmetic Expression
b. Mixed Mode Arithmetic Expression
2. Relational Expression
3. Logical Expression
4. Assignment Expression
5. Short hand assignment expression

Prepared By : c.valarmathi CSE Page 22


Regulation – 2018
18CS23 –C programming and Problem Solving

1. Arithmetic Expression:
 Arithmetic expression is a combination of variables, arithmetic operators and
constants.
 The arithmetic expression produces a single value when it evaluated.
 Example: x + y
Where,
x and y are called operands
+ is called operator
 There are 2 types of Arithmetic Expression
a. Single Mode Arithmetic Expression
b. Mixed Mode Arithmetic Expression

a. Single Mode Arithmetic Expression:


 In single mode arithmetic expression, all operands should be same datatype.
 The datatype of result is same as datatype of operand.
 There 2 types of single mode arithmetic expression
(i) Integer mode Arithmetic Expression
(ii) Floating mode Arithmetic Expression
(i) Integer mode Arithmetic Expression:
 In this type of expression all the operands or constants are integer datatype and
result is also integer datatype.
 Example:
4+4=8
5*5=25
(ii) Floating mode Arithmetic Expression:
 In this type of expression all the operands or constants are float datatype and result
is also float datatype.
 Example:
4.55-3.45=1.1
8.5 / 2.2= 3.86
b. Mixed Mode Arithmetic Expression:
 In this type of expression all the operands or constants contain both the integer and
Real Datatype (i.e. float datatype (or) double datatype) and result is always real
datatype.
 In mixed mode arithmetic expression, integer operands are converted into real
datatype before doing any computation.
 Example:
2+ 2.5= 4.5
1 / 2.0 = 0.5
2. Relational Expression:
 This type of expression contains relational operators, variables and constants. The
result of the relational expression is either TRUE or FALSE.
 Example:
Prepared By : c.valarmathi CSE Page 23
Regulation – 2018
18CS23 –C programming and Problem Solving

(a+b) > (c+d)


(b*b-4*a*c) >= 0
3. Logical Expression:
 This type of expression contains logical operators and relational expressions. The
result of the logical expression is either TRUE or FALSE.
 Example:
(a<=b) && (c>=d)
4. Assignment Expression:
 This type of expression contains assignment operator, variables and constants.
 Example:
c = a+b

5. Short hand assignment expression:


 This type of expression contains shorthand assignment operator, variables and
constants.
 Example:
a+=5

PROGRAMMING EXAMPLES
Hello world program:
//C hello world example
#include <stdio.h>
int main()
{
printf("Hello world\n");
return 0;
}

Addition of TWO numbers:


#include<stdio.h>
int main()
{
int a, b, c;
printf("Enter two numbers to add\n");
scanf("%d%d",&a,&b);

c = a + b;

printf("Sum of entered numbers = %d\n",c);


return 0;
}
Addition, Subtraction, Multiplication and Division:
#include <stdio.h>

Prepared By : c.valarmathi CSE Page 24


Regulation – 2018
18CS23 –C programming and Problem Solving

int main()
{
int first, second, add, subtract, multiply;
float divide;

printf("Enter two integers\n");


scanf("%d%d", &first, &second);

add = first + second;


subtract = first - second;
multiply = first * second;
divide = first / (float)second; //typecasting

printf("Sum = %d\n",add);
printf("Difference = %d\n",subtract);
printf("Multiplication = %d\n",multiply);
printf("Division = %.2f\n",divide);

return 0;
}

Increment and Decrement Operator Program:


//Example for increment operators

#include <stdio.h>
int main()
{
int i=1;
while(i<10)
{
printf("%d ",i);
i++;
}
}

//Example for decrement operators

#include <stdio.h>
int main()
{
int i=20;
while(i>10)
{
printf("%d ",i);

Prepared By : c.valarmathi CSE Page 25


Regulation – 2018
18CS23 –C programming and Problem Solving

i--;
}
}

Conditional Operator:
#include <stdio.h>
void main()
{
int a = 10, b = 11;
int c;
c = (a < b)? a : b;
printf(“%d”, c);
}

Bitwise AND
#include <stdio.h>
Void main()
{
int a=12, b=25;
printf(“Output=%d”, a&b);
}

Output:
Output=8

Prepared By : c.valarmathi CSE Page 26

You might also like