0% found this document useful (0 votes)
18 views15 pages

Wa0008

The document provides an overview of programming in C, including definitions of computer programs, programming languages, and the differences between high-level and low-level languages. It explains characteristics of high-level languages, types of programming languages, and the role of translation software like compilers and interpreters. Additionally, it covers the components of an Integrated Development Environment (IDE) and the significance of reserved words and header files in programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views15 pages

Wa0008

The document provides an overview of programming in C, including definitions of computer programs, programming languages, and the differences between high-level and low-level languages. It explains characteristics of high-level languages, types of programming languages, and the role of translation software like compilers and interpreters. Additionally, it covers the components of an Integrated Development Environment (IDE) and the significance of reserved words and header files in programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

1

Unit-2: Programming in C Jinnah Jam-e School & College

Programming in C

Q1. Q. What is computer program?


Ans. Computer Program
“The set of instructions that tells a computer what to do is called computer program.”
 The programs are developed to perform different task according to user requirements.
 A computer works according to the instructions in a program.
 The computer programs are written by using different types of programming languages.
 A person who develops a computer program is called programmer.
Example of Programming Languages
C, C++, C# and JAVA etc.

Q. Differentiate between program syntax and program semantic?


Ans. Program Syntax
“Syntax refers to the general rule of programming language according to which statements
of a program is to be written.”
Program syntax defines the way to write correct statements in a program. Every
statement of a program is written according to its syntax. For example syntax for declaring
variable is:
Syntax
data type variable_name;
Where data type tells the compiler that which type of data is going to be stored in
variable and variable_name is any valid variable name.
Example
int sum;
Where int is the data type and sum is the variable name.

Program Semantic
“Program semantic describes meaningful statement of a programming language”.
It describes the sequence of questions to be performed by a computer when executing the
statements of a computer program.
For example:
 Multiply two variables x and y and store the result in variable Product.
Product = x * y;
 Adding two variables x and y and store the result in variable Sum.
Sum = x + y;

Q. Differentiate between Low level and high level languages.


Ans. The main difference between low level and high level program is as follow:
High Level Language Low Level Language
1. High level languages are easy to learn. 1. Low level languages are difficult to learn.
2. High level languages are near to human 2. Low level languages are near to

By: Khan Jadoon (MCS BED) Email: [email protected]


2
Unit-2: Programming in C Jinnah Jam-e School & College

languages. Computer hardware.


3. High level languages programs are slow in 3. Low level languages programs are fast
execution. in execution.
4. High level languages programs are easy to 4. Low level languages programs are
modify. difficult to modify.
5. Deep knowledge of hardware is not 5. Deep knowledge of hardware is
required to write programs required to write programs
6. These languages are normally used to 7. These languages are normally used to
write application software. write system software.
7. Example: 7. Example:
 Procedural Language  Machine Language
 OOP  Assembly Language

Q. What are the characteristics of high level languages?


Ans. Characteristics of High Level Languages
There are some common characteristics of high level languages:
1. Easy to Learn
The high Level Languages are easy to learn because they are close to human language.
The instructions written in high level languages are similar to English like words and
Statements. This makes the high level languages easy to learn and use.
2. Easy to Understand
The program written in high level language by one programmer can easily be
understood by another programmer because the program instructions are similar to the
English language.
3. Easy to Write Program
The Program written in high level language is shorter than the program written in low
level language. That’s why it easy to write program in high level languages.
4. Easy to Detect and Remove
The logic of the program written in high level languages is very simple and easy.. In case
of errors, it is very easy to find and remove errors in the program. It is also easy to modify
the program.
5. Built-in Library Functions
Every high level language provides a large number of Built in functions (or library
functions). These built in functions reduces the burden of work from programmer and lots
of time and effort of programmer is saved.
6. Machine Independent
The program written in high-level language is machine independent. It means that a
program written on one type of Computer can be executed on another type of computer.

Q. What is programming language? Explain different types of Low-Level


Languages.
Ans. Programming Languages
A set of words, symbols and codes used to write programs is called programming
language. Different types of programming languages are available for writing computer
programs. With the help of computer language, a programmer tells a computer what to do.
The computer programs are written according to the rules of programming language.

By: Khan Jadoon (MCS BED) Email: [email protected]


3
Unit-2: Programming in C Jinnah Jam-e School & College

Low-level Languages
Low level languages are near to computer hardware. Computer can easily understand
these languages. Writing a program in low level languages requires a deep knowledge of
computer hardware. There are two types of low level languages.
Types of low-level language
 Machine Language
 Assembly Language
1. Machine Language
 A type of language in which instructions are written in binary form (0, 1) is called machine
language.
 It is the only language that is directly understood by the computer.
 It is the fundamental language of the computer.
 Machine language programs are machine dependent.
 Machine language is also known as First Generation Language.
Advantages
 Execution of machine language program is very fast.
 There is no need of translation software for machine language program.
 Less memory is required.
Disadvantages
 Machine language programs are machine dependent.
 Machine language programs are difficult develop.
 Machine language programs are difficult to modify.
2. Assembly Language
 Assembly language is a type of low level language.
 It is one step higher than machine language.
 In assembly language, hardware names and symbols are used for programming instead of
binary code.
 These symbols are called mnemonics.
For example:
LOAD AC, 5
This instruction will store 5 in accumulator register AC.

 Assembly language is also called symbolic language.


 Assembly language is mostly used for writing system software.
 Assembly language is also known as Second Generation Language.
Advantages
 Assembly language is easy to understand and use.
 It is easier to modify program instructions.
 Less knowledge of hardware is needed.
Disadvantages
 Assembly language programs are long and occupy more memory.
 Assembly language programs are machine dependent.
 Knowledge of hardware is needed for programming.

By: Khan Jadoon (MCS BED) Email: [email protected]


4
Unit-2: Programming in C Jinnah Jam-e School & College

Q. What is High level Language? Explain any five types of High-Level


Languages.
Ans. High level Language
In the effort to make programming easier, computer programmers developed more advance
programming languages, which are called high level languages. The high level languages have
following features:
 A type of language that is close to human languages is called high level language.
 The program instructions of high level languages are written in English like words such as
input and print etc.
 The statements of high level languages are written according to some rule, and these rules
are called syntax of the programming language.
 The program written in high level language must be converted to machine code before
execution.
Examples of High level language
BASIC, C, C++, JAVA and Pascal etc.
Advantages
 Easy to learn and understand.
 Less time and effort is required to write program.
 Programs are Easy to modify.
 It is Easy to remove errors.
 Programs are Machine independent.
 Programs related to every field can be developed.
Disadvantages
 Slow program execution.
 More memory is required.
 Computer hardware cannot be programmed by the help of high level languages.

Types of High Level Languages


1. C Language
 C language is a popular high level language.
 It was developed by Dennis Ritchie at Bell Labs in 1972.
 It is a procedural programming language.
 C language programs are portable.
 The operating system UNIX was written in C.
 It is also known as middle level language because it provides the facilities to write
application software as well as system software.
2. C++ Language
 C++ was developed by Bjarne Stroustrup in early 1980’s.
 It is pronounced as “C plus plus”.
 It is an extension of C programming language.
 C++ includes all the elements of the C language.
 It is an object oriented programming (OOP) language, which allows programmers to
create objects within the code.
 Use of objects makes programming easier and more efficient.
Application of C++
Word processors and Spreadsheet software’s etc.

By: Khan Jadoon (MCS BED) Email: [email protected]


5
Unit-2: Programming in C Jinnah Jam-e School & College

3. Visual Programming Languages


 Visual programming are used for development of Graphical applications which support GUI
environment.
 Visual programming languages lets the programmer to develop program by using graphical
elements rather than textually.
 Sometime it is also called Fifth Generation Languages, and object oriented programming
languages.
Example
Visual Basic (VB) and Visual C++.
4. C # Language
 C# is pronounced as "C-sharp". C# is an object-oriented programming language.
 C# is high level language and itt was developed in 2000 by Microsoft.
 C# is based on C++ and contains features similar to Visual Basic and Java.
 C# is used to develop variety of applications like websites and developing tools like
compiler.
5. JAVA Language
 Java is a high level object oriented programming language.
 It is also called pure object oriented programming language.
 Java Programming language was released by Sun Microsystems in 1995.
 It is very similar to C++ but it is especially used for web page designing.
 Java applets are developed in Java language.
 Java applet is a small Java program that runs inside another program such as browser.
 It is embedded into web pages to rotate images, for animation and to access databases on
the Internet.

Q. What is meant by translation software? Differentiate between compiler and


interpreter?
Ans. Translation Software
“Translation software is a type of software which converts program other than machine
language into machine language”. There are three types of translation software:
 Compiler
 Interpreter
 Assembler
1. Compiler
Compiler is a type of translation software that translates high level language program into
machine language. Compiler translates the whole program into machine language at once and
then executes it. Compiler is the most suited translator for the small programs.
Programming Languages that use Compiler
C language, C++, Pascal and JAVA etc.

Working Diagram
Source Code Compiler Object Code

Error Message

By: Khan Jadoon (MCS BED) Email: [email protected]


6
Unit-2: Programming in C Jinnah Jam-e School & College

2. Interpreter
Interpreter is a type of translation software that translates high level language program into
machine language statement by statement. It converts one statement of a program into
machine language, executes it, and then move to the next statement. Each statement of a
program must be translated into machine language before execution. Interpreter is most suites
for large programs.
Programming Languages that use Interpreter
Java Script, BASIC, Visual Basic and Perl etc.

Working Diagram: Error Message

Source Code Interpreter Object Code

3. Assembler
Assembler is used to convert assembly language program into machine language. It is not
used with high level languages.

Q. What is IDE?
Ans. Integrated Development Environment (IDE)
 IDE stands for Integrated Development Environment.
 IDE include editor, compiler, linker and debugger.
 C language also provides IDE for developing C language programs.
 C language IDE is used to create, compile and run programs.
 In these days IDE provides GUI for developing programs.

Q. Define Programming Environment? Explain different parts/ modules of C


language IDE.
Ans. Programming Environment
 Programming environment consist of set of processes and programming tools that are used
to develop computer program.
 It includes editor, compiler, linker, debugger etc.
 It is also called Integrated Development Environment (IDE) because all the programming
tools (editor, compiler, linker and debugger) are available at one place for developing
computer programs.
Integrated Development Environment (IDE)
 IDE stands for Integrated Development Environment.
 IDE include editor, compiler, linker and debugger.
 C language also provides IDE for developing C language programs.
 C language IDE is used to create, compile and run programs.
 In these days IDE provides GUI for developing programs.
 C language series also uses IDE, such as Turbo C, Turbo C++ and C++ Builder etc.

By: Khan Jadoon (MCS BED) Email: [email protected]


7
Unit-2: Programming in C Jinnah Jam-e School & College

Modules of the C language IDE


The following are modules of the C programming environment:
i. Editor
An Editor is a part of IDE which is used to write C language program and then user can edit
it. When a program is saved then it has extension .c, e.g. sum.c and first.c etc.
ii. Compiler
A compiler is a part of IDE which is used to translates C language source program into
object program, that can be understood and executed by the computer.
After compilation the extension of C language compiled program is .obj. e.g. sum.obj and
first.obj. The process of compilation is shown in figure.

Source Code Compiler Machine Code

iii. Linker
A linker is a part of IDE which is used to link C language library function with object code and
combines them into a single executable program. The extension of C language executable
program is .exe. e.g. sum.exe and first.exe.
iv. Loader
The loader is a part of IDE which is used to load C language program from secondary
memory into main memory and then executes it.
v. Debugger
The Debugger is that part of IDE which is used to find and remove errors from C language
program and execute it.

Q. What are reserved words?


Ans: Reserved Words
Reserved words are also called keywords. Reserved words are those words which are
predefined in programming language. Every reserved word has some special purpose for which
it can be used. Reserved words cannot be used as the name of a variable, constant or function.
Each reserved word has fixed meaning and that cannot be change by user.
Note: All reserved words must be in lower-case letters
Examples
Following are some examples of reserved words used in C++:
int, float, char, main, for, do, while , switch, if and else etc.

Q. Differentiate between source program and object program.


Ans: Source Program
A program written other than machine language is called source program. Source program is
also called source code. The C language source program is stored with file extension .c. for
example first.c

By: Khan Jadoon (MCS BED) Email: [email protected]


8
Unit-2: Programming in C Jinnah Jam-e School & College

Object Program
When the source program is compiled and converted into machine code is called object
program. Object program is called also machine code or target code. It is stored in a new file
with extension .obj for example first.obj
Difference between Source program and Object program
Source Program Object Program
 Source program is written in high level or  Object program is written in machine
assembly language. language through compiler or assembler.
 Source program is easy to understand.  Object program is difficult to
Understand.
 Source program is easy to modify.  Object program is difficult to modify.
 Source program contains fewer  Object program contains more statements
statements that object program. that source program.

Q. What is Header file?


Ans: Header File
The header files are included at the top of a program. Header files are included in
program by using #inlude preprocessor directive. A user can include header file in a program
according to his requirements
The Header files are collection of library functions. When a header file is included in a
program then the all the library function defined in that header file becomes available in our
program. There are many header files available for different purpose in C language.
The extension of header file is .h. The #include preprocessor directive is used to include header
files in the program. The name of header file is written in angle brackets/double quotes.
Example
#include<stdio.h>
#include “stdio.h”
The word “stdio” stands for standard input/output. This header file contains the definitions of
built-in input and output functions which are printf() and scanf().
Some other Header files:
#include <maths.h>, #include<conio.h> and #include<string.h> etc.

Q. What are variables and constants? Differentiate them with examples.


Ans: Variable
“An element whose value may change during execution of the program is called variable.”
When a variable is declared then a memory location is reserved in memory for that variable and
data is stored into that memory location during the execution of program. Name of the
memory location, i.e. the variable name, remains fixed during execution of the program but the
data stored in that location may change from time to time.
Examples Variables
int sum;
int marks;
float pi;
char ch;

By: Khan Jadoon (MCS BED) Email: [email protected]


9
Unit-2: Programming in C Jinnah Jam-e School & College

Constant
“An element that does not change its value during the execution of program is called constant.”
These fixed values are also called literals. Constants are treated just like regular variables except
that their values cannot be changed after their definition. Constants can be of any of the data
types like an integer constant, a floating constant, a character constant, or a string constant.
Examples of Constant:
const char Name[15] = “Jinnah Jam-e”; const int a=60;
const float g=9.8;

Q. Write rules for naming variable or identifier?


Ans. Rules for Writing Variable Names
The symbol used of variable name is called an identifier or variable name. The following rules
must be followed while writing the name of variable in a C program:
1. Variable name is sequence of one or more letters, digits or underscore (_) characters.
2. First letter of variable must be alphabet or underscore (_), remaining characters can be
any combination of alphabets, digits, and underscore.
3. In no case, a variable name can begin with a digit.
4. Blank space is not allowed inside the name of variable.
5. Variable name should be meaningful.
6. Reserved word of C++ cannot be used as a variable name.
7. It should not contain any special character like #, ^ ,@ etc except (_).
8. A variable name cannot repeat in the program.
9. The length of a variable name depends upon the compiler of C++, but should not be too
long.
C++ is a case-sensitive language which means that a variable name in capital letters is not
equivalent to the one written in small letters
For example: Pay and pay or Roll_Num and roll_num are different variables.

Q. What is the purpose of const qualifier?


Ans: Constant Qualifier---const qualifier:
The const keyword is used to define a constant identifier whose values remain constant during
the whole life of program. const identifier must be assigned the value when declared, and then
that value cannot be changed
Syntax:
const data_type identifier = value;
Where
const is a keyword used to define a constant
data_type indicates the data type of the constant
identifier represents the name of the constant
value represents the value with which the constant is initialized.
Examples:
const int dollar_rate= 125;
const float pi=3.1416;

By: Khan Jadoon (MCS BED) Email: [email protected]


10
Unit-2: Programming in C Jinnah Jam-e School & College

Q. Write a note on general structure of C program.


Ans. Structure of C Program
General syntax of C program is given below.

Preprocessor directives <header file>


............
void main( )
{
Statement(s); Body of main function
}

Each C program has three main components.


 Preprocessor directives
 main function
 C statements
a. Preprocessor directives
The instructions that are compiled before the beginning of the actual program are called
preprocessor directives.
Every C program contains a preprocessor directive. The #include preprocessor directives
is commonly used to insert header files in a program. For example
#include <iostream.h>
#include<conio.h>
In C the other commonly used preprocessor directive in #define which is used to define
symbolic constants. For example
#define pi 3.1416
#define newline ‘\n’
b. main( ) function
The main( ) function is the point from where C program starts the execution. The main( )
function must be included in every C program. When C program is executed, the control
goes directly to the main( ) function.
void main( )
{
Statement(s); Body of main function
}
The statement within the main( ) function are the main body of the C program. If main( )
function is not included in a program then the program will generate an error message.
c. Body of main( )/C statements
The C language statements of a program are written under the main( ) function between the { }.
These statements are called body of the program. Each statement ends with semicolon (;).
The C language is a case sensitive language. The C statements are usually written in lowercase.
The C program statements are the actual instructions to the computer for taking an action.
For example
printf( “Pakistan Zindabad”);
This statement performs the only action of displaying the message “Pakistan Zindabad” on the
screen.

By: Khan Jadoon (MCS BED) Email: [email protected]


11
Unit-2: Programming in C Jinnah Jam-e School & College

Q. What is meant by comments? Explain types of comments used in C language.


Ans. Comments in C
Comments are the part of source code that is ignored by the compiler. They do nothing
but simply allow programmer to insert notes/remarks within source code for clarifying the
logic of statement.
In C language there are two types of comments can be used:
 Single line comment
 Multiline comment
a. Single line comment
The comments after double slash // are single line comments. It discards everything from
the part where // starts up to the end of that line.
b. Multiline comment
The comments enclosed in /*…………………………*/ are called multiline comments. The
characters between /* and */ are ignored by the compiler. These comments can include
more than one line.
Example
Program
// program for use of comments
#include<stdio.h>
#include<conio.h>
void main( ) // main function starts
{
printf “Welcome in C languae”
getch( );
/*getch is used to echo the
output on computer screen*/

Q. What are variables and constants? Differentiate them with examples.


Ans: Variable
“An element whose value may change during execution of the program is called variable.”
When a variable is declared then a memory location is reserved in memory for it and data is
stored into that memory location during the execution of program. Name of the memory
location, i.e. the variable name, remains fixed during execution of the program but the data
stored in that location may change from time to time.
Examples Variables
int sum;
int marks;
float pi;
char ch;

Constant
“An element that does not change its value during the execution of program is called constant.”
The constants are also called literals. Constants are treated like regular variables except that
their values cannot be changed after their definition. Constants can be of any of the data types
like an integer constant, a floating constant, a character constant, or a string constant.

By: Khan Jadoon (MCS BED) Email: [email protected]


12
Unit-2: Programming in C Jinnah Jam-e School & College

The const keyword is used for declaring variables.


Examples of Constant:
const char Name[15] = “Jinnah Jam-e”; (String enclosed in double quotes)
const int a=60;
const float g=9.8;

Q. Explain different data types used in C-language with examples.


Ans: Data Type used in C language
While programming, variables are used to store data in memory. And variables store data
according to their data type. Each variable must be assigned a specific data type that tells the
compiler that what types of data is going to be stored in it. The common data types used in C++
for different types of data are:
 Integer (int, short int, long int)
 Floating Point (float, double, long double)
 Character (char)
 Boolean
 Unsigned
a. Integer Data Types

Integers are whole numbers without decimal point. Integers include both positive and
negative values, but no decimal values. In C language for storing integer data following data
types are used.

i. int
ii. long
iii. unsigned int

i. int
The keyword int stands for the integer data type in C language. It is used to store integer
values. It occupies 2 bytes in computer memory. It can store values in the range of -32768 to
32768.
For example:
int a;
int sum;

ii. long int


The keyword long int stands for long integer in C language. It is used to store longer integer
values. It occupies 4 bytes in computer memory. It can store values in the range of -
2147483648 to +2147483647.
For example:
long int x;
long int y;

iii. unsigned int


The unsigned int is used to store integer data but wiout sign bit. It occupies 2 bytes in computer
memory. Its range is from 0 to 65,535.

For example:
Unsigned int x;
Unsigned int y;

By: Khan Jadoon (MCS BED) Email: [email protected]


13
Unit-2: Programming in C Jinnah Jam-e School & College

Variable Declaration No. of Bytes Range


int 2 -32,768 to +32,767
long 4 -2,147,483,648 to +2,147,483,647
unsigned int 2 0 to 65,535
b. Floating Point Data Types
The values which contain decimal point are called floating point values. The data types
which are used to store such values are called floating point data types. There are three
different types of floating point data types:
1. float
2. double
3. long double
i. float
In C language the float data type is used to declare floating point type of variables. It
occupies 4 bytes in computer memory.
For example:
float pi;
float g;
ii. double
The double keyword is used to represent double precision floating point values in C in C
language. The double data type occupies 8 bytes in computer memory.
iii. long double
In C language the long double data type is used to store long double type floating point
values and represent long double precision. It occupies 10 bytes in computer memory.
Variable Declaration No. of Bytes Range
float 4 3.4 × 10 to 3.4 × 1038
-38

double 8 1.7 × 10-308 to 1.7 × 10308


long double 10 3.4 × 10-4932 to 3.4 × 104932

c. Character (char) Data Type:


The keyword char represents character data type in C language. Any character belonging to
ASCII character set is called a character data type. It occupies 1 byte in computer memory.
Character variable can store only one character.

Example
char gender;
gender = ‘M’;
Variable No. of Bytes Range
Declaration
char 1 0 to 255

Q. How variables are declared and initialized in C language? Give examples.


Ans. Declaration of Variables
In order to use a variable in C program, we must declare it. The variable declaration
specifies that which type of data variable will store and memory is reserved for that variable
according to data type.

By: Khan Jadoon (MCS BED) Email: [email protected]


14
Unit-2: Programming in C Jinnah Jam-e School & College

Syntax
Data Type variable_name;
Where,
Data Type specifies data type of variables. For example, int, float and char etc.
Variable_name variable_name is the name of variable.
Example
int sum;

This statement declares a variable sum which will store integer values in it. When this statement
executes 2bytes memory in RAM will be reserved for variable sum.
More than one variables of the same data type can be declared in a single statement separating
with commas. For example:
int a, b, c;
This statement will declare three variables a, b and c all of type int.

Initialization of Variables
Assigning a value to a variable at the time of its declaration is called initialization of the variable.
For Examples:
To declare a variable a of integer data type and assigning value 110 to it the statement will be
as:
int a = 110;
In above statement variable is assigned the value during declaration is called
initialization of variable.

Q. What is type casting? What is the difference between implicit type casting
and explicit type casting?
Ans. Type Casting
The conversion of data from one type to another is called type casting. There are two types
of type casting:
 Implicit type casting
 Explicit type casting
1. Implicit type casting
In implicit type casting, the compiler automatically converts data from one type to another
the compiler implicitly (unconditionally) converts that value in the value of the new type.
For example:
float n=3;
Implicit casting the integer value of n=3 to double value of n=3.0
int b=3.1416;
Implicit casting the fractional value of b=3.1416 to integer value b=3
2. Explicit type casting
To convert one data type to another by new type enclosed in parentheses ( ) is called explicit
type casting. For example:

By: Khan Jadoon (MCS BED) Email: [email protected]


15
Unit-2: Programming in C Jinnah Jam-e School & College

int x;
float pi= 3.1416;
x = (int) pi;

In above example the float value of pi=3.1416 is converted to an integer vlue 3 and remainder is
discarded. Here the type casting operator is (int). Another way for explicit type casting is to
enclose expression in parentheses. For example
x= int (pi);

By: Khan Jadoon (MCS BED) Email: [email protected]

You might also like