C Unit-1 Notes
C Unit-1 Notes
There are several programming languages that use paradigms, but to do this, they
need to follow a strategy or methodology. Paradigms are not meant to be mutually
exclusive; a single program can feature multiple paradigms. Below is an overview
of programming languages and their paradigm methodology.
● Input Unit
A computer will only respond when a command is given to the device. These
commands can be given using the input unit or the input devices.
For example: Using a keyboard we can type things on a Notepad and the computer
processes the entered data and then displays the output of the same of the screen.
The data entered can be in the form of numbers, alphabet, images, etc. We enter the
information using an input device, the processing units convert it into computer
understandable languages and then the final output is received by a human-
understandable language.
● Output Unit
When we command a computer to perform a task, it reverts for the action performed
and gives us a result. This result is called output. There are various output devices
connected to the computer. The most basic of which is a monitor. Whatever we write
using a keyboard or click using a mouse, is all displayed on the monitor.Thus, the
output unit gives us the final result once the entire processing is done within the
mechanism of a device.
For example: when we visit an ATM, we enter our details like language, pin, amount
to be withdrawn, etc. and then the final money which the cash dispenser releases is
our outcome. In this case, the cash dispenser acts as an output unit.
To get a list of computer input and output devices and the function of the various I/O
devices, visit the linked article.
For better understanding and more interactive analysis of the components of the
computer, candidates can check the video given below and get detailed information
reading the five major components responsible for the functioning of a computer
device.
● Memory Unit
When we enter the data into the computer using an input device, the entered
information immediately gets saved in the memory unit of the Central Processing
Unit (CPU). Because of the presence of some existing programming, the Memory
Unit transmits the data further to the other parts of the CPU.
Similarly, when the output of our command is processed by the computer, it is saved
in the memory unit before giving the output to the user.
● Control Unit
This is the core unit which manages the entire functioning of the computer device.It
is one of the most essential components of the computer system.
The Control Unit collects the data entered using the input unit, leads it on for
processing and once that is done, receives the output and presents it to the user. Itcan
be said to the centre of all processing actions taking place inside a computer device.
Basically, the instructions taken, interpretation of entered data, issuing signals to
execute the data and then finally retrieving the data is all done in the Control Unit.
Arithmetic & Logical Unit
As the name suggests, all the mathematical calculations or arithmetic operations are
performed in the Arithmetic and Logical Unit of the CPU.
It can also perform actions like a comparison of data and decision-making actions.
The ALU comprises circuits using which addition, subtraction, multiplication,
division and other numerical based calculations can be performed.
All these three units are elements of CPU and together help in the efficient working
and processing of data. It is also known as the “Brain of Computer” and no action
can be conducted by a device without the execution and permission of the Central
Processing Unit.
The device is a close-knit circuit comparison microprocessor which helps infetching
the data and proving suitable results to the user. Thus, CPU is the main processing
unit of the computer.
Algorithm :
Step by step procedure of solving the problem
Definition : It is a sequence of steps to solve a particular problem or algorithm is an
ordered set of unambiguous steps that produces a result and terminates in a finite
number of steps/time.
Algorithm has following Characteristics:
Input : an algorithm may or may not require input
Finiteness : an algorithm must consist of finite number of steps
Effectiveness : an algorithm must consist of single entry and single exit.
Definiteness : each step of the algorithm must be defined precisely.
Readability : the steps of the algorithm must be easily readable.
Output : Each algorithm is expected to produce at least one output/result.
Example :
Normal instructions :
Step 3 : turn off the burner when the water starts boiling .
Step 9 : check if the water is 100 C Step 10 : if not , repeat the above step
Step 12 : stop
Example of Algorithm
Algorithm:
Step1: Start
Step6: End
Algorithm:
Step1: Start
Step 2: Read Temperature in Fahrenheit F
Step 3: C 5/9*(F32)
Step5: End
Type of Algorithms
They are:
1. Sequence
2. Branching (Selection)
3. Loop (Repetition)
● The sequence is exemplified by a sequence of statements placed one after the other
– the one above or before another gets executed first. In flowcharts, sequence of
statements is usually contained in the rectangular process box.
● The branch refers to a binary decision based on some condition. If the condition is
true, one of the two branches is explored; if the condition is false, the other
alternative is taken. This is usually represented by the ‘if-then’ construct in pseudo-
codes andprograms. In flowcharts, this is represented by the diamond-shaped
Decision box.This structure is also known as the selectionstructure.
Problem1: write algorithm to find the greater number between two numbers
Step1: Start
Step6: End
Step1: Start
Step2: Read/input x
Step6: End
Step7: End
The loop allows a statement or a sequence of statements to be repeatedly
executed based on some loop condition. It is represented by the ‘while’ and
‘for’ constructs in most programming languages, for unbounded loops and
bounded loops respectively. (Unbounded loops refer to those whose number of
iterations depends on the eventuality that the termination condition is satisfied;
bounded loopsrefer to those whose number of iterations is known before-hand.)
In the flowcharts, a back arrow hints the presence of a loop. A trip around the
loop is known as iteration. You must ensure that the condition for the
termination of the looping must be satisfied after some finite number of
iterations, otherwise it ends up as an infinite loop, a common mistake made by
inexperienced programmers. The loop is also known as the repetition structure.
Examples:
Problem2: Design an algorithm which gets a natural value, n,as its input and
calculates odd numbers equal or less than n. Then write them in the standard
output:
1. Start
2. Read n
3. I ← 1
4. Write I
5. I ← I + 2
6. If ( I <= n) then go to line 4
7. End
Flowchart
Flowchart Symbols
Different flowchart shapes have different conventional meanings. The
Terminator
The terminator symbol represents the starting or ending point of the system.
Process
Document
This represents a printout, such as a document or a report.
Decision
A diamond represents a decision or branching point. Lines coming out
sub-processes.
Data
It represents information entering or leaving the system. An input might be
On-Page Reference
This symbol would contain a letter inside. It indicates that the flow
a different page.
Delay or Bottleneck
Identifies a delay or a bottleneck.
Flow
Lines represent the flow of the sequence and direction of a process.
https://www.cimt.org.uk/projects/mepres/book8/bk8i1/bk8_1i2.htm
https://dyclassroom.com/flowchart/exercise-1
https://www.cimt.org.uk/projects/mepres/book8/bk8i1/bk8_1i3.htm
https://www.cimt.org.uk/projects/mepres/book8/book8int.htm
Introduction to ‘C’ programming
C is a general-purpose programming language that is extremely
popular, simple, and flexible to use. It is a structured programming language that is
machine-independent and extensively used to write various applications, Operating
Systems like Windows, and many other complex programs like Oracle database, Git,
Python interpreter, and more.
It is said that ‘C’ is a god’s programming language. One can say, C is a base for
the programming. If you know ‘C,’ you can easily grasp the knowledge of the other
programming languages that uses the concept of ‘C’.
Structure of c program
The structure of a C program means the specific structure to start the programming
in the C language. Without a proper structure, it becomes difficult to analyze the
problem and the solution. It also gives us a reference to write more complex
programs.
Importance of structure of a C program
Sometimes, when we begin with a new programming language, we are not aware
about the basic structure of a program. The sections of a program usually get
shuffled and the chance of omission of error rises. The structure of a language gives
us a basic idea of the order of the sections in a program. We get to know when and
where to use a particular statement, variable, function, curly braces,
parentheses, etc. It also increases our interest in that programming language.
Thus, the structure helps us analyze the format to write a program for the least
errors. It gives better clarity and the concept of a program.
Parts of C program-
Live Demo
#include <stdio.h>
int main() {
/* my first program in C */
return 0;
There are three basic phases occurred when we execute any C program.
● Preprocessing
● Compiling
● Linking
Preprocessing Phase
and produces a pure form of C code that contains no preprocessing statements (like
#include).
Compilation Phase
The C compiler accepts a preprocessed output file from the preprocessor and
produces a special file called an object file. Object file contains machine code
The link phase is implemented by the linker. The linker is a process that
accepts as input object files and libraries to produce the final executable program.
Start.
#include<stdio.h>int main()
int A, B, Answer;
}
Where is C used? Key Applications
Variables in C
■ A variable is a name of the memory location. It is used to store data. Its value can be changed,
and it can be reused many times.
■ It is a way to represent memory location through symbol so that it can be easily identified.
Let's see the syntax to declare a variable:
type variable_list;
int a;
float b;
char c;
Rules for defining variables
■ A variable can have alphabets, digits, and underscore.it should always begin with alphabet.
■ A variable name can start with the alphabet, and underscore only. It can't start with a digit.
■ No whitespace is allowed within the variable name.
■ No special characters are allowed except underscores(_).
■ A variable name must not be any reserved word or keyword,
e.g. int, float, etc.
■ Maximum length of variables is 31 characters. But when the first eight characters are the same
then the variable names are not different.
Valid variable names:
int a;
int _ab;
int a30;
Types of Variables in C
■ Local variable
■ Global variable
■ Static variable
■ Automatic variable
■ External variable
Local Variable
■ A variable that is declared inside the function or block is called a local variable.
■ It must be declared at the start of the block.
Example code:
void function1()
{
int x=10;//local variable
}
■ You must have to initialize the local variable before it is used.
Global Variable
■ A variable that is declared outside the function or block is called a global variable. Any
function can change the value of the global variable. It is available to all the functions.
■ It must be declared at the start of the block.
Example code:
int value=20;//global variable
void function1(){
int x=10;//local variable
}
Static Variable
■ A variable that is declared with the static keyword is called static variable.
■ It retains its value between multiple function calls.
void function1(){
int x=10;//local variable
static int y=10;//static variable
x=x+1;
y=y+1;
printf("%d,%d",x,y);
}
■ If you call this function many times, the local variable will print the same value for each
function call, e.g, 11,11,11 and so on. But the static variable will print the incremented
value in each function call, e.g. 11, 12, 13 and so on.
Automatic Variable
■ All variables in C that are declared inside the block, are automatic variables by default. We
can explicitly declare an automatic variable using auto keyword
Example code:
void main(){
int x=10;//local variable (also automatic)
auto int y=20;//automatic variable
}
External Variable
■ We can share a variable in multiple C source files by using an external variable. To declare
an external variable, you need to use extern keyword
■ myfile.h
extern int x=10;//external variable (also global)
■ program1.c
#include "myfile.h"
#include <stdio.h>
void printValue()
{
printf("Global variable: %d", global_variable);
}
a = a + b;
b = a - b;
a = a - b;
printf("a = %d\nb = %d\n",a,b);
return 0;
}
What is constants ?
■ As the name suggests the name constants is given to such variables or values in C/C++
programming language which cannot be modified once they are defined.
■ They are fixed values in a program.
■ There can be any types of constants like
■ Integer, float, octal, hexadecimal, character constants etc.
Range of constants
■ Every constant has some range.
■ The integers that are too big to fit into an int will be taken as long.
■ Now there are various ranges that differ from unsigned to signed bits.
■ Under the signed bit, the range of an int varies from -128 to +127 and under the unsigned bit, int
varies from 0 to 255.
Defining Constants
■ In C/C++ program we can define constants in two ways as shown below:
■ Using #define preprocessor directive
■ Using a const keyword
■ What is Literals:
■ The values assigned to each constant variables are referred to as the literals. Generally, both
terms, constants and literals are used interchangeably.
NUMBER SYSTEM
Number systems are the technique to represent numbers in the computer system architecture,
every value that you are saving or getting into/from computer memory has a defined number
system.
A Binary number system has only two digits that are 0 and 1. Every number (value) represents
with 0 and 1 in this number system. The base of binary number system is 2, because it has only
two digits.
Octal number system has only eight (8) digits from 0 to 7. Every number (value) represents with
0,1,2,3,4,5,6 and 7 in this number system. The base of octal number system is 8, because it has
only 8 digits.
Decimal number system has only ten (10) digits from 0 to 9. Every number (value) represents
with 0,1,2,3,4,5,6, 7,8 and 9 in this number system. The base of decimal number system is 10,
because it has only 10 digits.
A Hexadecimal number system has sixteen (16) alphanumeric values from 0 to 9 and A to F.
Every number (value) represents with 0,1,2,3,4,5,6, 7,8,9,A,B,C,D,E and F in this number
system. The base of hexadecimal number system is 16, because it has 16 alphanumeric values.
Here A is 10, B is 11, C is 12, D is 14, E is 15 and F is 16.
CONVERSIONS
DECIMAL TO OTHER
1. DECIMAL TO BINARY
To convert Number system from Decimal Number System to Any Other Base is quite easy;
you have to follow just two steps:
A) Divide the Number (Decimal Number) by the base of target base system (in which you want
to convert the number: Binary (2), octal (8) and Hexadecimal (16)).
B) Write the remainder from step 1 as a Least Signification Bit (LSB) to Step last as a Most
Significant Bit (MSB).
Binary Number is
(11000000111001)2
2. DECIMAL TO OCTAL
Octal Number is
(30071)8
3. DECIMAL TO HEXADECIMAL
Decimal to Hexadecimal Conversion Result
Example 1
Decimal Number is : (12345)10
Hexadecimal Number is
(3039)16
Example 2
Decimal Number is : (725)10 Hexadecimal Number is
(2D5)16
Convert
10, 11, 12, 13, 14, 15
to its equivalent...
A, B, C, D, E, F
BINARY TO OTHER
A) Multiply the digit with 2(with place value exponent). Eventually add all the multiplication
becomes the Decimal number.
1. BINARY TO DECIMAL
2. BINARY TO OCTAL
An easy way to convert from binary to octal is to group binary digits into sets of three, starting
with the least significant (rightmost) digits.
3. BINARY TO HEXADECIMAL
An equally easy way to convert from binary to hexadecimal is to group binary digits into sets of
four, starting with the least significant (rightmost) digits.
OCTAL TO OTHER
1. OCTAL TO BINARY
Converting from octal to binary is as easy as converting from binary to octal. Simply look up
each octal digit to obtain the equivalent group of three binary digits.
Octal: 0 1 2 3 4 5 6 7
Binary: 000 001 010 011 100 101 110 111
Octal = 3 4 5
Binary = 011 100 101 = 011100101 binary
2. OCTAL TO HEXADECIMAL
When converting from octal to hexadecimal, it is often easier to first convert the octal number
into binary and then from binary into hexadecimal. For example, to convert 345 octal into hex:
Hexadecimal: 0 1 2 3 4 5 6 7
Hexadecimal: 8 9 A B C D E F
Hexadecimal = E 5 = E5 hex
Therefore, through a two-step conversion process, octal 345 equals binary 011100101 equals
hexadecimal E5.
3. OCTAL TO DECIMAL
The conversion can also be performed in the conventional mathematical way, by showing each
digit place as an increasing power of 8.
OR
Solution:
2=2
C = 12
7=7
Hence,
= 512 + 192 + 7
= 711
Solution:
1=1
A = 10
7=7
D = 13
Hence,
**********************************************