Unit-I:: Introduction To Computers: Introduction To C
Unit-I:: Introduction To Computers: Introduction To C
Tech I Year)
------------------------------------------------------------------------------------------------------------------------------------------
UNIT–I:
Introduction to Computers: Computer hardware and software, System Software, Programming
languages, Program Developing Steps, Algorithms, Flow charts.
Introduction to C: History of C, Structure of C Program, Keywords, Identifiers , Data Types,
Constants, Variables, Operators, Expressions, Precedence and Order of Evaluation Type
Conversion and type casting.
1. List and explain the functions of various parts of computer hardware and software.
Ans: Computer System:
1
A. Input Devices: The input device is usually a keyboard where programs and
data are entered into the computer. Other Input Devices : a touch screen , a mouse , a
pen , an audio input unit.
E. Auxiliary Storage Devices ( secondary storage devices ):It is used for both input and
output. It is also known as secondary storage. It is a place where the programs and data
are stored permanently. When we turn off the computer the programs and data
remain in the secondary storage, ready for the next time when we need them.
i. Operating System Software: It provides services such as a user interface, files and
data base access and interfaces to communication systems such as Internet
protocols. The primary purpose of this software is to keep the system operating in
an efficient manner while allowing the users access to the system.
ii. System Support Software: System Support Software provides system utilities and other
operating services. Examples of system utilities are sort programs and disk format
programs. Operating services consist of programs that provide performance statistics for
the operational staff and security monitors to protect the system and data.
iii. System Development Software: It includes language translators that convert programs in
to machine language for execution , debugging tools to ensure that programs are error -
free and computer -assisted software engineering ( CASE ) systems.
D. Executing Programs : Once our program has been linked, it is ready for
execution. To execute a program, we use operating system command, such as
run to load the program in to main memory and execute it. Getting program
into memory is the function of an Operating System programs called loader.
Loader locates the executable program and reads it into memory. In a
typical program execution, the program reads data for processing, either from
user or from file. After the program processes the data, it prepares output. Data
output can be to user‘s monitor or to a file. When program has executed,
Operating System removes the program from memory.
Ans: Algorithm :
Algorithm is a finite set of instructions that , if followed accomplishes a particular
task.
Algorithm should satisfy the following criteria
1. Input : Zero or more quantities are externally supplied.
2. Output: At least one quantity is produced.
3. Definiteness : Each instruction is clear and unambiguous.
Ex: Add B or C to A
4. Finiteness : Algorithm should terminate after finite number of steps
when traced in all cases. Ex: Go on adding elements to an array
5. Effectivenes: Every instruction must be basic i.e., it can be carried out, by a
person using pencil and paper.
Algorithm must also be general to deal with any situation.
4. List out the advantages and disadvantages of
algorithm. Ans: Advantages of Algorithms:
It provides the core solution to a given problem. the solution can be implemented on a
computer system using any programming language of user‘s choice.
It facilitates program development by acting as a design document or a blue print of a
given problem solution.
It ensures easy comprehension of a problem solution as compared to an equivalent
computer program.
It eases identification and removal of logical errors in a program.
It facilitates algorithm analysis to find out the most efficient solution to a given problem.
Disadvantages of Algorithms:
In large algorithms the flow of program control becomes difficult to track.
Algorithms lack visual representation of programming constructs like flowcharts; thus
understanding the logic becomes relatively difficult.
Ans: Flowchart :
A flowchart is a visual representation of the sequence of steps for solving
a problem . A flowchart is a set of symbols that indicate various operations in
the program. For every process , there is a corresponding symbol in the flowchart . Once
an algorithm is written , its pictorial representation can be done using flowchart
symbols. In other words, a pictorial representation of a textual algorithm is done using
a flowchart.
A flowchart gives a pictorial representation of an algorithm.
The first flowchart is made by John Von Neumann in 1945.
It is a symbolic diagram of operations sequence, dataflow, control flow and processing
logic in information processing.
The symbols used are simple and easy to learn.
It is a very helpful tool for programmers and beginners.
Purpose of a Flowchart :
Provides communication.
Provides an overview.
Shows all elements and their relationships.
Quick method of showing program flow.
Checks program logic.
Facilitates coding.
Provides program revision.
Provides program documentation.
Advantages of a Flowchart :
Flowchart is an important aid in the development of an algorithm itself.
Easier to understand than a program itself.
Independent of any particular programming language.
Proper documentation.
Proper debugging.
Easy and clear presentation.
Limitations of a Flowchart :
Complex logic.
Drawing is time consuming.
Difficult to draw and remember.
Technical detail.
7. Explain the Symbols in a Flowchart ?
Ans: Symbols : Symbols are divided in to the following two parts.
I. Auxiliary Symbols.
II. Primary Symbols.
Some of the common symbols used in flowcharts are shown below :
Step 1 : Start
Step 2 : Input num1 , num2
Step 3 : temp = num1
Step 4 : num1 = num2
Step 5 : num2 = temp
Step 6 : Output num1 , num2
Step 7 : Stop
Step 1 : Start
Step 2 : Input num1 , num2
Step 3 : calculate num1 = num1 + num2
Step 4 : calculate num2 = num1 – num2
Step 5 : calculate num1 = num1 – num2
Step 6 : Output num1 , num2
Step 7 : Stop
Flowcharts :
Step 1 : Start
Step 2 : Input A , B
Step 3 : if A > B then output A
else output B
Step 4 : Stop
Flowchart
:
10. Write an algorithm and flowchart to find the largest among three
numbers.
Ans: Algorithm:
Step 1 : Start
Step 2 : Input A, B, C
Step 3 : if A > B go to step 4 , otherwise go to step 5
Step 4 : if A > C go to step 6 , otherwise go to step 8
Step 5 : if B > C go to step 7, otherwise go to step 8
Step 6 : print ― A is largest ‖ , go to step 9
Step 7 : print ― B is largest ‖, go to step 9
Step 8 : print ― C is largest ‖ , go to step 9
Step 9 : Stop
Flowchart:
Algorithm:
Step 1 : Start
Step 2 : Input A, B, C
Step 3 : Let max = A
Step 4 : if B > max then max = B
Step 5 : if C > max then max = C
Step 6 : output max is largest
Step 7 : Stop
Flowchart:
Algorithm:
Step 1: Start
Step 2: Input n
Step 3: Initialize counter variable, i , to 1 and factors = 1
Step 4: if i <= n go to step 5 otherwise go to step 7
Step 5: calculate factors = factors * i
Step 6: increment counter variable, i, and go to step 4
Step 7: output factors.
Step 8: stop
12. Write an algorithm and flowchart to find whether a number is prime or not.
Hint: A number is said to be prime number for which the only factors are 1 and itself
Ans:
Flow chart:
Step 3: Let i = 1, count=0
Step 4: if i > n/2 go to step
7
Step 5: if (n % i = = 0) count = count + 1
Step 6: increment i and go to step 4
Step 7: if count=2 then print ―prime number‖ else print
―not prime number‖
Step 8: Stop
14. Write an algorithm and flowchart to find the Sum of individual digits if a given
number
Ans: Algorithm :-
Step 1 : Start
Step 2 : read n
Step 3 : Sum = 0
Step 4 : While n > 0 do
Step 4.1 : r = n % 10
Step 4.2 : Sum = Sum + r
Step 4.3 : n = n/10
Step 5 : End while
Step 6 : Write Sum
Step 7 : Stop
Flowchart :-
Ans: ―The language used in the communication of computer instructions is known as the
programming language‖. The computer has its own language and any communication with the
computer must be in its language or translated into this language. Three levels of programming
languages are available. They are:
1. Procedure-oriented languages
High level languages designed to solve general-purpose problems are called procedural
languages or third generation languages. These include BASIC, COBOL, FORTRAN, C, C++,
and JAVA, which are designed to express the logic and procedure of a problem.
2. Problem-oriented languages
Problem-oriented languages are used to solve specific problems and are known as the fourth-
generation languages. These include query languages, report generators and application
generators which have simple, english-like syntax rules.
3 . Natural languages
Natural languages are designed to make a computer to behave like an expert and solve
problems. The programmer just needs to specify the problem and the constraints for problem-
solving. Natural languages such as LISP and PROLOG are mainly used to develop artificial
intelligence and expert systems. These languages are widely known as fifth generation
languages.
16. Explain the history of C ?
17. What is the general structure of a `C' program and explain with example?
Documentation section :
This section consists of a set of comment lines giving the name of the program, and other details.
which the programmer would like to user later.
Ex:- /*Addition of two numbers */
Link section: Link section provides instructions to the compiler to link functions from the
system library.
Ex:- # include<stdio.h>
#
include<conio.h>
Definition section: Definition section defines all symbolic constants.
Ex:- # define A 10.
Global declaration section: Some of the variables that are used in more than one function
throughout the program are called global variables and declared outside of all the functions. This
section declares all the user-defined functions.
Main() function section:
Every C program must have one main ( ) function section. This contains two parts.
i) Declaration part: This part declares all the variables used in the executable part.
Ex:- int a,b;
ii) Executable part: This part contains at least one statement .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. All the statements in the declaration and executable parts end with a
semicolon (;).
Sub program section: This 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.
Ex:
18. What is a variable? Write the rules for constructing a variable?
Ans: Variable: It is a data name that may be used to store a data value. It cannot be changed
during the execution of a program. A variable may take different values at different times during
execution. A variable name can be chosen by the programmer in a meaningful way so as to
reflect its function or nature in the program.
Rules: Variable names may consist of letters, digits and under score( _ ) character.
First char must be an alphabet or an ‗-‘
Length of the variable cannot exceed upto 8 characters, some C compilers can recognized
upto 31 characters.
White space is not allowed.
Variables name should not be a keyword.
Uppercase and lower case are significant.
Ex:- mark,sum1,tot_value,delhivalid
Prics$, group one, char invalid
The declaration of variables must be done before they are used in the program.
data-type v1,v2,…….,vn;
v1,v2,…,vn are the names of variables. Variables are separated by commas. A declaration
statement must end with a semicolon. For example , valid declarations are:
int count;
double ratio;
/*…………….Program Name……………..*/
main()
/*……………….Declaration…………………………..*/
float x,y;
int code;
shortint count;
longint amount;
double deviation;
unsigned n;
char c;
/*…………………………Computation…………………………*/
}/*…………………………Program ends…………………..*/
C does not initialize variables automatically. So if you do not initialize them properly, you
can get unexpected results. Fortunately, C makes it easy to initialize variables when you
declare them.
For Example :
int x=45;
Week-f = Monday
-
-
-
(or)
enum day{Monday…Sunday}week-f, week-end;
Note that the character constant‘5‘ is not same as the number 5. The last constant is a blank
space.Character constant has integer values known as ASCII values. For example, the statement
Printf(“%d”,a); would print the number 97,the ASCII value of the letter a. Similarly, the
statement printf(“%c”,97); would output the letter ‗a‘
Backslash Character Constants: C supports some special backslash character constants that are
used in output functions. Some of the back slash character constants are as follows:
Constant Meaning
‗\0‘ Null
‗\t‘ Horizontal tab
‗\b‘ Back space
‗\a‘ Audible alert
‗\f‘ Form feed
‗\n‘ New line
‗\r‘ Carriage return
‗\v‘ Vertical tab
‗\‘‘ Single quote
‗\‖‘ Double quote
‗\?‘ Question mark
‗\\‘ backslash
Integer division truncates any fractional part. The modulo division operation produces the
Ex:Here a and b are operands, let a=14 and b=4 we have the following results
a-b = 10
a+b = 18
a*b = 56
a/b = 3(coefficient)
a%b = 2(remainder)
2. Relational Operators:
Relational operators are used for comparing two quantities, and take certain decisions. For
example we may compare the age of two persons or the price of two items….these comparisons
can be done with the help of relational operators.
Ex:- 4.5<=10(true)
6.5<-10(false)
10<4+12(true)
When arithmetic expression are used on either side of a relational operator, the arithmetic
expression will be evaluated first and then the results compared, that means arithmetic operators
have a higher priority over relational operators.
3. Logical Operator:
C has 3 logical operators. The logical operators are used when we want to test more than one
condition and make decisions. The operators are as follows:
Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT
The logical operators && and || are used when we test more than one condition and make
decisions.
This expression combines two or more relational expressions, is termed as a logical expression
or a compound relational expression. The logical expression given above is true only if a>b is
true and x==10 is true. If both of them are false the expression is false. The truth table for logical
and and logical or operators are:
4. Assignment operator:
These operators are used to assign the result of an expression to a variable. The usual assignment
operator is „=‟. In addition ,C has a set of shorthand assignment operators of the form:
v op = exp;
v =v op (exp);
The use of short hand assignment operators has the following advantages:
1) What appears on the left- hand side need not be repeated and therefore it becomes easier
to write
++ and - - are increment and decrement operators in C. The operator ++ adds 1 to the operand,
while - - subtracts 1.Both are unary operators.
We use the increment and decrement statements in for and while loops
A prefix operator first adds 1 to the operand and then the result is assigned to the variable on left.
On the other hand, a postfix operator first assigns the value to the variable on left and then
increments the operand.
6. Conditional operator:
The operator?: works as follows: exp1 is evaluated first. If it is non-zero (true), then the
expression exp 2 is evaluated and becomes the value of the expression. If exp1 is false, exp3 is
evaluated and its value becomes the value of the expression.
X = (a>b) ?a:b;
7. Bitwise Operators:
C supports a special operator knows as bitwise operators for manipulation of data at bit level.
These operators are used for testing the bits, or shifting them right to left. Bitwise operators may
not be applied to float or double.
Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
<< Shift left
>> Shift right
8. Special operators:
C supports some special operators such as comma operator, size of operator, pointer operators
(& and *) and member selection operators (. and -> ).
comma operator: The comma operator is used to link the related expressions together. A
comma- linked list of expressions is evaluated left to right and the value of right- most
expression is the value of the combined expression. For example, the statement
This statement first assigns the value 10 to x, then assigns 5 to y and finally assigns 15.
sizeof operator: The sizeof is a compile time operator and when used with an operand, it
returns the number of bytes the operand occupies. The operand may be variable, a constant or a
data type qualifier.
m = sizeof (sum);
The sizeof operator is normally used to determine the lengths of arrays and structures when their
sizes are not known to the programmer. It is also used to allocate memory space dynamically to
variables during execution of a program.
Ans: Type conversions: converting a variable value or a constant value temporarily from one
data type to other data type for the purpose of calculation is known as type conversion.
1. Implicit: In this higher data type can be converted into lower data type automatically.
The figure below shows the C conversion hierarchy for implicit –type conversion in an
expression:
fig: Conversion hierarchy
The sequence of rules that are applied while implicit type conversion is as follows:
All short and char are automatically converted into int then
1. if one of the operands is long double the other will be converted to long double and the
result will be long double.
2. else, if one of the operand is double,the other will be converted to double and the result
will be double.
3. else, if one of the operand is float ,the other will be converted to float and the result will
be float.
4. else, if one of the operand is unsigned long int, the other will be converted to unsigned
long int and the result will be unsigned long int.
5. else, if one of the operand is long int, the other is unsigned int then
a) if unsigned int can be converted into long int, the unsigned int operand will be
converted as such and the result will be long int.
b) else both operands will be converted to unsigned long int and the result will be
unsigned long int.
6. else if one of the operand is long int ,the other will be converted into long int and the
result will be long int.
7. else if one of the operand unsigned int ,the other will be converted into unsigned int and
the result will be unsigned int.
The final result of an expression is converted to the type of the variable on the left of the
assignment sign before assigning the value to it. The following changes are introduced during the
final assignment.
long int to int causes dropping of the excess higher order bits
int i,x;
float f;
double d;
longint l;
2. Explicit: In this type of conversion, the programmer can convert one data type to other data
type explicitly.
y= cos(double(x))
double a = 6.5
double b = 6.5
int a=10
float(a)->10.00000
Ans: Expressions:
An expression in C is some combination of constants, variables, operators
and function calls.
Sample expressions are:
a+b
tan(angle)
• Most expressions have a value based on their contents.
• A statement in C is just an expression terminated with a semicolon.
For example:
sum = x + y + z;
printf("Go Buckeyes!");
The rules given below are used to evaluate an expression,
1) If an expression has parenthesis, sub expression within the parenthesis is
evaluated first and arithmetic expression without parenthesis is evaluated first.
2) The operators of high level precedence are evaluated first.
3)The operators at the same precedence are evaluated from left to right or right
to left depending on the associativity of operators.
Expressions are evaluated using an assignment statement of the form:
variable = expression;
variable is any valid C variable name. When the statement is encountered, the expression is
evaluated first and the result then replaces the previous value of the variable on the left-hand
side. All variables used in the expression must be assigned values before evaluation is attempted.
Ex:- x = a*b-c;
y = b/c*a;
z = a-b / c+d;
Ex:- x= a-b/3+c*2-1 when a=9, b=12, and c=3 the expression becomes.
x = 9-12/3 +3*2-1
Step1: x = 9-4+3*2-1
Step2: x = 9-4+6-1
Step3: x = 5+6-1
Step4: x = 11-1
Step5: x = 10
Important note:
1. Precedence rules decide the order in which different operators are applied
2. Associativity rule decides the order in which multiple occurrences of the same
level operators are applied.
Hierarchy of operators in C
There are some operator which are given bellow with their meaning. The higher the position
of an operator is, higher is its priority.
Operator Type
! Logical
*/ % Arithmetic
+- Arithmetic
<><=>= Relational
==!= Relational
&& Logical
|| Logical OR
= Assignment
Associativity of operators
When an expression contains two operators of equal priority the tie between them is settled using
the associativity of the operators.
Left to Right associativity means that the left operand must be unambiguous. Unambiguous in
what sense? It must not be involved in evaluation of any other sub-expression. Similarly, in case
of Right to Left associativity the right operand must be unambiguous. Let us understand this with
an example.
Consider expression a=3/2*5 Here there is a tie between operators of same priority, that is
between / and *. This tie is settled using the associativity of / and *. But both enjoy Left to Right
associativity.
While executing an arithmetic statement, which has two or more operators, we may have some
problem as to how exactly does it get executed.
3rd = Assignment
For Example :
i= 2*3/4+4/4+8-2+5/8
i=6/4+4+8-2+5/8
i=1+4/4+8-2+5/8
i=1+1+8-2+5/8
i=1+1+8-2+0
i=2+8-2+0
i=10-2+0
i=8+0
i=8