Introductionof C Part 1
Introductionof C Part 1
Advantages:
—Machine Level Language can be executed very fast by
computer.
—Any type of translation of machine level language is not
required.
Limitations:
—Machine Dependent.
Advantages:
—Easier to understand and use.
—Easier to modify.
Limitations:
—Machine Dependent.
—Time consuming.
Types of Programming Language
(Contd.)
High Level Language:
High Level Language of a computer is written using
English Words and familiar mathematical symbols and
expressions.
Advantages:
—Machine Independent.
Limitations:
—Translator (Compiler / Interpreter) required.
Language .
Structured Programming
Top-down development.
Modular design.
Structured Programming (Contd.)
Sequential
Selection (if-then-else)
• Constants:
A constant is an entity that doesn’t change its value.
C
C Constant
Constant
Primary Secondary
Secondary
Primary Constant
Constant
Constant
Constant
Array
Array
Integer
Integer Constant
Constant (1,
(1, 2,
2, 3…)
3…)
Pointer
Pointer
Real
Real Constant
Constant (1.2,
(1.2, 2.5,
2.5, 3.0…)
3.0…)
Structure
Structure
Character
Character Constant
Constant (’A’,
(’A’, ’b’,
’b’, ’C’…)
’C’…)
Union
Union etc.
etc.
Constants, Variables, Keywords
(Contd.)
• Rules For Constructing Integer Constants:
— An integer constant must have at least one digit.
— It must not have a decimal point.
— It can be either positive or negative.
— If no sign precedes an integer constant, it is assumed to
positive.
— No comas or blanks are allowed within an integer constant.
• Variables:
— An entity that may vary its value during program execution is called
variable.
— Variables names are names given to locations in memory.
— These locations can contain some numeric or character values.
Ex: x=3
• Data type defines the set of data and the operations can
be applied on to the particular type of data (e.g. {1, 2, 3,…….
…… +, -, /, *…….}).
• It also defines the size of the memory blocks occupied
by the compiler.
Data
Data Types
Types Sum
Memory location
Size=2bytes
Data Types(Contd.)
C
C Data
Data Types
Types
Primary
Primary Data
Data Types
Types Secondary
Secondary Data
Data Types
Types
int
int Array
Array
float
float Pointer
Pointer
double
double Structure
Structure
char,
char, etc
etc Union
Union etc.
etc.
C
C Instructions
Instructions
Type
Type Declaration
Declaration Arithmetic
Arithmetic Instruction
Instruction Control
Control Instruction
Instruction
Instruction
Instruction
• Arithmetic Instruction:
—A C arithmetic instruction consists of a variable name on
Ex:
Code Output
printf(“%d”,x) Print the value of integer variable x.
Print the value of integer variable sum,
printf(“%d %f %c ”,sum,sal,code) floating point variable sal and also the
character variable code.
Print “Simple Interest” the value of variable
printf(“\“Simple Interest\”%f \
si and “Rate” the value of variable r in a
n \“Rate\”%f”,si,r)
newline.
Receiving Input
scanf() is a library function which is already
defined by the C compiler to take some values
from input device.
The general form of printf() function is:
EX:
scanf(“%d”, &x)
scanf(“%d”,
x Memory
&x) 3
Location
1001
Name of the variables
Location number
The main() function
main() function is the entry point of any C program.
main() function is a collective name given to a set of instructions.
It can not be anything else.
All the instructions that belongs to main() function are enclosed
within a pair of braces ‘{ }’.
Like other C functions it has a return type named void. void is a
keyword in C. It means nothing.
The detailed discussion about of function will be delivered in
further session.
Ex:
void
void main()
main()
{{
Instruction1
Instruction1
Instruction2
Instruction2
-----------------
-----------------
-----------------
-----------------
}}
A simple C Program
Rules for writing C program:
Each instruction in a C program is written as a separate
statement.
The statements in C program must appear in the same order
be written.
Every C statements must end with a ‘;’.
Addition.C
Addition.C
#include<stdio.h>
#include<stdio.h>
#include<conio.h>
#include<conio.h>
void
void main()
main()
{{
int
int a,b,c;
a,b,c;
clrscr();
clrscr();
printf("\nEnter
printf("\nEnter the
the first
first value:");
value:");
scanf("%d",&a);
scanf("%d",&a);
printf("\nEnter
printf("\nEnter the
the second
second value:");
value:");
scanf("%d",&b);
scanf("%d",&b);
c=a+b;
c=a+b;
printf(“\nThe
printf(“\nThe sum
sum of
of two
two integers
integers is=%d",c);
is=%d",c);
getch();
getch();
}}
Compilation & Execution
• Write a program using editor (Turbo C).
• Save the file with .c extension.
• To compile the program you can select compile from compile
menu and press enter or you can press Alt+f9.
• It will check the errors if program has any error it will terminate the
program then you can find out the error and you can correct it.
After correcting the error you can compile the program again.
After compiling successfully it will create an object (.obj) file.
• Once the source program has been converted into an object file, it
is still not in the form that you can run. The reason behind this is
that there may be some references to standard library functions or
user-defined functions in other object files. To run the object file
press Ctrl+F9 . It will create an executable (with .exe extension) file
and display the desired output in output device.
Compilation & Execution (Contd.)
Compilation
Source
Source Code
Code Object
Object File
File
Compiler
Compiler
(Addition.C)
(Addition.C) (Addition.obj)
(Addition.obj)
Yes Synta
Synta No
xx
Error?
Error?
Execution Executable
Executable file
file
Desired
Desired output
output (Addition.exe)
(Addition.exe)