0% found this document useful (0 votes)
23 views25 pages

Chapter

Uploaded by

omernasir109
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views25 pages

Chapter

Uploaded by

omernasir109
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

GO TO TO°*T DYWORL n .

COM TO
GET NOTE* OF ANY CLA** TO
GET HIGH MARK*
SHORT .žND LONFì QUESTIONS
Q1. Define œmputer progamming.
Ans: Computer Programming:
Computer programming is the process of writing a computer program in
computer language to solve a particular problem.
Computer program controls the operation of a computer and it is
developed in a computer language to perform a task Computer languages are also
known as programming languages
Q2. Define Programming Language. Explain low level and high level
languages
OR
Explain popular programming languages and their uses.
Ans: Programming Languages:
A programming language is a language which is understood by computer It
is designed to give instructions to a computer to perform a specific task. It is used to
write computer programs.
Classifiœtion of Programming Languages:
Programming languages can be classified into two categories. that is, low
level languages and high level languages.
i. Lęw Level Lanquąqes:
Low level language is machine-ońented language To understand low level
language. detailed knowledge of internal working of computer is required Low level
languages include mechJne čnztj/uage and assembly čaztguoge.
clawifiœtion of Low Level Language:
t uachine Language:
Pr ogramming language that is directly understood by computer hardware is
known as machine language Machine language is associated with a rchitecture of
COmputer Therefore, programs written in machine language for one computer will
not work on another because of design differences. It consists of zeroes and ones It
is almost impossible for humans to use machine language because it entirely
consists of numbers Therefore. practically no programming is done in machine
language Instead. assembly languages and high level languages are used
t assembly Language:
Assembly language consists of symbolic codes or abbreviations known as
mnemonics It was developed to make computer programming easier than machine
language The abbreviations used in assembly language make it easier to learn and
write programs compared to machine language A program written in assembly
language must be converted into machine language before it is executed by
assembl er is used to translate assembly iangUag O'

co mp ter. program known as


into
AU machine language
Characteristics of Assemb¥ language:
mbly languag e are:
SO me important charameristics of Assehammers aii the special
G Assembly langUa9* allOWS p fo g have access to are
to
operations which
features of the computer they a e using. Certain types of
not possible in high level languages A re easily programmes using assembly
language.
¥ Generally a pogram wñtten in assembly language will require less storage and
less running time than one prepared in a high level language.
¥ Assembly languages are still the best choice in some applications but their use
is gradually declining.
ii. High Level Languages (HQ and they are
High level languages are English-oriented languages
commonly used for whiting computer programs These languages use English
language words such as print, goto. if, end, etc Therefore. they sre easy to learn
and use.
Examples: Some examples of high level languages are ViSMaI Basic, C, JaVa
and Pascal.
A program known as compiler/interpreter is required to translate a high level
program into machine language. Coding and debugging of a high level language
program is much easier than a program witten in a low level language.
CDaification of High Level Lsnguagee:
High-level languages csn be classified into procedural. structured and
object-oriented prograrrwiing languages.
d Pro<adural languages:
Procedural programming is based upon the concept of modular
programming. In modular programming. programs are d‹vidsd into smaller parts
known as modules. Modular programs consist of one O‹ Max modules. A module is
a group of statements that can be executed more than once in a program. Each
module in a program performs a sp6cific task.
Advantages of procedural languages:
It is easy to design. modify and debug a program in a procedura l la ngU g
a e
since it provides better programming facilities.
Examples of procedural languages:
e a
a»sic ***’ “ ”" O cedl/ ral languages are FORTRAN, Pascai. c and
G ”o
Structured Languages:
Structured languages consist
sequence, selection and repetition.
It means. writing pr‹zgram statements in a iogical sequenoe Each step in the
sequence must logically prog‹e•• to the next without p
roducing any undesirabl effects
e
eva !( QIIOW s the selection of any number
S CONd itl on which may be
Of statemen ts based on the result of
luation of true or false Exampies of Statements
P! ‹»ent selection in programming that
are *‹ elm-if, swiws etc
i
R It means executing one or more statements a number of times until a
ll
condition is satisfied. Repetition is implemented in programs using statements such
aS fOr and while loops
Some examples of structured languages are ALGOL, PL/1, Ada and Pascal
G Object-Oriented Programming languages (oops):
Object-oriented programming (oops) refers to a programming method that is
based on objects such as student, vehicle, building etc. Object-oriented
programming language provides a set of rules for defining and managing objects
An object can be considered a thing that can perform a set of activities
For example, the object vehicle can be defined as an object that has
number of wheels. number of doors. color, number of seats. etc The set of activities
that can be performed on this object include Steer, Accelerate. Brake, etc
Complicated and large computer programs are difficult to design develop,
maintain and debug The concept of object-oriented programming solves this
problem.
The most widely used object-oriented programming languages are C++, CA,
php and Java.
Q3. What is programming environment?
OR
Describe the concept of integrated development environment
Ans: Programming Environment:
Programming environment is the set of processes and programming tools
used to develop computer programs. In the past, programmers used various
standalone programm ing tools for developing computer programs. These included
editor, c ompiler, linkef, debugger, etc. Using separate programs provided a difficult
and time consuming environment for creating computer programs
Today, programmers use Integrated Development Environment for
developing computer programs Integrated Development Environment is an
application package that consists of editor, compiler, linker and debugger with a
graphical user interface.
integrated development environment modules of
c language. OR
terms of C language. (Editor, Compiler, Linker,
oefine the followinger)
Loader and Debug9
Environment of C Language:
4 C language IDE consists of the following modules G Linker
g Text EdltOf G Comp ler
g Loade r G Debugger
t
Tex Editor: editor iS a simple word-processof that is used to create and edit
A text
a program as sfrown in Fig Files created by a text editor are plain
so urce code of ally highlight compile errors to simplify
text files MoSt of the editors automatiC
re moving them
rik tat svcb r,aw r,o;a<t «x+ra« De»w T+«k C•/S W*nd- Meip

Text editor used rOF creating and editing a program

A compiler is a computer
Software that translate s language program
C
(8OUfC8 program) into machine code (object program) that can be understood and
executed by the computer as shown in Fig. It also detects syqtax errors in the
source program and gives hints IO the pfogrammer to correct them A compiler will
only create object program if all the syntax errors in a program
if they existed. A program written in C language has have been corrected
the extension .c (for example
first c) and after compilation the object program extension
. t will b•. obj (for example

C Library Functions

Source Compiler Soft'w'are OQect Program Linker


Program
SOftware

Executable Program
Linkür:
\.inker is a software tnat tianslate» object prcgran› ir.to a sirple
executable t›rugrari, as shown in Fig During this process. if i: cnuld nnt find the
definition of a paiti u:ai fun-tion that is used in the prog‹am, then it would assume
that it is öefined in Î!biary lt will raplace thi9 function in the object piogram with
the CcdC ffom C
!iUrury anô tnen cieate a single executable program
Loadet:
it s a software thai Iuads progfgfTl9 into memory and then executes tI›em
Debuggar:
it is a software that executes a program line by line. examiner the values
stureJ m yariaMes and helps Jn finding and remcving errors in progæms.
QS. List tbe uses of C language.
Ans: uses of C longuage:
C is a popu:ar and wideiy used programming language for creating coniputcr
prutJ‹u‹i›s A Ia•ge varmty of application programs and many modern operating
S}/SttîlTlS telU +vrIÎtBn l‹1 C
Q6. ExçdÆift fi lQ* 9 Uage cbaracter sel
Ans: C Language Charac1er Set:
"fhe C Language character set includes

C languaqe compnses the following set of letters to form a standard program


They are
5 A to Z in Capital letters
G a to z in Small letters
4 In C p‹og‹amm‹ng, small latter and caps latter are distinct.
Digits:
G C Language comprises the follawing sequence of numbers to associate the
lettors 0 to 9 digits
speûal Characters:
C language contains the following special character in association with the
leviers and d'gits
symhoi •seaning Symhol Meaning Symbol ä4eaning
Underscore h ke
Plus siçn Colun
Vertical bar " Qt/ota(iun mark
Backslash ; Semicolon
AgO5t£Oghe < Open‹ng angie
tjrgcket
Caret - Minus sign > **°^•3 angle
bracket
Amporsdnd Equal to s•s• t Queston mark
• Astorir.k L=ft biac•. Comma
( Lest por cnthesis Ftight brace PeMod
) Right ç•arc'nthc'sis Left brackct / S!ash
q7. Define hader fila.
Ans: Huadar Fila:
C language contains a number of standard functions in library file that
perform various tasks in C programs These tasks include all the input/output
operations and all the math operations. Library file contains header files and each
header file contains a set of functions.
QS. Write the uses of header files.
Ans: Uwc of Needer Files:
Some commonly used header files are stdio h, conio h and math h
G Some commonly used functions that are included in stdio.h file are
printf(), scanf(), etc
6 Some of the functions that are included in conio.h file are clrscr(), getch()
and getche().
G The math h header file contains mathematical function such as sqrt(). pow(),
log(), sin(). cos() and tan().
In order to use a iibrary function, the programmer must include
appropriate heaoer file at the beginning of the program as shown in figure When
source program is translated into executable file, a copy of code of function is pasted
in the source program by the linker from the header file before creating the
executable file
Source Program stdio.h header file
# include <stdic4h set of functions
that are included
in stdio.h

Useofheaderftle!naCprogram
Q9. What is the structure of a C progmmf
Ans: Struca›re of a C Program:
The format according to which a program is written is called the structure of
program. The following is the structure of a C program.
Preprocessor direcfñ-es
Global dectarafioas

) ^°d: •* •••^O +U •« o•
L'ser-defined function
Fuarrio» 1 (optioo to use›-)
Fuacfioa 2

To introduce the structure of a C program. consider a very simple


program
that will print the message
1 Luve Pakistan:
The progr am may be written as shown in Fig

t£Pio•ecll) t inc l udo <stdio. h>

pr Inc f ( • I Love Pakiscsn•l ;

”5 2 i«wsa i «a‹ ”s nfi« “” ” ” ” “


Progcamtoprtntamessageonthescreen
This program consists of three main parts which are preprocessor difective,
the main() function and the body of main()
Q1o. Explain the types of comments in C language:
Ans: Types of comments:
1“here axe two types of comments.
i. Single line comment ii. Multiple line comment
i. Single lin k comment
Tne /I is used as single line comment The syntax of single line comment is
ii comment
An exemple is
ii Programmer M Sajjad Heder
ii. Multiple line comment:
The /” “/ ÏS USeö for multiple line comments.
shown in the following example
Comment can span to multiple lines as f
/• Pro gramme r: M Sajjad Hgde
Programming Language C
Date Program was Wrrtten 15-04-2014 ”/
Prograzathatdcmonstratestheuseofcomment
The program in Figur• demonstrates how single line comrrænts are used in a
prog am, In this program comments describe the purpose of statements.
pyy, ExpIpigi t/›e dlffezÆfit data types USed in C proga‹rNS.
Ans: Da@ Types Used In C ProgrQf¥tSi
C pro ’ides three main data types for variables, that is, integer. floating-point
and character variables

lnteger vafläblâ declaration statement has the form


Type specifier Variables
For exemple: int cum;
The declaratioo COflS tSt6 of the type name, Int, foltowed by the name of the
variable, sum. AII thä variables used in a C program must be declared If there are
mnre than one variable of tu same type, s‹. Carats the variable names with co•nmas
d eciaratio n statement.
as shown ifi the following
int a,b, aum, p
blv ccf/z true computer the name of the variable and its
type This enables the mpiler toesrecognize the valid variable «
names in prog am
variable name in his
This is very helpful if thC user typ the wrong speJlings of
that the variable is not
program, the compiter will give an error message
indicating
declared on of ä l Ï intege f vä riablC CBFA æ gin with the type qualifiers. short.
Declarati
ahort Int num;
long int StJm, ¥Vgî
unsigned int COUftti
short unsigned int count;
Type quallfJa.cs refer to the number of bytes used for storing the irteger on
a particular computer Refer to computer manual to ńnd out the sizes of the
computer being used. Generally, the number of bytes set aside by the cnmpilør for
the above type qualiùers are as given in table.
Int er data s used in C:
Variable Declaration No. of B s nan
- 2 -32 768 - +32,767
long int 4 -2,14 ,463,Ø8 -
+2,147,483,67
unsigned int 2" 0 - 65,5Ś5 "
Here, unsigned implies that the value of variable is greater than or equal to
zero The qualifier. signed. is rarely used since by default. an Int declaration
assumes a signed number Note that in the above examples, tńe word Int may be
omitted when it begins withlong or unsigned type qualifiers in the declaration
statements
For example you can write. short num;
I Sætinq-pgint Da@Tyge:
Floating-point variables are used for storing floating-point numbers Floating
point numbers are stored in memory in two parts. The first part is the mantissa and
the second part is the exponent
The mantissa is the value of the number and the exponent is the powér to
which it is raised Some examples of floating-point variable declaration statements
are
float base,height;
double float a,b,total;
long double float size,x,y;
Usually the number of bytes set aside by the compiler for the above floating-
point type qualifiers are as given in table
Floatin - int data s used In C:
Variable No. of Bytes Range
Declaration
float 10 - 10
Wİth 8 or 7 di its of recision
double float 10 - š0
with 15 di its of rec'1ion)
long double float 10 10*°°’ - 10*^°
Wİth twiCØ the precision of double)
Here, precision means the number of digits after the decimai Pöint. The word
float may be omitted when it is preceded by double or long double \ype qualifiers
in the declaration statements.
There is another method of representing floating-point r umhzrs
known as
exponential notation
Example:
For example, the number 23,689 woult1 be represented as 2. .1€›P6e4 H•<<.
2 3GtJ9 iQ the vnlue of lhr•. number (mantissa) and 4 is the execnent(e) *’tio
exponent can also be negative
Exam@e.
For example, the number 0 0005672 is ‹epres•-med as 072e-4 .n
exponential notation. Exponential notatio» is used to represent very large and very
small numbers In C, a floating point number nf type flnat is stored In four bytes
Three bytes for the mantissa and one byte for the exponent. nnzi 5 or 7
prc•vidñS digits of precision.

Character variables are used tn store character mnsten!s ErnmpI=s of


declaration of character variables are.
char ch,letter,a;
A character variable can only stcre one character The cemp:•ler sets aside
one byte of memory for storing a character in a character x’ariabIe
g12. Define constant qualifier.
Ans: Tha Const Qualifier:
The const qualifier is used with a data type decIa‹»tion to specify a const3n!
value.
Exam@es: Some examples of const qualifiers are
const Int COUNT-S;
csnst flaat Pl°3.14;
The .same thing cen nlsc be nch›sve« using #define preprocessor as q•ven beIc›v/.
¥define COUNT S
€deQne PI 3.14
In most of the situations there is nc difference betwe.•i› using ‹lefine •r
conat qualifier. The only difference is define is a preprocess‹›r directi›ze that sio„ Iy
replaces a symbol w‹th a constant value throughout the prcgiam. The conat
qualifier declares a vañaW type and assigns it a constant va!us It h.a: cr r=o
advantage at advance isvel of programming in C language
Q1Z. Explain 13›e main() Function.
Ans: The main().Function:
A C program consists of one cr more functions. C usos ’ c›f 'l ctions at fhp
building blocks of its programs. A function qe‹forms s single well-rlefine‹J tn \
C program must have the function malnf) which is the first s•.ction to be executed
when the program runs.
The word void before the fu.actionmain() means that this function
return a value end the S6cond void inaide the brackets m»> ‹I does does rct
ncthave ary
argument.
A program may also ccntairi user- functions User-defned
defined
are written by programmers and they are not included in the I.brary file These
functions may be written before or after the main() funct:on and are used inside
main() function When we run a program the main() functicn is a!w'd’y9 >x^•cuted
the
first no matter where ‘it appears in the program
The Body of main() Function:
The body of the function main()is surrounded by braces (curly brar.ket9 ( and
)) The left brace indicates the start of the body of the function and the matching
right brace indicates the end of the body of the function.
The body of the function in the program (see Figure)consists of a single
statement printf{) which ends with a semicolon(:).printf{) is the standard outpUt
function The text to be printed is enclosed in double quotes. A statement in C is
terminated with a semicolon. Therefore. a semicolon is used at the end of printff)
statement. This program will print the message I Love Paki»Nn on the screen
Q1“4. Differentiate between Global and Local Variable.
Ans: Global and Local Variables:
Variables are used in C programs to store values. All the variables used in a
program must be declared. There are two types of variables, global variabtes
end local variables.
In the program structure, global variables are declared before the main()
function and local variables are declared within it.
Global variables are those variables which can be used in any function
in the program whereas local variable are used only within a function in which they
are declared

KEY POINTS
t Syntax refers to the rules of a programming language according to which
statements of a program are to be written
Semantic gives meaning to statements of a programming language.
Machine language is a programming language that is directly
understood by computer hardware It consists of zeroes and ones.
t Assamb§ language consists of symbolic codes or abbreviations. It was
developed to make computer programming easier than machine language. A
program called assembler is required to convert assembly language to
machine language for execution by the computer.
High level language is an English-oriented language. It is commonly used
for wnting computer programs A program called compiler/interpreter is
required to tfanslate htgh level language into machine language for
executo n by the computer.
Compiler is a computer program that translates a program written in a high
level language into machine language equivalent program . It converts the
entire program into machine language before execution by the computer
interpreter is a computer program that translates a program written in a
high level language into machine language It translates one instruction at a
time and executes it immediately before translating the next instruction
Programming environment is the set of processes and programming
tools used to develop computer programs
Integrated Development Environment (lDEj i= a comouter software tha!
is used to create, compile and run programs It bring9 all the processes ané
tools required for program dev•lopment into one place
Text ed"‹tor is a simple word-processor that is used to creat• and edit
source code of a program
Linker is a software that takes one or more object files, gene,rated by o
compiler and combines them into a single executable rogram.
Loader is a software that loads programs into memuy and then executes
them.
Debugger is a software that executes a program line by line. examines the
values stored in variables and helps in finding and removing errors in

Header file is a file that contains predefined functions of C language


Including a header fiie in a C source file produces the same result as
copying the header file into the source file
Reserved words are those words that are part of a programming language
and have special purpose in computer programs
Preprooessor directives are instructions for the C compiler at the
beginning of program.
Comments are notes that are included in programs when a fact is
necessary to be brought to the attention of program’s reader.
Constant is a quantity whcse value dces not change during program
execution.
Varfable is a symbolic name that represents a value that can change during
execution of a program.
Typecasting is a way to convert a variable from on• data type to another
data type.

q1. sele«t tft'e best an6wer for the following ucqs.


i. Rhat defines the rules of alid statements in programmingy
A Compiler B Interpreter
C Syntax D. Semantic
ii. Which Dnguage is directty understood by the computer?
A. Machine language B Assembly language
C High level language C language
Wken was C language developedy
A. Late 1960s B Early 1970s
C. 1980s D. 1900s
lv. Who developed Java language?
A Dennis Ritchw B. Microsoft
C. Sun Microsystems D IBM
Integrated Development Environment (lDEj i= a comouter software tha!
is used to create, compile and run programs It bring9 all the processes ané
tools required for program dev•lopment into one place
Text ed"‹tor is a simple word-processor that is used to creat• and edit
source code of a program
Linker is a software that takes one or more object files, gene,rated by o
compiler and combines them into a single executable rogram.
Loader is a software that loads programs into memuy and then executes
them.
Debugger is a software that executes a program line by line. examines the
values stored in variables and helps in finding and removing errors in

Header file is a file that contains predefined functions of C language


Including a header fiie in a C source file produces the same result as
copying the header file into the source file
Reserved words are those words that are part of a programming language
and have special purpose in computer programs
Preprooessor directives are instructions for the C compiler at the
beginning of program.
Comments are notes that are included in programs when a fact is
necessary to be brought to the attention of program’s reader.
Constant is a quantity whcse value dces not change during program
execution.
Varfable is a symbolic name that represents a value that can change during
execution of a program.
Typecasting is a way to convert a variable from on• data type to another
data type.

q1. sele«t tft'e best an6wer for the following ucqs.


i. Rhat defines the rules of alid statements in programmingy
A Compiler B Interpreter
C Syntax D. Semantic
ii. Which Dnguage is directty understood by the computer?
A. Machine language B Assembly language
C High level language C language
Wken was C language developedy
A. Late 1960s B Early 1970s
C. 1980s D. 1900s
lv. Who developed Java language?
A Dennis Ritchw B. Microsoft
C. Sun Microsystems D IBM
What ls the other word used for Reœwed Wofds7
A Compiler words B Keywords
C Special programmin,q words D Mnemonics
vi.
How many bytes are set aside by the empiler for a variable of type
int7
A. 2 B 3
c î a 5
How many bytes are œt aside by the compiler for a variaŒe of type
ñoat7
A 2 B 3
C 4 D. 5
What is the range of numbers that can & stoed in a variable of
type double äoat7
A. -32,768 - +32,767 B 10”” - 10”
0 0
C 10 ”’ ’- 10 ’ D 10“” - 10* *2
ix. Which program translates high leve! language into machine
language?
A Compiler B. Linker
C Loader D. Debugger
x. Which software helps in finding and removing errors in programsl
A Linker B Text Editor
B Loader D. Debugger
Answers

S HORT QUESTIONS
q2. Give short answers to the following questions:
i. Define computer program.
Ans: ¢'omputer Program:
A computer program is a set of instructions (statements) written in a
programming language to solve a particular problem and achieving specific results
Any task performed by a computer is controlled by a set of instructions that are
executed by the micfoprocessor A large variety of programming languages have
been developed for writing computer programs to use the computer as a proLiem-
solving tool Each statement of a programming language has syntax and semai itic.
Dlfferentiate between syntax and semantîc.
Ans: t Syhtê^'
Syntax refers to the rules of a prngramming language according to which
statements of a program are to be written It describes the way to write correct
statements in a program Syntax of a programming language is similar to the
grammar of a natural language
For example, an assignment statement
consists of a ’zariahle and an
This is the syntax of
expression separated by equal sign as an assignment operator
assignment statement and it can be expressed as given beIOW
variable - expression.
4 Semantic: siatements of a proq amming language. It
Semantic gives meaning to n to be performed by a COf¥l uter when
describes the sequence of operatio s
executing the statements of a computer program.
For example, in the assignment statement.
sum = a + b; perform the expression , that is. add the
the semantic of the statement is to
values stored in variables a and b and store the result in variable
then
iil. Write three differences between assembly language and HLLs.
Ans: Differences {between high level and as9en'iMy langu6g9°
Features of high level language {HLLs):
i. High level languages are easily understandable.
ii. The programs that are developed in high level language are portable
iii. In cese of high level languages debugging of the coda is easy and the
program wntten is not machine dependent.
Features of Assembly language:
i. Although Assembly level languages are not easy to understand they are
relatively easier as compared to machine level languages.
ii. The programs written in this language are not portable and the
debugging process is also not very easy.
iii. The programs developed in assembly language are thoroughly machine
dependent.
iv. W"rce féur characteristics of HLLs.
Ans: Characteristics of High Level Languages:
Hiyh-level languages have the following characteristics.
1) These languages were developed to make computer programming simp!e,
easier and less prone to errors.
2) High level languages are not machine dependent. They enable programmers
to write programs that are independent of a particular type of computer
3) PfDgfams written in high-level languages must be translated into machine
language fry a cnmpiIe‹ or an interpreter before execution fry the computer
4) The process of finding and removing errors in programs (debugging) IS
easier in high-tevel languages compared to low level language.
s) High-level language programs are highly structured They allow
programmers to break lengthy programs into a number of modules which
can be written and tested independently. This makes writing and testing of
programs easier.
v, o«nn« I ntegrated Development Environment (IDE).
Ans: Integated Development Environment (IDE):
IDE is computer software that brings all the processes and tools required for
program development into one place. IDE’s aim is to make the iife of programmers
easier by grouping together all the tasks needed for building applications into one
environment
Today 's modern lDEs have user-friendly Graphical User lnterface (CUI)
Most of the new programming languages use Integrated Development Environment
tIDE) to craate. compile and run programe.
vi. Differenoatc between constant and variable.
Ans: ConctaetQ and Variables:
Constant and variables are used in expressions in computer programe. After
an expression is evaluated, the result of the expression is also stored in a variable.
4 59nstant:
Consta«ts are quantities whose values do not change during program
execution
Types of Connant:
They may be numenc. character or string.
i. Numeric Constants:
Numeric Constants are of two types, inteqer and floating-point numbers
Integer constants represent values that are counted, like the number of
students in a class Some examples of inteqer constants are 7145, -234, 26. etc.
Floating-point constants are used to represent values that-are measured,
like the height of a person which might have a value of 186.75 cm or the weight
such as 82.6 kilograms.
ii. Character Constant:
Character Constant is one of the symbols in C character set. It includes
digits 0 to 9, upper-case letters A to Z, lowei-case letters a to z, punctuation
symbols such as semicolon (;), comma (,), period (.) and special symbols such as +
-, =, >, etc A c,haracter constant is enclosed by single quotes such as ’a'.’s’. etc
iii. String Constant:
String Constant contains a string of characters within doubte quotes such as
“Hello Ahmed•. °a•, etc

A variable is a symbolic name that represents a value that can change


during execution of a program. A variable has a name, known as variable name and
it holds data of other types. A number or any other type of data held in a variable is
called its value. It aiso indicates the types of value a variable can represent.
Types of Variables:
Variables are of two types, numeric and character.
i. Numeric variables:
Numeric variables are used to represent numeric vslues in computer
programs They represent integer and floating-point values Some exampk z of
numeric variables are sum, average, length, salary, marks, etc.
fi. Character variabtas:
Character vsriabtes represent character values in computer programs. It can
represent a single character or’a string of characters. Some examples of character
variables are name, city, gender, etc.
When a variable is used in a computer program, the computer associates it
with a particular memory location. The value of a variable at any time is the value
stored in the essociated memo¿ location at that time Variables are used so that the
same space in memory can hold different values at different times.
Vii› Which of De following ars valid C variables? Give the eason if not
a valid variable aea, 5x, Sum, net pay, Èæç _age, ele, caw‹
Ñze22, my_welght
Valid: - my welghç area, siza22, Sum
Not Vaild.-
5xA variable can’t be start with number
s can’t be usaJ as variabe name
cial characterCant be used in variable name Commands cannot be uwd as
Space not allowed in variable names

Special characters »nd numbers are not allowed start the


name of variable.
Rewne words are not allowed.
viii. What are reserved words? Why they should not be used as var%be
names7
Ans. Reserved Words.
The words that are part of programming language and have special
purposes in computer programs are called reserved words or keywords.
reserved words heve predefined use and cannot be zzsed for any other
purpose. Reserved words are always wntten in lcwercase. There are 32 words
defined as reserved words in C. A complete list of reserved words used in C
language is given in table.

R=---xc.Ëîords
] double . int I struct
break switch
e»«e } enum register \ typedef
gyt | extern reNrn I union
[unsigned
void
I •°i•tii•
@ whilo
do1 !*
ix. j/yhy comments ara * prog*nms*
lt ‹s a good programming practice to add comments in prograrn to make it
easy for others ta understand it. COfTlITiCft(ti B
the source oode are ignored by the
compiler.
Comments are udded in prrpraiiis when a tact s necessary to be
brought to the attention of program’s reader Generaily. programmers write
CGmmentu at the begir›ning of the program explair mg the reader what the program
is intended tu achieve and inside the program code where something is to be
clarified about tt e structure of the program
There are two types of comments.
4 Single line ‹x›mment
0 Multiple line comment
x. What is the uw of typecasting in C pograms3'
Ans: Typecasting in C Language:
Typecasting is a method to convert a •JariabIe from one data type to another
data tyye during program execution It makes a variable uf one type to act like
another typa.
For example, a variable of type int can act as a variable of type char
using typecasting This is required in some situations in programming in C language
For example, one use for typecasting is when there is need to /xoduce
ACSII characters for decimal cudes (0 to 127) To do this, the programmer will need
ie use to typecast to print out the integer variable as its charac1er equivalent usmg
fcnnat sp«cifar */ c.
The program that implements this is shown in Fig.

faz{x-0, x « -127, x'r+)


pyzstf(" \a 46d - 4tc ‘, x, x};

TypecastlnglnCprogram
There are two types o/ typecas¢fng In C
I Implicit typecasting
I Explicit typecasting
EXTENSIVE QUESTIONS
Q*• Ascribe tkefollowing HLLs.
a) C/C++ b) Visual Basic
c) CP d) Java
n^: Popular High Level Languages
(HLLs):
) C/C++:
C language was developed in early 1970s by Dennis Ritchie at Ball
L4boratones. C has become one of the most popular prcgrainming languages
t4day. It is a highly. struoures programming Iai›guage that is easy to understand and
use. In the pest, it was mainly used for writing system programs such as operating
systems, compilers, assemblers, etc
Today, it is used for writing all types of application prograrils as well, such as
wurd-processing programs, spreadsheet programs. database management
systems, educational prog•a * 8ames, etc.
C++ was developed by Bjarne Stroustrup also at Bell Laboratories during
1983-1985. C+• is a superset of C, meaning that any valid C program is also a valid
C++ program. The purpose of developing C++ was to provide programming facilities
tc easily and quickly write more powerful programs.
b) Visual Basic:
Visual Basic (VB) is a high level language which evolved from the earlier
version called BASIC. BASIC stands for Beginner’s All-purpose Symbolic Instruction
Code. VB is a very popular programming language for writing Windows and Web
applications. It provides a graphical development environment to programmers to
develop powerful Windows and Web applications
VB is commonly used for developing business programs such as payroll
system and inventory control program The user can also write programs related
with engineering, science. arts, education games, etc

c‹ (pronounced as C-sharp) is a language developed in 2000 by


Corporation. It ‹» a Microsoft simple, modern. general-purpose programming
language.
Syntax of C# is ve‹y similar to C and C++. It also has some features of Java.
It is a language that fTiakes computer programming easy and efficient. It provides
facilities to write Web applica(lOnS thatgames,
can be used across the Internet.
utilities, operating systems. compilers.
All types of programs including
business applications and Web based applications can be developed in CA
d) Java: a higr -level language developed by Sun Microsystems It is very
Java is
similar in syntax to cer•na C++ In Jova, tu user can write all types of programs as
prog‹afñ ‹»g languages and small programs that can be
those written in cth accessed thfoUgfl Internet. Java is an ideal language for
embedded in a Web page
network computing It is us ed writing programs for a wide range of devices.
for
in Web applications The current ver sio ns
computers and networks. It IS Widely used A few browsers that support
of most of tr›e Web browsers are made Java enabled
Ja a are Microsoft's Internet Explorer, Firefox and Mozilla
,}4. Differentiate between compiler and interpreter.
Ans: Compiler and Interpreten:
Compiler:
A compiler is computer software tf›at translates source program into object
Program as shown in Flg

Source Program Compiler Object Program

Trana)ation of source program Into object program


Source program consists of statements written in a high level language such
as C, Pascal. Java, etc
For example, a program written in C language by a programmer to print
table of a number is known as source program. When it is translated with a compiler
into machine language. the resulting program is known as object program. The
object program is understandable by computer processor but difficult for a human to
read and understand because it consists of zeroes and ones.

interpreted translates h!gh level languase programs into machine language


but it translates one instruction at a t.me and executes it immediately before
translating the next instruction
Examples of programming languages that use interpreter are Java Script,
BASIC. Visual Basic and Perl.
Interpreter reads each statement of source program, one at a time and
determines what it means as it executes it It means each time a statement is read
it must be translated into machine language before execution Compiler translates
the entire program into object program befnre execution by computer. Therefore. a
compiled program runs fast.
Q5. Describe the functions of linker and loader programs.

Linke is a software that trans'ates object program into a single executable


program During this process, if it could not find the defin•tici› of a particular function
that is used in the program, then it would assume that it is defined in C library lt will
replace this function in the object program with the code from C library and then
create a single executable program
Loader:
It is a software that loads programs into memory and then executes them
p6. what are the rules Vor specifying a variable name in C language7
Ans: Rules for Specifyinq Variable Names in C language:
The following are the rules fo• specifyirig vanable names in C language
¥ A vaoable begins wi!h ä letter Gr underscore ( _ ) and may consist of letters.
unzserscorcs and/ur digits
The underscure may be used te iniproze readaöi!ity of the variable name
For example. over _time.
7here is no rest«ction on the length of a variab!e name However only the
iimi 31 cl›aräCtCfS Of d V:‹•iahie are significant ThIS MCdF'S that if two
variables have the same first 31 characters they are considered to be the
same variables
Both upper and lower case letters are allowed in naming variables An upper
case letter is considered different from a lower case letter For example the
variable AVG is different from Avg or avg.
I Special chafacters can!1Ot be used as variable name. e g # * Z e:c
I Reserved words of C language such as int, case if, etc can no: be used as
variable names
¥ There must be no embedded blank in the name of variable For example ma
ss is not correct
Q7. What is the difference between implicit type cauing and explicit
type casting7 Give examples.
Ans: Typecasting In C Language:
Typecasting is a method to convert a variable from one data type to another
data type during program execution. It makes a variable of one type to act like
another type. For example. a variable of type int can act as a variable of type
char using typecasting. This is required in some situations in programming in C
language.
For example, one use for typecasting is when there is need to produce
ACSII characters for decimal codes (0 to 127). To do this, the programmer will need
to use to typecast to print out the integer variable as its character equivalent
using format specifier °/ c.
The fogfaM that implements this is shown in Fig.

¢1nc1uoc<scd1o.b>
Idtd

fa(n-0; x < -127; x++J

Typecasting in C program
There are two tyyes o{ typecuslitig in C
5 Implicit typecasting
¥ Explicit typecasting
Implicit Typecasting:
implicit typecasting is performed automatically by the compiler without
programmer s intervention. In thus type of casting the compiler converts all operands
into the data type of the largest operand Suppose an expression contains variables
of integer and double data types, the result will be evaluatéd to a double data type
For example in the program given below, compiler converts the sum of
two float numbers to an int.
#include <stdio.h>
void main(void)

float value1 = 2.5:


float value2 = 5.3:
int result;
result = vaIue1 + value2,
printf("Result ' o/ad", result),

The program in Figure is also an example of implicit typecasting


Explicit Typecasting:
Explicit casting ‹s performed by programmer The programmer explicitly defines
the data type in parenthesis before the variable or expression, as follows:
(type) expression
For example in the program given below, the programmer converts the
division of two int numbers to float.
#include <stdio.h>
void main(void)

int vaIue1 = 30;


int value2 = 7,
float result.
result = (float)value1 / value2:
printf("Result °/»f', result),

Q8. What is a preproCegSo‹’? GiVe eXarNpleS.


Ans: Preprocessor DirectlYOSt
Preprocessor directives are instructions for the C compiler Every C
Language program contains certain p,reprocessor.directivbs at the beginning of the
program Before translating a language program into machine language. the
compiler of C language carnes out the processor directives These directives start
with number sign (#)
-The most commonly used preprocessor directives are include and define
t The include Preprooessor Directive:
lt has the following syntax
5ínclude<header file name>
When this preprocessor is carried out by the C compiler it wili search for the
header file that is written within the less than (<) and greater than (>) symbols and
copy it into the source file
In the above program the header file stdio.h is used It tells the C compiler
to copy the stdio.h header file into the program The stdio.h header file stands for
standard input-output header. It includes the standard printf() and scanf() function
prototypes In the above program the printf() functions is used Therefore it is
required to include this header file in the include preprocessor directive
G The define Preprocessor Directive:
The define preprocessor is used for defining constants (i e Symbolic
constants) in C programs It directs the compiler to replace all the occurrences of
one or more variables in a program with the specified constant The purpose of
using this directive is to give a meaningful name to a constant value that is to be
used in the program.
It has the following syntax
¥define SYMBOL value/expression
Here, SYMBOL is a valid C variable name and it is by convention written in
uppercase. An examples of define preprocessor directive is given below
Sdefine HEIGHT 12
This preprocessor directive tells the compiler to replace all the occurrences
of the variable HEIGHT with the value 12, in the program during its execution
An example of define preprocessor directive using an expression is given below
¥define CUBE(a) (a•a”a)
It can be used in a program as given below
volume•CUBE(side);
The volume will be calculated as.
volume=(side•side•side);

You might also like