Programming Fundamentals
Outline
• Course introduction
• Programming languages concepts
• C Programming Basics
• Machine languages
• Assembly languages
• High-level languages
Basic Structure of C Program
3. 3
• Theoretical work
Maximum Marks 100
Attendance 10
Tests 05
Assignments 05
Mid Semester Examination 20
Final Semester Examination 60
– 1CHs (42 lectures) :
• Practical work
Semester Lab Examination 30
4. Course Introduction
4
• Recommended Text Book:
– Turbo C Programming for the PC
» Robert Lafore
• Reference Books:
– Let Us C, Lates Edition
» Yashavant P. Kanetkar
– A Book On C
» Al Kelley and Ira Pohl
• Online Literature/Tutorials etc.
5. Course Introduction
5
• Recommended Tool:
– DevC++
• Practical: SD Lab
– Class exercises and lab tasks
– Assisted by Mr. Shabir Ali Shah
• Teaching plan:
– 02 tests:
• Each after 20TH
and 35TH
lectures
– Assignments
6. Course Introduction
6
• 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
• C projects
– ATM
– Marks Certificate
7. Course Introduction
7
– Traffic Control System
– Simple Games
– …
• Lecture slides/announcements/results can
be found at course home page:
http://saleem.quest.edu.pk/
9. 9
Programming Languages Concept
• What is a programming language?
• Why do we need programming languages?
• What are the types of programming languages?
10. Programming Languages Concepts
10
• What is a programming language?
– A programming language is a set of rules that provides
a way of telling a computer how and what operations
to perform.
• What is a programming language?
– The description of a programming language is usually
split into the two components of syntax (form) and
semantics (meaning).
11. Programming Languages Concepts
11
– A program can execute only if it is both syntactically
and semantically correct.
• Why do we need programming languages?
– Facilitate users in performing tasks which are:
1. Faster,
2. Correct, and
3. Economically cheaper
3. What are the types of programming language?
12. Programming Languages Concepts
12
– Programming languages may be divided into three (03)
general types:
1. Machine languages
2. Assembly languages
3. High-level languages
• Machine languages
– Strings of 0’s and 1’s telling computers to perform
basic operations one at a time; e.g.:
01001110
00111001
13. Programming Languages Concepts
13
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.
• Assembly languages
– Symbolic operation codes replaced binary operation
codes; e.g.:
LOAD R1, sessional
LOAD R2, final
14. Programming Languages Concepts
14
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.
• High-level languages
– Closer to English but included simple mathematical
notation; e.g.:
total_marks = sessional + final
15. Programming Languages Concepts
15
– 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”.
16. 16
Broad Categories of Programming
Languages
• Programming languages may be divided into two
(02) broad categories:
– Traditional/Procedural programming languages
» C, BASIC, PASCAL, PYTHON etc.
– Object-oriented programming languages
» C++, C#, JAVA, CURL etc.
18. 18
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.
25. c Structure of C Program
{
printf(“This is my first
program in C”);
Program body
getch(); getch function
that keeps output visible on
screen
}
• Function Definition
• Delimiter
26. c Structure of C Program
• Statement Terminator • The printf() function
• Preprocessor Directive
• 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.
27. c Structure of C Program
– The word "void“ preceding the "main" specifies that
the function main( ) will not return a value. The second
"void", in the parenthesis, specifies that the function
takes no arguments.
• 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.
28. c Structure of C Program
– The closing brace " } " terminates the block code.
• 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.
29. c Structure of C Program
– You can put as many or as few white spaces characters
in your program as you like; since they are invisible to
the compiler.
• 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.
30. c Structure of C Program
– 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.
• 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.
31. c Structure of C 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( ).
32. 32
Pre-processor Directives
Pre-processor directive header/include file
#include <stdio.h>
#include <conio.h>
• Program instruction (which end with semicolon) are
instructions to the computer to do something; pre-processor
directives are instruction to the compiler.
• A part of the compiler called the pre-processor deals with
these directives before it begins the real compilation
process.
33. Exploring the printf() Function
33
• #include tells the compiler to insert another file into your
source file.
• The type file included by #include is called a header/include file
• 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);
}
34. Exploring the printf() Function
34
• 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
35. Exploring the printf() Function
35
• The function printf() can be given more than one
argument. For example in the above program we
have taken 2 arguments in the printf() function.
• The two arguments are separated by a comma.
• The printf() function takes the vales on the right of
the comma and plugs it into the string on the left.
36. 36
Format Specifiers
• The format specifier tells the printf() where to put
avalue in a string and what format to use in
printing the values.
– In the previous program, the %d tells the printf() to
print the value 20 as a decimal integer.
List of Format Specifiers for printf()
40. Using Format Specifiers in printf()
40
void main(void)
{
printf("The letter %c is ", 'j');
printf("pronounced as %s", 'jay');
}
void main(void)
{
41. Using Format Specifiers in printf()
41
printf("The letter %c is ", 'j');
printf("pronounced as %s", 'jay');
}
Output:
The letter j is pronounced as jay
• In the above program, there are two new
things to note:
42. Using Format Specifiers in printf()
42
– 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.
• 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
43. Using Format Specifiers in printf()
43
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?
• 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
44. Using Format Specifiers in printf()
44
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?
void main(void)
{
45. Using Format Specifiers in printf()
45
printf("The letter %c is n", 'j');
printf("pronounced as %s", 'jay');
}
void main(void)
{
printf("The letter %c is n", 'j');
printf("pronounced as %s", 'jay');
}
46. Using Format Specifiers in printf()
46
Output:
The letter j is
pronounced as jay
• 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.,
47. Using Format Specifiers in printf()
47
following ' n ' character, printing is resumed at
the beginning of the next line.