0% found this document useful (0 votes)
16 views54 pages

01

Uploaded by

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

01

Uploaded by

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

re

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

after the other


Software: programs for doing tasks on computers
ed
at
re

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

102,…from right to left for integers


ed

 Example: 723 = 3x100 + 2x101 + 7x102


at
re

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

6 110 10100  1x24 + 0x23 + 1x22 + 0x21 + 0x20 = 20


(10100)2 = (20)10
at

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

 In 4 bytes = 4294967295 (= 32 1’s)


ed

 No. of integers that can be represented in 1 byte =


256 (the integers 0, 1, 2, 3,….255)
at
re

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

printf (“Sum = %d\n”, sum);


ed

printf (“Larger = %d\n”, max);


return 0;
at

}
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

declarations, assignment, condition check, looping


ed

etc.
at

 Statements are executed one by one in order


re

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

printf (“Sum = %d\n”, sum); Output statement


at

printf (“Larger = %d\n”, max);


re

return 0; Return statement


17
}
r
to
di
Writing a C program

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

you just have to or it is not a C program!!


ed

 Compiler will give error if your typed program does not


match required C syntax
at

 There are other rules to follow


re

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

whitespace characters (space, tab, …)


re

A C program should not contain anything else 20


r
to
Variables

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

 The value of a variable can be changed during the


ed

execution of the program


at
re

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

 A variable is mapped to a location of the memory,


called its address
ed
at
re

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

printf("x = %d, y= %d\n", x, y);


ed

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

 double - can store double-precision floating point


numbers (usually 8 bytes)
ed

 char - can store a character (1 byte)


at
re

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

 A value can also be assigned to a variable at the


ed

time the variable is declared.


at

int speed = 30;


re

char flag = ‘y’;


33
r
to
Variable Names

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

 i rank1 MAX max Min class_rank


ed

 Examples of invalid names:


at

 a’s fact rec 2sqroot class,rank


re

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

printf( “%f plus %f is %f\n”, x, y, x+y);


ed

printf( “%d minus %d is %d\n”, d1, d2, d1-d2);


at

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

 Prevents accidental change of the value


at
re

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

 Exponential (scientific) notation


ed

3.45e23, 0.123e-12, 123e2


at

e means “10 to the power of”


re

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

‘\”’ double quote


ed

‘\\’ 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

 sizeof(char) will give 1, sizeof(int) will give 4 and so on your


at

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

Variable list (note the &


before a variable name)
float c;
at

scanf(“%d%d%f”, &a, &b, &c);


re

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

a float type variable named length


ed

 scanf (“%d%d”, &a, &b);


 Reads two integers from keyboard, the first one in an int
at

type variable named a and the second one in an int type


re

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

lines), or scanf will just wait and the next statement


ed

will not be executed


at
re

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

 Program waits at the getchar() line until a


character is typed, and then reads it and stores it
at

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

a decimal number in place of %d


ed

 Output will appear as: The number is 125


at
re

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

 The conversion characters are the same as in scanf


ed
at
re

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

 Read from the book


ed
at
re

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

scanf(“%d%d”, &x1, &x2);


ed

printf(“f1 = %f, f2 = %5.2f\n”, f1, f2);


printf(“x1 = %d, x2 = %10d\n”, x1, x2);
at

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

it is printed in (the 9 spaces before will be occupied by blanks or other


digits of the integer). Remember that different integers can have
at

different number of digits


6. Repeat 5, but now the first integer of each integer should be exactly 8
re

spaces away from the beginning of the line it is printed in. 58

You might also like