1 Introduction
1 Introduction
Some Terminologies
◻ Algorithm / Flowchart
🞑A step-by-step procedure for solving a particular
problem.
🞑Independent of the programming language.
◻ Program
🞑A translation of the algorithm/flowchart into a form
that can be processed by a computer.
🞑Typically written in a high-level language like C,
C++, Java, etc.
Variables and Constants
◻ Most important concept for problem
solving using computers
Address N-1
Variables in Memory
X = 10 10
T
i X = 20 20
m
e
X=X+1 21
X=X*5 105
Variables in Memory…
Variable
Instruction executed X Y
X = 20 20 ?
T
i Y = 15 20 15
m
e
X=Y+3 18 15
Y=X/6 18 3
Data Types
◻ Three common data types used:
Computation
Input / Output
Decision Box
Start / Stop
Flowchart: basic symbols…
Flow of
control
Connector
Example 1: Adding three numbers
START
READ A,
B, C
S=A+B+C
OUTPUT
S
STOP
Example 2: Larger of two numbers
START
READ X,
Y
YES IS NO
X>Y?
OUTPUT OUTPUT
X Y
STOP STOP
Example 3: Largest of three numbers
START
READ X,
Y, Z
YES IS NO
X > Y?
Max = X Max = Y
IS
YES NO
Max >
OUTPUT Z? OUTPUT
Max Z
STOP STOP
Example 4: Sum of first N natural numbers
START
READ N
SUM = 0
COUNT = 1
COUNT = COUNT + 1
NO IS YES OUTPUT
COUNT > SUM
N?
STOP
Example 5: SUM = 1 + 2 + 3 + N
2 2 2 2
START
READ N
SUM = 0
COUNT = 1
COUNT = COUNT +
1
NO IS YES OUTPUT
COUNT >
SUM
N?
STOP
Example 6: SUM = 1.2 + 2.3 + 3.4 + to N terms
START
READ N
SUM = 0
COUNT = 1
STOP
Example 7: Computing Factorial
START
READ N
PROD = 1
COUNT = 1
COUNT = COUNT + 1
NO IS YES OUTPUT
COUNT >
PROD
N?
STOP
Example 8: Roots of a quadratic equation
ax2 + bx + c = 0
TRY YOURSELF
Example 9: Grade computation
MARKS ≥ 90 🡺 Ex
89 ≥ MARKS ≥ 80 🡺 A
79 ≥ MARKS ≥ 70 🡺 B
69 ≥ MARKS ≥ 60 🡺 C
59 ≥ MARKS ≥ 50 🡺 D
49 ≥ MARKS ≥ 35 🡺 P
34 ≥ MARKS 🡺 F
Example 9: Grade computation…
START
READ
MARKS
Preprocessor
#include
<stdio.h>
/* This program Both of these programs are exactly
prints “Hello
the same as the original as far as
World”
*/
your compiler is concerned.
int
main( )
{
printf(“Hello
World!
\n”)
;
}
The C Character Set
◻ The C language alphabet:
🞑Uppercase letters ‘A’ to ‘Z’
🞑Lowercase letters ‘a’ to ‘z’
🞑Digits ‘0’ to ‘9’
🞑Certain special characters:
! # % ^ & * ( )
- _ + = ~ [ ] \
| ; : ‘ “ { } ,
Numeric Character
Constants Constants
#include <stdio.h>
main( )
{
printf(“%d %d %d \n”, 32767, 32767+1, 32767+10);
printf(“%ld %ld %ld \n”, 32767L, 32767L+1L,
32767L+10L);
Output:
Integral Types
Integer character
signed unsigned type char
int unsigned int signed char
short int unsigned short int unsigned char
long int unsigned long int
Floating point type
float void
double
long double
Data Types…
Type Size - bits(bytes) Range
char or signed char 8(1 byte) -128 to +127
unsigned char 8(1) 0 to 255
int or signed int 16(2) -32768 to +32767
unsigned int 16(2) 0 to 65535
short int or signed short 8(1) -128 to +127
int
unsigned short int 8(1) 0 to 255
long int or signed long int 32(4) -2147483648 to
+2147483647
unsigned long int 32(4) 0 to 4294967295
float 32(4) 3.4E-38 to 3.4E+38
double 64(8) 1.7E-308 to 1.7E+308
long double 80(10) 3.4E-4932 to 3.4E+4932
Declaration of Variables
◻ Declaration does two things:
🞑Tells the compiler what the variable name is
🞑Specifies what type of data the variable will hold
◻ Primary type declaration
data-type v1,v2, … vn;
◻ Valid declarations are
🞑int count;
🞑int number, total;
🞑double ratio;
Data types and their keyword equivalents
main()
{
float x,y;/8
int code;/2
short int count;/1
long int amount;/4
double deviation;/8
unsigned n;/2
char c;/1
}
User defined type declaration
◻ Allow user to define an identifier that would
represent an existing data type
◻ The user defined data type can later be used
to declare variables
typedef type identifier;
◻ e.g. typedef int units;
typedef float marks;
units batch1, batch2;
marks stud_1, stud_2;
User defined type declaration…
◻ Another user-defined data type is enumerated
data type
enum identifier {value1, value2, …
valuen};
1 2 3
enum identifier v1, v2, …vn;
0
v1=value3; v2=valuen;
enum day {Monday, Tuesday, Friday, Sunday};
enum day week_st, week_end;
week_st = Monday;
week_end = Sunday;
User defined type declaration…
◻ Another user-defined data type is enumerated
data type
enum identifier {value1, value2, …
valuen};
2 v1, v2,
enum identifier 3 …vn; 4
1
v1=value3; v2=valuen;
enum day {Monday=1, Tuesday=2, Friday=3,
Sunday=4} week_st, week_end;
week_st = Monday;
week_end = Sunday;
Assigning values to variables
◻ variable_name=constant;
◻ e.g.
🞑 initial_value =0;
🞑 final_value=100;
🞑 balance = 75.84;
🞑 yes = ‘x’;
🞑 year = year +1;
◻ Combined declaration and assignment
🞑 data_type variable_name = constant;
🞑 e.g.
■ int final_value = 100;
■ char yes = ‘x’;
■ double balance = 75.84;
◻ Initialization of more than one variables
🞑 p=q=s=0;
🞑 x = y = z = ‘a’;
Program illustrating assignments
main()
Output:
{
float x,p; m = -11234
double y,q; n = 1234567890
unsigned k;
int m=54321;//signed
x = 1.234567880630
long int n = 1234567890; x = 1.234568
x=1.234567890000;
y = 9.876543210000
y=9.87654321;
k=54321; y = 9.876543
p=q=1.0; k = 54321 p = 1.000000 q =
printf(“m = %d\n”, m); 1.000000000000
printf(“n = %ld\n”, n);
printf(“x = %.12f\n”, x);
printf(“x = %f\n”, x);
printf(“y = %.12f\n”, y);
printf(“y = %lf\n”, y);
printf(“k = %u p = %f q = %.12f \n”, k,p,q);
}
Reading data from keyboard
◻ using scanf function
scanf(“Control string”, &variable1,
&variable2, …);
◻ e.g.
scanf(“%d”, &number);
scanf(“%d %f %d”, &intvar1,
&floatvar, &intvar2);
◻ A program to illustrate scanf
Reading data from keyboard
◻ A program to illustrate scanf
main()
{
int number;
printf(“Enter the number\n”);
scanf(“%d”,&number);
}
Defining symbolic constants
◻ Certain constants that appear repeatedly in a
number of places in the program
◻ e.g. 3.14 i.e. the value of pi
◻ We face two problems in the subsequent use
of such program
🞑problem in modification of the program, and
🞑problem in understanding the program
Defining symbolic constants…
◻ Program documentation
🞑 Insert comments in the program to make it easy to understand.
🞑 Never use too many comments.
◻ Program indentation
🞑 Use proper indentation.
🞑 Structure of the program should be immediately visible.
Indentation Example: Good Style
#include <stdio.h>
main()
{
int a, b, c;