38
1 Mrs. V. S. Patki (Electronics Department) Walchand Institute Of Technology, Solapur Computer Programming 1. Explain Block diagram of computer. The basic components of a computer are: Input Unit Output Unit Memory / Storage Unit Arithmetic Logic Unit Control Unit Central Processing Unit When a computer is asked to do a job, it handles the task in a very special way: 1).It accepts the information from the user. This is called input. 2).It stored the information until it is ready for use. The computer has memory chips, which are designed to hold information until it is needed. 3).It processes the information. The computer has an electronic brain called the Central Processing Unit, which is responsible for processing all data and instructions given to the computer. 4).It then returns the processed information to the user. This is called output. Input Unit Input Unit accepts the instrunctions and data from the outside world. Then it converts these instrunctions and data in computer acceptable form. after that it supplies the converted instrunctions and data to the computer system for further processing.

Computer Programming · 2019. 4. 25. · 1 Mrs. V. S. Patki (Electronics Department) Walchand Institute Of Technology, Solapur Computer Programming 1. Explain Block diagram of computer

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Computer Programming · 2019. 4. 25. · 1 Mrs. V. S. Patki (Electronics Department) Walchand Institute Of Technology, Solapur Computer Programming 1. Explain Block diagram of computer

1 Mrs. V. S. Patki (Electronics Department)Walchand Institute Of Technology, Solapur

Computer Programming

1. Explain Block diagram of computer.

The basic components of a computer are:

∑ Input Unit∑ Output Unit∑ Memory / Storage Unit∑ Arithmetic Logic Unit∑ Control Unit∑ Central Processing Unit

When a computer is asked to do a job, it handles the task in a very special way:

1).It accepts the information from the user. This is called input.2).It stored the information until it is ready for use. The computer has memory chips, which are designed to hold information until it is needed.3).It processes the information. The computer has an electronic brain called the Central Processing Unit, which is responsible for processing all data and instructions given to the computer.4).It then returns the processed information to the user. This is called output.

Input Unit

Input Unit accepts the instrunctions and data from the outside world. Then it converts these instrunctions and data in computer acceptable form. after that it supplies the converted instrunctions and data to the computer system for further processing.

Page 2: Computer Programming · 2019. 4. 25. · 1 Mrs. V. S. Patki (Electronics Department) Walchand Institute Of Technology, Solapur Computer Programming 1. Explain Block diagram of computer

2 Mrs. V. S. Patki (Electronics Department)Walchand Institute Of Technology, Solapur

Output Unit

The output unit is just reverse of the input unit. it accepts the result produced by computer, which are in coded form and can't be easily understood by us. Then it converts these coded result into human readable form. After that it supplies the converted results to the outside world.

Memory Unit

The Memory Unit is the part of the computer that holds data and instructions for processing. Although it is closely associated with the CPU, in actual fact it is seperate from it. Memory associated with the CPU is also called primary storage, primary memory, main storage, internal storage and main memory.When we load software from a floppy disk, hard disk or CD-ROM, it is stored in the Main Memory.

There are two types of computer memory inside the computer:

∑ Primary Memory∑ Secondary Memory

Primary Memory

Primary storage, presently known as main memory, is the only one directly accessible to the CPU. The CPU continuously reads instructions stored there and executes them as required. Any data actively operated on is also stored there in uniform manner. However, the primary memory can hold information only while the computer system is ON. As soon as the computer system is switched off or rest, the information hold in primary memory disappears. Also it has limited storage capacity because it is very expensive. It is made up of semiconductor devices.

Secondary Memory

Secondary storage, sometimes called auxiliary storage, is all data storage that is not currently in a computer's primary storage or memory. This is computer memory that is not directly accessible to the processor but uses the I/O channels. It is for storing data not in active use and preserves data even without power, meaning it is non-volatile.In a personal computer, secondary storage typically consists of storage on the hard disk and on any removable media, if present, such as a CD or DVD.

Arithmetic Logical Unit

An arithmetic logic unit (ALU) is a digital circuit that performs arithmetic and logical operations. The ALU is a fundamental building block of the central processing unit (CPU) of a computer, and even the simplest microprocessors contain one for purposes such as maintaining timers. The processors found inside modern CPUs and graphics processing units (GPUs) accommodate very powerful and very complex ALUs; a single component may contain a number of ALUs. Most ALUs can perform the following operations:

1).Integer arithmetic operations (addition, subtraction, and sometimes multiplication and division).2).Bitwise logic operations (AND, NOT, OR, XOR)3).Bit-shifting operations (shifting or rotating a word by a specified number of bits to the left or right, with or without sign extension). Shifts can be interpreted as multiplications by 2 and divisions by 2.

Page 3: Computer Programming · 2019. 4. 25. · 1 Mrs. V. S. Patki (Electronics Department) Walchand Institute Of Technology, Solapur Computer Programming 1. Explain Block diagram of computer

3 Mrs. V. S. Patki (Electronics Department)Walchand Institute Of Technology, Solapur

Control Unit

The control unit (often called a control system or central controller) directs the various components of a computer. It reads and interprets (decodes) instructions in the program one by one. The control system decodes each instruction and turns it into a series of control signals that operate the other parts of the computer. Control systems in advanced computers may change the order of some instructions so as to improve performance. its functions are:

1.Read the code for the next instruction from the cell indicated by the program counter.2. Decode the numerical code for the instruction into a set of commands or signals for each of the other systems.3. Increment the program counter so it points to the next instruction.4. Read whatever data the instruction requires from cells in memory (or perhaps from an input device). The location of this required data is typically stored within the instruction code.5. Provide the necessary data to an ALU or register.6. If the instruction requires an ALU or specialized hardware to complete, instruct the hardware to perform the requested operation.7. Write the result from the ALU back to a memory location or to a register or perhaps an output device.8. Jump back to step (1).

Central Processing Unit

The control unit and ALU of a computer system are jointly known as the CPU. The CPU is the brain of computer system. It takes all calulations and comparisons in a computer systems and it s also responsible for activating and controlling the operations of other units of computer system.

2. Write a short note on C.

C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computerin 1972.

In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available description of C, now known as the K&R standard.

The UNIX operating system, the C compiler, and essentially all UNIX application programs have been written in C. C has now become a widely used professional language for various reasons −

∑ Easy to learn∑ Structured language∑ It produces efficient programs∑ It can handle low-level activities∑ It can be compiled on a variety of computer platforms

Facts about C

∑ C was invented to write an operating system called UNIX.∑ C is a successor of B language which was introduced around the early 1970s.

Page 4: Computer Programming · 2019. 4. 25. · 1 Mrs. V. S. Patki (Electronics Department) Walchand Institute Of Technology, Solapur Computer Programming 1. Explain Block diagram of computer

4 Mrs. V. S. Patki (Electronics Department)Walchand Institute Of Technology, Solapur

∑ The language was formalized in 1988 by the American National Standard Institute (ANSI).∑ The UNIX OS was totally written in C.∑ Today C is the most widely used and popular System Programming Language.∑ Most of the state-of-the-art software have been implemented using C.∑ Today's most popular Linux OS and RDBMS MySQL have been written in C.

Why use C?

C was initially used for system development work, particularly the programs that make-up the operating system. C was adopted as a system development language because it produces code that runs nearly as fast as the code written in assembly language. Some examples of the use of C might be −

∑ Operating Systems∑ Language Compilers∑ Assemblers∑ Text Editors∑ Print Spoolers∑ Network Drivers∑ Modern Programs∑ Databases∑ Language Interpreters∑ Utilities

Advantages or Importance of 'C' language.

1. Program written in 'C' are efficient and fast.2. Several standard build in functions are available which can be used for developing programs.3. 'C' is highly portable, programs written for one computer can be run on another with little or new modification.4. It is robest can be used to write any complex program.

3. Explain C tokens.

∑ In C Programming punctuation,individual words,characters etc are called tokens.∑ Tokens are basic building blocks of C Programming

Page 5: Computer Programming · 2019. 4. 25. · 1 Mrs. V. S. Patki (Electronics Department) Walchand Institute Of Technology, Solapur Computer Programming 1. Explain Block diagram of computer

5 Mrs. V. S. Patki (Electronics Department)Walchand Institute Of Technology, Solapur

Token Example :

Basic Building Blocks and Definition :

Token Meaning

Keyword

A variable is a meaningful name of data storage location in computer memory. When using a variable you refer to memory address of computer

Example: do , while, int, float

Constant

Constants are expressions with a fixed value ,

Types: Primary Constant : Integer, Float,Character

Secondary Constant :Array,Pointer,Structure,Union,Enum

Example : (interger -76 , 34) (float - 56.98)

Single Character Constant :Character Constant Can hold Single character at a time. ‘a’, ‘1’

String constant With Multiple Characters: "abcdefg"

Special Backslash Character Constants in C : ‘\a’ –Audibel alert

‘\b’- back space , ‘\n’- new line , ‘\0’ Null , ‘\t’- horizontal tab

IdentifierThe term identifier is usually used for variable names

Example: number , name , sum

StringSequence of characters

Example : “abcd” , “WIT”

Special SymbolSymbols other than the Alphabets and Digits and white-spaces

Example: *, % , #

OperatorsA symbol that represent a specific mathematical or non mathematical action

Example - ++, /

Page 6: Computer Programming · 2019. 4. 25. · 1 Mrs. V. S. Patki (Electronics Department) Walchand Institute Of Technology, Solapur Computer Programming 1. Explain Block diagram of computer

6 Mrs. V. S. Patki (Electronics Department)Walchand Institute Of Technology, Solapur

4. What are different rules for defining a variable in C?

A variable is an entity whose value keeps on changing throughout the program execution. A variable is the name given to the memory location. A variable name is an entity that points to a particular memory location. A variable is just a named area of storage that can hold a single value (numeric or character). The C language demands that you declare the name of each variable that you are going to use and its type, or class, before you actually try to do anything with it.

The Programming language C has two main variable types

∑ Local Variables∑ Global Variables

Rules for constructing variable names

1. A Variable name consists of any combination of alphabets, digits and underscores. Some compiler allows variable names whole length could be up to 247 characters. Still it would be safer to stick to the rule of 31 characters. Please avoid creating long variable name as it adds to your typing effort

2. The first character of the variable name must either be alphabet or underscore. It should not start with the digit

3. No commas and blanks are allowed in the variable name4. No special symbols other than underscore are allowed in the variable name

We need to declare the type of the variable name before making use of that name in the program. Type declaration can be done as follows:

To declare a variable as integer, follow the below syntax:

Data_type variable_name;

int variable_name;

//Here int is the type of the variable named variable_name. ‘int’ denotes integer type.

Following are the examples of type declaration statements:

int p, n;float r;

5. Explain basic data types in C.

C has a concept of 'data types' which are used to define a variable before its use. The definition of a variable will assign storage for the variable and define the type of data that will be held in the location.The value of a variable can be changed any time.

C has the following basic built-in datatypes.

∑ int

Page 7: Computer Programming · 2019. 4. 25. · 1 Mrs. V. S. Patki (Electronics Department) Walchand Institute Of Technology, Solapur Computer Programming 1. Explain Block diagram of computer

7 Mrs. V. S. Patki (Electronics Department)Walchand Institute Of Technology, Solapur

∑ float∑ double∑ char

int - data type -- int is used to define integer numbers. { int Count;

Count = 5; }

float - data type-- float is used to define floating point numbers.{ float Miles;

Miles = 5.6; }

double - data type --double is used to define BIG floating point numbers. It reserves twice the storage for the number. On PCs this is likely to be 8 bytes.

{ double Atoms;Atoms = 2500000; }

char - data type-- char defines characters.

{char Letter;Letter = 'x';

}

Modifiers--- The data types explained above have the following modifiers.

∑ short∑ long∑ signed∑ unsigned

The modifiers define the amount of storage allocated to the variable. The amount of storage allocated is not cast in stone. ANSI has the following rules:

short int <= int <= long intfloat <= double <= long double

Type Bytes Range---------------------------------------------------------------------

short int 2 -32,768 -> +32,767 (32kb)unsigned short int 2 0 -> +65,535 (64Kb)

unsigned int 4 0 -> +4,294,967,295 ( 4Gb)int 4 -2,147,483,648 -> +2,147,483,647 ( 2Gb)

long int 4 -2,147,483,648 -> +2,147,483,647 ( 2Gb)signed char 1 -128 -> +127

unsigned char 1 0 -> +255float 4 double 8

long double 12

Page 8: Computer Programming · 2019. 4. 25. · 1 Mrs. V. S. Patki (Electronics Department) Walchand Institute Of Technology, Solapur Computer Programming 1. Explain Block diagram of computer

8 Mrs. V. S. Patki (Electronics Department)Walchand Institute Of Technology, Solapur

Here is an example to check size of memory taken by various datatypes.main(){

printf("sizeof(char) == %d\n", sizeof(char));printf("sizeof(short) == %d\n", sizeof(short));printf("sizeof(int) == %d\n", sizeof(int));printf("sizeof(long) == %d\n", sizeof(long));printf("sizeof(float) == %d\n", sizeof(float));printf("sizeof(double) == %d\n", sizeof(double));printf("sizeof(long double) == %d\n", sizeof(long double));printf("sizeof(long long) == %d\n", sizeof(long long));

}

6. Explain the various types of operators with example.

7. Explain following control structures 1.if else 2. For loop 3. While loop

if else statement

Decision Control Statements and Flowchart

The if StatementIt is used to execute an instruction or sequence/block of instruction only if a condition is fulfilled.Difference forms of implements if-statement are:

∑ Simple if statement∑ if-else statement∑ Nested if-else statement∑ else if statement

Figure: Simple if statement syntax and flowchart

Page 9: Computer Programming · 2019. 4. 25. · 1 Mrs. V. S. Patki (Electronics Department) Walchand Institute Of Technology, Solapur Computer Programming 1. Explain Block diagram of computer

9 Mrs. V. S. Patki (Electronics Department)Walchand Institute Of Technology, Solapur

Figure: if-else statement syntax and flowchart

Nested if-else statementIn nested if...else statement, an entire if...else construct is written within either the body of the if statement or the body of an else statement.

The syntax is as follows:if(condition_1){if(condition_2){block statement_1;

}else{block statement_2;

}}else{block statement_3;

}block statement_4;

Else if statement

Page 10: Computer Programming · 2019. 4. 25. · 1 Mrs. V. S. Patki (Electronics Department) Walchand Institute Of Technology, Solapur Computer Programming 1. Explain Block diagram of computer

10 Mrs. V. S. Patki (Electronics Department)Walchand Institute Of Technology, Solapur

It is used in multi-way decision on several conditions. This works by cascading comparisons. As soon as one of the conditions is true, the statement or block of statements following them is executed and no further comparison are performed.The else...if syntax is as follows:

if(condition_1)block statement_1;

else if(condition_2)block statement_2;

else if(condition_n)block statement_n;

elsedefault statement;

For loop -- A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

Syntax: for ( init; condition; increment )

{statement(s);

}

Here is the flow of control in a 'for' loop −

∑ The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.

∑ Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and the flow of control jumps to the next statement just after the 'for' loop.

∑ After the body of the 'for' loop executes, the flow of control jumps back up to the incrementstatement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.

Page 11: Computer Programming · 2019. 4. 25. · 1 Mrs. V. S. Patki (Electronics Department) Walchand Institute Of Technology, Solapur Computer Programming 1. Explain Block diagram of computer

11 Mrs. V. S. Patki (Electronics Department)Walchand Institute Of Technology, Solapur

∑ The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the 'for' loop terminates.

Flow Diagram

Example

#include <stdio.h>

void main (){

int a;

/* for loop execution */for( a = 10; a < 20; a = a + 1 ){

printf("value of a: %d\n", a);}

}

When the above code is compiled and executed, it produces the following result −

value of a: 10value of a: 11value of a: 12value of a: 13value of a: 14value of a: 15

Page 12: Computer Programming · 2019. 4. 25. · 1 Mrs. V. S. Patki (Electronics Department) Walchand Institute Of Technology, Solapur Computer Programming 1. Explain Block diagram of computer

12 Mrs. V. S. Patki (Electronics Department)Walchand Institute Of Technology, Solapur

value of a: 16value of a: 17value of a: 18value of a: 19

While loop--- A while loop in C programming repeatedly executes a target statement as long as a given condition is true.

Syntax while(condition)

{statement(s);

}

Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any nonzero value. The loop iterates while the condition is true.When the condition becomes false, the program control passes to the line immediately following the loop.

Flow Diagram

Here, the key point to note is that a while loop might not execute at all. When the condition is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.

Example

#include <stdio.h>

void main () {

/* local variable definition */int a = 10;

/* while loop execution */while( a < 20 ) {printf("value of a: %d\n", a);a++;}

Page 13: Computer Programming · 2019. 4. 25. · 1 Mrs. V. S. Patki (Electronics Department) Walchand Institute Of Technology, Solapur Computer Programming 1. Explain Block diagram of computer

13 Mrs. V. S. Patki (Electronics Department)Walchand Institute Of Technology, Solapur

}

When the above code is compiled and executed, it produces the following result −

value of a: 10value of a: 11value of a: 12value of a: 13value of a: 14value of a: 15value of a: 16value of a: 17value of a: 18value of a: 19

do...while loop ---- In C, do...while loop is very similar to while loop. Only difference between these two loops is that, in while loops, test expression is checked at first but, in do...while loop code is executed at first then the condition is checked. So, the code are executed at least once in do...while loops.

Syntax of do...while loops

do {

some code/s;

}while (test expression);

At first codes inside body of do is executed. Then, the test expression is checked. If it is true, code/s inside body of do are executed again and the process continues until test expression becomes false(zero).

Notice, there is semicolon in the end of while (); in do...while loop.

Example of do...while loop

Write a C program to add all the numbers entered by a user until user enters 0.

Page 14: Computer Programming · 2019. 4. 25. · 1 Mrs. V. S. Patki (Electronics Department) Walchand Institute Of Technology, Solapur Computer Programming 1. Explain Block diagram of computer

14 Mrs. V. S. Patki (Electronics Department)Walchand Institute Of Technology, Solapur

/*C program to demonstrate the working of do...while statement*/#include <stdio.h>Void main(){

int sum=0,num;do */ Codes inside the body of do...while loops are at least executed once.*/

{printf("Enter a number\n");scanf("%d",&num);sum+=num;

}while(num!=0);printf("sum=%d",sum);

}

Output

Enter a number3Enter a number-2Enter a number0sum=1

In this C program, user is asked a number and it is added with sum. Then, only the test condition in the do...while loop is checked. If the test condition is true,i.e, num is not equal to 0, the body of do...while loop is again executed until num equals to zero.

8. What is difference between while and do while loop?9. Explain the use of break statement with example.

There are two statements built in C programming, break; and continue; to alter the normal flow of a program. Loops perform a set of repetitive task until text expression becomes false but it is sometimes desirable to skip some statement/s inside loop or terminate the loop immediately without checking the test expression. In such cases, break and continue statements are used. The break; statement is also used in switch statement to exit switch statement.

break Statement : In C programming, break is used in terminating the loop immediately after it is encountered. The break statement is used with conditional if statement.

Syntax of break statement: break;

The break statement can be used in terminating all three loops for, while and do...while loops.

Page 15: Computer Programming · 2019. 4. 25. · 1 Mrs. V. S. Patki (Electronics Department) Walchand Institute Of Technology, Solapur Computer Programming 1. Explain Block diagram of computer

15 Mrs. V. S. Patki (Electronics Department)Walchand Institute Of Technology, Solapur

The figure below explains the working of break statement in all three type of loops.

Example of break statement

#include <stdio.h>

void main (){

int a = 10; /* local variable definition */while( a < 20 ) /* while loop execution */{

printf("value of a: %d\n", a);a++;if( a > 15){break; /* terminate the loop using break statement */}

}

}

When the above code is compiled and executed, it produces the following result −

Page 16: Computer Programming · 2019. 4. 25. · 1 Mrs. V. S. Patki (Electronics Department) Walchand Institute Of Technology, Solapur Computer Programming 1. Explain Block diagram of computer

16 Mrs. V. S. Patki (Electronics Department)Walchand Institute Of Technology, Solapur

value of a: 10value of a: 11value of a: 12value of a: 13value of a: 14value of a: 15

10. Explain the use of continue statement with example.

It is sometimes desirable to skip some statements inside the loop. In such cases, continue statements are used.

Syntax of continue Statement

continue;

Just like break, continue is also used with conditional if statement.

For better understanding of how continue statements works in C programming. Analyze the figure below which bypasses some code/s inside loops using continue statement.

Page 17: Computer Programming · 2019. 4. 25. · 1 Mrs. V. S. Patki (Electronics Department) Walchand Institute Of Technology, Solapur Computer Programming 1. Explain Block diagram of computer

17 Mrs. V. S. Patki (Electronics Department)Walchand Institute Of Technology, Solapur

Example of continue statement

Write a C program to find the product of 4 integers entered by a user. If user enters 0 skip it.

//program to demonstrate the working of continue statement in C programming # include <stdio.h>void main(){

int i,num,product;for(i=1,product=1;i<=4;++i){

printf("Enter num%d:",i);scanf("%d",&num);if(num==0)continue; / *In this program, when num equals to zero, it

skips the statement product*=num and continue the loop. */

product*=num;}

printf("product=%d",product);}

Output

Enter num1:3Enter num2:0Enter num3:-5Enter num4:2product=-30

11. Explain the use of goto statement with example.

In C programming, goto statement is used for altering the normal sequence of program execution by transferring control to some other part of the program.

Syntax of goto statement

goto label;.......................................label: statement;

In this syntax, label is an identifier. When, the control of program reaches to goto statement, the control of the program will jump to the label: and executes the code below it.

Page 18: Computer Programming · 2019. 4. 25. · 1 Mrs. V. S. Patki (Electronics Department) Walchand Institute Of Technology, Solapur Computer Programming 1. Explain Block diagram of computer

18 Mrs. V. S. Patki (Electronics Department)Walchand Institute Of Technology, Solapur

Example of goto statement

/* C program to demonstrate the working of goto statement. *//* This program calculates the average of numbers entered by user. *//* If user enters negative number, it ignores that number and

calculates the average of number entered before it.*/

# include <stdio.h>main(){

float num,average,sum;int i,n;printf("Maximum no. of inputs: ");scanf("%d",&n);for(i=1;i<=n;++i){

printf("Enter n%d: ",i);scanf("%f",&num);if(num<0.0)goto jump; /* control of the program moves to label jump */sum=sum+num;

}jump:average=sum/(i-1);printf("Average: %.2f",average);

}

Output

Maximum no. of inputs: 4Enter n1: 1.5Enter n2: 12.5Enter n3: 7.2Enter n4: -1Average: 7.07

Page 19: Computer Programming · 2019. 4. 25. · 1 Mrs. V. S. Patki (Electronics Department) Walchand Institute Of Technology, Solapur Computer Programming 1. Explain Block diagram of computer

19 Mrs. V. S. Patki (Electronics Department)Walchand Institute Of Technology, Solapur

12. Explain switch case with example.

Decision making are needed when, the program encounters the situation to choose a particular statement among many statements. If a programmer has to choose one block of statement among many alternatives, nested if...else can be used but, this makes programming logic complex. This type of problem can be handled in C programming using switchstatement.

Syntax of switch...case

switch case statement:

switch( choice ){

case 1:statement 1;break;

case 2:statement 2;break;

case n:statement n;break;

default:

Code to execute if <variable> does not equal the value following any of the cases

break;}(in above module n = number of cases.)

The following rules apply to a switchstatement −

∑ The expression used in a switch statement must have an integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type.

∑ You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.

∑ The constant-expression for a case must be the same data type as the variable in the switch, and it must be a constant or a literal.

∑ When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.

∑ When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.

∑ Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.

Page 20: Computer Programming · 2019. 4. 25. · 1 Mrs. V. S. Patki (Electronics Department) Walchand Institute Of Technology, Solapur Computer Programming 1. Explain Block diagram of computer

20 Mrs. V. S. Patki (Electronics Department)Walchand Institute Of Technology, Solapur

∑ A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.

Example of switch...case statement

Write a program that asks user an arithmetic operator('+','-','*' or '/') and two operands and perform the corresponding calculation on the operands.

/* C program to demonstrate the working of switch...case statement *//* C Program to create a simple calculator for addition, subtraction,

multiplication and division */

# include <stdio.h>main(){

char ch;float num1,num2;printf("Select an operator either + or - or * or / \n");scanf("%c",&ch);printf("Enter two operands: ");scanf("%f%f",&num1,&num2);switch(ch){

case '+':printf("%.1f + %.1f = %.1f",num1, num2, num1+num2);break;

case '-':printf("%.1f - %.1f = %.1f",num1, num2, num1-num2);break;

case '*':printf("%.1f * %.1f = %.1f",num1, num2, num1*num2);break;

case '/':printf("%.1f / %.1f = %.1f",num1, num2, num1/num2);break;

default:/* If operator is other than +, -, * or /, error message is

shown */printf("Error! operator is not correct");break;

}}

Output

Enter operator either + or - or * or / *

Page 21: Computer Programming · 2019. 4. 25. · 1 Mrs. V. S. Patki (Electronics Department) Walchand Institute Of Technology, Solapur Computer Programming 1. Explain Block diagram of computer

21 Mrs. V. S. Patki (Electronics Department)Walchand Institute Of Technology, Solapur

Enter two operands: 2.34.52.3 * 4.5 = 10.3

The break statement at the end of each case cause switch statement to exit. If break statement is not used, all statements below that case statement are also executed.

13. What is array? Explain one and two dimensional array with example.

In C programming, one of the frequently arising problem is to handle similar types of data. For example: If the user want to store marks of 100 students. This can be done by creating 100 variable individually but, this process is rather tedious and impracticable. These type of problem can be handled in C programming using arrays.

An array is a sequence of data item of homogeneous value(same type).

Arrays are of two types:

1. One-dimensional arrays2. Multidimensional arrays

Declaration of one-dimensional array

data_type array_name[array_size];

For example: int age[5];

Here, the name of array is age. The size of array is 5,i.e., there are 5 items(elements) of array age. All element in an array are of the same type (int, in this case).

Array elements

Size of array defines the number of elements in an array. Each element of array can be accessed and used by user according to the need of program. For example:

int age[5];

Note that, the first element is numbered 0 and so on.

Here, the size of array age is 5 times the size of int because there are 5 elements.

Suppose, the starting address of age[0] is 2120d and the size of int be 4 bytes. Then, the next address (address of a[1]) will be 2124d, address of a[2] will be 2128d and so on.

Page 22: Computer Programming · 2019. 4. 25. · 1 Mrs. V. S. Patki (Electronics Department) Walchand Institute Of Technology, Solapur Computer Programming 1. Explain Block diagram of computer

22 Mrs. V. S. Patki (Electronics Department)Walchand Institute Of Technology, Solapur

Initialization of one-dimensional array:

1. Compile time initialization - Arrays can be initialized at declaration time in this source code as:

int age[5]={2,4,34,3,4};

It is not necessary to define the size of arrays during initialization.

int age[]={2,4,34,3,4};

In this case, the compiler determines the size of array by calculating the number of elements of an array.

2. Dynamic initialization or run time initialization –

For this scanf funtion is used in for loop to initialize all the elements of array.

int age[5];

for(i=0,i<5,i++)

{

scanf( “%d”,&age[i]);

}

Accessing array elements

In C programming, arrays can be accessed and treated like variables in C.

For example:

scanf("%d",&age[2]); // run time initialization of array element/* statement to insert value in the third element of array age[]. */

printf("%d",age[0]); /* statement to print first element of an array. */

printf("%d",age[i]); /* statement to print (i+1)th element of an array. */

Example of array in C programming

/* C program to find the sum marks of n students using arrays */

Page 23: Computer Programming · 2019. 4. 25. · 1 Mrs. V. S. Patki (Electronics Department) Walchand Institute Of Technology, Solapur Computer Programming 1. Explain Block diagram of computer

23 Mrs. V. S. Patki (Electronics Department)Walchand Institute Of Technology, Solapur

#include <stdio.h>void main(){

int marks[10],i,n,sum=0;printf("Enter number of students: ");scanf("%d",&n);for(i=0;i<n;++i){

printf("Enter marks of student%d: ",i+1);scanf("%d",&marks[i]);sum+=marks[i];

}printf("Sum= %d",sum);

}

Output

Enter number of students: 3Enter marks of student1: 12Enter marks of student2: 31Enter marks of student3: 2sum=45

Multidimensional arrays ---

C programming language allows programmer to create arrays of arrays known as multidimensional arrays. For example:

float a[2][6];

Here, a is an array of two dimension, which is an example of multidimensional array.

For better understanding of multidimensional arrays, array elements of above example can be thinked of as below:

Initialization of Multidimensional Arrays

Page 24: Computer Programming · 2019. 4. 25. · 1 Mrs. V. S. Patki (Electronics Department) Walchand Institute Of Technology, Solapur Computer Programming 1. Explain Block diagram of computer

24 Mrs. V. S. Patki (Electronics Department)Walchand Institute Of Technology, Solapur

In C, multidimensional arrays can be initialized in different number of ways.

int c[2][3]={{1,3,0}, {-1,5,9}};OR

int c[][3]={{1,3,0}, {-1,5,9}};OR

int c[2][3]={1,3,0,-1,5,9};

Initialization Of three-dimensional Array

double cprogram[3][2][4]={ {{-0.1, 0.22, 0.3, 4.3}, {2.3, 4.7, -0.9, 2}},{{0.9, 3.6, 4.5, 4}, {1.2, 2.4, 0.22, -1}},{{8.2, 3.12, 34.2, 0.1}, {2.1, 3.2, 4.3, -2.0}}

};

Suppose there is a multidimensional array arr[i][j][k][m]. Then this array can hold i*j*k*m numbers of data.

Similarly, the array of any dimension can be initialized in C programming.

Example of Multidimensional Array In C

Write a C program to find sum of two matrix of order 2*2 using multidimensional arrays where, elements of matrix are entered by user.

#include <stdio.h>void main(){

float a[2][2], b[2][2], c[2][2];int i,j;printf("Enter the elements of 1st matrix\n");

/* Reading two dimensional Array with the help of two for loop. If there was an array of 'n' dimension, 'n' numbers of loops are needed for inserting data to array.*/

for(i=0;i<2;++i)for(j=0;j<2;++j){printf("Enter a%d%d: ",i+1,j+1);scanf("%f",&a[i][j]);

}printf("Enter the elements of 2nd matrix\n");for(i=0;i<2;++i)

for(j=0;j<2;++j){printf("Enter b%d%d: ",i+1,j+1);scanf("%f",&b[i][j]);}

Page 25: Computer Programming · 2019. 4. 25. · 1 Mrs. V. S. Patki (Electronics Department) Walchand Institute Of Technology, Solapur Computer Programming 1. Explain Block diagram of computer

25 Mrs. V. S. Patki (Electronics Department)Walchand Institute Of Technology, Solapur

for(i=0;i<2;++i)for(j=0;j<2;++j){

/* Writing the elements of multidimensional array using loop. */c[i][j]=a[i][j]+b[i][j]; /* Sum of corresponding elements of two arrays. */}

printf("\nSum Of Matrix:");for(i=0;i<2;++i)

for(j=0;j<2;++j){

printf("%.1f\t",c[i][j]);if(j==1) /* To display matrix sum in order. */

printf("\n");}

}

Ouput

Enter the elements of 1st matrixEnter a11: 2;Enter a12: 0.5;Enter a21: -1.1;Enter a22: 2;Enter the elements of 2nd matrixEnter b11: 0.2;Enter b12: 0;Enter b21: 0.23;Enter b22: 23;

Sum Of Matrix:2.2 0.5-0.9 25.0

14. How the elements of one dimensional array are accessed using pointer with example.

#include <stdio.h>void main(){

int data[5], i;printf("Enter elements: ");for(i=0;i<5;++i)

scanf("%d",data+i);printf("You entered: ");for(i=0;i<5;++i)

printf("%d\n",*(data+i));}

Output

Page 26: Computer Programming · 2019. 4. 25. · 1 Mrs. V. S. Patki (Electronics Department) Walchand Institute Of Technology, Solapur Computer Programming 1. Explain Block diagram of computer

26 Mrs. V. S. Patki (Electronics Department)Walchand Institute Of Technology, Solapur

Enter elements: 12354You entered: 12354

15. What is function? Why there is need of writing functions.

In programming, a function is a segment that groups code to perform a specific task.A C program has at least one function main( ). Without main() function, there is technically no C program.

Types of C functions: There are two types of functions in C programming:

∑ Library function∑ User defined function

Library function

Library functions are the in-built function in C programming system. For example:

main() - The execution of every C program starts from this main()function.

printf() - prinf() is used for displaying output in C.scanf() - scanf() is used for taking input in C.

User defined function

C allows programmer to define their own function according to their requirement. These types of functions are known as user-defined functions. Suppose, a programmer wants to find factorial of a number and check whether it is prime or not in same program. Then, he/she can create two separate user-defined functions in that program:

#include <stdio.h>void function_name()// function declaration(prototype) and defination{................................}main(){......................function_name(); // function call

Page 27: Computer Programming · 2019. 4. 25. · 1 Mrs. V. S. Patki (Electronics Department) Walchand Institute Of Technology, Solapur Computer Programming 1. Explain Block diagram of computer

27 Mrs. V. S. Patki (Electronics Department)Walchand Institute Of Technology, Solapur

...........

...........}

Every C program begins from main() and program starts executing the codes inside main() function. When the control of program reaches to function_name() inside main() function. The control of program jumps to void function_name() and executes the codes inside it. When all the codes inside that user-defined function are executed, control of the program jumps to the statement just after function_name() from where it is called. Analyze the figure below for understanding the concept of function in C programming.

Remember, the function name is an identifier and should be unique.

Advantages of user defined functions

1.User defined functions helps to decompose the large program into small segments which makes programmer easy to understand, maintain and debug.

2.If repeated code occurs in a program. Function can be used to include those codes and execute when needed by calling that function.

3. Programmer working on large project can divide the workload by making different functions.

Function prototype(declaration):

Every function in C programming should be declared before they are used. These type of declaration are also called function prototype. Function prototype gives compiler information about function name, type of arguments to be passed and return type.

Syntax of function prototype

return_type function_name(type(1) argument(1),....,type(n) argument(n));

1. name of the function is add()2. return type of the function is int.3. two arguments of type int are passed to function.

Page 28: Computer Programming · 2019. 4. 25. · 1 Mrs. V. S. Patki (Electronics Department) Walchand Institute Of Technology, Solapur Computer Programming 1. Explain Block diagram of computer

28 Mrs. V. S. Patki (Electronics Department)Walchand Institute Of Technology, Solapur

Function prototype are not needed if user-definition function is written before main() function.

Function call: Control of the program cannot be transferred to user-defined function unless it is called invoked.

Syntax of function call: function_name(argument(1),....argument(n));

In the above example, function call is made using statement add(num1,num2); from main(). Thismake the control of program jump from that statement to function definition and executes the codes inside that function.

Function definition: Function definition contains programming codes to perform specific task.

Syntax of function definitionreturn_type function_name(type(1) argument(1),..,type(n) argument(n)){

//body of function}Example—/*Program to demonstrate the working of user defined function*/#include <stdio.h>int add(int a, int b); //function prototype(declaration)void main(){

int num1,num2,sum;printf("Enters two number to add\n");scanf("%d %d",&num1,&num2);sum=add(num1,num2); //function call printf("sum=%d",sum);

}

int add(int a,int b) //function declaration{/* Start of function definition. */

int add;add=a+b;return add; //return statement of function

/* End of function definition. */}

16. What do you mean by structures? Explain with example.

Structure is the collection of variables of different types under a single name for better handling. For example: You want to store the information about person about his/her name, citizenship number and salary. You can create these information separately but, better approach will be collection of these information under single name because all these information are related to person.

Structure Definition in C: Keyword struct is used for creating a structure.

Page 29: Computer Programming · 2019. 4. 25. · 1 Mrs. V. S. Patki (Electronics Department) Walchand Institute Of Technology, Solapur Computer Programming 1. Explain Block diagram of computer

29 Mrs. V. S. Patki (Electronics Department)Walchand Institute Of Technology, Solapur

Syntax of structure: struct structure_name

{data_type member1;data_type member2;data_type memeber;

};We can create the structure for a person as mentioned above as:struct person{

char name[50];int cit_no;float salary;

};

This declaration above creates the derived data type struct person.

Structure variable declaration

When a structure is defined, it creates a user-defined type but, no storage is allocated. For the above structure of person, variable can be declared as:

struct person{

char name[50];int cit_no;float salary;

};

//Inside main function:struct person p1, p2, p[20];

Another way of creating sturcture variable is:

struct person{

char name[50];int cit_no;float salary;

}p1 ,p2 ,p[20];

In both cases, 2 variables p1, p2 and array p having 20 elements of type struct person are created.

Accessing members of a structure

There are two types of operators used for accessing members of a structure.

1. Member operator (.)2. Structure pointer operator (->)

Page 30: Computer Programming · 2019. 4. 25. · 1 Mrs. V. S. Patki (Electronics Department) Walchand Institute Of Technology, Solapur Computer Programming 1. Explain Block diagram of computer

30 Mrs. V. S. Patki (Electronics Department)Walchand Institute Of Technology, Solapur

3. Any member of a structure can be accessed as: structure_variable_name.member_name

Suppose, we want to access salary for variable p2. Then, it can be accessed as:

p2.salary OR p1. City_no

Example of structure

Write a C program to add two distances entered by user. Measurement of distance should be in inch and feet.(Note: 12 inches = 1 foot)

#include <stdio.h>struct Distance{

int feet;float inch;

}d1,d2,sum;void main(){

printf("1st distance\n");printf("Enter feet: ");scanf("%d",&d1.feet); /* input of feet for structure variable d1 */printf("Enter inch: ");scanf("%f",&d1.inch); /* input of inch for structure variable d1 */printf("2nd distance\n");printf("Enter feet: ");scanf("%d",&d2.feet); /* input of feet for structure variable d2 */printf("Enter inch: ");scanf("%f",&d2.inch); /* input of inch for structure variable d2 */sum.feet=d1.feet+d2.feet;sum.inch=d1.inch+d2.inch;if (sum.inch>12){ //If inch is greater than 12, changing it to feet.

++sum.feet;sum.inch=sum.inch-12;

}printf("Sum of distances=%d\'-%.1f\"",sum.feet,sum.inch);

/* printing sum of distance d1 and d2 */}

Output

1st distanceEnter feet: 12Enter inch: 7.92nd distanceEnter feet: 2Enter inch: 9.8

Page 31: Computer Programming · 2019. 4. 25. · 1 Mrs. V. S. Patki (Electronics Department) Walchand Institute Of Technology, Solapur Computer Programming 1. Explain Block diagram of computer

31 Mrs. V. S. Patki (Electronics Department)Walchand Institute Of Technology, Solapur

Sum of distances= 15'-5.7"

17. Give difference between structure and union with example.

Structure Union1.The keyword struct is used to define a structure

Example structure declaration:

struct s_tag {

int ival;float fval;char *cptr;

}s;

1. The keyword union is used to define a union.

Example union declaration:

union u_tag {

int ival;float fval;char *cptr;

}u;

2. When a variable is associated with a structure, the compiler allocates the memory for each member. The size of structure is greater than or equal to the sum of sizes of its members. The smaller members may end with unused slack bytes.

2. When a variable is associated with a union, the compiler allocates the memory by considering the size of the largest memory. So, size of union is equal to the size of largest member.

3. Each member within a structure is assigned unique storage area of location.

3. Memory allocated is shared by individual members of union.

4. The address of each member will be in ascending order This indicates that memory for each member will start at different offset values.

4. The address is same for all the members of a union. This indicates that every member begins at the same offset value.

5 Altering the value of a member will not affect other members of the structure.

5. Altering the value of any of the member will alter other member values.

6. Individual member can be accessed at a time 6. Only one member can be accessed at a time.7. Several members of a structure can initialize at once.

7. Only the first member of a union can be initialized.

18. What is pointer? Explain the & and * operators.

Pointers are the powerful feature of C and (C++) programming, Pointer is variable which stores address of another variable.

Pointers are used in C program to access the memory and manipulate the address.

Reference operator(&) If var is a variable then, &var is the address in memory.

/* Example to demonstrate use of reference operator in C programming. */#include <stdio.h>void main(){

int var=5;

Page 32: Computer Programming · 2019. 4. 25. · 1 Mrs. V. S. Patki (Electronics Department) Walchand Institute Of Technology, Solapur Computer Programming 1. Explain Block diagram of computer

32 Mrs. V. S. Patki (Electronics Department)Walchand Institute Of Technology, Solapur

printf("Value: %d\n",var);printf("Address: %d",&var); //Notice, the ampersand(&) before var.

}

Output

Value: 5 Address: 2686778

In above source code, value 5 is stored in the memory location 2686778. var is just the name given to that location.

Reference operator(*) and Pointer variables

Pointer variable or simply pointer are the special types of variables that holds memory address rather than data, that is, a variable that holds address value is called a pointer variable or simply a pointer.

Declaration of Pointer

data_type* pointer_variable_name;int * p;

Above statement defines, p as pointer variable of type int.

Initialization of Pointer:

Pointer_variable_name = & other_variable_name;

int a =10;

p=&a;

Example To Demonstrate Working of Pointers

/* Source code to demonstrate, handling of pointers in C program */#include <stdio.h>void main(){

int* pc;int c;c=22;printf("Address of c:%d\n",&c);printf("Value of c:%d\n\n",c);pc=&c;printf("Address of pointer pc:%d\n",pc);printf("Content of pointer pc:%d\n\n",*pc);c=11;

Page 33: Computer Programming · 2019. 4. 25. · 1 Mrs. V. S. Patki (Electronics Department) Walchand Institute Of Technology, Solapur Computer Programming 1. Explain Block diagram of computer

33 Mrs. V. S. Patki (Electronics Department)Walchand Institute Of Technology, Solapur

printf("Address of pointer pc:%d\n",pc);printf("Content of pointer pc:%d\n\n",*pc);*pc=2;printf("Address of c:%d\n",&c);printf("Value of c:%d\n\n",c);

}

Output

Address of c: 2686784Value of c: 22

Address of pointer pc: 2686784Content of pointer pc: 22

Address of pointer pc: 2686784Content of pointer pc: 11

Address of c: 2686784Value of c: 2

19. Explain the difference between call by value and call by reference with example.

Difference between call by value and call by reference

call by value call by reference

In call by value, a copy of actual arguments is passed to formal arguments of the called function and any change made to the formal arguments in the called function have no effect on the values of actual arguments in the calling function.

In call by reference, the location (address) of actual arguments is passed to formal arguments of the called function. This means by accessing the addresses of actual arguments we can alter them within from the called function.

In call by value, actual arguments will remain safe, they cannot be modified accidentally.

In call by reference, alteration to actual arguments is possible within from called function; therefore the code must handle arguments carefully else you get unexpected results.

#include <stdio.h>void swapByValue(int, int);

/*Prototype */

int main() /* Main function */{int n1 = 10, n2 = 20;

/* actual arguments will be as it is */swapByValue(n1, n2); //function call

printf("n1: %d, n2: %d\n", n1, n2);}

#include <stdio.h>void swapByReference(int*, int*);/* Prototype */

int main() /* Main function */{

int n1 = 10, n2 = 20;

/* actual arguments will be altered */swapByReference(&n1, &n2);//function call

printf("n1: %d, n2: %d\n", n1, n2);}

Page 34: Computer Programming · 2019. 4. 25. · 1 Mrs. V. S. Patki (Electronics Department) Walchand Institute Of Technology, Solapur Computer Programming 1. Explain Block diagram of computer

34 Mrs. V. S. Patki (Electronics Department)Walchand Institute Of Technology, Solapur

//function definationvoid swapByValue(int a, int b){int t;t = a; a = b; b = t;

}OUTPUT======n1: 10, n2: 20

//function definationvoid swapByReference(int *a, int *b){

int t;t = *a; *a = *b; *b = t;

}OUTPUT

======n1: 20, n2: 10

In call by value , both the actual and formal parameters will be created in different memory locations.and it is sending the value to the called fuction.

In call by reference , both the actual and formal paramerers will be created at the same location and it is sending the address of variables to the called function.

20. Explain algorithm and flowchart.Algorithm

An algorithm is a finite set of steps defining the solution of a particular problem. An algorithm can be expressed in human readable language like as English. Algorithm is language depended, well structured and detailed. OR

1. A sequential solution of any program that written in human language,called algorithm.2. Algorithm is first step of the solution process, after the analysis of problem, programmer write the

algorithm of that problem.

Rules for constructing an Algorithm

When you are going to create an algorithms, keep following point in mind as:

∑ Input: There should be zero or more values which are to be supplied.∑ Output: At least one result is to be produced.∑ Definiteness: Each step must be clear and unambiguous.∑ Finiteness: If we trace the steps of an algorithm, then for all cases, the algorithm must terminate

after a finite number of steps.∑ Effectiveness: Each step must be sufficiently basic that a person using only paper and pencil can in

principle carry it out. In addition, not only each step is definite, it must also be feasible.∑ Comment Session: Comment is additional info of program for easily modification. In algorithm

comment would be appear between two square bracket []. For example: [ this is a comment of an algorithm ]

. Write algorithm to calculate the sum and average of two numbers.

[procedure for calculate sum and average of two numbers]Step1 : StartStep2 : Read two numbers n,mStep3 : Calculate sum=n+mStep4 : Calculate avg=sum/2

Page 35: Computer Programming · 2019. 4. 25. · 1 Mrs. V. S. Patki (Electronics Department) Walchand Institute Of Technology, Solapur Computer Programming 1. Explain Block diagram of computer

35 Mrs. V. S. Patki (Electronics Department)Walchand Institute Of Technology, Solapur

Step5 : Print sum,avg

Step5 : Stop[End of procedure for calculate sum and average of two numbers]Example of Algorithms:

Flowchartflow chart is a block diagram, describing an algorithm. It displays the sequence of steps that you need to do to complete a given task.Flowcharts use special shapes to represent different types of actions or steps in a process. Lines and arrows show the sequence of the steps, and the relationships among them. These are known as flowchart symbols. The type of diagram dictates the flowchart symbols that are used.1. Graphical representation of any program is called flowchart.2. There are some standard graphics that are used in flowchart as following:

Page 36: Computer Programming · 2019. 4. 25. · 1 Mrs. V. S. Patki (Electronics Department) Walchand Institute Of Technology, Solapur Computer Programming 1. Explain Block diagram of computer

36 Mrs. V. S. Patki (Electronics Department)Walchand Institute Of Technology, Solapur

Assembler: A computer will not understand any program written in a language, other than its machine language. The programs written in other languages must be translated into the machine language. Such translation is performed with the help of software. A program which translates an assembly language program into a machine language program is called an assembler. If an assembler which runs on a computer and produces the machine codes for the same computer then it is called self assembler or resident assembler. If an assembler that runs on a computer and produces the machine codes for other computer then it is called Cross Assembler.

Assemblers are further divided into two types: One Pass Assembler and Two Pass Assembler. One pass assembler is the assembler which assigns the memory addresses to the variables and translates the source code into machine code in the first pass simultaneously. A Two Pass Assembler is the assembler which reads the source code twice. In the first pass, it reads all the variables and assigns them memory addresses. In the second pass, it reads the source code and translates the code into object code.

Compiler: It is a program which translates a high level language program into a machine language program. A compiler is more intelligent than an assembler. It checks all kinds of limits, ranges, errors etc. But its program run time is more and occupies a larger part of the memory. It has slow speed. Because a compiler goes through the entire program and then translates the entire program into machine codes. If a compiler runs on a computer and produces the machine codes for the same computer then it is known as a self compiler or resident compiler. On the other hand, if a compiler runs on a computer and produces the machine codes for other computer then it is known as a cross compiler.

Interpreter: An interpreter is a program which translates statements of a program into machine code. It translates only one statement of the program at a time. It reads only one statement of program, translates it and executes it. Then it reads the next statement of the program again translates it and executes it. In this way it proceeds further till all the statements are translated and executed. On the other hand, a compiler goes through the entire program and then translates the entire program into machine codes. A compiler is 5 to 25 times faster than an interpreter.

Page 37: Computer Programming · 2019. 4. 25. · 1 Mrs. V. S. Patki (Electronics Department) Walchand Institute Of Technology, Solapur Computer Programming 1. Explain Block diagram of computer

37 Mrs. V. S. Patki (Electronics Department)Walchand Institute Of Technology, Solapur

By the compiler, the machine codes are saved permanently for future reference. On the other hand, the machine codes produced by interpreter are not saved. An interpreter is a small program as compared to compiler. It occupies less memory space, so it can be used in a smaller system which has limited memory space.

Linker: In high level languages, some built in header files or libraries are stored. These libraries are predefined and these contain basic functions which are essential for executing the program. These functions are linked to the libraries by a program called Linker. If linker does not find a library of a function then it informs to compiler and then compiler generates an error. The compiler automatically invokes the linker as the last step in compiling a program.

Not built in libraries, it also links the user defined functions to the user defined libraries. Usually a longer program is divided into smaller subprograms called modules. And these modules must be combined to execute the program. The process of combining the modules is done by the linker.

Loader: Loader is a program that loads machine codes of a program into the system memory.In Computing, a loader is the part of an Operating System that is responsible for loading programs. It is one of the essential stages in the process of starting a program. Because it places programs into memory and prepares them for execution. Loading a program involves reading the contents of executable file into memory. Once loading is complete, the operating system starts the program by passing control to the loaded program code. All operating systems that support program loading have loaders. In many operating systems the loader is permanently resident in memory.

Step By Step Execution Of C Program

Step 1 : Edit

1. This is First Step i.e Creating and Editing Program.2. First Write C Program using Text Editor , such as [ Borland C/C++ 3.0 , Notpad++,Notpad ]3. Save Program by using [.C] Extension.4. File Saved with [.C] extension is called “Source Program“.

Step 2 : Compiling

1. Compiling C Program : C Source code with [.C] Extension is given as input to compiler and compiler convert it into Equivalent Machine Instruction.

2. In Borland C/C++ 3.0 program can be compiled using key [Alt + F9 ].3. Compiler Checks for errors . If source code is error-free then Code is converted into Object File

[.Obj ].

Step 3 : Checking Errors

1. During Compilation Compiler will check for error, If compiler finds any error then it will report it.

2. User have to re-edit the program.3. After re-editing program , Compiler again check for any error.

Page 38: Computer Programming · 2019. 4. 25. · 1 Mrs. V. S. Patki (Electronics Department) Walchand Institute Of Technology, Solapur Computer Programming 1. Explain Block diagram of computer

38 Mrs. V. S. Patki (Electronics Department)Walchand Institute Of Technology, Solapur

4. If program is error-free then program is linked with appropriate libraries.

Step 4 : Linking Libraries

1. Program is linked with included header files.2. Program is linked with other libraries.3. This process is executed by Linker.

Step 5 : Error Checking

1. If run time error occurs then “Run-time” errors are reported to user.2. Again programmer have to review code and check for the solution.