Wa0008
Wa0008
Programming in C
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;
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.
Working Diagram
Source Code Compiler Object Code
Error Message
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.
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.
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.
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.
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;
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.
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;
For example:
Unsigned int x;
Unsigned int y;
Example
char gender;
gender = ‘M’;
Variable No. of Bytes Range
Declaration
char 1 0 to 255
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:
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);