01
01
at
ed
in
M
as
te
rP
D
Introduction
F
E
di
5
to
r
r
to
di
Basic Components in a Computer
E
F
D
Input Central Output
rP
Devices: Processing Devices:
Monitor,
te
Keyboard, Unit (CPU)
printer,…
as
mouse,… M
Main
in
Memory (RAM)
ed
at
Disk
re
6
r
to
di
Programming and Software
E
F
D
A computer needs to be programmed to do tasks
rP
Programming is the process of writing instructions in a
language that can be understood by the computer so
te
that a desired task can be performed by it
as
Program: sequence of instructions to do a task,
M
computer processes the instructions sequentially one
in
7
r
to
di
Three steps in writing programs
E
F
Step 1: Write the program in a high-level language (in
D
rP
your case, C)
Step 2: Compile the program using a C compiler
te
Step 3: Run the program (as the computer to execute
it) as
M
in
ed
at
re
8
r
to
Binary Representation
di
E
F
Numbers are represented inside computers in the
D
base-2 system (Binary Numbers)
rP
Only two symbols/digits 0 and 1
te
Positional weights of digits: 20, 21, 22,…from right to
left for integers
as
Decimal number system we use is base-10
M
10 digits, from 0 to 9, Positional weights 100, 101,
in
9
r
to
Binary Numbers
di
E
Dec Binary
F
D
0 0
rP
1 1
te
2 10
3 11
as
M
4 100
in
5 101
ed
6 110
at
7 111
re
8 1000 10
r
to
Binary Numbers
di
E
Dec Binary
Binary to Decimal Conversion
F
D
0 0
rP
1 1 101011 1x25 + 0x24 + 1x23 + 0x22 + 1x21 + 1x20
= 43
te
2 10 (101011)2 = (43)10
3 11
as
111001 1x25 + 1x24 + 1x23 + 0x22 + 0x21 + 1x20
M
4 100 = 57
in
5 (111001)2 = (57)10
101
ed
7 111
re
8 1000 11
r
to
di
Bits and Bytes
E
F
Bit – a single 1 or 0
D
rP
Byte – 8 consecutive bits
2 bytes = 16 bits
te
4 bytes = 32 bits
as
Max. integer that can represented
M
in 1 byte = 255 (=11111111)
in
12
re
at
ed
in
M
as
te
rP
D
F
E
di
Fundamentals of C
13
to
r
r
to
di
First C program – print on screen
E
F
#include <stdio.h>
D
Output
rP
int main() Hello, World!
{
te
printf ("Hello, World! \n") ;
return 0;
as
M
}
in
ed
at
re
14
r
to
A Simple C program
di
E
When you run the program
#include <stdio.h>
F
int main()
D
{
rP
int x, y, sum, max; Output after you type 15 and 20
te
scanf(“%d%d”, &x, &y); 15 20
Sum = 35
as
sum = x + y;
Larger = 20
if (x > y) max = x;
M
else max = y;
in
}
re
15
r
to
di
Structure of a C program
E
F
A collection of functions (we will see what they are
D
later)
rP
Exactly one special function named main must be
te
present. Program always starts from there.
as
Until we study functions in detail, this is the only
M
function your programs will have for now
Each function has statements for variable
in
etc.
at
16
r
to
di
E
#include <stdio.h> main function
int main()
F
Declaration statement
D
{
rP
int x, y, sum, max;
scanf(“%d%d”, &x, &y); Input statement
te
sum = x + y; Assignment statements
if (x > y)
as
M
max = x; Control statement
else
in
max = y;
ed
E
F
You will have to understand what different statements
D
do to decide which you should use in what order to
rP
solve your problem
te
There is a fixed format (“syntax) for writing each
as
statement and other things. Need to remember the
syntax
M
Do not question why you have to type exactly like this,
in
18
r
to
Things you will see in a C program (we
di
will look at all these one by one)
E
F
D
Variables
rP
Constants
Expressions (Arithmetic, Logical, Assignment)
te
Statements (Declaration, Assignment, Control
as
(Conditional/Branching, Looping)
M
Arrays
in
Functions
ed
Structures
at
Pointers
re
19
r
to
The C Character Set
di
E
The C language alphabet
F
D
Uppercase letters ‘A’ to ‘Z’
rP
Lowercase letters ‘a’ to ‘z’
Digits ‘0’ to ‘9’
te
Certain special characters:
! # % as ^ & * ( )
M
- _ + = ~ [ ] \
in
| ; : ‘ “ { } ,
ed
. < > / ?
at
di
E
F
Very important concept for programming
D
An entity that has a value and is known to the
rP
program by a name
te
Can store any temporary result while executing a
as
program M
Can have only one value assigned to it at any given
time during the execution of the program
in
21
r
to
Contd.
di
E
Variables stored in memory
F
D
Memory is a list of consecutive storage locations,
rP
each having a unique address
A variable is like a bin
te
as
The contents of the bin is the value of the variable
The variable name is used to refer to the value of the
M
variable
in
22
r
to
di
Example
E
F
#include <stdio.h>
D
int main( )
rP
{
te
int x;
int y;
as
M
x=1;
y=3;
in
return 0;
at
}
re
23
r
to
di
Variables in Memory
E
F
Instruction executed
D
Memory location allocated
rP
to a variable X
X = 10
te
T
10
as
i X = 20
m
M
e
X = X +1
in
ed
X = X*5
at
re
24
r
to
di
Variables in Memory
E
F
Instruction executed
D
Memory location allocated
rP
to a variable X
X = 10
te
T
20
as
i X = 20
m
M
e
X = X +1
in
ed
X = X*5
at
re
25
r
to
di
Variables in Memory
E
F
Instruction executed
D
Memory location allocated
rP
to a variable X
X = 10
te
T
21
as
i X = 20
m
M
e
X = X +1
in
ed
X = X*5
at
re
26
r
to
di
Variables in Memory
E
F
Instruction executed
D
Memory location allocated
rP
to a variable X
X = 10
te
T
105
as
i X = 20
m
M
e
X = X +1
in
ed
X = X*5
at
re
27
r
to
di
Variables (contd.)
E
F
D
rP
X = 20
te
X
Y=15 20
X = Y+3 as
M
? Y
in
Y=X/6
ed
at
re
28
r
to
di
Variables (contd.)
E
F
D
rP
X = 20
te
X
Y=15 20
X = Y+3 as
M
15 Y
in
Y=X/6
ed
at
re
29
r
to
di
Variables (contd.)
E
F
D
rP
X = 20
te
X
Y=15 18
X = Y+3 as
M
15 Y
in
Y=X/6
ed
at
re
30
r
to
di
Variables (contd.)
E
F
D
rP
X = 20
te
X
Y=15 18
X = Y+3 as
M
3 Y
in
Y=X/6
ed
at
re
31
r
to
Data Types
di
E
F
Each variable has a type, indicates what type of
D
values the variable can hold
rP
Four common data types in C (there are others)
te
int - can store integers (usually 4 bytes)
as
float - can store single-precision floating point
M
numbers (usually 4 bytes)
in
32
r
to
di
Contd.
E
F
First rule of variable use: Must declare a variable
D
(specify its type and name) before using it
rP
anywhere in your program
te
All variable declarations should ideally be at the
as
beginning of the main() or other functions
M
There are exceptions, we will see later
in
di
E
Sequence of letters and digits
F
D
First character must be a letter or ‘_’
rP
No special characters other than ‘_’
te
No blank in between
different names)as
Names are case-sensitive (max and Max are two
M
Examples of valid names:
in
34
r
to
di
More Valid and Invalid Identifiers
E
F
D
Valid identifiers Invalid identifiers
rP
X 10abc
abc my-name
te
simple_interest “hello”
a123 as simple interest
M
LIST (area)
in
stud_name %rate
ed
Empl_1
at
Empl_2
re
avg_empl_salary
r
to
di
C Keywords
E
F
D
Used by the C language, cannot be used as variable
rP
names
Examples:
te
int, float, char, double, main, if else, for, while. do,
as
struct, union, typedef, enum, void, return, signed,
M
unsigned, case, break, sizeof,….
There are others, see textbook…
in
ed
at
re
r
to
di
Example 1
E
F
#include <stdio.h>
D
int main()
rP
Three int type variables declared
{
te
int x, y, sum;
sum = x + y; as
scanf(“%d%d”,&x,&y); Values assigned
M
printf( “%d plus %d is %d\n”, x, y, sum );
in
return 0;
ed
}
at
re
37
r
to
di
Example 2
E
F
#include <stdio.h>
D
rP
int main()
{ Assigns an initial value to d2,
te
can be changed later
float x, y;
int d1, d2 = 10;
as
M
scanf(“%f%f%d”,&x, &y, &d1);
in
return 0;
re
} 38
r
to
Read-only Variables
di
E
F
Variables whose values can be initialized during
D
declaration, but cannot be changed after that
rP
Declared by putting the const keyword in front of
te
the declaration
as
Storage allocated just like any variable
M
Used for variables whose values need not be
in
changed
ed
39
r
to
Correct
di
int main() {
E
const int LIMIT = 10;
F
int n;
D
scanf(“%d”, &n);
rP
if (n > LIMIT) Incorrect: Limit changed
te
printf(“Out of limit”);
return 0;
as int main() {
const int Limit = 10;
M
}
int n;
in
scanf(“%d”, &n);
ed
Limit = Limit + n;
printf(“New limit is %d”, Limit);
at
return 0;
re
} 40
r
to
Constants
di
E
Integer constants
F
Consists of a sequence of digits, with possibly a plus or
D
a minus sign before it
rP
Embedded spaces, commas and non-digit characters
te
are not permitted between digits
as
Floating point constants
M
Two different notations:
Decimal notation: 25.0, 0.0034, .84, -2.234
in
41
r
Contd.
to
di
E
Character constants
F
Contains a single character enclosed within a pair of
D
single quote marks.
rP
Examples :: ‘2’, ‘+’, ‘Z’
te
Some special backslash characters
as
‘\n’ new line
M
‘\t’ horizontal tab
‘\’’ single quote
in
‘\\’ backslash
at
‘\0’ null
re
42
r
Typical Size of Data Types
to
di
E
char – 1 byte
F
int – 4 bytes
D
float – 4 bytes
rP
double – 8 bytes
te
as
“Typical”, because some of them vary depending on
M
machine/OS type
Never use the values (1, 4, 8) directly, use the sizeof()
in
operator given
ed
PC/Laptop
re
43
r
Input: scanf function
to
di
E
Performs input from keyboard
F
It requires a format string and a list of variables into
D
which the value received from the keyboard will be
rP
stored
format string = individual groups of characters
te
(usually ‘%’ sign, followed by a conversion
as
character), with one character group for each
M
variable in the list
in
int a, b;
ed
Format string 44
r
to
di
E
Commonly used conversion characters
F
D
c for char type variable
rP
d for int type variable
te
f for float type variable
lf
as
for double type variable
M
in
ed
at
re
45
r
to
Examples
di
E
scanf ("%d", &size) ;
F
Reads one integer from keyboard into an int type variable
D
named size
rP
scanf ("%c", &nextchar) ;
te
Reads one character from keyboard into a char type
variable named nextchar
as
scanf ("%f", &length) ;
M
Reads one floating point (real) number from keyboard into
in
variable named b
46
r
to
di
E
F
Important:
D
scanf will wait for you to type the input from the
rP
keyboard
te
You must type the same number of inputs as the
as
number of %’s in the format string
Example: if you have scanf(“%d%d”,…), then you
M
must type two integers (in same line or different
in
47
r
to
di
Reading a single character
E
F
A single character can be read using scanf with
D
rP
%c
It can also be read using the getchar() function
te
char c; as
M
c = getchar();
in
ed
in c
re
48
r
to
Output: printf function
di
E
Performs output to the standard output device
F
(typically defined to be the screen)
D
rP
It requires a format string in which we can specify:
The text to be printed out
te
Specifications on how to print the values
as
printf ("The number is %d\n", num);
M
The format specification %d causes the value listed
after the format string to be embedded in the output as
in
49
r
to
di
Contd.
E
F
General syntax:
D
printf (format string, arg1, arg2, …, argn);
rP
format string refers to a string containing formatting
te
information and data types of the arguments to be
output
as
the arguments arg1, arg2, … represent list of
M
variables/expressions whose values are to be printed
in
50
r
to
di
E
Examples:
F
D
printf (“Average of %d and %d is %f”, a, b, avg);
rP
printf (“Hello \nGood \nMorning \n”);
printf(“%3d %3d %5d”, a, b, a*b+2);
te
as
printf(“%7.2f %5.1f”, x, y);
Many more options are available for both printf and
M
scanf
in
51
r
to
di
E
F
D
rP
te
More Examples
as
(Explain the outputs to test if you understood format strings etc.)
M
in
ed
at
re
52
r
to
di
More print
E
F
Output
#include <stdio.h>
D
Hello, World! Hello
int main()
rP
World!
{
te
printf ("Hello, World! ") ;
as
printf ("Hello \n World! \n") ;
M
return 0;
in
}
ed
at
re
53
r
to
di
Some more print
E
Output
F
Hello, World!
#include <stdio.h>
D
Hello
int main()
rP
World!
{ Hell
te
o World!
printf ("Hello, World! \n") ;
as
printf ("Hello \n World! \n") ;
M
printf ("Hell\no \t World! \n") ;
in
return 0;
}
ed
at
re
54
r
to
Some more print Output
di
Enter values for f1 and f2:
E
#include <stdio.h> 23.5 14.326
F
int main() Enter values for x1 and x2:
D
{ 54 7
rP
f1 = 23.500000, f2 = 14.33
float f1, f2;
x1 = 54, x2 = 7
te
int x1, x2;
Can you explain why 14.326
as
printf(“Enter values for f1 and f2: \n”); got printed as 14.33?
scanf(“%f%f”, &f1, &f2);
M
printf(“Enter values for x1 and x2: \n”);
in
return 0;
re
} 55
r
to
di
Some more print
E
Output
F
ab
#include <stdio.h>
D
ab
int main()
rP
{
te
char c1, c2;
as
scanf(“%c%c”, &c1, &c2);
M
printf(“%c%c”, c1, c2);
in
return 0;
ed
}
at
re
56
r
to
di
What about this?
E
Output
F
ab
#include <stdio.h>
D
a
int main()
rP
Can you explain why only ‘a’
{ was printed this time, even
te
though it is the same program
char c1, c2; as in the last slide? Note the
as
scanf(“%c%c”, &c1, &c2);
difference from the last slide
carefully. Also note that two
M
characters were read this time
printf(“%c%c”, c1, c2); also, or scanf would have
in
waited
return 0;
ed
}
at
re
57
r
to
Practice Problems
di
E
Write C programs to
F
1. Read two integers and two floating point numbers, each in a separate
D
scanf() statement (so 4 scanf’s) and print them with separate printf
rP
statements (4 printf’s) with some nice message
2. Repeat 1, but now read all of them in a single scanf statement and
print them in a single printf statement
te
3. Repeat 1 and 2 with other data types like double and char
as
4. Repeat 1 and 2, but now print all real numbers with only 3 digits after
the decimal point
M
5. Read 4 integers in a single scanf statement, and print them (using a
in
single printf statement) in separate lines such that the last digit of
each integer is exactly 10 spaces away from the beginning of the line
ed