Lecture 03 Bscs
Lecture 03 Bscs
contents
o programming
o C programming language
o C program development environment
o first C program
o datatypes and variables
o operators and expressions
o concepts discussed in the lecture
o exercises
programming
o is to instruct and command computer to perform user’s desired task
C programming language
o history
in 1972 Dennis Ritchie developed C language
C language was evolved from two earlier programming languages namely BCPL
and B
B language was developed at Bell Telephone laboratories (currently AT&T Bell
Labs) by Ken Thompson
till 1978, C was primarily used at Bell Labs
in 1978 Brian Kernighan and Dennis Ritchie published a book named “The C
Programming Language”
the book was central to the development and popularization of the C
programming language and is still widely read and used today
C language of 1978 is called K&R C
American National Standards Institute (ANSI) published a standard for C language
in 1989 which is call ANSI C or C89, second version of the book was published to
cover the ANSI C standard, the same standard was adopted by the International
Organization for Standards (ISO) in 1990 hence it is also called C90
in 1995 International Organization for Standards (ISO) made few extensions in C
language standards which is called C95
later the ANSI adopted ISO C95 standard which is called C99
in 2011 ISO has included improved C99 and this is call C11
current standard of C language is C17 which has improved few defects of C11
software developers writing in C language are encouraged to conform to the
standards, as doing so helps portability between different compilers
o significance
C language is used to develop high performance demanding systems like
operating systems, real time systems, embedded systems etc.
o language
to program in C language a programmer should know about
C language syntax
keywords
1 // A first program in C.
2 #include <stdio.h>
3
4 // function main begins program execution
5 int main(void) {
6 printf("Pakistan Zindabad\n");
7 } // end function main
o program structure
//
it is used for comments, compiler ignore these statements, these are
meant for programmer’s personal use to remember and recall what s/he
has done and why
#include
it is called preprocessor directive, which tells complier to include code
from a specified file into current program file
<stdio.h>
used for header file, a file that stores declarations of standard input and
output functions used for taking input form user and given output to user
at user’s console
int main (void)
it is called main function, which is the program entry point, all C programs
start from this function
functions can receive a value to act upon and may return a value after
processing the code inside function,
here main function does not receive any value which is represented by
keyword void and
returns a value written at left of the function name which is the keyword
int
printf(“ ”)
it is an output function, used to display information at user’s console, this
function is declared in stdio.h header file
function printf prints a string (set of characters) at console
this string can have special meanings to character starting with \ which
are called escape sequences for example
o \n is called newline which moves cursor on next line at console
o \t is called horizontal tab which moves cursor to the next
horizontal table position at console
o \v is called vertical tab which moves cursor to the next vertical
tab position at console
o \a is called alert which generate an audible (bell)
o \\ is called backslash which outputs the backslash \ at console
1 // program which takes a number form the user and prints its value at console
2 #include <stdio.h>
3
4 // function main begins program execution
5 int main(void) {
6 int a;
7 scanf(“%d”, &a);
8 printf("%d\n", a);
9 } // end function main
Input: 10
Output: 10
line 6 - int a – this statement defines a variable named a in memory having type
integer, which can store a whole number inside it
line 7 – scanf(”%d”, &a) – this is a C standard library function named scanf which
will take a value from the keyboard and store that value as a whole number in
variable a in decimal format
line 8 – printf(”%d”, a) – this is a C standard library function named printf which
will print the value of variable a stored in the memory at console as a whole
number in decimal format
datatypes and variables
o datatypes
is a set of possible values and a set of possible operation that could be performed
on it.
It tells the compiler how the programmer intends to use the data.
basic data types include
integer numbers (which can store whole number), represented with
keyword int in C language
floating-point numbers (which approximate real numbers), represented
with keyword float in C language
characters (which store alphabets, punctuations, etc.), represented with
keyword char in C language
datatype defines which possible values that a variable might take and the
operations that can be done on the variable
variable
a named memory location of a particular datatype which can store data
a variable could be named as a language identifier
o identifier: consist of letters, digits and underscores ( _ ), but may
not begin with a digit
o case sensitivity: a1 and A1 are different identifiers
definition: variable could be defined by simply writing datatype and
then a variable name
o it is necessary to define a variable before it is used in the
program
o int a;
o float b;
o char c;
o above statements define three variables namely a, f and c of
datatypes int, float and char which can store whole number real
number and a character respectively
o when a programmer define a variable, the program reserve a
specified location of a particular size in the RAM
o the program reserve one byte for a char type variable and four
bytes each for an int and float types variable each
o range of a datatype is minimum and maximum value that a
datatype can hold
a char type variable can store values from -128 to 127
an int type variable can store values from -2147483648
to 2147483647
an float type variable can store values from -3.4e+38 to
-3.4e+38, here e+38 means 1038
o memory occupied by each datatype can be found be using
sizeof operator in a program as follows
sizeof (int)
o when a variable is define without giving it any value, then it
holds a garbage (unknown random) value
initialization: it good to assign a value to a variable at the time of its
definition this called variable initialization
int a = 12;
float b = 13.51;
char c = ‘A’;
o after definition of initialization of a variable, programmer can
assign a permissible value to a variable using assignment
operator =
a = 100;
b = 26.75;
c = ‘Z’;
output statement for variables
arithmetic operators:
o these operators perform arithmetic including addition,
subtraction, multiplication, division, and modulus operations on
their operands represented by +, - , *, / and %
// L03-C04
1. int a, b, c;
2. a = 10;
3. b = 20;
4. c = a + b;
5. printf(“%d”, c);
o output: 30
o line 1: above code defines three integer type variables namely a,
b and c, each variable occupies four bytes each in the RAM
o line 2&3: assign value 10 to variable a and value 20 to variable b
o line 4: an arithmetic operation of addition + is performed on
variable a and b which results value 30 and this value is stored in
variable c
o line 5: prints the value stored in variable c at console
o statements at line 2, 3 and 4 are called expression
an expression is a combination of variables, constants
and operators
relational operators:
o these operators are used to check the relationship between two
operands. For example >, <, ==, !=, >=, <=
o checks if a is greater than b a > b; here, > is a relational operator.
It checks if a is greater than b or not
o after operating on two operands relational operator return
either of two values which are 1 (true) or 0 (false)
// L03-C05
1. int a, b, c;
2. a = 10;
3. b = 20;
4. c = a > b;
5. printf(“%d”, c);
o output: 0
o line 4:
contains a relation operator having two operands a > b
this relational expression a > b will result 0 (false), which
will be stored in variable c, and subsequently printed at
console
logical operator:
o are designed for operations with the true (1) or false (0) values
within the logical expressions
o there are three logical operators namely and, or and not
operators represented by &&, ||, ! symbols respectively
o after operating on two operands logical operator return either of
two values which are 1 (true) or 0 (false)
o and operator represented by && symbols will return 1 (true) if
both operands are non-zero (both are true) otherwise it returns
0 (false)
o or operator represented by || symbols will return 1 (true) if any
of the operands is non-zero (any one is true) otherwise it returns
0 (false)
o not operator represented by ! symbols will return 1 (true) its
operand 0 (false) otherwise it returns 1 (true), note that not
operator has only on operand
o please note that logical operators consider all non-zero values
as true (1)
o logical operators are commonly used with relational operators to
form an meaningful expression
// L03-C06
1. int a, b, c, d;
2. a = 10;
3. b = 20;
4. c = 30;
5. d = (a > b) && (b > c)
6. printf(“%d”, d);
o output: 0
o line 5:
contains a logical and operator having two operands first
is (a > b) and second is (b > c)
the first relational expression a > b will result 0 (false)
the second relational expression b > c will also result 0
(false)
hence both operands of and logical operator would be 0
so the result of 0 && 0 would be 0 (false), which would
be printed at console
o integer division
when an integer is divided by an integer it results into an integer value which is
called integer division, it is important to note that such division truncate
remainder part of the division process
// L03-C07
1. int a, b, c;
2. a = 10;
3. b = 3;
4. c = a / b;
5. printf(“%d”, c);
output: 3
line 4:
please note that here the result should be 3.33333 which is truncated due
to integer division and only 3 is stored in variable c
if we use c as a float variable it will still not resolve issue because the
operand of division operator are both integers the result of a / b would
be 3 and stored in c as 3.000000
to resolve this issue we should use float division instead of integer
division
if either of a or b as a float variable then the division process return a float
value
// L03-C08
1. int a;
2. float b, c;
3. a = 10;
4. b = 3;
5. c = a / b;
6. printf(“%f”, c);
output: 3.333333
line 5:
an integer 10 is divided by a float value 3.000000, hence the result would
be promoted to a float value which would be 3.333333
as this resultant value is stored in another float variable hence the
variable c will store 3.333333, which would be printed at console
note if we will use c as an integer variable it will again print 3 at console
due to truncation of value after decimal places
o converting mathematical expressions into programming expressions
a mathematical expression should be converted into a straight-line form while
writing a program code
expressions like “a divided by b” must be written as a / b with all operators and
operands in a straight line. The mathematical or algebraic notation for this
𝑎
expression is
𝑏
parentheses are used in C expressions in the same manner as in algebraic
expressions, for example, to multiply variable a times the quantity b + c, it is
written as a * (b + c)
o operator precedence
determines the grouping of terms in an expression and decides how an
expression is evaluated or which part of the expression is evaluated prior to other
o C language, BCPL, B, Standard, ANSI, ISO, K&R C, ANSI C, C89, C95, C99, C11, C17,
operating systems, real time systems, embedded systems,
o development environment, C language syntax, preprocessor directive, comments, header
file, program entry point, output function, statement terminator, keywords, reserved
word, void, int, char, float, case sensitive, braces, parenthesis, statement, code block,
function, standard library function, user defined function, argument, return value,
function declaration, function definition, control structure, sequence, selection,
repetition,
o edit, preprocess, compile, link, load, execute, c/program file, header file, object file,
library file, executable file, syntax error, compile-time-error, linker error or link-time-
error, loading error, execution error, runtime error, logical error
o Standard Library, standard input and standard output, printf, scanf, %d, format specifier,
escape sequences, statement terminator, datatype, variable, identifier, rang of datatype,
size of datatype, sizeof, garbage value, variable definition, variable initialization, variable
assignment, variable overflow and underflow, promotion and demotion,
o operator, assignment operator, binary operator, arithmetic operator, relational operator,
logical operator, operator precedence, operator associativity, expression, AND, OR, NOT,
integer division, straight-line form
exercises
o L03.E01: write a C program that
reads in the radius (r) of a circle and
displays its diameter (2*r), circumference (2πr) and area (πr2 )
Sample Input / Output
Input: 2
Output:diameter is 4
Output:diameter is 12.571428571428
Output:diameter is 12.571428571428
o L03.E02: write a C program that
takes temperature in Fahrenheit and converts it into Celsius using formula C = (F
– 32) * 5/9
and displays temperature in Celsius
Sample Input / Output
Input: 98
Output:36.666666666667
o L03.E03: write a C program that
takes age of the user in years and
displays user’s age in months, days and hours.
Sample Input / Output
Input: 30
Output:it is 360 months
Output:it is 10950 days
Output:it is 262800 hours
o L03.E04: write a C program that
takes age of the user in years and