2-Introduction to Structured Programming - C language History and Introduction - Datatypes-Basic IO statements - Variables - Keywords - Expression evaluation using operator precedence-17-12-2024
2-Introduction to Structured Programming - C language History and Introduction - Datatypes-Basic IO statements - Variables - Keywords - Expression evaluation using operator precedence-17-12-2024
oriented programming
Dr. P.SURESH
Associate Professor
School of Computer Science &
Engineering(SCOPE)
VIT,Vellore.
BCSE102L- Structured and object-oriented programming
BCSE102L- Structured and object-
oriented programming – Text Books and
Reference Books
BCSE102P- Structured and object-
oriented programming Laboratory
BCSE102L- Structured and Object-
Oriented Programming
Module-1:C Program Fundamentals -
Introduction
◦ Variables
◦ Reserved Words
◦ Data types
◦ Expressions
◦ Type Conversions
◦ I/O Statements
.
BCSE102L- Structured and Object-
Oriented Programming
Module-1: C Program Fundamentals –
Branching and Looping
◦ if, if-else, nested if, else-if ladder
◦ Switch Statement
◦ Goto Statement
◦ Looping
For
◦ Break Statement
◦ Continue Statement
.
Getting Started with C
Communicating with a computer involves speaking the
language the computer understands.
What is the learning method of English??
Steps in learning C
C Character Set
A character denotes any alphabet, digit or special
symbol used to represent information.
Constants,Variables and Keywords
Alphabets, Digits and special symbols when properly
combined form constants, variables and keywords.
Constant is an entity that doesn’t change whereas a variable
is an entity that may change.
For Example: 3 is stored in a memory location and a name
“x” is given to it , Then assign a new value 5 to the same
memory location “x” – It overwrites earlier value 3 since
memory location can hold only one value at a time.
Fractional Form
1. A real constant must have at least one digit.
2. It must have a decimal point.
3. It could be either positive or negative.
4. Default sign is positive.
5. No commas or blanks are allowed within a real constant.
Compiler:
Interpreter:
Assembler:
%d – integer values
%f – float values
%c – character values
etc…………….
Purpose of scanf()
The Programs should ask the user to supply the values
of any variable through the keyboard during execution
is done by using scanf function.
# include <stdio.h> Scanf(“Control String”, arg1, arg2, ………..argn)
int main( ) & - “Address of “- it gives
{ location number(Address)
Used by the variable in the
int p, n ; memory
float r, si ;
printf ( "Enter values of p, n, r" ) ;
scanf ( "%d %d %f", &p, &n, &r ) ;
si = p * n * r / 100 ;
printf ( "%f\n" , si ) ;
return 0 ;
}
INSTRUCTIONS
Types of Instructions
◦ Type Declaration Instruction – Instruction used
to declare the type of variables used in a C program.
◦ Arithmetic Instruction – Instruction used to
perform arithmetic operations on constants and
variables.
◦ Control Instruction – Instruction used to control
the sequence of execution of various statements in
a C program.
STRUCTURE OF C PROGRAM
Formatted Output
Formatted Input
Formatted Output - printf()
SYNTAX:
Format string:
1. Characters that will printed on the screen
2. Format specifications that define the output format for display
of each item
3. Escape Character sequences such as \n, \t, \b
Example
printf(“%d\n”,1234); // Normal way
printf(“%7d\n”,1234); // right Justified
printf(“%2d\n”,1234);
printf(“%-7d\n”,1234); // left justified
printf(“%07d\n”,1234);// padding zeros
% w.p f % w.p e
Output of Characters/ Single character
char x= ‘I’;
printf(“%c\n”,x);
printf(“%3c\n”,x);
printf(“%5c\n”,x);
printf(“%-3c\n”,x);
printf(“%-5c\n”,x);
% wc
Output of Characters/ Strings
char name[25]=“ VIT VELLORE TAMILNADU”
printf(“%s\n”,name);
printf(“%25s\n”,name);
printf(“%25.10s\n”,name);
printf(“%.5s\n”,name); % w.ps
printf(“%-25.10s\n”,name);
printf(“%5s\n”,name);
Format string:
1. Data to be entered- consisting of % (conversion
character), datatype character and optional number
specifying the field width.
List of Variables/Arguments:
1. List of variable i.e arguments specify the address of the
locations where the data to be stored.
2. Escape Character sequences such as \n, \t, \b
Inputting Integer numbers
%wd
% - conversion character
w- integer number that specifies field width of the
number to be read.
d – data type character
Example
scanf(“%d\n”,&n); // Normal way
scanf(“%2d\n”,&n);
scanf(“%5d\n”,&n);
scanf(“%2d %5d\n”,&n1,&n2); // 85 12345
scanf(“%d %d\n”,&n1,&n2);
Inputting Real numbers
Example
char name1[10], name2[20];
scanf(“%15c\n”,&name1);
scanf(“%s\n”,&name1);
scanf(“%15s\n”,&name2);
Reading Mixed Data types
Example:
scanf(“%s %c %d %f \n”,&name,&gender,&age, &salary);
Thank You