Download as pdf or txt
Download as pdf or txt
You are on page 1of 63

CSE-1121 (Structured Programming Language)

Department of Computer Science and Engineering


DUET, Gazipur-1700

Md. Mahbub Alam


Lecture, Department of CSE

November, 2012(EVEN SEMESTER)

Md. Mahbub Alam Structured Programming Language 1


(CSE-1121)
Text Books and Other Class Room Material

 Text Books
1. Programming in ANSI C (3rd Edition) by E Balagurusamy
2. C – The Complete Reference (4th Edition) by Herbert Schildt
3. C – How To Program (4th Edition) by Deitel & Deitel

 Useful Links
• http://www.lysator.liu.se/c/bwk-tutor.html
• http://www.cs.cf.ac.uk/Dave/C

Md. Mahbub Alam Structured Programming Language 2


(CSE-1121)
Course Outline
 Introduction to C programming  Introduction to C programming
 Constants, variable & data types  Constants, variable & data types
 C Operators & expressions  C Operators & expressions
 Managing I/O operations in C  Managing I/O operations in C
 C Control structure  C Control structure
 C Arrays  C Arrays
 C Characters and Strings  C Characters and Strings
 C Functions
 C Pointers
 Structure and Union in C
 Dynamic Memory Allocation in C
 File processing in C
 Graphics programming in C
 Built in functions in C

Md. Mahbub Alam Structured Programming Language 3


(CSE-1121)
Class Hour
Theory: 2 lecture per week
Tuesday 08:30 AM – 09:20 AM
Thursday 09:20 AM – 10:10 AM
Room No - 414
Lab: Thursday 10:40 AM – 01:10 PM & 02:00 PM – 04:30 PM
Lab – Software Lab

How Contact With Me


Room No : 332
Mobile : +8801672917778
Email : emahbub.cse@gmail.com

How I Contact With You


Google Group
Group Name : DUET CSE 12TH BATCH
Group Email : cse12th_duet@googlegroups.com

Md. Mahbub Alam Structured Programming Language 4


(CSE-1121)
Marks Distribution
Total Mark 300

Class Attendance 10% 30

Class Test/Quiz Test/Assignment 20% 60

Final Exam 70% 210

Total 100% 300

Grading System
Numerical Grade Letter Grade Grade point
80% or above A+ (A Plus) 4.00
75% to less than 80% A (A Regular ) 3.75
70% to less than 75% A- (A Minus) 3.50
65% to less than 70% B+ (B Plus) 3.25
60% to less than 65% B (B Regular) 3.00
55% to less than 60% B- (B Minus) 2.75
50% to less than 55% C+ (C Plus) 2.50
45% to less than 50% C (C Regular) 2.25
40% to less than 45% D 2.00
Less than 40% F 0.00

Md. Mahbub Alam Structured Programming Language 5


(CSE-1121)
Lecture01 (Introduction to C programming )
June 26, 2010
 Today's Outline
 History of C
 Importance of C
 What is C used for?
 C vs. Related language
 Basic Structure of C program
 “Hello World Program”
 C program development environment

Md. Mahbub Alam Structured Programming Language 6


(CSE-1121)
History of C
 C evolved from two previous programming languages, BCPL and B
by “Dennis Ritchie” at Bell Laboratories on DEC PDP-11 machine
in 1972.
- In 1967, BCPL (Basic Combined Programming Language) was developed by
Martin Richards as a language for writing operating system software &
compilers.
- In 1970, Ken Thompson developed B using many features of BCPL and early
versions of UNIX operating system at Bell Laboratories on DEC PDP-7
machine.
 Initially, C widely known as the development language of the UNIX
operating system but today virtually all new major operating systems
are written in C and/or C++.
 By the late 1970's, C had evolved into what is now referred as
"Traditional C“.
Md. Mahbub Alam Structured Programming Language 7
(CSE-1121)
History of C (Cont.)
 Rapid expansion of C over various types of computers led to many
variations of the language that were similar but often incompatible
which was serious problem for program developers who needed to
develop platform independent code.
 A technical committee (X3J11) was created under ANSI to define a
standard version of C. In 1989, the committee approved a standard
version of which was known as ANSI C (C89).
 In 1990, this standard was approved by ISO referred to as
ANSI/ISO Standard C which was updated in 1999 referred to as
C99.
 In 2007 – work on new C standard C1X announced.
• In this course: ANSI/ISO Standard C (C89/C90)

Md. Mahbub Alam Structured Programming Language 8


(CSE-1121)
Why teach C?
• C is small (only 32 keywords).
• C has rich set of built-in functions and support variety of data
types & operators.
• C is highly portable (Machine independent).
• C is structured.
• C has ability to extend itself.
• C is stable (the language doesn’t change much).
• C is quick running (code written in c is efficient & fast).
• C is the basis for many other languages (C++, C#, Java, Perl etc).
• C is a Programmers Language.

 It may not feel like it but C is one of the easiest language to


learn.

Md. Mahbub Alam Structured Programming Language 9


(CSE-1121)
What is C used for?

• Systems programming:
– OSes, like Linux
– microcontrollers: automobiles and airplanes
– Embedded processors: phones, portable electronics, etc.
– DSP processors: digital audio and TV systems.

Md. Mahbub Alam Structured Programming Language 10


(CSE-1121)
C vs. Related Language
• More recent derivatives: C++, Objective C, C#
• Influenced: Java, Perl, Python (quite different)
• C lacks:
– exceptions
– range-checking
– garbage collection• object-oriented programming
– polymorphism...
– Low-level language faster code (usually)

Md. Mahbub Alam Structured Programming Language 11


(CSE-1121)
Basic Structure of C Program
Documentation Section Set of comments lines( prg. name, author & other info)
Link Section Link the program with functions of system library
Definition Section Defines all symbolic constants
Global Declaration Section Declares global variables & user-defined functions
main() Function Section
Every program must have main() function. Program
{ execution begins at opening braces & ends at closing braces
} of main() function.
Subprogram Section
function1()
{ Definition of all user-defined functions
}
::::::::::
functionN()
{
}

Md. Mahbub Alam Structured Programming Language 12


(CSE-1121)
Hello World Program

Comments

/* My first C program which prints Hello World */

#include <stdio.h>
Main() function begin here
int main ()
{ Library function
printf("Hello World!\n");
return 0;
} Return 0 from main means our program
finished without errors

Main() function ends here

Md. Mahbub Alam Structured Programming Language 13


(CSE-1121)
Typical C program
development environment

Md. Mahbub Alam Structured Programming Language 14


(CSE-1121)
Any Question?

Md. Mahbub Alam Structured Programming Language 15


(CSE-1121)
Thank You All

Md. Mahbub Alam Structured Programming Language 16


(CSE-1121)
Lecture02(Constants, variable & data types)

 Today's Outline
 Data Types
 C Tokens
 Keywords & Identifiers
 Constants
 Variables
 The four C scopes
 Type Qualifiers
 Storage Class Specifiers

Md. Mahbub Alam Structured Programming Language 1


(CSE-1121)
C Tokens
 Token: Smallest individual units in C program are known as token.
e.g. Keywords - int, break, for, struct etc
Identifiers - count, amount etc
Constants - 10, 5.5, -7.5 etc
Strings - “DUET”, “1st Year” etc
Operators - +, -, *, /
Special Symbols - [] {} etc

Md. Mahbub Alam Structured Programming Language 2


(CSE-1121)
C Data Types
 The datatype of an object in memory determines the set of values it
can have and what operations that can be performed on it.
 All C compiler supports 5 foundational data types, namely
integer(int), floating-point(float), double precision floating-point
(double), character(char), & void.
Data Type Size (bits)
char 8
int 16 or 32
float 32
double 64

 void type either explicitly declares a function as returning no value


or creates generic pointers.

Md. Mahbub Alam Structured Programming Language 3


(CSE-1121)
C Keywords
 All keywords have fixed meanings & these meanings can not be
changed. Serve as basic building blocks for program statements.
 All keyword must be written in lowercase letters.
• Flow control (6) - if, else, return, switch, case,
default
• Loops (5) - for, do, while, break, continue
• Common types (5) - int, float, double, char, void
• Structures (3) - struct, typedef, union
• Counting and sizing things (2) - enum, sizeof
• Rare but still useful types (7) - extern, signed,unsigned,
long, short, static, const
• Evil keywords which we avoid (1) - goto
• Others (3) - auto, register, volatile
Md. Mahbub Alam Structured Programming Language 4
(CSE-1121)
C Identifiers
 In C, the names of variables, functions, labels & various other user-
defined items are called identifiers.
 Rules for identifiers:
• First character must be an alphabet or underscore and subsequent characters
must be either letter, digits, or underscore.
• Only 31 characters are significant.
• Can not be the same as a C keywords & should not have the same name as
functions that are in C library.
• Must not contain white space.

Md. Mahbub Alam Structured Programming Language 5


(CSE-1121)
Variables
 A variable is a named location in memory that is used to hold a value
that can be modified by the program.
 All variable must be declared before they can be used.
Format: type varable_name; e.g. int count;

 Variable name must be meaningful to reflect its function or nature.


 Variable initializations Format: type variable_name = constant;
 Where variables to be declared:
- Inside function (local variables)
- In the definition of function parameters (Formal parameters)
- Outside of all functions (global variables)

Md. Mahbub Alam Structured Programming Language 6


(CSE-1121)
Constants
 Constants refers to fixed values that do not change during program
execution.
 Constants can be of any type of the basic types.
 Constants are also called literals.
 Example: int 10, 135, -23
long int 5678349L
unisigned int 580U
unisigned long int 9845464UL
float 120.25f
long double 1020.75L
character constants ‘D’
string “DUET”
HEX 0x80
Octal 016

Md. Mahbub Alam Structured Programming Language 7


(CSE-1121)
Backslash Character Constants
 Backslash character constants are used in output functions.
 Also known as escape sequence
Code Meaning
\b Backspace
\n New line
\f Form Feed
\r Carriage Return
\t Horizontal tab
\v Vertical tab
\” Double quote
\’ Single quote
\\ Backslash
\a Alert
\? Question mark
\0 Null
Md. Mahbub Alam Structured Programming Language 8
(CSE-1121)
Any Question?

Md. Mahbub Alam Structured Programming Language 9


(CSE-1121)
Thank You All

Md. Mahbub Alam Structured Programming Language 10


(CSE-1121)
Lecture03(C Expressions & Operators)

 Today's Outline
 C operators
 Operator Precedence & Associativity
 C Expression Evaluation
 Type Conversion in Expressions

Md. Mahbub Alam Structured Programming Language 1


(CSE-1121)
C Operators
 An operator is a symbol that tells the computer to perform
certain mathematical or logical manipulations.
 Operators used in programs to manipulate data & variables
 C is very rich in built-in operators, classified into
following number of categories:
• Arithmetic
• Relational
• Logical
• Bitwise
• Assignment
• Increment/Decrement
• Conditional

Md. Mahbub Alam Structured Programming Language 2


(CSE-1121)
C Operators - Arithmetic Operators
 Following table lists C’s arithmetic operators:
Operator Meaning
+ Addition or unary plus
- Subtraction or unary minus
* Multiplication
/ Division
% Modulus

 Integer division(/) truncates any fractional part (remainder).


 Modulus operator (%) can not used be used on floating-point
data. During modulus operation, sign of the result is always the
sign of the first operand. .
e.g. 5/2 -> 2 -11%2 -> -1 11/-2 -> 1
Md. Mahbub Alam Structured Programming Language 3
(CSE-1121)
C Operators – Relational Operators
 Relational refers to the relationships that values can have with one
another.
 Expressions that use relational operators return 0 for false & 1 for
true.
Operator Meaning
> Is greater than
>= Is greater than or equal to
< Is less than
<= Is less than or equal to
== Is equal to
!= Is not equal to

Md. Mahbub Alam Structured Programming Language 4


(CSE-1121)
C Operators – Logical Operators
 Logical refers to the ways relationships can be connected.
 Expressions that use logical operators return 0 for false & 1 for true.
Operator Meaning
&& AND
|| OR
! NOT

 The truth table for logical operators is shown below:


P Q P && Q P || Q !P
0 0 0 0 1
0 1 0 1 1
1 1 1 1 0
1 0 0 1 0
Md. Mahbub Alam Structured Programming Language 5
(CSE-1121)
C Operators – Bitwise Operators
 Bitwise operation refers to testing, setting, or shifting the actual bits
in a byte or word, which correspond to the standard char or int data
types.
 Bitwise operator can not be used on float, double, long double or
void data types.
Operator Meaning
& AND
| OR
^ XOR
~ One’s Complement (NOT)
>> Shift right
<< Shift left

Md. Mahbub Alam Structured Programming Language 6


(CSE-1121)
C Operators – Assignment Operator
 ‘=‘ used to assign the result of an expression to a variable. General
form: variable_name = expression;
 Multiple assignments: x = y = z = 0;
 Compound assignments: The statement of the form var = var
operator expression can be written as var operator = expression
e.g. x = x + 1 can written as x += 1

Operator Example
+= X += 1
-= X -= 1
*= X *= Y
/= X /= Y
%= X %= Y

Md. Mahbub Alam Structured Programming Language 7


(CSE-1121)
C Operators – Increment/Decrement
 ‘ ++ ’ adds 1 to its operand and ‘ -- ’ subtracts 1 from its operand.
 x = x +1 is same as ++x and x = x – 1 same as --x
 Both the increment and decrement operator may either in prefix or
postfix form.
 When postfix (x++ or x--) is used with a variable in an expression,
the expression is evaluated first using the original value of the variable
and then the variable is incremented (or decremented) by one.
 When prefix (++x or --x) is used in an expression, the variable is
incremented (or decremented) first and then the expression is
evaluated using new value of the variable.

Md. Mahbub Alam Structured Programming Language 8


(CSE-1121)
C Operators – Conditional Operator
 Also called ternary operator “ ?: ”
 General form: exp1 ? exp2 : exp3
 operation same as if-then-else statement

a = 10; a = 10;
b = 15; b = 15;
If (a > b) x = (a > b) ? a : b;
x = a;
else
x = b;

Md. Mahbub Alam Structured Programming Language 9


(CSE-1121)
Md. Mahbub Alam Structured Programming Language 10
(CSE-1121)
Md. Mahbub Alam Structured Programming Language 11
(CSE-1121)
Md. Mahbub Alam Structured Programming Language 12
(CSE-1121)
Md. Mahbub Alam Structured Programming Language 13
(CSE-1121)
C Type Conversion
 Implicit type conversion: C automatically converts any
intermediate values to the proper type so that the expression can be
evaluated without loosing any significance, called implicit type
conversion.
Rule: If the operands are of different types, the ‘lower type’ is automatically
converted to ‘higher type’ before operation proceeds.

 Explicit type conversion: When we force an expression to be a


specific type, called explicit type conversion.
General form: (data_type) expression;
e.g. int x;
float y;
x = (int) (y + 10.5);
Md. Mahbub Alam Structured Programming Language 14
(CSE-1121)
Md. Mahbub Alam Structured Programming Language 15
(CSE-1121)
Any Question?

Md. Mahbub Alam Structured Programming Language 16


(CSE-1121)
Thank You All

Md. Mahbub Alam Structured Programming Language 17


(CSE-1121)
Lecture04(Control Structure PART-I)

 Today's Outline
 Decision making statements in C
• Simple if statement
• if_else statement
• else_if ladder
• nested if_else statement
• switch statement
• goto statement

Md. Mahbub Alam Structured Programming Language 1


(CSE-1121)
Simple if Statement
 Format:
If ( test condition )
statements;

scanf( “ %f “ , &marks );
if( marks >= 80 )
printf( "Yes, the student get A+“ );

Md. Mahbub Alam Structured Programming Language 2


(CSE-1121)
if_else Statement
 Format:
if ( test condition )
statements;
else
statements;

if( num % 2 == 0 )
printf( “Even number“ );
else
printf( “Odd number” );

Md. Mahbub Alam Structured Programming Language 3


(CSE-1121)
else_if ladder Statement
 Format:
if(condition 1) if( marks > 79 )
statements; printf( " Honours “ );
else if(condition 2) else if( marks > 59 )
statements; printf( " First Division “ );
else if(condition) else if( marks > 49 )
statements; printf( " Second Division “ );
.............. .. else if( marks > 39 )
.............. .. printf( " Third Division “ );
else if(condition n) else
statements; printf(" Fail ");
else
default statement;

Md. Mahbub Alam Structured Programming Language 4


(CSE-1121)
nested if_else Statement
 Format:
if ( condition ) if(a>b)
if ( condition ) {
if(a>c)
statement1; printf("a is the largest:%d",a);
else else
statement2; printf("c is the largest:%d",c) ;
else }
statement3 else
{
if(c>b)
printf("c is the largest:%d",c);
else
printf("b is the largest:%d",b) ;
}

Md. Mahbub Alam Structured Programming Language 5


(CSE-1121)
switch Statement
 Format: index=marks/10;
switch (index)
{
Switch ( expression ) case 10:
case 9:
{
case 8:
case constant1: printf(" Honours ");
statements; break;
break; case 7:
case 6:
case constant2:
printf(“ First Division ");
statements; break;
break; case 5:
............... printf(“ Second Division ");
break;
...............
case 4:
default: printf(“ Third Division ");
statements; break;
break; default:
printf( “ Fail ");
}
break;
}
Md. Mahbub Alam Structured Programming Language 6
(CSE-1121)
goto Statement
 Format:
goto label; main()
......... {
......... double x,y;
label: read:
statements; scanf("%lf",&x);
if(x<0)
label:
goto read;
statements;
y=sqrt(x);
.........
printf("%lf",y);
.........
}
goto label;

Md. Mahbub Alam Structured Programming Language 7


(CSE-1121)
Any Question?

Md. Mahbub Alam Structured Programming Language 8


(CSE-1121)
Thank You All

Md. Mahbub Alam Structured Programming Language 9


(CSE-1121)
Lecture05(Control Structure PART-II)

 Today's Outline
 Looping statements in C
• for loop
• while loop
• do-while loop
 Jump Statements in C
• continue
• break
• goto

Md. Mahbub Alam Structured Programming Language (CSE- 1


1121)
for loop
 General Format:
for( initialization; condition; increment/decrement )
{
statements; //body of the loop
}

Init

false
Condition

true

Body of loop

Inc / Dec

Md. Mahbub Alam Structured Programming Language (CSE- 2


1121)
for loop (Cont.)
 Example:

int sum = 0, count;


for(count = 1; count <= 5; count++)
sum = sum + count; //body of the loop
printf("%d", sum);

Md. Mahbub Alam Structured Programming Language (CSE- 3


1121)
while loop
 General Format:
while( condition )
{ Condition
false

statements; //body of the loop


true
}
Body of loop

int sum = 0, count;


count = 1;
while( count <= 5)
{
sum = sum + count;
count++;
}
printf("%d", sum);

Md. Mahbub Alam Structured Programming Language (CSE- 4


1121)
do while loop
 Format:
do
{
Body of loop
statements; //body of the loop
}while( condition );
true
Condition

int sum = 0, count;


false
count = 1;
do
{
sum = sum + count;
count ++;
}while( count <= 5);
printf("%d", sum);

Md. Mahbub Alam Structured Programming Language (CSE- 5


1121)
loop (cont.)
• Loop
– Group of instructions computer executes repeatedly while some
condition remains true
• Counter-controlled repetition
– Definite repetition:
– Known in advance how many times loop will execute
– Control variable used to count repetitions
• Sentinel-controlled repetition
– Indefinite repetition
– Used when number of repetitions not known in advance
– Sentinel value indicates "end of data"

Md. Mahbub Alam Structured Programming Language (CSE- 6


1121)
continue
 Skips the remaining statements in the body of a while, for or do…while
statement and proceeds with the next iteration of the loop
– while and do…while
• Loop-continuation test is evaluated immediately after the continue
statement is executed
– for
• Increment/ decrement expression is executed first, then the loop-
continuation test is evaluated
int i, sum = 0;
for(i=0; ;i++)
{
if(i%2==1)
continue;
sum += i;
}
Md. Mahbub Alam Structured Programming Language (CSE- 7
1121)
break
• Causes immediate exit from a while, for, do…while or switch
statement.
• Program execution continues with the first statement after the control
structure.
• Common uses of the break statement
- Escape early from a loop
- Skip the remainder of a switch statement

int i, sum=0;
for(i=1; ; i++)
{
sum += i;
if(sum >= 10)
break;
}
Md. Mahbub Alam Structured Programming Language (CSE- 8
1121)
goto Statement
 Format:
goto label;
main()
.........
{
.........
double x,y;
label:
read:
statements;
scanf("%lf",&x);
if(x<0)
label: goto read;
statements; y=sqrt(x);
......... printf("%lf",y);
......... }
goto label;

Md. Mahbub Alam Structured Programming Language (CSE- 9


1121)
Any Question?

Md. Mahbub Alam Structured Programming Language (CSE- 10


1121)
Thank You All

Md. Mahbub Alam Structured Programming Language (CSE- 11


1121)

You might also like