0% found this document useful (0 votes)
10 views121 pages

Notes

The document provides a comprehensive overview of the block diagram of a digital computer, detailing its functional units such as the input unit, storage unit, output unit, arithmetic logical unit (ALU), control unit, and central processing unit (CPU). It also classifies computers into types based on speed and computing power, including personal computers, workstations, minicomputers, mainframes, and supercomputers. Additionally, it discusses computer hardware, software, algorithms, and the structure of a C program.

Uploaded by

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

Notes

The document provides a comprehensive overview of the block diagram of a digital computer, detailing its functional units such as the input unit, storage unit, output unit, arithmetic logical unit (ALU), control unit, and central processing unit (CPU). It also classifies computers into types based on speed and computing power, including personal computers, workstations, minicomputers, mainframes, and supercomputers. Additionally, it discusses computer hardware, software, algorithms, and the structure of a C program.

Uploaded by

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

WORKING OF THIS DIAGRAM

Block Diagram of Computer & [Link]

Block Diagram of Digital computer


A digital computer is considered to be a calculating device that can perform
arithmetic

operations at enormous speed. It is defined as a device that operates upon


information/data. To

be able to process data the computer is made of various functional units to


perform its specified

task.

Input Unit:

Computers need to receive data and instruction in order to solve any problem.
Therefore, we

need to input the data and instructions into the computers. The input unit
consists of one or

more input devices. Keyboard is the one of the most commonly used input
device. Other

commonly used input devices are the Mouse, Scanner, Microphone etc. All
the input devices

perform the following functions.

• Accept the data and instructions from the outside world.

• Convert it to a form that the computer can understand.


• Supply the converted data to the computer system for further processing.

Storage Unit:

The storage unit of the computer holds data and instructions that are entered
through the input

unit, before they are processed. It preserves the intermediate and final results
before these are

sent to the output devices. It also saves the data for the later use. The various
storage devices

of a computer system are divided into two categories.

a) Primary Storage: Stores and provides very fast. This memory is generally
used to hold

the program being currently executed in the computer, the data being received
from the

input unit, the intermediate and final results of the program. The primary
memory is

temporary in nature. The data is lost, when the computer is switched off. In
order to store
the data permanently, the data has to be transferred to the secondary memory.
The cost of

the primary storage is more compared to the secondary storage. Therefore,


most computers

have limited primary storage capacity.

b) Secondary Storage: Secondary storage is used like an archive. It stores


several programs,

documents, data bases etc. The programs that you run on the computer are
first transferred

to the primary memory before it is actually run. Whenever the results are
saved, again they

get stored in the secondary memory. The secondary memory is slower and
cheaper than the

primary memory. Some of the commonly used secondary memory devices are
Hard disk,

CD, etc.,

Memory Size:

All digital computers use the binary system, i.e. 0’s and 1’s. Each character or
a number is

represented by an 8-bit code.

The set of 8 bits is called a byte. A character occupies 1-byte space. A


numeric occupies 2-byte
space. Byte is the space occupied in the memory.

The size of the primary storage is specified in KB (Kilobytes) or MB


(Megabyte). One KB is

equal to 1024 bytes and one MB is equal to 1000KB. The size of the primary
storage in a

typical PC usually starts at 16MB. PCs having 32 MB, 48MB, 128 MB,
256MB memory are

quite common.

Output Unit:

The output unit of a computer provides the information and results of a


computation to outside

world. Printers, Visual Display Unit (VDU) are the commonly used output
devices. Other

commonly used output devices are Speaker, Headphone, Projector etc.

Arithmetic Logical Unit:

All calculations are performed in the Arithmetic Logic Unit (ALU) of the
computer. It also

does comparison and takes decision. The ALU can perform basic operations
such as addition,

subtraction, multiplication, division, etc and does logic operations viz, >, <, =,
‘etc. Whenever
calculations are required, the control unit transfers the data from storage unit
to ALU once the

computations are done, the results are transferred to the storage unit by the
control unit and

then it is send to the output unit for displaying results.

Control Unit:

It controls all other units in the computer. The control unit instructs the input
unit, where to

store the data after receiving it from the user. It controls the flow of data and
instructions from

the storage unit to ALU. It also controls the flow of results from the ALU to
the storage unit.

The control unit is generally referred as the central nervous system of the
computer that control

and synchronizes its working.

Central Processing Unit:

The Control Unit (CU) and Arithmetic Logic Unit (ALU) of the computer are
together known

as the Central Processing Unit (CPU). The CPU is like brain performs the
following functions:

• It performs all calculations.


• It takes all decisions.

•It controls all units of the computer.

Types of Computer

Computers can be broadly classified by their speed and computing power.

1 PC (Personal Computer) It is a single user computer system having


moderately

powerful microprocessor

2 WorkStation It is also a single user computer system which is similar to

personal computer but have more powerful microprocessor.

3 Mini Computer It is a multi-user computer system which is capable of


supporting hundreds of users simultaneously.

4 Main Frame

It is a multi-user computer system which is capable of

supporting hundreds of users simultaneously. Software

technology is different from minicomputer.

5 Supercomputer It is an extremely fast computer which can execute hundreds

of millions of instructions per second.

PC (Personal Computer)

A PC can be defined as a small, relatively inexpensive computer designed for


an individual

user. PCs are based on the microprocessor technology that enables


manufacturers to put an

entire CPU on one chip. Businesses use personal computers for word
processing, accounting,

desktop publishing, and for running spreadsheet and database management


applications. At

home, the most popular use for personal computers is playing games and
surfing Internet.

Although personal computers are designed as single-user systems, these


systems are normally

linked together to form a network. In terms of power, now-a-days High-end


models of the

Macintosh and PC offer the same computing power and graphics capability as
low-end

workstations by Sun Microsystems, Hewlett-Packard, and Dell.

Workstation

Workstation is a computer used for engineering applications (CAD/CAM),


desktop publishing,

software development, and other such types of applications which require a


moderate amount

of computing power and relatively high quality graphics capabilities.

Workstations generally come with a large, high-resolution graphics screen,


large amount of
RAM, inbuilt network support, and a graphical user interface. Most
workstations also have a

mass storage device such as a disk drive, but a special type of workstation,
called a diskless

workstation, comes without a disk drive.

Common operating systems for workstations are UNIX and Windows NT.
Like PC,

Workstations are also single-user computers like PC but are typically linked
together to form

a local-area network, although they can also be used as stand-alone systems.

Minicomputer

It is a midsize multi-processing system capable of supporting up to 250 users


simultaneously.

Mainframe

Mainframe is very large in size and is an expensive computer capable of


supporting hundreds
or even thousands of users simultaneously. Mainframe executes many
programs concurrently

and supports many simultaneous execution of programs.

Supercomputer

Supercomputers are one of the fastest computers currently available.


Supercomputers are very

expensive and are employed for specialized applications that require immense
amount of

mathematical calculations (number crunching). For example, weather


forecasting, scientific

simulations, (animated) graphics, fluid dynamic calculations, nuclear energy


research,

electronic design, and analysis of geological data (e.g. in petrochemical

Introduction to computer hardware and software.


What is the Computer Hardware?

Hardware refers to the physical components of a computer. Computer


Hardware is any part of the computer that we can touch these parts. These
are the primary electronic devices used to build up the computer. Examples
of hardware in a computer are the Processor, Memory Devices, Monitor,
Printer, Keyboard, Mouse, and Central Processing Unit.

Different Types of Computer Hardware

●​ Input Devices: Input Devices are those devices through which a user
enters data and information into the Computer or simply, User interacts
with the Computer. Examples of Input Devices are Keyboard, Mouse,
Scanner, etc.
●​ Output Devices: Output Devices are devices that are used to show the
result of the task performed by the user. Examples of Output Devices
are Monitors, Printers, Speakers, etc.
●​ Storage Devices: Storage Devices are devices that are used for storing
data and they are also known as Secondary Storage Data. Examples of
Storage Devices are CDs, DVDs, Hard Disks, etc.
●​ Internal Component: Internal Components consist of important
hardware devices present in the System. Examples of Internal
Components are the CPU, Motherboard, etc.

What is the Computer Software?

Software is a collection of instructions, procedures, and documentation that


performs different tasks on a computer system. We can say also Computer
Software is a programming code executed on a computer processor. The
code can be machine-level code or code written for an operating system.

Examples of software are MS- Word, Excel, PowerPoint, Google Chrome,


Photoshop, MySQL, etc.

Different Types of Computer Software

●​ System Software: System Software is a component of Computer


Software that directly operates with Computer Hardware which has the
work to control the Computer’s Internal Functioning and also takes
responsibility for controlling Hardware Devices such as Printers,
Storage Devices, etc. Types of System Software include Operating
Systems, Language processors, and Device Drivers.
●​ Application Software: Application Software is the software that works
the basic operations of the computer. It performs a specific task for
users. Application Software includes Word Processors, Spreadsheets,
etc. Types of Application software include General Purpose Software,
Customized Software, etc.

The algorithm is a step–by–step procedure that is helpful in solving a


problem. If it is written in English-like sentences then, it is called
PSEUDO CODE. It is a formula or a set of steps that solves a particular
problem for a specified problem. Each step in the algorithm must be
specified clearly.

A programming algorithm is a process or formula for solving a problem. It


involves a sequence of specified actions that describe how to perform a
task, which the computer executes consistently. An algorithm follows a
procedure consisting of inputs and produces a result, known as the output.

Properties of an Algorithm

An algorithm must possess the following five properties −

●​ Input
●​ Output
●​ Finiteness
●​ Definiteness
●​ Effectiveness

The Algorithm for finding the average of three numbers is as follows: It reads
three numbers, calculates their average and sum, and then prints the
average value. It involves basic arithmetic operations and simple
input/output steps.
●​ STEP 1 : Start
●​ STEP 2 : Read 3 numbers a,b,c
●​ STEP 3 : Compute sum = a+b+c
●​ STEP 4 : Compute average = sum/3
●​ STEP 5 : Print average value
●​ STEP 6 : Stop

Diagrammatic Representation

A diagrammatic representation of an algorithm is called a flow chart.


Flowcharts include inputs, outputs, sequence of actions, decision points,
sequence of actions, and process measurements. Symbols used in the
flowchart are mentioned below −

Na Symbol Shape Purpose


m
e

Ter Oval start/stop/begin/en


m d
i
n
al

Inpu Parallelo Input/output of


t/ gram data
o
u
t
p
u
t

Proc Rectangl To represent a


e e process
s
s

Deci Diamon Decision


si d operation that
o determines
n which of the
b alternative
o paths to be
x followed

Con Circle Used to connect


n different parts
e of the
ct flowchart
o
r

Flo Arrows Join 2 symbols


w and also
represent flow
of execution

Pred Double Module (or)


e Sided subroutines
fi Recta specified
n ngle elsewhere
e
d
p
r
o
c
e
s
s

Page Pentago Used to connect


c n flowcharts in 2
o different pages
n
n
e
ct
o
r

For Hexagon shows


l initialization,
o condition, and
o incrementation
p of loop
s variable
y
m
b
o
l
Doc Printout Shows the data
u that is ready
m for printout
e
n
t

Flow Chart

Given below is the flowchart for finding an average of three numbers −

Structure of the C Program


The basic structure of a C program is divided into 6 parts which makes it easy
to read, modify, document, and understand in a particular format. C
program must follow the below-mentioned outline in order to successfully
compile and execute. Debugging is easier in a well-structured C program.
Sections of the C Program

There are 6 basic sections responsible for the proper execution of a program.
Sections are mentioned below:

1.​ Documentation
2.​ Preprocessor Section
3.​ Definition
4.​ Global Declaration
5.​ Main() Function
6.​ Sub Programs

1. Documentation

This section consists of the description of the program, the name of the
program, and the creation date and time of the program. It is specified at
the start of the program in the form of comments. Documentation can be
represented as:

The basic structure of a C program is divided into 6 parts which makes it easy
to read, modify, document, and understand in a particular format. C
program must follow the below-mentioned outline in order to successfully
compile and execute. Debugging is easier in a well-structured C program.
Sections of the C Program

There are 6 basic sections responsible for the proper execution of a program.
Sections are mentioned below:

1.​ Documentation
2.​ Preprocessor Section
3.​ Definition
4.​ Global Declaration
5.​ Main() Function
6.​ Sub Programs

1. Documentation

This section consists of the description of the program, the name of the
program, and the creation date and time of the program. It is specified at
the start of the program in the form of comments. Documentation can be
represented as:

// description, name of the program, programmer name, date, time etc.

or

/*

description, name of the program, programmer name, date, time etc.

*/

Anything written as comments will be treated as documentation of the


program and this will not interfere with the given code. Basically, it gives
an overview to the reader of the program.

2. Preprocessor Section

All the header files of the program will be declared in the preprocessor section
of the program. Header files help us to access others improved code into
our code. A copy of these multiple files is inserted into our program before
the process of compilation.
Example:

#include<stdio.h>

#include<math.h>

3. Definition

Preprocessors are the programs that process our source code before the
process of compilation. There are multiple steps which are involved in the
writing and execution of the program. Preprocessor directives start with the
‘#’ symbol. The #define preprocessor is used to create a constant
throughout the program. Whenever this name is encountered by the
compiler, it is replaced by the actual piece of defined code.

Example:

#define long long ll

4. Global Declaration

The global declaration section contains global variables, function declaration,


and static variables. Variables and functions which are declared in this
scope can be used anywhere in the program.

Example:

int num = 18;

5. Main() Function

Every C program must have a main function. The main() function of the
program is written in this section. Operations like declaration and
execution are performed inside the curly braces of the main program. The
return type of the main() function can be int as well as void too. void()
main tells the compiler that the program will not return any value. The int
main() tells the compiler that the program will return an integer value.

Example:

void main()

or

int main()

6. Sub Programs

User-defined functions are called in this section of the program. The control of
the program is shifted to the called function whenever they are called from
the main or outside the main() function. These are specified as per the
requirements of the programmer.

Example:

int sum(int x, int y)

return x+y;

}
Structure of C Program with example

Example: Below C program to find the sum of 2 numbers:

/**

* file: sum.c
* author: you

* description: program to find sum.

*/​​ ​ ​ ​ ​ ​ // Documentation

#include <stdio.h>​​ ​ ​ ​ // Link

int main()​ ​ ​ ​ ​ // Main() Function

int y = 55;​ ​ ​ ​ ​ // variable Declaration and


initialization

printf("%d",y);

return 0;

Difference between Keyword and Identifier in C


In C, keywords and identifiers are basically the fundamental parts of the
language used. Identifiers are the names that can be given to a variable,
function or other entity while keywords are the reserved words that have
predefined meaning in the language.

The below table illustrates the primary differences between the keywords
and identifiers:

Parameters Keywords Identifiers

Keywords are
predefined word
Identifiers are the values used to
that gets reserved
define different programming
for working
items such as variables,
Definition program that have
integers, structures, unions and
special meaning
others and mostly have an
and cannot get
alphabetic character.
used anywhere
else.

Specify the type/kind Identify the name of a particular


Use
of entity. entity.

It always starts with a First character can be a uppercase,


lowercase letter. lowercase letter or underscore.
A keyword should be
An identifier can be in upper case
in lower case and
Rules of or lower case and can consist of
can only contains
Definition alphabetical characters, digits
alphabetical
and underscores.
characters.

They help to identify


a specific property They help to locate the name of the
Purpose that exists within entity that gets defined along
a computer with a keyword.
language.

int, char, if, while,


Examples Test, count1, high_speed, etc.
do, class etc.

Keywords

Keywords are specific reserved words in C each of which has a specific


feature associated with it. Almost all of the words which help us use the
functionality of the C language are included in the list of keywords. So,
you can imagine that the list of keywords is not going to be a small one!
There are a total of 32 keywords in C:
br conti defa
con
aut e cas cha n u
s do
o a e r u l
t
k e t

dou ext
el en
b e floa got
s u for if
l r t o
e m
e n

reg
i ret sta
lo size
s u shor signe t
int n o
t r t d i
g f
e n c
r

s
typ unsi vola w
w
stru e uni g t h
i
c d o n void i i
t
t e n e l l
c
f d e e
h
Identifiers

Identifiers are used as the general terminology for naming of variables,


functions and arrays. These are user defined names consisting of arbitrarily
long sequence of letters and digits with either a letter or the underscore(_)
as a first character. Identifier names must differ in spelling and case from
any keywords. You cannot use keywords as identifiers; they are reserved
for special use. Once declared, you can use the identifier in later program
statements to refer to the associated value. A special kind of identifier,
called a statement label, can be used in goto statements.

#include <stdio.h>

int main() {​ ​ ​ ​ ​ // 'main' is also an identifier

// although it is predefined

int age = 25;​ ​ ​ ​ // Example of a keyword: int

printf("Age: %d\n", age);​ ​ // Here, age is identifier

return 0; ​ ​ ​ ​ ​ // return is a keyword that exits


function

}
Difference Between Constants and Variables in C

The constants and variables in C are both used to store data. So it is essential
to know the difference between the variables and constants in C so that we
can decide which one to use based on the situation.

In this article, we will discuss the basic difference between a constant and a
variable in C language.
Variables in C

A variable in simple terms is a storage place that has some memory allocated
to it. It is used to store some form of data and retrieve it when required.
Different types of variables require different amounts of memory and have
some specific set of operations that can be applied to them.
C Variable Declaration

type variable_name;

type variable1_name, variable2_name, variable3_name;

A variable name can consist of alphabets (both upper and lower case),
numbers, and the underscore ‘_’ character. However, the name must not
start with a number.
Example of C Variable

#include <stdio.h>

int main()

​ // declaration and definition of variable 'a123'

​ char a123 = 'a';

​ // This is also both declaration

​ // and definition as 'b' is allocated


​ // memory and assigned some garbage value.

​ float b;

​ // multiple declarations and definitions

​ int _c, _d45, e;

​ // Let us print a variable

​ printf("%c \n", a123);

​ return 0;

OUTPUT : a

Constants in C

The constants are those variables or values in the C which cannot be modified
once they are defined in the program.

●​ They have fixed values till the program’s life.


●​ We can only assign value to the constant in the declaration.
●​ There can be any type of constant like integer, float, octal, hexadecimal,
character constants, etc.

Example of C Constant

#include <stdio.h>

#define val 10 ​ ​ ​ // Constants Macro

int main()​ ​ ​ ​ // Driver code

​ const float floatVal = 5.8;​​ ​ ​ // constant variables

​ const char charVal = 'a';

​ printf("Integer Constant: %d\n", val);​ ​ // printing constants

​ printf("Floating point Constant: %f\n", floatVal);

​ printf("Character Constant: %c\n", charVal);


​ return 0;

Output

Integer Constant: 10

Floating point Constant: 5.800000

Character Constant: a

Difference between Constant and Variables

The following table lists the differences between the constant and variables in
C:

Constant Variables

A constant is a variable or value that cannot A variable is a name associated


be altered once defined. with some memory location.

A constant is used to hold the fixed values A variable is used to hold some
which we can retrieve later but cannot value that can be changed
change. according to the requirement.
The variables are stored inside a
The constants are generally stored in the text data segment, heap, or stack
segment as they are read-only depending on the environment
it is declared in.

We can only assign a value to the constant We can assign value to the
while defining it. variable anytime.

A variable can only be defined


A constant can be defined by using #define
using the standard variable
or const keyword.
definition syntax.

Example: #define pi 3.14 Example: int var = 25;

const int pi = 3.14; var = 10;

Data Types in C

Each variable in C has an associated data type. It specifies the type of data that
the variable can store like integer, character, floating, double, etc. Each
data type requires different amounts of memory and has some specific
operations which can be performed over it.
The data types in C can be classified as follows:

Data
Types Description Typ
es

int,
Primiti Primitive data types are the most char,
ve basic data types that are used float,
Data for representing simple values doub
Typ such as integers, float, le,
es characters, etc. void

array,
Derive The data types that are derived point
d from the primitive or built-in ers,
Typ datatypes are referred to as funct
es Derived Data Types. ion

User structur
Defi e,
ned The user-defined data types are unio
Data defined by the user himself. n,
Typ enu
es m
Understanding C’s data types is critical for writing efficient programs.

The following are some main primitive data types in C:


Table of Content

●​ Integer Data Type


●​ Character Data Type
●​ Float Data Type
●​ Double Data Type
●​ Void Data Type

Integer Data Type

The integer datatype in C is used to store the integer numbers (any number
including positive, negative and zero without decimal part). Octal values,
hexadecimal values, and decimal values can be stored in int data type in C.

●​ Range: -2,147,483,648 to 2,147,483,647


●​ Size: 4 bytes
●​ Format Specifier: %d

Syntax of Integer

We use int keyword to declare the integer variable:

int var_name;

The integer data type can also be used as


1.​ unsigned int: Unsigned int data type in C is used to store the data

values from zero to positive numbers but it can’t store negative values
like signed int.
2.​ short int: It is lesser in size than the int by 2 bytes so can only store

values from -32,768 to 32,767.


3.​ long int: Larger version of the int datatype so can store values greater

than int.
4.​ unsigned short int: Similar in relationship with short int as unsigned

int with int.

Note: The size of an integer data type is compiler-dependent. We can use


sizeof operator to check the actual size of any data type.

Example of int

// C program to print Integer data types.

#include <stdio.h>

int main()

// Integer value with positive data.

int a = 9;
// integer value with negative data.

int b = -9;

// U or u is Used for Unsigned int in C.

int c = 89U;

// L or l is used for long int in C.

long int d = 99998L;

printf("Integer value with positive data: %d\n", a);

printf("Integer value with negative data: %d\n", b);

printf("Integer value with an unsigned int data: %u\n",

c);

printf("Integer value with an long int data: %ld", d);

return 0;
}

Output

Integer value with positive data: 9

Integer value with negative data: -9

Integer value with an unsigned int data: 89

Integer value with an long int data: 99998

Syntax of char

The char keyword is used to declare the variable of character type:

char var_name;

Example of char

// C program to print Integer data types.

#include <stdio.h>

int main()

char a = 'a';
char c;

printf("Value of a: %c\n", a);

a++;

printf("Value of a after increment is: %c\n", a);

// c is assigned ASCII values

// which corresponds to the

// character 'c'

// a-->97 b-->98 c-->99

// here c will be printed

c = 99;

printf("Value of c: %c", c);

return 0;
}

Output

Value of a: a

Value of a after increment is: b

Value of c: c
Float Data Type

In C programming float data type is used to store floating-point values. Float


in C is used to store decimal and exponential values. It is used to store
decimal numbers (numbers with floating point values) with single
precision.

●​ Range: 1.2E-38 to 3.4E+38


●​ Size: 4 bytes
●​ Format Specifier: %f

Syntax of float

The float keyword is used to declare the variable as a floating point:

float var_name;

Example of Float

// C Program to demonstrate use

// of Floating types
#include <stdio.h>

int main()

float a = 9.0f;

float b = 2.5f;

// 2x10^-4

float c = 2E-4f;

printf("%f\n", a);

printf("%f\n", b);

printf("%f", c);

return 0;

Output
9.000000

2.500000

0.000200

Syntax of Double

The variable can be declared as double precision floating point using the
double keyword:

double var_name;

Example of Double

// C Program to demonstrate

// use of double data type

#include <stdio.h>

int main()

double a = 123123123.00;

double b = 12.293123;

double c = 2312312312.123123;
printf("%lf\n", a);

printf("%lf\n", b);

printf("%lf", c);

return 0;
}

Output

123123123.000000

12.293123

2312312312.123123
Void Data Type

The void data type in C is used to specify that no value is present. It does not
provide a result value to its caller. It has no values and no operations. It is
used to represent nothing. Void is used in multiple ways as function return
type, function arguments as void, and pointers to void.

Syntax:

// function return type void


void exit(int check);

// Function without any parameter can accept void.

int print(void);

// memory allocation function which

// returns a pointer to void.

void *malloc (size_t size);

Example of Void

// C program to demonstrate

// use of void pointers

#include <stdio.h>

int main()

int val = 30;

void* ptr = &val;

printf("%d", *(int*)ptr);

return 0;
}

Output

30
Size of Data Types in C

The size of the data types in C is dependent on the size of the architecture, so
we cannot define the universal size of the data types. For that, the C
language provides the sizeof() operator to check the size of the data types.

Example

// C Program to print size of

// different data type in C

#include <stdio.h>

int main()

int size_of_int = sizeof(int);

int size_of_char = sizeof(char);

int size_of_float = sizeof(float);


int size_of_double = sizeof(double);

printf("The size of int data type : %d\n", size_of_int);

printf("The size of char data type : %d\n",

size_of_char);

printf("The size of float data type : %d\n",

size_of_float);

printf("The size of double data type : %d",

size_of_double);

return 0;

Output

The size of int data type : 4

The size of char data type : 1


The size of float data type : 4

The size of double data type : 8

Size
Form
(
at
b
S
y
Data Type Range pe
t
ci
e
fi
s
er
)

short int 2 -32,768 to 32,767 %hd

unsigned
2 0 to 65,535 %hu
short int

unsigned int 4 0 to 4,294,967,295 %u

int 4 -2,147,483,648 to 2,147,483,647 %d


long int 4 -2,147,483,648 to 2,147,483,647 %ld

unsigned
4 0 to 4,294,967,295 %lu
long int

long long
8 -(2^63) to (2^63)-1 %lld
int

unsigned
long 8 0 to 18,446,744,073,709,551,615 %llu
long int

signed char 1 -128 to 127 %c

unsigned
1 0 to 255 %c
char

float 4 1.2E-38 to 3.4E+38 %f

double 8 1.7E-308 to 1.7E+308 %lf


long double 16 3.4E-4932 to 1.1E+4932 %Lf

Different data types also have different ranges up to which they can store
numbers. These ranges may vary from compiler to compiler. Below is a list
of ranges along with the memory requirement and format specifiers on the
32-bit GCC [Link]: The long, short, signed and unsigned are
datatype modifiers that can be used with some primitive data types to
change the size or length of the datatype.
Enumeration (or enum) in C

Enumeration (or enum) is a user-defined data type in C. It is mainly used to


assign names to integral constants, the names make a program easy to read
and maintain.

enum State {Working = 1, Failed = 0};


The keyword ‘enum’ is used to declare new enumeration types in C and
C++. Enums in C allow you to assign names to integral constants, making
code more readable.

Following is an example of enum declaration. ​

// The name of the enumeration is "flag" and the constant

// are the values of the flag. By default, the values

// of the constants are as follows:

// constant1 = 0, constant2 = 1, constant3 = 2 and


// so on.

enum flag{constant1, constant2, constant3, ....... };


Variables of type enum can also be defined. They can be defined in two
ways: ​

// In both of the below cases, "day" is

// defined as the variable of type week.

enum week{Mon, Tue, Wed};

enum week day;

// Or

enum week{Mon, Tue, Wed}day;

// An example program to demonstrate working

// of enum in C

#include<stdio.h>
enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};

int main()

enum week day;

day = Wed;

printf("%d",day);

return 0;

Output: ​


In the above example, we declared “day” as the variable and the value of
“Wed” is allocated to day, which is 2. So as a result, 2 is printed.​
Another example of enumeration is: ​

// Another example program to demonstrate working


// of enum in C

#include<stdio.h>

enum year{Jan, Feb, Mar, Apr, May, Jun, Jul,

Aug, Sep, Oct, Nov, Dec};

int main()

int i;

for (i=Jan; i<=Dec; i++)

printf("%d ", i);

return 0;

Output: ​

0 1 2 3 4 5 6 7 8 9 10 11

In this example, the for loop will run from i = 0 to i = 11, as initially the
value of i is Jan which is 0 and the value of Dec is 11. ​
Interesting facts about initialization of enum. ​
1. Two enum names can have same value. For example, in the following C
program both ‘Failed’ and ‘Freezed’ have same value 0. ​

#include <stdio.h>

enum State {Working = 1, Failed = 0, Freezed = 0};

int main()

printf("%d, %d, %d", Working, Failed, Freezed);

return 0;

Output:

1, 0, 0


2. If we do not explicitly assign values to enum names, the compiler by
default assigns values starting from 0. For example, in the following C
program, sunday gets value 0, monday gets 1, and so on. ​
#include <stdio.h>

enum day {sunday, monday, tuesday, wednesday, thursday, friday, saturday};

int main()

enum day d = thursday;

printf("The day number stored in d is %d", d);

return 0;

Output:

The day number stored in d is 4


What is a C Operator?

An operator in C can be defined as the symbol that helps us to perform some


specific mathematical, relational, bitwise, conditional, or logical
computations on values and variables. The values and variables used with
operators are called operands. So we can say that the operators are the
symbols that perform operations on operands.
For example,

c = a + b;

Here, ‘+’ is the operator known as the addition operator, and ‘a’ and ‘b’ are
operands. The addition operator tells the compiler to add both of the
operands ‘a’ and ‘b’. To dive deeper into how operators are used with data
structures, the C Programming Course Online with Data Structures
covers this topic thoroughly.
Types of Operators in C
C language provides a wide range of operators that can be classified into 6
types based on their functionality:

1.​ Arithmetic Operators


2.​ Relational Operators
3.​ Logical Operators
4.​ Bitwise Operators
5.​ Assignment Operators
6.​ Other Operators

1. Arithmetic Operations in C

The arithmetic operators are used to perform arithmetic/mathematical


operations on operands. There are 9 arithmetic operators in C language:

Symb Operat
S. No. Description Syntax
ol or

1 + Plus Adds two numeric values. a+b

Subtracts right operand from left


2 – Minus a–b
operand.

Multipl
3 * Multiply two numeric values. a*b
y
4 / Divide Divide two numeric values. a/b

Modulu Returns the remainder after diving the


5 % a%b
s left operand with the right operand.

Unary
6 + Used to specify the positive values. +a
Plus

Unary
7 – Min Flips the sign of the value. -a
us

Increm
8 ++ Increases the value of the operand by 1. a++
ent

Decrem Decreases the value of the operand by


9 — a–
ent 1.

Example of C Arithmetic Operators

// C program to illustrate the arithmetic operators

#include <stdio.h>
int main()

int a = 25, b = 5;

// using operators and printing results

printf("a + b = %d\n", a + b);

printf("a - b = %d\n", a - b);

printf("a * b = %d\n", a * b);

printf("a / b = %d\n", a / b);

printf("a % b = %d\n", a % b);

printf("+a = %d\n", +a);

printf("-a = %d\n", -a);

printf("a++ = %d\n", a++);

printf("a-- = %d\n", a--);


return 0;

Output

a + b = 30

a - b = 20

a * b = 125

a/b=5

a%b=0

+a = 25

-a = -25

a++ = 25

a-- = 26
2. Relational Operators in C

The relational operators in C are used for the comparison of the two operands.
All these operators are binary operators that return true or false values as
the result of comparison.

These are a total of 6 relational operators in C:


S.
N Symb Operat
Description Syntax
o ol or
.

Less
Returns true if the left operand is less than
1 < tha a<b
the right operand. Else false
n

Greate
r Returns true if the left operand is greater
2 > a>b
tha than the right operand. Else false
n

Less
tha
n
Returns true if the left operand is less than
3 <= or a <= b
or equal to the right operand. Else false
equ
al
to

Greate
r
tha Returns true if the left operand is greater
4 >= a >= b
n than or equal to right operand. Else false
or
equ
al
to

Equal
5 == Returns true if both the operands are equal. a == b
to

Not
equ Returns true if both the operands are NOT
6 != a != b
al equal.
to

Example of C Relational Operators

// C program to illustrate the relational operators

#include <stdio.h>

int main()

int a = 25, b = 5;

// using operators and printing results


printf("a < b : %d\n", a < b);

printf("a > b : %d\n", a > b);

printf("a <= b: %d\n", a <= b);

printf("a >= b: %d\n", a >= b);

printf("a == b: %d\n", a == b);

printf("a != b : %d\n", a != b);

return 0;

Output

a<b :0

a>b :1

a <= b: 0

a >= b: 1

a == b: 0
a != b : 1

Here, 0 means false and 1 means true.


3. Logical Operator in C

Logical Operators are used to combine two or more conditions/constraints or


to complement the evaluation of the original condition in consideration.
The result of the operation of a logical operator is a Boolean value either
true or false.

S
Sym
b Operator Description Syntax
ol

Logical
1 && Returns true if both the operands are true. a && b
AND

Logical Returns true if both or any of the operands


2 || a || b
OR is true.

Logical
3 ! Returns true if the operand is false. !a
NOT

Example of Logical Operators in C


// C program to illustrate the logical operators

#include <stdio.h>

int main()

int a = 25, b = 5;

// using operators and printing results

printf("a && b : %d\n", a && b);

printf("a || b : %d\n", a || b);

printf("!a: %d\n", !a);

return 0;

}
Output

a && b : 1

a || b : 1

!a: 0
4. Bitwise Operators in C

The Bitwise operators are used to perform bit-level operations on the


operands. The operators are first converted to bit-level and then the
calculation is performed on the operands. Mathematical operations such as
addition, subtraction, multiplication, etc. can be performed at the bit level
for faster processing.

There are 6 bitwise operators in C:

Sy
S.
m
N Operat
b Description Syntax
o or
o
.
l

Bitwis
e Performs bit-by-bit AND operation and
1 & a&b
AN returns the result.
D
Bitwis
Performs bit-by-bit OR operation and
2 | e a|b
returns the result.
OR

Bitwis
e Performs bit-by-bit XOR operation and
3 ^ a^b
XO returns the result.
R

Bitwis
e
Firs
t Flips all the set and unset bits on the
4 ~ ~a
Co number.
mpl
em
ent

Bitwis
e Shifts the number in binary form by one
<
5 Lef place in the operation and returns the a << b
<
tshi result.
ft

Bitwis Shifts the number in binary form by one


>
6 e place in the operation and returns the a >> b
>
Rig result.
hts
hilf
t

Example of Bitwise Operators

// C program to illustrate the bitwise operators

#include <stdio.h>

int main()

int a = 25, b = 5;

// using operators and printing results

printf("a & b: %d\n", a & b);

printf("a | b: %d\n", a | b);

printf("a ^ b: %d\n", a ^ b);

printf("~a: %d\n", ~a);


printf("a >> b: %d\n", a >> b);

printf("a << b: %d\n", a << b);

return 0;

Output

a & b: 1

a | b: 29

a ^ b: 28

~a: -26

a >> b: 0

a << b: 800
5. Assignment Operators in C

Assignment operators are used to assign value to a variable. The left side
operand of the assignment operator is a variable and the right side operand
of the assignment operator is a value. The value on the right side must be
of the same data type as the variable on the left side otherwise the compiler
will raise an error.
The assignment operators can be combined with some other operators in C to
provide multiple operations using a single operator. These operators are
called compound operators.

In C, there are 11 assignment operators :

S. No. Symbol Operator Description Syntax

Simple Assign the value of the


1 = Assignme right operand to the a=b
nt left operand.

Add the right operand


Plus and and left operand and
2 += a += b
assign assign this value to
the left operand.

Subtract the right


operand and left
Minus and
3 -= operand and assign a -= b
assign
this value to the left
operand.

Multiply and Multiply the right


4 *= operand and left a *= b
assign
operand and assign
this value to the left
operand.

Divide the left operand


with the right
Divide and
5 /= operand and assign a /= b
assign
this value to the left
operand.

Assign the remainder in


the division of left
Modulus and
6 %= operand with the a %= b
assign
right operand to the
left operand.

Performs bitwise AND


AND and and assigns this
7 &= a &= b
assign value to the left
operand.

Performs bitwise OR
OR and and assigns this
8 |= a |= b
assign value to the left
operand.
Performs bitwise XOR
XOR and and assigns this
9 ^= a ^= b
assign value to the left
operand.

Performs bitwise
Rightshift
Rightshift and assign
10 >>= and a >>= b
this value to the left
assign
operand.

Performs bitwise
Left shift and Leftshift and assign
11 <<= a <<= b
assign this value to the left
operand.

Example of C Assignment Operators

// C program to illustrate the assignment operators

#include <stdio.h>

int main()

int a = 25, b = 5;
// using operators and printing results

printf("a = b: %d\n", a = b);

printf("a += b: %d\n", a += b);

printf("a -= b: %d\n", a -= b);

printf("a *= b: %d\n", a *= b);

printf("a /= b: %d\n", a /= b);

printf("a %%= b: %d\n", a %= b);

printf("a &= b: %d\n", a &= b);

printf("a |= b: %d\n", a |= b);

printf("a >>= b: %d\n", a >>= b);

printf("a <<= b: %d\n", a <<= b);

return 0;

}
Output

a = b: 5

a += b: 10

a -= b: 5

a *= b: 25

a /= b: 5

a %= b: 0

a &= b: 0

a |= b: 5

a >>= b: 0

a <<= b: 0

6. Other Operators

Apart from the above operators, there are some other operators available in C
used to perform some specific tasks. Some of them are discussed here:

sizeof Operator

●​ sizeof is much used in the C programming language.


●​ It is a compile-time unary operator which can be used to compute the
size of its operand.
●​ The result of sizeof is of the unsigned integral type which is usually
denoted by size_t.
●​ Basically, the sizeof the operator is used to compute the size of the
variable or datatype.

Syntax

sizeof (operand)

To know more about the topic refer to this article.

Comma Operator ( , )

●​ The comma operator (represented by the token) is a binary operator that


evaluates its first operand and discards the result, it then evaluates the
second operand and returns this value (and type).
●​ The comma operator has the lowest precedence of any C operator.
●​ Comma acts as both operator and separator.

Syntax

operand1 , operand2
To know more about the topic refer to this article.

Conditional Operator ( ? : )

●​ The conditional operator is the only ternary operator in C++.


●​ Here, Expression1 is the condition to be evaluated. If the
condition(Expression1) is True then we will execute and return the
result of Expression2 otherwise if the condition(Expression1) is false
then we will execute and return the result of Expression3.
●​ We may replace the use of if..else statements with conditional operators.

Syntax

operand1 ? operand2 : operand3;

To know more about the topic refer to this article.

dot (.) and arrow (->) Operators

●​ Member operators are used to reference individual members of classes,


structures, and unions.
●​ The dot operator is applied to the actual object.
●​ The arrow operator is used with a pointer to an object.

Syntax
structure_variable . member;

and

structure_pointer -> member;

To know more about dot operators refer to this article and to know more about
arrow(->) operators refer to this article.

Cast Operator

●​ Casting operators convert one data type to another. For example,


int(2.2000) would return 2.
●​ A cast is a special operator that forces one data type to be converted
into another.
●​ The most general cast supported by most of the C compilers is as
follows − [ (type) expression ].

Syntax

(new_type) operand;

To know more about the topic refer to this article.

addressof (&) and Dereference (*) Operators

●​ Pointer operator & returns the address of a variable. For example &a;
will give the actual address of the variable.
●​ The pointer operator * is a pointer to a variable. For example *var; will
pointer to a variable var.
To know more about the topic refer to this article.

Example of Other C Operators

// C Program to demonstrate the use of Misc operators

#include <stdio.h>

int main()

// integer variable

int num = 10;

int* add_of_num = &num;

printf("sizeof(num) = %d bytes\n", sizeof(num));

printf("&num = %p\n", &num);

printf("*add_of_num = %d\n", *add_of_num);

printf("(10 < 5) ? 10 : 20 = %d\n", (10 < 5) ? 10 : 20);

printf("(float)num = %f\n", (float)num);


return 0;

Output

sizeof(num) = 4 bytes

&num = 0x7ffe2b7bdf8c

*add_of_num = 10

(10 < 5) ? 10 : 20 = 20

(float)num = 10.000000
Unary, Binary and Ternary Operators in C

Operators can also be classified into three types on the basis of the number of
operands they work on:

1.​ Unary Operators: Operators that work on a single operand.

2.​ Binary Operators: Operators that work on two operands.

3.​ Ternary Operators: Operators that work on three operands.

Operator Precedence and Associativity in C


In C, it is very common for an expression or statement to have multiple
operators and in these expressions, there should be a fixed order or priority
of operator evaluation to avoid ambiguity.
Operator Precedence and Associativity is the concept that decides which
operator will be evaluated first in the case when there are multiple
operators present in an expression.

The below table describes the precedence order and associativity of operators
in C. The precedence of the operator decreases from top to bottom.

Precedence Operator Description Associativity

Parentheses (function
() left-to-right
call)

Brackets (array
[] left-to-right
1 subscript)

Member selection via


. left-to-right
object name
Member selection via a
-> left-to-right
pointer

Postfix
a++ , a– increment/decrement left-to-right
(a is a variable)

Prefix
++a , –a increment/decrement right-to-left
(a is a variable)

+,– Unary plus/minus right-to-left

Logical negation/bitwise
2 !,~ right-to-left
complement

Cast (convert value to


(type) temporary value of right-to-left
type)

* Dereference right-to-left
& Address (of operand) right-to-left

Determine size in bytes


sizeof on this right-to-left
implementation

Multiplication/division/m
3 *,/,% left-to-right
odulus

4 +,– Addition/subtraction left-to-right

Bitwise shift left, Bitwise


5 << , >> left-to-right
shift right

Relational less than/less


< , <= left-to-right
than or equal to

6
Relational greater
> , >= than/greater than or left-to-right
equal to
Relational is equal to/is
7 == , != left-to-right
not equal to

8 & Bitwise AND left-to-right

9 ^ Bitwise XOR left-to-right

10 | Bitwise OR left-to-right

11 && Logical AND left-to-right

12 || Logical OR left-to-right

13 ?: Ternary conditional right-to-left

= Assignment right-to-left
14
Addition/subtraction
+= , -= right-to-left
assignment

Multiplication/division
*= , /= right-to-left
assignment

Modulus/bitwise AND
%= , &= right-to-left
assignment

Bitwise
^= , |= exclusive/inclusive right-to-left
OR assignment

Bitwise shift left/right


<<=, >>= right-to-left
assignment

15 , expression separator left-to-right

Error Handling in C



Although C does not provide direct support to error handling (or
exception handling), there are ways through which error handling
can be done in C. A programmer has to prevent errors in the first
place and test return values from the functions.

A lot of C function calls return -1 or NULL or set an in case of an


error code as the global variable errno, so quick tests on these
values are easily done with an instance of ‘if statement’.

What is errno?

errno is a global variable indicating the error occurred during any


function call and it is defined inside <errno.h> header file.

When a function is called in C, a variable named errno is


automatically assigned a code (value) which can be used to identify
the type of error that has been encountered. Different codes (values)
for errno mean different types of errors.

Below is a list of a few different errno values and their


corresponding meaning:

errno Error
va
lu
e

1 Operation not
permitted

2 No such file or
directory

3 No such process

4 Interrupted system
call

5 I/O error
6 No such device or
address

7 The argument list is


too long

8 Exec format error

9 Bad file number

10 No child processes

11 Try again
12 Out of memory

13 Permission denied

Example of errno

​ C

#include <errno.h>

#include <stdio.h>
int main()

// If a file is opened which does not exist,

// then it will be an error and corresponding

// errno value will be set

FILE* fp;

// opening a file which does not exist


fp = fopen("[Link]", "r");

printf("Value of errno: %d\n", errno);

return 0;

Output

Value of errno: 2
Note: Here the errno is set to 2 which means “No such file or
directory”. It may give Errno 13 in online IDE, which says
permission denied.

Different Methods for Error Handling in C

Different methods are used to handle different kinds of errors in C.


Some of the commonly used methods are:

1.​ perror()
2.​ strerr()
3.​ ferror()
4.​ feof()
5.​ clearerr()
6.​ Exit Status
7.​ Divide by Zero Error

1. perror()

The perror() function is used to show the error description. It displays


the string you pass to it, followed by a colon, a space, and then the
textual representation of the current errno value.

Syntax

void perror(const char *str);


Parameters

●​ str: It is a string containing a custom message to be printed


before the error message itself.

Example

​ C

// C implementation to see how perror() function is used to

// print the error messages.

#include <errno.h>

#include <stdio.h>

#include <string.h>
int main()

FILE* fp;

// If a file is opened which does not exist,

// then it will be an error and corresponding

// errno value will be set


fp = fopen(" [Link] ", "r");

// opening a file which does

// not exist.

printf("Value of errno: %d\n ", errno);

perror("Message from perror");

return 0;
}

Output

Value of errno: 2

Message from perror: No such file or directory

2. strerror()

The strerror() function is also used to show the error description. This
function returns a pointer to the textual representation of the current
errno value.

Syntax

char *strerror(int errnum);

Parameters

●​ errnum: It is the error number (errno).

Example

​ C
// C implementation to see how strerror() function is used

// to print the error messages.

#include <errno.h>

#include <stdio.h>

#include <string.h>

int main()
{

FILE* fp;

// If a file is opened which does not exist,

// then it will be an error and corresponding

// errno value will be set

fp = fopen(" [Link] ", "r");


// opening a file which does

// not exist.

printf("Value of errno: %d\n", errno);

printf("The error message is : %s\n", strerror(errno));

return 0;

Output

Value of errno: 2
The error message is : No such file or directory

3. ferror()

The ferror() function is used to check whether an error occurred


during a file operation.

Syntax

int ferror(FILE *stream);

Parameters

●​ stream: It is the pointer that points to the FILE for which we


want to check the error.

Return Value

●​ It returns a non-zero value if an error occurred, otherwise it


returns 0.

Example

​ C
// C program to demonstrate the ferror() function

#include <stdio.h>

int main()

// Open the file in read mode

FILE* file = fopen("nonexistent_file.txt", "r");


if (file == NULL) {

// Print an error message

// if file opening fails

perror("Error opening file");

// Return with non-zero exit status to

// indicate an error

return 1;

}
int c;

// Process the character

// Add your code here to perform operations on each

// character read from the file

while ((c = fgetc(file)) != EOF) {

}
if (ferror(file)) {

// Print an error message if an error occurred

// during file reading

printf(

"An error occurred while reading the file.\n");

else {

// Print success message if file reading completed


// without errors

printf("File read successfully.\n");

// Close the file

fclose(file);

// Return with zero exit status to indicate successful

// execution

return 0;
}

Output

Error opening file: No such file or directory

4. feof()

The feof() function is used to check whether end-of-file indicator is


set for a file steam.

Syntax

int feof(FILE *stream);

Parameters

●​ stream: It is the pointer that points to the FILE for which we


want to check the error.

Return Value

●​ It returns a non-zero value if an error occurred, otherwise, it


returns 0.

Example
​ C

// C program to demonstrate the feof() function

#include <stdio.h>

int main()

FILE* fptr = fopen("[Link]", "r");

if (fptr == NULL) {
// Print error message if file opening fails

perror("Error opening the file");

return 1;

// Perform file operations

if (ferror(fptr)) {
// Print error message if file operation fails

perror("Error occurred during file operations");

// Handle the error

// Close the file

fclose(fptr);

return 0;

Output
Error opening the file: No such file or directory

5. clearerr()

The clearerr() function is used to clear both end-of-file and error


indicators for a file stream.

Syntax

void clearerr(FILE *stream);

Parameters

●​ stream: It is the pointer that points to the FILE for which we


want to check the error.

Example

​ C
#include <stdio.h>

int main()

FILE* file = fopen("[Link]", "r");

// Open the file in read mode

if (file == NULL) {
// Print an error message

// if file opening fails

perror("Error opening file");

// Return with non-zero exit status to

// indicate an error

return 1;

}
// Perform file operations

// Add your code here to perform operations on the

// opened file

if (ferror(file)) {

// Print an error message

// if an error occurred

// during file operations


printf("An error occurred while performing file "

"operations.\n");

// Clear the error indicators for the

// file stream

clearerr(file);
// Continue with file operations

// Add your code here to continue with further

// operations on the file

// Close the file

fclose(file);

return 0;

}
6. Exit Status

Exit status is the value returned by the program after its execution is
completed which tells the status of the execution of the program.

The C standard specifies two constants: EXIT_SUCCESS and


EXIT_FAILURE, that may be passed to exit() to indicate
successful or unsuccessful termination, respectively. These are
macros defined in <stdlib.h> header file.

Example

​ C
// C implementation which shows the

// use of EXIT_SUCCESS and EXIT_FAILURE.

#include <errno.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>
int main()

FILE* fp;

fp = fopen("[Link]", "rb");

if (fp == NULL) {

printf("Value of errno: %d\n", errno);

printf("Error opening the file: %s\n",


strerror(errno));

perror("Error printed by perror");

exit(EXIT_FAILURE);

printf("I will not be printed\n");

else {
fclose(fp);

exit(EXIT_SUCCESS);

printf("I will not be printed\n");

return 0;

Output

Value of errno: 2

Error opening the file: No such file or directory

Error printed by perror: No such file or directory


7. Divide by Zero Errors

A common pitfall made by C programmers is not checking if a divisor


is zero before a division command. Division by zero leads to
undefined behavior, there is no C language construct that can do
anything about it. Your best bet is to not divide by zero in the first
place, by checking the denominator.

Example

​ C
// C program to check and rectify

// divide by zero condition

#include <stdio.h>

#include <stdlib.h>

void function(int);
int main()

int x = 0;

function(x);

return 0;

void function(int x)
{

float fx;

if (x == 0) {

printf("Division by Zero is not allowed");

fprintf(stderr, "Division by zero! Exiting...\n");

exit(EXIT_FAILURE);

}
else {

fx = 10 / x;

printf("f(x) is: %.5f", fx);

Output

Division by zero! Exiting...

You might also like