CH 2 Programming in C
CH 2 Programming in C
VI. differentiate between constant and variable? meaning and purpose. The meaning and purpose of a keyword is
A variable is a named memory location or memory cell. It is defined by the developer of the language. So they cannot be used
used to store programs input data and its computational results as variable name. They cannot be changed or redefined by the
during execution. The value of variable may change during the user. Examples include if, while, int, and const. there are nearly
A constant is a quantity that cannot be changed during program Extensive questions exercise
execution. Example marks=67; Q3. Describe the following HLLs. C/C++, Visual Basic, C# and JAVA.
Here marks is variable and 67 is constant. C
It was developed in early 1970s by Dennis Ritchie at Bell Laboratories. C
VII. Which of the following are valid C variables? Give the reason if not
has become one of the most popular programming languages today. It is a
a valid variable area, 5x, Sum, net pay, float, _age, else, case, size22,
highly structured programming language which is easy to understand and
my_weight?
use. In the past, it was mainly used for writing system programs such as
1. Valid Variables:
2. my_weight, area. size22, Sum operating systems, compilers, assemblers etc. Today it is used for writing all
3. Not Valid Variables: types of application programs. For example word- processing programs,
spreadsheet programs, database management systems, educational programs,
5x A variable can’t be start with number
games etc.
Net pay Space can’t be used in variable name
C++:
Float Data types can’t be used in variable name
C++ was developed by Bjarne Stroustrup also at Bell Laboratories during
_age Special character (_) can’t be used in variable name
1983- 1985. C++ is a superset of C, meaning that any valid C program is
Else Commands cannot be used as variable name also a valid C++ program. The purpose of developing C++ was to provide
Case Commands cannot be used as variable name programming facilities to easily and quickly write more powerful programs.
VIII. What is keyword or reserved word? Why not can be Visual Basic:
used as variable. Visual Basic (VB) is a high level language which evolved from the earlier
Keyword is a word in C++ language that has a predefined version called BASIC. BASIC stands for Beginner’s All-purpose Symbolic
PISK Computer Science GRADE 10 13
Instruction Code. VB is a very popular programming language for writing C language modules
windows and Web applications. It provides a graphical development 1. Creating and Editing a C program
environment. VB is commonly used for developing business programs such
Text editor is a simple word processor that is used to create and
as payroll system and inventory control program. The user can also write
edit source program. The process of writing C program is known
programs related with engineering, science, art, education, games etc.
as editing. It is process includes Writing, modifying and deleting
C#:
program statements.
C# (C-sharp) is a language developed in 2000 by Microsoft Corporation.
It is a simple, modern, general-purpose programming language. Syntax of 2. Compiler
C# is very similar to C and C++. It also has some features of Java. It is a A compiler is a program that converts the instruction of a high-
language that makes computer programming easy and efficient. It provides level language into machine language as a whole. The compiler
facilities to write Web applications that can be used across the internet. All converts the source program into machine code. The machine
types of programs including games, utilities, operating system, compilers, code program is known as object program.
business applications and Web based applications can be developed in C#. 3. Debugger or Interpreter
Java:
It is a software that checks each statement in the source program
Java is a high-level language developed by Sun Microsystem. It is very
line by line and generates machine instructions. That is why it is
similar in syntax to C and C++. Java is an ideal language for network
slower than compiler. It also checks syntax errors in program. A
computing. It is used for writing programs for many devices, computers and
source program containing an error cannot be executed.
networks. It is widely used in web applications. The current versions of most
4. Linker
of the Web browsers are made Java enabled.
Q4. What is C language IDE, Explain its modules in detail. The process of linking library files with object code is known as
C language IDE is computer software that brings all the processes and tools linking. A program that combines the object program with
required for program development into one place. Its aim is to make the life additional library files is known as linker. It is a part of compiler.
of programmers easier by grouping together all the tasks needed for building It combines object program and library files and saves the final
applications into one environment. Modern C/C++ IDEs has easy GUI machine language program as executable file.
interface which use IDE to create, compile and run programs.
PISK Computer Science GRADE 10 14
The program that places executable files in memory is called include preprocessor directive is used to include header files in
Q5. List out different rules for declaring variables in C++. Syntax #include <header file>
underscore (Like A or _A). Where stdio.h is a header file. Header files are collection of
ii. Blank spaces are not allowed in variable names (Like A standard library functions to perform different tasks like printf
1). and scanf.
iii. Variable may include number and underscore (Like A_1). Q. what is the use of "define" preprocessor?
iv. Reserved word cannot be used as variable name (like int
The define directive is used to define a constant. It starts with the
and if).
symbol #. It is not terminated with semicolon. It can be used
v. No restriction on length of variable name, however only
anywhere in the program
first 31 characters are significant.
vi. Upper and lower cases are allowed. For example, X2, x2, Syntax #define identifier= value
Q. what is preprocessor directive, what is the use of Example #define CUBE(A) (A*A*A)
"#include" preprocessor? and #include <stdio.h> # include<stdio.h>
The preprocessor directives are commands that give #define PI 3.14
instructions to C preprocessor. Preprocessor directives start with void main(void)
hash symbol # and the keyword include or define. These {
directives are written at the start of program. Two types of printf("%f",PI);
preprocessor directives used in C language are include and }
define. Like #include <stdio.h>
Q. What is constant qualifier? of 256 characters. C language character set contains the following set of
We use the const qualifier to declare a variable as constant. That means characters...
that we cannot change the value once the variable has been initialized. For
1. Alphabets
example,
2. Digits
const float PI=3.14;
3. Special Symbols
int main() {
const int x = 10; Alphabets
x = 12;
return 0;} /* This will show error because we are C language supports all the alphabets from the English language. Lower-
going to change constant value of x*/ and upper-case letters together support 52 alphabets.
Q. Explain character set in c language. C language supports a rich set of special symbols that include symbols to
perform mathematical operations, to check conditions, white spaces,
As every language contains a set of characters used to construct words,
backspaces, and other special symbols.
statements, etc., C language also has a set of characters which
include alphabets, digits, and special symbols. C language supports a total Special Symbols - ~ @ # $ % ^ & * ( ) _ - + = { } [ ] ;
PISK Computer Science GRADE 10 16
Q. what is an identifier? Give some examples of identifier. level language into machine language as a whole. The compiler
The identifiers are the names used to represent variable, converts the source program into machine code. The machine
constants, types, functions and labels in the program. code program is known as object program.
A type of identifier that has special meaning in C is known as The interpreter checks each statement in the source program and
standard identifier. C cannot use a standard identifier for its generates machine instructions. It also checks syntax errors in
original purpose if it is redefined; printf and scanf are examples program. A source program containing an error cannot be
The type of identifier that is defined by the programmer to An assembler is translating program that translates the
access memory location is known as user-defined identifier. The instruction of an assembly language into machine language.
user-defined identifiers are used to store data and program Q. what is a variable explain with types?
results. Some examples of user-defined identifiers are num, age, A variable is a named memory location or memory cell. It is
marks etc. used to store programs input data and its computational results
Q. what is meant by language processor? Explain different during execution. The value of variable may change during the
types of translators. execution of the program. However, the name of variable cannot
Language processor be changed. For example marks, avg and age etc. it may be up to
31 characters long.
Language processor or translator is software that converts the
programs of high-level languages into machine language. Every Types of variable
computer language has its own translators. Different types of C language provides following two types of variable.
translators include compiler, interpreter and assembler. 1. Numeric variable
1. Compiler It is used to represent numeric value. It has further two types
A compiler is a program that converts the instruction of a high
PISK Computer Science GRADE 10 17
Integer variable are the quantities which can hold both positive Syntax data type list of variables;
and negative numbers. Some examples of integer variable Example int marks;
A=5; B=-10; X=100; where 5, -10 and 100 are values Q. what is C known as strongly typed language?
b. floating point variable: C is a strongly typed language. It means that a variable must
Floating point variable are the quantities which can hold both always be declared before it can be used in a program. The
positive and negative numbers with fraction or decimal point. compiler gives an error if an undeclared variable is used in a
program.
Some examples of floating variable are A, B and X
Q. What is variable initialization?
A=5.5; B=-10.6; X=100.9; where 5.5, -10.6 and 100.9 are values
The process of assigning a value to a variable at the time of
II. Character variable:
declaration is known as variable initialization. The equal sign (
Any character variables are the quantities which can hold any
=) is used to initialize a variable. Variable name is written on the
singe character, number, or special character written in single
left side and the value is written on the right side of equal sign.
quotation mark. Some examples of character variable are A=
Syntax data type variables = value;
‘A’, B=‘9’, ‘=’ C= ‘$’ etc.
Example int marks =90;
String variable: A quantity that can store a collection of
characters in double quotation marks is called string variable. It Q. what is constant, explain different types of constants.
may consist of any alphabetic characters, digits and special A constant is a quantity that cannot be changed during program
symbols. For example A=“I love Pakistan” execution.
Types of constant Q. Name and give the purpose of basic data type available in
constants and character constants. int used to store whole numbers (integers).
It has two types (integer and float) char stores a single character including letters, punctuation
a. Integer constants: Integer constants are numeric values marks and digits.
consist of any alphabetic characters, digits and special symbols. American Standard Code for Information Interchange. The
For example “I love Pakistan” (string not included in scheme) ASCII code values of characters are used when they are added,
subtracted or compared.
PISK Computer Science GRADE 10 19
Comments are the lines of program that are not executed. The Implicit type casting
compiler ignores comments and does not include them in the Implicit type casting is performed automatically by the C
executable program. That is why the comments do not affect the Compiler. The operands in arithmetic operations must be of
size of executable program. similar types. If the data types of operand are different, the value
/* single line comments*/ with lower data type is converted into higher data type. Its
/* multiline var=type(expression)
Q. Why comments do not affect the size of program? Explicit type casting
The compiler ignores comments and does not include them in Explicit casting is performed by programmer. It is performed by
the executable program. That is why the comments do not affect using cast operator. The cast operator tells the computer to
the size of executable program. convert the data type of a value. Its Syntax is
var=type(expression)
Q. Why are comments used in C Program?
Example v=int(2.5*1.0 ) we shall get v=3
Comments are used to increase the readability of the program.
Comments are notes about different lines or code that explain the Q. Where comments can be added in programs?
purpose of code. The user can insert information notes in the Comments can be added anywhere in the code. Comments can
code. It helps in debugging and modifying the program later. be added in programs into 2 ways. The user can write comments
Q. What is type casting and explain its types? on single line or on multiple lines.
The process of converting the data type of value during Q. How are comments added on single line?
execution is known as type casting. The type castings are Comments on single lines are added by using double slash ‘//’ .
implicit type casting and explicit type casting. Its syntax is Anything written on right side of double slash is considered as
PISK Computer Science GRADE 10 20
Q.. Write a declaration statement and an assignment (input) 1. Integer data is the numeric value with no decimal point or
statement to assign 30 meter to a variable named length. fraction. It includes both positive and negative values. The minus
sign – is used to indicate negative value. If no sign is used the
int length;
value will be positive by default. Some examples of integer
length = 30;
values are 10,520 and -20
OR
Types of integer data
int length = 30;
C provides different types of integer data are
Q. Write the following statement in C: a = 2bc 3
I. 1. short int/int or short
a=2*b*c*c*c II. 2. long int or long
I. Int/short int data type:- value. If no sign is used then it is positive. Some examples of
This is used to represent integer values. An integer variable real values are 10.5, 5.3 and -10.91 etc.
stores a number with no fractional part such as 11,10 and 25 etc Types of floating pointes
.It makes 2 bytes in memory depending on the computer and I. float
compiler being used. it takes 2 bytes and its range is from -32768 II. double or double float
to +32767. III. long double or long double float
This is used to store only positive integer values. It takes 2 bytes This is used to store real values. It takes 4 bytes in memory. It
in memory. Its range is from 0 to 65,635.The unsigned int is also range from 10-38 to 1038.It provides the precision of six decimal
called unsigned. places.
This is rarely used because default value of integer is always This is used to store large real values. It takes 8 bytes in
signed. It takes 2 bytes in memory. memory. Its range is from 10-308 to 10+308.It provides the
This is used to store large integer values. It takes 4 bytes in III. Long double data type:
memory. Its range from -2,147,483,647 to 2,147,483,647. This is used to store very large values. Its range 10-4932 to
Unsigned long/ Unsigned long int:- This is used to store large 10+4932.It provides the precision of nineteen decimal places. It
positive values. It takes 4 bytes in memory. It range is from 0 to takes 10 bytes of memory or double memory as double data
The floating-point data is a numeric value with decimal point or This is used to store character constant values. It takes 1 bytes in
fraction. It is also called real type data. It includes both positive memory. Any character written single quotation mark is called
and negative values. The minus sign- is used to indicate negative character constant. Some examples of character data type here
PISK Computer Science GRADE 10 22
what to do. And how to do. There is also a less possibility of Language understand by computer is known as Low-level
errors in the programs. Some example are BASIC, FORTRAN, languages. These are machine language and assembly language.
COBOL, PASCAL and C. Machine language
Q. Differentiates between unstructured programming It is written in the form of 0 and 1. A program written in
language and structured programming language. machine language can be executed very fast by computer. The
In unstructured programming languages, the entire logic of computer does not need any translator to understand this
the program is implemented in a single module or function. The language.
programs written in these languages are error prone, difficult to Assembly language
understand, modify and debug.
Assembly language is a low-level language. It is one step higher
In structured programming languages, the entire logic of the than machine language. In assembly language, machine
program is divided into number of smaller modules or function. instructions are replaced with English-like words known as
Each module is a piece of code that implements a different mnemonics (like ADD, SUB).
functionality. There is also a less possibility of errors in the
2. High-level languages
programs. It consist of
Language near to human language is known as high-level
Sequence
languages. The instructions in these languages are similar to
Selection
English language such as input and print etc. These languages
Repetition
are easy to understand. Common high-level languages are
Object oriented language
C/C++, Java, Pascal, FORTRAN, BASIC, and COBOL.
Q. Explain different type of programming languages.
Q. Define source code and object code.
The languages understand by computer is known as
Source code
programming language. There are two types of computer
Source code is a computer program written in a high-level
programming languages (Low-level languages and high level).
programming language like C, C++, Visual Basic or Java. The
1. Low-level languages
source code cannot be executed by computer directly. It is
PISK Computer Science GRADE 10 24
converted into machine code and then executed. Example: program that print message “I Love Pakistan”.
A function is a block of code that performs a single task. The Q. define the term bug and debug.
main () function is the place where the execution of a C program An error in a computer program is known as bug. The
starts. When the program is executed, the control enters main () programmer can make different errors while writing programs.
function and starts executing its statements. Each program must The errors must be removed from the program before it can be
contain main () function. compiled and executed. The process of finding and removing
Body of main function Different types of errors are syntax errors, logical errors and run-
time errors.
}
PISK Computer Science GRADE 10 25
Syntax error is a type of error that occurs when an invalid Q. what is main difference between low-level and high-level
statement is written in program. Syntax errors are detected by languages?
compiler. A program containing syntax errors cannot be High–level languages are easy and low-level languages are
compiled successfully. Typing 'forr' instead of for' is an example difficult. Low-level languages provide more hardware support
of syntax error. then high-level languages. The programs written in low-level
Logical errors languages are faster in execution.
A type of error that occurs due to poor logic of the programmer Q. what do you mean by delimiters?
is known as logical error. A statement with logical error may The statements of the programs are written in curly braces. The
produce unexpected and wrong results in the program. Typing a curly brace {is called opening brace and} is called closing brace.
wrong formula to calculate the result is an example of logic The braces are also known as delimiters. These statements are
error. collectively known as the body of a program.
Run-time errors Q. Why C is called middle level language?
A type of error that occurs during the execution of program is C is called middle level language because we can write
known as run-time error. It is caused when a statement directs instruction in high level language as well as in low level
the computer to execute an illegal operation such as dividing a language using C language.
number by zero.
Q. describe the characteristics of high-level programming
Q. Why is logical error the most difficult error? languages.
• It cannot be detected by the compiler. 1,Easy to learn:
• It does not crash the program. That is why it is difficult to High level languages are closer to human languages and far
• The user needs to review the whole program to find out 2. Easy to error detection:
High level languages are easy to read and modify. it makes easy
PISK Computer Science GRADE 10 26
to find errors in programs written in high level languages. 70. It provided the basis for the development of C. the C
3. Standardized syntax: language was originally designed to write system programs
The syntaxes of high-level languages are standardized. These under UNIX operating system. The power and flexibility of C
languages describe a well-defined way of writing programs. made it popular industry for a wide range of application.
4. Deep hardware knowledge not required: The earlier version of C was known as K&R. The ANSI
developed a standard version of the language. It was known as
These languages do not require deep knowledge of hardware or
ANSI-C.
machine architecture. A programmer can write efficient
programs without a deep knowledge of hardware. Q. Briefly describe the basic structure of a C program.
Programs written in high level languages are shorter than low Preprocessor directive #include<stdio.h>
2. Creating and Editing a C program The process of running an executable file is known as
executing. The C++ program can be executed after compiling
The process of writing C program is known as editing. The
and linking. A program that places and executable file in the
process includes Writing, modifying and deleting program
memory is known as Loader. The program can be loaded
statements.
into memory by selecting Run > Run .
3. Saving a C Program:
The process of storing the program on disk is known as
Q, Write Components of System:
saving. A program should be saved on disk to be used
repeatedly. C programs are saved with .c extension. Following are different components of an information system:
as source program. The source program cannot be executed The floating point number can also be written in an exponential
by computer directly. A compiler converts the source or scientific notation .This notation represents large floating
program into object program and save it in a separate file. point number in a short way.
The object program is saved with .obj extension. Exponential notation
5. Linking a C program: The notation consists of two parts
The process of linking library files with object program is
1. Mantissa
known as linking. These files are used to accomplish
different tasks such as input/output. A Library file must be 2. Exponent
linked with the object file before execution of program. A The general form of writing floating point values in exponential
program that combines the object program with additional notation is as follow +m e +n
library files is known as Linker. It is part of C++ compiler. The value before e is mantissa and the value after e is exponent.
PISK Computer Science GRADE 10 28
The possible values that a floating type variable can store are
described in terms of precision and range
l Format Specifier
Example
A=5000 B=5000