0% found this document useful (0 votes)
20 views32 pages

Introductionof C Part 1

Uploaded by

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

Introductionof C Part 1

Uploaded by

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

 Session I

 Introduction to Programming Language.


 Introduction to C Programming
Language
Objectives
 What is Program?
 What is Programming Language?
 Types of Programming Language.
 Structured Programming.
 History of C Programming Language.
 Variables, Constants, Keywords.
 Data Types.
 C Instructions.
 Type Declaration Instruction.
 Arithmetic Instruction.
 Displaying Output
 Receiving Inputs.
 main() function.
 A simple C program.
 Compilation & Execution.
Program & Programming Language

 Program is a set of ordered instructions, which are


sequentially executed to perform a specific task for
any electronic device.

 The language is simply a set of notations or symbols


to express our thoughts. We can communicate each
other using any languages (e.g. English, Hindi,
Bengali, etc.) . Every language has it’s own rules to
write something.

 A programming language is a language used to write


computer programs. It defines set of notation and
rules for writing programs.
Types of Programming Language
 Programming languages fall into three broad
categories:

 Machine Level Language:

 Assembly Level Language:

 High Level Language:


Types of Programming Language
(Contd.)
Machine Level Language:
Machine Level Language of a computer is written into as
series of binary 0 and 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.

—Difficult to write program.

—Difficult to modify program.


Types of Programming Language
(Contd.)
Assembly Level Language:
Assembly Level Language of a computer is written
using alphanumeric mnemonic codes (e.g. ADD, SUB, START).

Advantages:
—Easier to understand and use.

—Easier to modify.

Limitations:
—Machine Dependent.

—Translator (Assembler) required.

—Knowledge of hardware required.

—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.

—Easier to write program.

—Easier to modify program.

Limitations:
—Translator (Compiler / Interpreter) required.

—Execution time is higher than Machine Level & Assembly Level

Language .
Structured Programming

• Structured programming techniques assist the


programmer in writing effective error free
programs.

• The elements of structured of programming


include:

 Top-down development.

 Modular design.
Structured Programming (Contd.)

• The Structure Theorem:


It is possible to write any computer program by using only
three (3) basic control structures, namely:

Sequential

 Selection (if-then-else)

 Advanced Selection (Switch Case)

 Repetition (looping-for, while, do-while)


History of C Programming
Language
• The C Programming language was written by Dennis Ritchie of
Bell Laboratories in 1972.
• C is based on the languages BCPL and B, hence the name C. B
was written in 1970 by Ken Thompson.
• C is a relatively “low level” language.
• Neither does the C programming language has any way of
communicating with the outside world.
• These operations are provided by a collection of standard
library functions which are available with every
implementation of C (i.e. C compiler).
Constants, Variables, Keywords

• 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.

Ex: 462, +782, -675, etc.


Constants, Variables, Keywords
(Contd.)
• Rules For Constructing Real Constants:
— A real constant must have at least one digit.
— It must have a decimal point.
— It can be either positive or negative.
— Default sign is positive.
— No comas or blanks are allowed within a real constant.

Ex: 46.2, +7.82, -675.0, etc.


Constants, Variables, Keywords
(Contd.)
• Rules For Constructing Character Constants:
— A Character constant must be enclosed within a single
inverted Commas.
— Both the inverted commas should point to the left.
— It could be a single alphabet, a single digit or a single
special symbol.
— The maximum length of a character constant must be 1
character.

Ex: ’A’, ’5’, ’=’, etc.


Constants, Variables, Keywords
(Contd.)

• 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

x=4 Memory Location


Value of x at any execution
x 3
Name of the
memory location Value of x at any
x 4 another execution
Constants, Variables, Keywords
(Contd.)
• Rules For Constructing Variable Names:
— A variable name could be formed using any combination of 1 to 26
alphabets in English language, 0 to 9 decimal digits, and also a special
symbol “underscore” (_) .

— The first character in the variable name must be an alphabet or underscore


. It can’t be a digit.

— No comas or blanks are allowed within a variable name.

— No special symbol other than an underscore (e.g. gross_sal) can be used


in a variable name.

— The variable name should reflects its purpose of usage.

Ex: sum, sal_emp, result1, result2, _name, etc.


Constants, Variables, Keywords
(Contd.)
• Keywords:
— Keywords are the words whose meaning has already been
explained to the C compiler .
— The Keywords cannot be used as variable names.
— There are only 32 keywords available in C.
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
Data Types

• 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.

The variable sum can hold only the


int
int sum;
sum; integer constant values like 1,2,3,……
and +, -, *, /,…… can be applied .
Variable
Variable name
name

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.

Ex: int sum, float area, char choice, double salary,……….


Data Types(Contd.)

• The C language provides a lot of basic types. Most of


them are formed from one of the four basic arithmetic type
identifiers in C (char, int, float and double), and optional
specifiers (signed, unsigned, short, long).
Type Length Range
char 8 bits -128 to 127
int 16 bits -32,768 to 32,767
float 32 bits 3.4*(10-38) to 3.4*(1038)
double 64 bits 1.7*(10-308) to 1.7*(10308)
unsigned char 8 bits 0 to 255
unsigned int 16 bits 0 to 65,535
short int 16 bits -32,768 to 32,767
long int 32 bits -2,147,483,648 to 2,147,483,647
unsigned long int 32 bits 0 to 4,294,967,295
long double 80 bits 3.4*(10-4932) to 1.1*(104932)
C Instruction

There are basically three types of instructions in C:

C
C Instructions
Instructions

Type
Type Declaration
Declaration Arithmetic
Arithmetic Instruction
Instruction Control
Control Instruction
Instruction
Instruction
Instruction

Type Declaration Arithmetic Instruction


Control Instruction is
Instruction is used to is used to perform
used to control the
declare the type of arithmetic operations
sequence of execution
variables used in a C between constants and
of various statements
program. variables in a C
in a C program.
program.
C Instruction (Contd.)
• Type Declaration Instruction:

—Any type of variable used in a C program must be


declared before using it in further instructions.
Ex: int range, float salary, char code,…………

• Arithmetic Instruction:
—A C arithmetic instruction consists of a variable name on

the left hand side of equals to (=) operator and variable


names and constants on the right hand side of = operator
are connected by arithmetic operators like +, -, *,……...

Ex: b=a + 2, c=a + b, x=4,………


Displaying Output
 printf() is a library function which is already defined by
the C compiler to print something into output device.

 The general form of printf() function is:


printf(“<format string>”,<list of variables>)

 <format string> can contain:


—%f for printing real values.
—%d for printing integer values.
—%c for printing character values.
—Any character other than the above described
format specifiers. These characters are printed in a
order as they are written, when printf() is executed.
Displaying Output (Contd.)
 Output of printf function is right justified.
 In printf() function compiler calculates the
values from right to left, but prints the values
from left to right.
 printf() function can not only print the values of
the variables, but also it can print the result of
an expression.
 Backslash “\” (escape character) are used to
change the meaning of the next character after
it. The backslash means “give the next
character its alternate meaning.
Displaying Output (Contd.)

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:

scanf(“<format string>”,<list of variables preceded


by &>)
 <format string> can contain same format
specifiers used in printf() function .
 A blank (using spacebar), tab (using Tab key),

newline(using Enter key) must separate the


values supplied to the scanf() function. These
whitespace character are skipped when scanf()
receive a value from input device.
Receiving Input (Contd.)

EX:
scanf(“%d”, &x)

• The ‘&’ is an “Address of” operator. It give the location


number used by the variable in memory.

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

in which programmer wish them to be executed.


 Blank spaces should be inserted between two words to

improve the readability of the statements.


 All statements are entered in small case letter.

 C has no specific rules for position at which a statements is to

be written.
 Every C statements must end with a ‘;’.

 Every C program must include some pre-defined header file

using directive preprocessor like #include<stdio.h>,


#include<conio.h>, etc. The detailed description of C
preprocessors will be discussed in further session.
A simple C Program(Contd.)

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?

Loads the library function


Loader
Loader Linker
Linker

Links the library function with object file

Execution Executable
Executable file
file
Desired
Desired output
output (Addition.exe)
(Addition.exe)

You might also like