Sunday, January 14, 2024

4. C Programming - SEE COMPUTER SCIENCE 2080

 4. C Programming

 



Structured programming

The programming that follows a top- down approach, on which developers separate the overall program structure into different sub section, is called structured programming.

 

Advantages of structured programming

a)     Reduces complexity and easy to code.

b)     It takes less time to program and easy to debug.

c)     Reuse of modules and flow of control is clear.

 

C language
C Language is a high-level structured programming language which is used to develop system software.

Dennis Ritchie at Bell telephone laboratory developed C-language in early 1970s. 

C is called middle level language because it combines elements of high level language with some features of assembler.

 

Data types used in C
Data type are means to identify the type data and associated operation of handling it.

 

Data types used in C language

a) char       b) int   c) float                         d) double         e) void

Features of C language

a)     C is a Structured Programming Language. which break down a program in several small modules and makes the programmer easier to manage and debug the code.

b)     C combines elements of a high level language with some features of assembler which helps to manipulates memory address.

c)     C is a case-sensitive programming language. It understands the capital alphabets and small alphabets as different values.

d)     C is many time faster and efficient than BASIC due to the variety of data type and a list of powerful operators, programs written in C language.

 

Limitations of C language

a)     There is no runtime checking. It has poor errors detection system.

b)     It does not support object oriented programming.

c)     C language has only 32 Keywords

d)     There is no strict type checking int data type to float variables.

 

Application of C Language

a) Operating System

b) Language Compilers/Interface

c) Assemblers

d) Text Editors

e) DBMS

f) Utilities etc.

 

C Token

C tokens are the basic buildings blocks in C language which are constructed together to write a C program.

Each and every smallest individual unit in a C program are known as C tokens

 

 

C tokens are of six types.

a) Keywords (eg: int, while),

b) Identifiers (eg: main, total),

c) Constants (eg: 10, 20),

d) Strings (eg: “total”, “hello”),

e) Special symbols (eg: (), {}),

f) Operators (eg: +, /,-,*)

 

C keywords (reserved word)

Keyword is a set of special words which are already defined for some tasks.

C has only a set of 32 keywords, which have their predefined meaning and cannot be used as a variable name.

E.g. auto, double, int struct, break, else, long, switch, case, enum, register, typedef, char, extern, return, union, continue, for, signed, void, do, if, static, while, default, goto, sizeof, volatile, const, float, short, unsigned

 

C Character set

Character Set is a group of valid characters and symbols supported by a programming language. Alphabets, Digits and Special characters.

 

Identifiers

Identifiers are the names given to the entities such as variables, functions, arrays structures and unions.

E.g. int price; float total; Here, price and total are called identifiers.

 

Rules for naming Identifiers:

i) The Identifier must start with an alphabet or an under score (_).

ii) Identifier can be made from the combination of alphabets, digits and under score.

iii) No any C keyword can be used as an Identifier.

iv) Identifier must be unique and can be used for a single purpose only.

 

Format Specifier

The format specifier is used during input and output operation. It tells the compiler what type of data is stored in a variable during the input and output operation such as taking data from keyboard and display data on the screen. Some examples are %c, %d, %f, etc.

Data Type

Format Specifier

short int

%hd

unsigned short int

%hu

unsigned int

%u

int

%d

long int

%ld

unsigned long int

%lu

char

%c

float

%f

double

%lf

 

C Header Files

stdio.h - Used for standard input and output (I/O) operations. [printf( ), scanf( ), getchar( )]

conio.h -  Contains declaration for console I/O functions. [clrscr( ), exit( )]

math.h - D e c l a r e s m a t h e m a t i c a l functions and macros. [pow(), squr(), cos(), tan(), sin(), log() ]

string.h - Used for manipulating strings. [strlen(), strcpy(), strcmp(), strcat(), strlwr(), strupr(), strrev() ]

 

Escape Sequence

Escape Sequence is a pair of character that is used with printf() function to display non-printing character or a special character on the screen.

Some Examples of Escape Sequence:

\n - new line

\t - tab

\b - backspace

\o - null character

\? - question mark

\\ - slash

\' - single quote

\” - double quote

 

Format Identifier

We need to include format identifier to tell the data type in the area of format string of printf() function.

Variable Type

Format Identifier

char

%c

int

%d

long int

%ld

float

%f

 

Variable

A variable is a location in the memory of a computer which holds data in a program and its value may change during the execution of program.

 

Constant

Constant are the values that do not change during the execution of a program.

 

String Constant

 String constant is a message to be displayed along with the other values stored in variables. It is enclosed within double quotation (" ").

 

Output Function in C

Output function is used to show the calculated result or output on the screen.

In C language, printf() is one of the output function defined in header file.

 

printf() function

In C Language, printf() function is used to print the valued on the screen.

It is defined in header file. So, the header file must be added to use this function.

 

Input Function in C

 Input function is used to ask data for processing.

In C language, scanf() is one of the input function defined in header file.

 

scanf() Function

scanf() is one of the most important functions of C Program.

This function is also defined in the header file and used to ask value from keyboard.

 

getch() function

getch() function is another input function of C language.

This function is defined in the header file .

This function is used to ask any one character from keyboard.

Arithmetic Calculations in C Program

There are basically four types of arithmetic operations:

 i) Addition

ii) Subtraction

iii) Multiply

iv) Division

 

List of Arithmetic Operators in C

+ (Plus) - Addition

- (Minus) – Subtraction

* (Asterisk) – Multiplication

/ (Slash) – Division

% (Percentage Symbol) – Modulus Division

++ (Plus Plus) – Increment Operator

- - ( M i n u s M i n u s ) – Decrement Operator

 

Operator  

Operators are special symbols that are meant for specific tasks or operators.

The different types of C operators are:- Arithmetic operators, Assignment operators, Unary operators,  Relational operators, Logical operators and Conditional operators.

Arithmetic operators are the operators that are used for manipulating arithmetic data and performs arithmetic operation. The arithmetic operators are:-   +,     - ,      * ,    / and   %

The operator that operate on one operand variable or constant is called urinary operator. E.g. ++, - -

 

C Expression

An expression consists of at least one operand with one or more operators. It is a legal combination of symbols that represents a value.

 

Logical Calculation in C

The calculation that is done based on one or more conditions is called logical calculations.

Several relational or comparison operators are used to check the condition which gives True or False as a calculated result.

 

Relational Operators in C

Relational Operator checks the relationship between two operands and returns either 1 (True) or 0 (False).

In C programming, relational operators can be used to take decisions and provide condition in looping statements.

 

List of Relational Operators in C

= = Equal to

> Greater than

< Less than

!= Not equal to

>= Greater than or equal to

<= Less than or equal to

 

 

 

 

 

 

 

Control structures in C

 

a) Sequence structure (straight line paths)

The statements are executed one after another sequentially from top to bottom without changing the flow of program.

 

b) Selection structure (one or many branches)

The control of the program is transferred from one part of the program to another on the basis of specified condition or without condition.

If statement and switch statement are the examples of selection structure.

 

c) Loop structure (repetition of a set of activities)

Looping is the process of repeating the execution of a statement or a block of statements guided by a condition. A loop is terminated when the given condition is satisfied.

 

Looping statements in C

C provides three kinds of loops: i) while loop ii) do loop iii) for loop


 

 

3.2 File Handling in QBASIC - SEE COMPUTER SCIENCE 2080

 3.2 File Handling in QBASIC




File

The collection of different data or information or instructions, which is stored in secondary storage devices under a unique file name, is known as file

 

File handling

File handling is a process to create a data file, write data to the data file and read data from the specified data file.

 

Data file

Data file is a collection of data such as name, address, class, etc. required for data processing.

The different types of data file are:

a)     Sequential Access files - Sequential Access to a data files means that the computer system reads or writes information to the file sequentially, starting from the beginning of the file and proceeding step by step.

b)     Random access files - Random Access to a file means that the computer system can read or write information anywhere in the data file.

 

Program File

The set of instruction written in a computer language for data processing under unique file name is known as program file.

 

Mode of data file

Mode of data file means opening a sequential file for one of the three modes of operation like output mode, input mode and append mode.

 

Modes of operation for opening a sequential file

a)     Output Mode: It is used to create a new sequential data file and write data in it. If the file already exists its current contents will be destroyed. It starts writing data always from the beginning of the file (BOF).

b)     Input Mode: It is used to read / display data or information from the existing sequential data file.

c)     Append Mode: It is used to add more records in the existing sequential file.. If the specified file does not exist APPEND mode creates it. It starts adding of data always from the end of the file (EOF).

 

File number

The number assigned to a file in order to identify it during processing is called file number.

 

Write down the functions of:

 

OPEN statement: It opens a sequential file for one of the three possible operations (reading, writing, appending).

 

WRITE statement: It writes data to a sequential file. It inserts commas between the data items. It encloses strings in double quotation marks.

 

PRINT#: It writes data to a sequential file It adds spaces between data items while storing data. It does not enclose strings in double quotation marks.

 

CLOSE statement : It closes one or all open files.

 

INPUT# statement: It reads data items from a sequential file and assigns them to variables.

 

EOF( ) function: It is used to check whether the record pointer reaches at end of file or not.

LINE INPUT statement: It reads an entire line without delimiters from a sequential file to a string variable.

 

INPUT$ function: It returns a string of characters read from the specified file.

 

NAME statement : The NAME statement changes the name of a disk file or directory

 

KILL statement : The KILL statement  deletes a file.

 

MKDIR statement : It creates a new directory..

 

CHDIR statement: It changes the current default directory for the specified drive.

 

RMDIR statement : It remove an existing empty directory.

 

FILES statement: The FILES statement displays the list of files residing on the specified disk.

 

SHELL: The SHELL statement executes a DOS command.

 

SYSTEM : This command is used to close QBASIC program window

 

INT ( ) : It rounds and returns the largest integer less than or equal to a numeric expression.

 

PRINT  : Display result on screen.

 

MID$ ( ) : t is a string function that returns the specified number of characters from the specified location of string.

 

LEN ( ): Returns the number of characters in a string or the number of bytes required to store a variable.

 

SQR ( ) : Returns the square root of a numeric expression.

Qbasic Operators

Operators are special symbols that are meant for specific tasks or operators.

i) Arithmetic operators

ii) Relational operators

iii) Logical Operators

iv) String operator

Static variable

The variable which is declared by using the “STATIC” keyword is called static variable.

Comparing C and QBASIC language

 

QBASIC

C

It is a high level language without feature of low level language.

It is a high level language with some features of low level language.

It is mostly used to design application software.

It is mostly used to prepare system software.

It supports structure programming with sub and function procedure.

It is a structured programming language with function procedure.

It is not case sensitive

It is case sensitive

IBM PC version of BASIC has around 159 keywords.

It has only 32 keywords.

 

3.1 Modular Programming - SEE COMPUTER SCIENCE 2080

 3.1 Modular Programming






 

Modular programming
Modular programming is a technique used to divide our program into many small logical, manageable and functional modules or blocks.

 

Advantages of modular programming

l Many programmers can write different program modules independently.

l The debugging of the program becomes easier and faster.

l The same procedure can be used in different places, which reduces the program codes.

l It improves the readability of a program.

l It is easy to design code.

 

Types of  Procedure in Modular Programming

a) Sub-procedure

b) Function-procedure

 

Main Module - The main part of the program inside which several submodules are created.

 

Sub-Program - A group of statements written in a separate module to perform one or more tasks

 

Sub module / Procedure
Module is a block of statement that solves a particular problem.

SUB procedure
A sub-procedure is a small manageable and functional part of a program that performs specific tasks and does not return any value to the calling module.  

A CALL statement is used to call the sub procedure in a program.

SUB-procedure’s name does not accept data type symbol because it does not need to return a value.

Passing arguments by value (Call by value)

Call by value is a method of calling a procedure in which actual data are passed to the calling procedure module. It is done by enclosing each parameter by a separate parenthesis ( ).

 

Passing arguments by reference (Call by Reference)

Call by reference is a method of calling a procedure in which the reference (address) of parameters are passed to the calling procedure module instead of actual value. By default, the parameters are passed using call by reference method.

 

Formal parameters

Formal parameters are variables which are used to specify or declare types of data to be passed to the procedures either sub or function. A formal parameter is always variable(s).

 

Actual or Real parameters
Actual or Real parameters are arguments which are used to pass real value or data to the procedure.

Actual parameters may be variables or constant values or can also be in the form of expression.

 

Local variable

A variable which is defined in a module and is not accessible to any other modules is known as local variable

It is only accessible to the module where it is defined.

Value of local variable is destroyed when execution of module is over.

 

Global variable

A variable in main module which can be accessed from any module or procedure of a program is known as a global variable.

Variable can be made global declaring them with DIM SHARED or COMMON SHARED or SHARED statement.

 

 

FUNCTION procedure

A function-procedure is a small manageable and functional part of a program that performs specific tasks and returns a value to the calling module.  

FUNCTION-procedure is called by statement and expression method.

FUNCTION-procedure’s name accepts data type symbols such as $, %, !, #, &, etc. and it depends on the type of value to be returned. E.g.: FUNCTION REV$ returns string.

String function [SEE 2074 U]

String function is used with string it returns string value.

 

Numeric function [SEE 2074 U]

Numeric function is used with number it returns numeric value.

 

Library functions [SQE 2074K]
Library functions are built-in or readymade functions provided by QBASIC. E.g. MID$( ), LEN( ), SQR( ) etc.

User defined function
Function which is defined by the user according to the need is called user defined function.

 

 

CALL statement [SLC 2071] [PMT 2075K]
The function of CALL statement is to
transfer control to another sub procedure from main module.

 

DECLARE statement
The function of DECLARE statement is to declare procedure such as FUNCTION or SUB in modular programming.

 

DIM SHARED / COMMON SHARED statement - to declare global variables in main module.

SHARED - to declare global variables in sub module