Lec. Introduction
Lec. Introduction
Lecture – 01 and 02
Dated: 21-01-2025
1
Outline
• Course introduction
• Programming languages concepts
• C Programming Basics
2
Course Introduction
3
Course Introduction
• Course CHs:
– 03 CHs (42 lectures) :
• Theoretical work
Maximum Marks 100
Tests 05
Assignments 05
Mid Semester Examination 20
Final Semester Examination 60
• Reference Books:
– Let Us C, Latest Edition
» Yashavant P. Kanetkar
– A Book On C
» Al Kelley and Ira Pohl
• Practical: SD Lab
– Class exercises and lab tasks
6
Course Introduction
• Teaching plan:
– 02 tests:
• Each after 20TH and 35TH lectures
– Assignments
• At the end of each book chapter (Robert Lafore)
• Assignments are to be collected 1 week after 42nd lecture
– Practical exercises:
• Almost in each practical
• Maintenance of practical
• C project 7
Course Introduction
• C projects
– ATM
– Marks Certificate
– Traffic Control System
– Simple Games
–…
8
Course Introduction
• Lecture slides/announcements/results can be
found at course home page:
http://saleem.quest.edu.pk/
https://sites.google.com/a/quest.edu.pk/saleem
9
Important Instructions
• During the class:
– Keep silent
– Turn off mobile phones
– No attendance for late comers
10
Programming Languages Concept
11
Programming Languages Concepts
• What is a programming language?
12
Programming Languages Concepts
• What is a programming language?
13
Programming Languages Concepts
• What is a programming language?
14
Programming Languages Concepts
• Why do we need programming languages?
– Facilitate users in performing tasks which are:
1. Faster,
2. Correct, and
3. Economically cheaper
15
Programming Languages Concepts
3. What are the types of programming language?
16
Programming Languages Concepts
• Machine languages
– Strings of 0’s and 1’s telling computers to perform
basic operations one at a time; e.g.:
01001110
00111001
01101010
– Machine dependent i.e., a code written for one
machine may not run on the other.
– Programming in machine languages is too slow,
tedious, and error-prone.
17
Programming Languages Concepts
• Assembly languages
– Symbolic operation codes replaced binary operation
codes; e.g.:
LOAD R1, sessional
LOAD R2, final
ADD R1, R2
STORE total_marks
– Assembly language programs need to be “assembled” for
execution by the computer. Each assembly language
instruction is translated into one machine language
instruction.
– Very efficient code and easier to write.
18
Programming Languages Concepts
• High-level languages
– Closer to English but included simple mathematical
notation; e.g.:
total_marks = sessional + final
– Programs written in source code which must be
translated into machine language programs called
object code.
– The translation of source code to object code is
accomplished by a machine language system program
called a “compiler”.
19
Broad Categories of Programming
Languages
• Programming languages may be divided into two
(02) broad categories:
20
C Programming Basics
21
C Programming
• C is a programming language
developed at AT & T’s Bell
Laboratories of USA in 1972.
• The inventor of the C language
is Dennis Ritchie.
• C language is reliable, simple
and easy to use.
• Using C, one can develop
programs for mobile phones,
microwave ovens, 3D games,
and so on.
22
Getting Started
#include <stdio.h> Pre-processor
directive
#include <conio.h>
Function return type
main function
28
The Basic Structure of C Program
• Function Definition
• Delimiter
• Statement Terminator
• The printf() function
• Preprocessor Directive
29
The Basic Structure of C Program
• Function Definition
– All C Language programs are divided into units called
"Functions".
– A function in C has ( ) at its end. main( ) is always be
the first function to be executed and is the one to
which control is passed when the program is
executed.
– The word “int“ preceding the "main" specifies that
the function return type. The second "void", in the
parenthesis, specifies that the function takes no
arguments.
30
The Basic Structure of C Program
• Delimiter
– Following the function definition are the braces,
which signals the beginning and end of the body of
the function.
– The opening brace " { " indicates that a block of code
that forms a distinct unit is about to begin.
– The closing brace " } " terminates the block code.
31
The Basic Structure of C Program
• Statement Terminator
– The line in our program that begins with the word
"printf " is an example of a statement.
– A statement in C Language is terminated with a
semicolon " ; ".
– The C Language pays no attention to any of the
"White Space Character": the carriage return
(newline), the spacebar and the tab key.
– You can put as many or as few white spaces
characters in your program as you like; since they are
invisible to the compiler.
32
The Basic Structure of C Program
• The printf ( ) function
– The program line printf ("This is my first program in C
Language."); causes the phrase in quotes to be
printed on the screen.
– The word printf is actually a function, just as " main "
is a function name. Since "printf" is a function name,
therefore it is followed by parenthesis.
33
The Basic Structure of C Program
• The printf ( ) function
– The DevC++ linker looks in the stdio.h file of INCLUDE
directory, finds the section of this file containing
printf( ) and causes this section to be linked with the
source program.
– C Language distinguishes between uppercase and
lowercase letters i.e., C language is case sensitive.
– Thus the function PRINTF( ) and Printf( ) are not the
same as the function printf( ).
34
Pre-processor Directives
Pre-processor directive header/include file
#include <stdio.h>
#include <conio.h>
void main(void)
{
printf("I am %d years old", 20);
}
36
Exploring the printf() Function
• The printf ( ) function
– The printf() function uses a unique format for printing
constants and variables. For example
void main(void)
{
printf("I am %d years old", 20);
}
▪ Output:
I am 20 years old
37
Exploring the printf() Function
38
Format Specifiers
39
List of Format Specifiers for printf()
40
Using Format Specifiers in printf()
41
Using Format Specifiers in printf()
42
Using Format Specifiers in printf()
void main(void)
{
printf("The letter %c is ", 'j');
printf("pronounced as %s", 'jay');
}
43
Using Format Specifiers in printf()
void main(void)
{
printf("The letter %c is ", 'j');
printf("pronounced as %s", 'jay');
}
▪ Output:
The letter j is pronounced as jay
44
Using Format Specifiers in printf()
• In the above program, there are two new
things to note:
– First we have used %c for printing a character
which is surrounded by single quotes and %s is
used for printing a string which is surrounded by
double quotes.
– This is how C language differentiates between a
character and a string.
45
Using Format Specifiers in printf()
• Second thing to note is that even though the
output statement is printed by two separate
program lines, it does not consist of two line
of text on the out put screen. That is because
printf() does not automatically prints a
newline character at the end of the line.
• So what to do for inserting a newline in a
printf() statement?
46
Using Format Specifiers in printf()
• Second thing to note is that even though the
output statement is printed by two separate
program lines, it does not consist of two line
of text on the out put screen. That is because
printf() does not automatically prints a
newline character at the end of the line.
• So what to do for inserting a newline in a
printf() statement?
47
Using Format Specifiers in printf()
void main(void)
{
printf("The letter %c is \n", 'j');
printf("pronounced as %s", 'jay');
}
48
Using Format Specifiers in printf()
void main(void)
{
printf("The letter %c is \n", 'j');
printf("pronounced as %s", 'jay');
}
▪ Output:
The letter j is
pronounced as jay
49
Using Format Specifiers in printf()
• The above example includes a new symbol,
the ' \n '.
• In C this means "newline ". The ' \n ' has the
effect of a carriage return and linefeed i.e.,
following ' \n ' character, printing is resumed
at the beginning of the next line.
50
Escape Sequences
• Escape sequences allow to control the output of the program
51