CSE-115
Course Title: Computer Programming
1
Contents
Basic Body of C Program
Data types and their memory allocation
Operators
Basic Input/ Output
2
Basic Body of C Program
#include<stdio.h>
int main(){
/// Your Code Here
}
3
Basic Body of C Program
4
Basic Body of C Program
#include<stdio.h>
.
.
.
int main(){
printf(“Hello World”);
}
Console/Terminal/Output Window
5
printf Statement
printf ( “Hello NAME Department” );
6
printf With Newline
7
Other Special Characters
8
Keywords
▪ Linguistically Keyword is a
word which has greater
significance
▪ From this, we can conclude,
Keywords in C are words
which has some special
significant meaning (which
can not be changed) in C
programming.
▪ There are 32 keywords in C.
Each of them serve a special
purpose.
9
Identifiers
▪ Identifier refers to the name of variable, functions
and arrays.
▪ It is a user defined name
▪ Identifier is a series of characters consisting of letters,
digits and underscore that does not begins with
digits.
▪ Both uppercase and lowercase are permitted.
10
Identifier Naming Rules
▪ An Identifier can only have alphanumeric characters (a-z , A-Z , 0-9)
and underscore(_).
▪ The first character of an identifier can only contain alphabet (a-z , A-
Z) or underscore (_).
▪ Identifiers are also case sensitive in C. For example name and Name are two
different identifiers in C. Two Identifiers with same name are not
allowed.
▪ Keywords are not allowed to be used as Identifiers.
▪ No special characters, such as semicolon, whitespaces, slash or comma are
permitted to be used in or as Identifier
▪ Should contain maximum 31 characters 11
Identifier Examples
_NME - Correct
NME21 - Correct
21NME - Incorrect
NME_21 - Correct
(NME21) - Incorrect
1234 - Incorrect
NME+21 - Incorrect
12
Variables
▪A variable is data name that may be used to store a data value.
─Variable names correspond to locations in the
computer's memory
─Every variable has a name, a type, a size and a value
─Whenever a new value is placed into a variable, it
replaces (and destroys) the previous value
─Variable must be declared before it is used
13
Variables - Declaration Syntax
14
Variables
Initializing to variables : initializing variables means
assigning a value to the variable.
Syntax: data_type variable_name=constant/literal/expression;
or
variable_name=constant/literal/expression;
Example:
int a = 5; [the variable a hold the value 5]
double a= 5.5;
15
Variable Declaration and Assignment
16
Variable Declaration and Assignment
Common Mistakes
17
Data Types
▪ In programming everything we deal with is “Data”
▪ There are various types of data. Like character, decimal
numbers, natural numbers etc.
▪ There are different data types for these.
18
Data Types
▪ To visualize the need for different data types we can see
an example:
▪ Suppose you have 3 material : sand, water, oxygen gas and
3 containers: 1 paper box, 1 bowl and 1 cylinder. You can
put one material in one container. What will you put in which
container?
19
Data Types
20
Data Types Different Types
Data Type Characteristic Size Format
Values Range Examples
Keywords s (bytes) Specifiers
-2147483648 to
int 4 %d 0, 5, -15
Positive and 2147483647
negative whole 343597383
long long int numbers 8 -(2^63) to (2^63)-1 %lld 68 (i.e.
2^35)
Upto 7 digits after
float 4 %f 1.25
Decimal/Fractio radical point
nal numbers Upto 15-16 digits
double 8 %lf 3.1416….
after radical point
Letters, ASCII Chart „A‟, „b‟, „9‟
char 1 %c 21
symbols, digits Characters etc.
ASCII Chart American Standard Code for Information Interchange
22
ASCII Chart - An interesting detail
The difference between numbers assigned to the small case
and the large case of the same letter in the ASCII chart is 32.
Example:
23
Format Specifiers in C
▪ The format specifier is used during taking input from an user
and showing output.
▪ It is a way to tell the compiler what type of data is in a variable
during taking input using scanf() or printing using printf().
Data Type Format Specifier Use
int %d To input/output integer value
float %f To input/output fractional value
double %lf To input/output fractional value
char %c To input/output character
24
Outputting Variable Syntax
and Format Specifiers….
declaring the variable
This is where the value in the variable giving the variable
gets pasted - a format specifier to printf
25
Outputting Variable Syntax
Notice the single quotation ‘
26
Outputting Variable Syntax
27
Effect of using incorrect format specifiers
#include<stdio.h>
int main()
{ ▪ Output:
char var;
var= ‘A’;
printf(‚\n%c‛, var); A
printf(‚\n%d‛, var); 65
}
28
Effect of using incorrect format
specifiers
#include<stdio.h> ▪ Output:
int main()
{
int var;
var= 1.3; 1
printf(“\n%d”, var);
}
This is implicit typecasting
29
Typecasting
Explicit Typecasting:
(int) float_variable;
Implicit Typecasting: Already Shown
30
Typecasting
▪ Converting one data type into another is known as type casting or, type-
conversion.
▪ Two types of conversion:
▪ Implicit conversion : If the operands are of different type the lower type is
automatically converted to the higher type before the operation proceeds.
The result is of higher type. Example: A float type variable will be converted
to double type automatically
▪ Explicit conversion : Conversion from higher type to lower type requires
explicit conversion. Example: Double to float conversion/ Float to int
conversion
▪ You can convert the values from one type to another explicitly using the
cast operator : (type_name) expression
31
Explicit Typecasting
32
Type Cast
Example Action
X = (int)3.5 3.5 is converted to integer
(This truncates not rounds so X=3)
X = (int)(a+b) The result of (a+b) is converted to integer
X = (int)a + b a is converted to integer and then added with
b
X = (int)a / (int)b a is converted to int, b is converted to
int, then division operation performed
X = (int)20.5 /(int) 20.5 converted to floor int(20), 4.33
4.33 converted to floor int(4), then
division operation is performed
33
More with Format Specifier
#include<stdio.h>
int main() {
float var=1.3143547; ▪ Output:
printf(‚\n%f‛, var); 1.3143547
printf(‚\n%.2f‛, var);
1.31
}
Prints upto 2 decimal point
34
More with Format Specifier
#include<stdio.h>
int main() { ▪ Output:
float var=1.3153547; 1.3143547
printf(‚\n%f‛, var);
1.32
printf(‚\n%.2f‛, var);
} Prints upto 2 decimal point and
rounds up
35
More with Format Specifier
#include<stdio.h> ▪ Output?
int main() { 1.3153547
float var=1.3153547;
printf(‚\n%f‛, var); 1.315
printf(‚\n%.3f‛, var);
} Rounds the value and Prints
upto 3 decimal point
36
scanf Statement
declaring the variable
Because we are taking an integer input memory address
of the variable
37
Memory Address
▪ Think of it like trying to find a student in a classroom using their
student_id.
▪ If they are arranged randomly, need to check every student from
start to end one by one.
▪ If the student are arranged according to their student_id, it makes
the search alot more efficient.
▪ That’s why computers use addresses. To make searching
information easier.
38
Memory Address
▪ Let’s assume computer memory looks like this 1 2 3 4
after running the following codes:
int a = 5; 1
int b;
2 a=5
▪ Here, address of a is 22, and address of b is 34.
3 b=
▪ These numbers are called memory addresses. garbage
value
Actual memory addresses can be huge numbers
and so used in Hexadecimal number system. 4
▪ In an actual computer memory, things are a lot
more complex, this is a mere simplified
analogy. 39
What is ‘&’ (ampersand) ?
▪ The scanf function expects to receive in the address of
the variable. It can then store the input value into the
memory address referred by the variable. ‘&’ sign
indicates the address of the variable.
▪ Every variable identifier indicate a value and the address
it stores the value at. Since just the identifier directly
corresponds to the value, an additional symbol (&) was
used along with it to indicate the address.
▪ ‘&’ reads as “address of”.
40
More about scanf format string
When you add text other than white space in a format string that
you pass as an argument to scanf, the user must type input
exactly as it appears in the format string.
You are essentially requiring the user to type input in a very
particular way.
41
More about scanf format string
In this given code:
42
More about scanf format string
In this given code,
Another Input & Output:
43
More about scanf format string
Here the user input had to have a comma (,) between first and
second character inputs exactly as determined by the format
string: " %c, %c %c".
Run this last program again, but this time, instead of typing the
comma after the 'a', just type the 'a', a blank and the '1'. You will
see that the values of the variables are not correct.
Although it is possible to include characters other than white
space in the format string, it is generally best not to.
The more specific the requirements for input, the greater the
chance for errors.
44
More about scanf format string
Read for More on [Link]
45
Input/Output with multiple variables
Operators
▪ Operator: Operator is a symbol that tells the computer to perform
certain mathematical or logical manipulations.
▪ Based on number of operands C operators can be classified into a
number of categories:
▪ Unary Operators
■ Syntax: operator variable;
▪ Binary Operators
■ Syntax: variable1 operator variable2;
▪ Ternary Operators
■ Syntax: variable1 operator variable2 operator variable3;
47
Operators
▪ Operator: Operator is a symbol that tells the computer to perform certain
mathematical or logical manipulations.
▪ Based on type of operations C operators can be classified into a number of
categories:
▪ Arithmetic operators
▪ Relational operators
▪ Logical operators
▪ Assignment operators
▪ Increment and decrement operators
▪ Bitwise operators
▪ Special operators
48
Arithmetic Operators
Arithmetic C Operation Example Possible
Operator operands
+ Addition 5+6, a+b, a+4 Any number
- Subtraction 6.5-5.0, a-b Any number
* Multiplication 6*5.5, a*b Any number
/ Division 6/2, a/b Any number
% Modulus 14%3 Only Integer number
49
Operators Precedence
● Parentheses: ()
● Multiplication and Division: */%
● Addition and Subtraction: +-
50
Operators Precedence
Goes left to right in the
same precedence
(in the order they are written in the code
not the order they are shown in this slide)
51
Operators Precedence
The result of int op int is int
The result of int op float is float
The result of float op int is float
The result of float op float is float
52
Operators Example
This is a constant expression
53
Operators Example
Notice how it is the same as
previous expression, except the
numbers were replaced with
variables.
54
Assignment Operator
Operator sign example Meaning in C
Name
Assignment = A = 5+6; Assigns the value of the
A= x+y; right expression or Variable
A = 5; to the left operand
A = x;
A=x=y;
Assignment Chaining
Here x=y executes first
Then A=result of that execution
55
Increment & Decrement Operators
Operator Type example Meaning in C
Name
Increment Prefix A = ++X Increments the value of X by 1
THEN Assigns the value to A
Postfix A = X++ Assigns the current value of X to A
THEN Increment the value of X by 1
Decrement Prefix A = --X Decrements the value of X by 1
THEN Assigns the value to A
Postfix A = X-- Assigns the current value of X to A
THEN Decrement the value of X by 1
56
Example:
Taking Input and Printing float Value
#include<stdio.h> ▪ Ampersand sign = &
int main() {
float operand1, operand2, result;
scanf(‚%f‛,&operand1 ); An alternative can be:
scanf(‚%f‛,&operand2 ); scanf(“%f%f”,&operand1 ,
result = operand1 + operand2; &operand2 );
printf(“\nResult of the addition of %f and %f is %f”, operand1, operand2,result );
57
Special Operator
▪ sizeof operator:
▪ It returns the number of bytes the operand occupies.
i=sizeof(int);
58
Special Operator
59
Comments
60
Practice at home
Take a small case letter as input. Output its corresponding large case
letter.
Take the radius of a circle as input. Calculate its area and output it with
the value rounded up to two decimal places.
Rahim and Karim are two friends who want to hangout but are too busy to
do so. But starting today, Rahim is free every xth day and Karim is free
every yth day. Print on whichth day Rahim and Karim will be able to
hangout?
61
Task
A circular track field is t metres long. A runner ran around the
track for x metres. Print how many more metres he needed to run
to cover an additional lap.
62
Thank You
63