Saturday, February 1, 2025

 



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

Why C is called a middle level language?

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

 

Features of C language

·      Structured Programming: C supports breaking down programs into functions, making the code easier to manage and understand.

·      Portability: C code can be compiled and executed on different systems with minimal modifications.

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

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

·      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

 

Give the name of any two header files used in C language

<stdio.h> and <conio.h>

 

Name the header/library file that handles mathematical functions in C Language. - <math.h>

 

What will be the value of ‘y’ in given C expression, y=64%5?

The value of y will be 4.

 

What will be the value of x in given c expression x=55 % 10?

The value value of x will be 5.

 

How many keywords are in C language? - 32

 

What is an operator in C language?

Operator is a symbol that is used to perform mathematical operations.

 

Write the name of unary operators used in C-language

Increment (++) and decrement (--).

 

Write the name of two selection statement used in C language.

- if and switch statement

Write any two looping statements used in C language.

for and while loop

List any two examples of format specifier (Conversion Character) used in C Language.   - %d (int) and %f (float)

 

Write down the backslash character (Escape sequence) for horizontal tab and new line in C program

 The backslash escape sequences for horizontal tab is \t and for new line is \n in C program.







3. Programming in QBasic [Modular programming & File Handling (Theory Part) - SEE COMPUTER SCIENCE 2081

 

3. P


rogramming in QBasic

Modular programming

Modular programming
Modular programming is a technique which divides program into many small logical, manageable and functional modules or blocks.

 

Advantages of modular programming

v Coding the program and testing is very easy.

v Different programmers can write different modules separately.

v Debugging of the program becomes easier and faster.

v Possible to use single module in different places.

 

Main Module

The top-level module is called main module which is located at the top of all procedures such as sub procedure or function procedure.

 

Sub module

Sub-module is a program which is written under main module.

 

Procedure / Sub Routine / Module
A procedure is a block of statement that solves a particular problem given by user.

Types of Procedure in Modular Programming

v Sub-procedure

v Function-procedure

 

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. 

 

Features of sub procedure

v It is called by using CALL statement.

v It cannot be used as a variable.

v It doesn't return value to the calling module.

v Sub procedure name cannot have type declaration sign.

 

 

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. 

 

Features of function procedure

v It returns value to the calling module.

v Function procedure name have type declaration sign.

v A function name can be used as an expression.

v Variables used inside a sub program are local by default.

 

Difference between sub and function procedure.

Sub procedure

Function procedure

Sub procedure name cannot have type declaration sign

Function procedure name not have type declaration sign

Sub procedure does not return value to calling module

Function procedure returns value to the calling module

                

DECLARE statement

The DECLARE statement is a non- executable statement that declares references to QBASIC sub-program.

 

CALL statement

The CALL statement is a statement that transfers control to another sub procedure.

 

Which statement is used to call sub-procedure?

CALL statement is used to call sub-procedure.

 

Formal parameter (Parameter)

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

 

Actual parameter (Real parameter/argument)

Actual parameter is argument which is 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.

 

 

What is passing argument by value in QBasic?

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 ( ).

 

What is passing argument by reference in QBasic?

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

 

Difference between passing argument by Reference and passing argument by value methods

Passing argument by reference

Passing argument by value

When arguments are passed by reference, the address of the variable are passed to the procedure.

When arguments are passed by value, it makes a duplicate copy of arguments and their values are used directly in the parameter.

The changes made in the procedure’s variable will affect the variables used at calling module.

It does not make any effect on values of variable which are passed to a procedure even they are changing in the procedure.

By default, the value is passed by reference.

To pass the argument by value, variable is enclosed in parenthesis

 

Local Variable

A variable which is defined in a module and is not accessible to any other module is called local variable.

The value of local variable is destroyed when execution of module is over.

 

Global variable

Variables which are declared outside the procedure and whose values can be accessed from any procedure or module are called global variables.

The value of global variable is not destroyed during the execution of program.

DIM SHARED, COMMON SHARED statement is used to declare global variable.

DIM SHARED statement

The DIM SHARED statement makes the variable accessible or global to all the modules. It is used in the main module.

 

 

COMMON SHARED statement

The COMMON SHARED statement is a non executable statement that declares variables as global, so that they can be shared between main module and sub modules. It appears only in the main module.

SHARED

The SHARED statement is used to share variables or part of a module among a module without making the variables global. It appears only in the sub module.

 

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 / Built-in Function [SQE 2074K]
Library functions are built-in or readymade functions provided by QBASIC. E.g. MID$( ), LEN( ), SQR( ) etc.

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

 

What is the function of MOD in Qbasic.

Ans: MOD returns the remainder of a division between two numbers.

 

What is the function of INT() in QBASIC programming?

Ans: The INT() function in QBASIC returns the integer portion of a number by removing the decimal part.

 

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

 

 


 

 

         (3.2) File processing (file handling)

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).

 

Which mode is used to add data items into the already existed data file in QBASIC?

APPEND mode is used to add data items into the already existed data file in QBASIC

 

Which mode is used to read and display data from an existing sequential data file?

-Input Mode

 

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.

 

What is the function of SQR in Q-Basic?

The function of SQR in Q-Basic us to calculate the square root of a given number.