SlideShare a Scribd company logo
1 of 37
Download to read offline
Do not Confuse between C and C++
• C is a structured language

• C++ is an object oriented language
• C++ supports most of the features of structure language
hence you may say C is a subset of C++ (with few
exceptions that you need not to worry at this stage)

CSC141 Introduction to Computer
Programming
1.
2.
3.
4.
5.
6.
7.
8.
9.

Declaration, definition and initialization of
variables
User input/output functions scanf() and printf()
printf () Function
Format specifier
Field width specifier
Escape sequence
Scanf( ) function
Address operator
For loop
•
•
•
•
•
•

Structure of the for loop
Initialization, test, increment expression
Body of the for loop
Operation of the for loop
Multiple statements in for loop
Multiple initialization in a for loop
CSC141 Introduction to Computer
Programming
Defining and Declaring variables
• A variable declaration only specifies the name
and type of the variable without allocating
memory to it
• Declarations are important in multi file programs
where a variable defined in one file must be
referred to in a second file.
• Variable definition on the other hand specifies
the name and type of the variable and also sets
aside memory for it. When a data is assigned first
time to the variable it is defined.
(We will discuss it in detail when study
functions)
CSC141 Introduction to Computer
Programming
Initializing variables
Examples
int num=2;
int char=„d‟,
float length=34.5677;
By initializing a variable we provide:
• Declaration of variable
• Definition of a variable
• Initial value loaded in the variable

CSC141 Introduction to Computer
Programming
Practice
• Declare a variable “marks” which can store
floating point values.
• Initialise a variable “score” of type integer
with value 20.
• Initialise a variable “ch” of type character with
value A.

CSC141 Introduction to Computer
Programming
User Input and output functions
• The printf() function is used to display output on the
screen
• The scanf() function is used to read input from the
keyboard

• printf() and scanf() both are defined in stdio.h called
header file which we include using:
# include <stdio.h>

CSC141 Introduction to Computer
Programming
The printf() function
• Printf() is a function like main()
• It caused the string written within the quotes “ ” inside
printf() function on the screen
• The string which is phrase in quotes is the function
argument passed to printf()
• “Hello World” is a string of characters. C compiler
recognizes a string when it is enclosed within quotes.

CSC141 Introduction to Computer
Programming
The definition of printf() and scanf() functions
• We have called the function printf() but have not defined it
anywhere
• The declaration is done in the header file called stdio.h
• Its definition is is provided in a standard Library which we
will study later on.
• The linker links our program with the ***.lib file at the time of
linking. This happens for all C library functions
• Remember the name of a function is like variable name also
called an identifier.
CSC141 Introduction to Computer
Programming
Exploring the printf() function
• Printing numbers (integers)
void main (void)
{
printf (“Printing number: %d”, 10);
}
Here printf took two arguments:
Printing number --- argument 1
10 ( what is that ? )--- argument 2
what is %d ---- Format specifier

CSC141 Introduction to Computer
Programming
Format Specifiers
• Tells the compiler where to put the value in string and
what format to use for printing the value

• %d tells the compiler to print 10 as a decimal integer
• Why not write 10 directly inside the string?
Possible !! However, to give printf() more capability of
handling different values, we use format specifiers and
variables

CSC141 Introduction to Computer
Programming
Different Format Specifiers
Format specifiers

used for

%c
%s
%d
%f
%e
%g

single character
string
signed decimal integer
floating point (decimal notation)
exponential notation
floating point (%f or %e
whichever is shorter)
unsigned decimal integer
unsigned hexadecimal
unsigned octal integer
prefix used with %d, %u, %x %o
to specify long integer e.g. %ld

%u
%x
%o
l

CSC141 Introduction to Computer
Programming
Field width Specifiers
Format specifier: %(-)w.nf
• f is a format specifier used for float data type
e.g %f
• .n means number of digits after decimal point
e.g %.2f
27.25 & %.3f means 27.250
• The digit (w) preceding the decimal point specifies the
amount of space that number will use to get displayed:

e.g

printf (“Age is % 2d” 33);
printf (“Age is % 4d” 33);
printf (“Age is % 6d” 33);

CSC141 Introduction to Computer
Programming

33
- - 33
- - - - 33
Field width Specifiers
void main (void)
{
printf(“8.1f %8.1f %8.1fn”, 3.0, 12.5, 523.3);
printf(“8.1f %8.1f %8.1fn”, 300.0, 1200.5, 5300.3);
}
3.0
300.0

12.5
1200.5

„523.3
5300.3

CSC141 Introduction to Computer
Programming
Field width Specifiers
void main (void)
{
printf(“-8.1f %-8.1f %-8.1fn”, 3.0, 12.5, 523.3);
printf(“-8.1f %-8.1f %-8.1fn”, 300.0, 1200.5, 5300.3);
}
3.0‟
„12.5 „ „ 523.3
300.0 „ „ 1200.5 „ „5300.3

CSC141 Introduction to Computer
Programming
Escape Sequence
• n prints carriage return and linefeed
combination
• t tab
• b backspace
• r carriage return
• ‟ single quote
•  “ double quote
•   backslash

CSC141 Introduction to Computer
Programming
Printing Strings using printf () function
void main (void)
{
printf(“%s is %d years old”,”Ahmed”,22);
}

CSC141 Introduction to Computer
Programming
Printing characters using printf () function
void main (void)
{
printf(“%c is pronounced as %s”, ’j’, ”jay”);
}

• Single quotes are for character whereas the
%c is format specifier
• Double quotes are for strings whereas the %s
is format specifier
CSC141 Introduction to Computer
Programming
Scanf() function
void main (void)
{
int userAge;
printf(“Enter the the age: ”);
scanf(“ %d”, &userAge);
}
• scanf() reads in data and stores it in one or more
variables.
• The first argument (the control string), contains a series of
placeholders
• The remaining arguments to scanf() is a variable name
proceed with & sign called address operator
CSC141 Introduction to Computer
Programming
scanf() can read data into multiple variables
int a, b,
float c;
char d;
scanf(“ %d %d %f %c” , &a, &b, &c, &d);
• Input is stored in these variables. Each variable name
is preceded by & , &a means “the memory address
associated with variable a”
• It uses any whitespace (space, newline or tab)
character to delimit inputs in case of multiple inputs
• Format specifiers are like the ones that printf() uses
e.g. %d = int, %f = float, %c = char, etc.
CSC141 Introduction to Computer
Programming
Comments
• /* --------------------*/ multiline comments
• // single line comments

CSC141 Introduction to Computer
Programming
Loops
• Computer has the ability to perform set of instructions
repeatedly
• This repetitive operation is performed using loops

• It involves repeating some portion of the program either
a specified number of times or until a particular condition
is being satisfied

CSC141 Introduction to Computer
Programming
Loops
• There are three types of loops in C
– For loop
– While loop
– Do while loop
• For loop is the most popular looping instruction used by
the programmers

CSC141 Introduction to Computer
Programming
For loop
• It specifies three things about a loop in one line
– Setting a loop counter to an initial value
– Testing the loop counter to determine whether its
value has reached the number of desired repetitions
– Increasing the value of loop counter each time the
program segment within the loop has been executed

CSC141 Introduction to Computer
Programming
Flowchart of the for loop

CSC141 Introduction to Computer
Programming
For loop … continued
The general form of for loop is:
for (initialize counter; test counter; increment/decrement
counter)
{
Do this;
And this;
And this;
}

CSC141 Introduction to Computer
Programming
Example:
Print an integer number from -4 to 0

CSC141 Introduction to Computer
Programming
Sample Program
void main()
{
int j;
for(j = -4; j <= 0 ; j = j + 1)
printf("%dn", j);
}

CSC141 Introduction to Computer
Programming
print numbers from 1 to 10
void main( )

{
int i ;
for ( i = 1 ; i <= 10 ; i = i + 1 )
printf ( "%dn", i ) ;
}
• Instead of i = i + 1, the statements i++ or i += 1 can also
be used.

CSC141 Introduction to Computer
Programming
Print numbers from 1 to 10
void main( )
{
int i ;
for ( i = 1 ; i <= 10 ; )
{
printf ( "%dn", i ) ;
i=i+1;
}
}
• Here, the increment is done within the body of the for
loop and not in the for statement. Note that despite of
this the semicolon after the condition is necessary.
CSC141 Introduction to Computer
Programming
print numbers from 1 to 10
void main( )
{
int i = 1 ;
for ( ; i <= 10 ; i = i + 1 )
printf ( "%dn", i ) ;
}
• Here the initialisation is done in the declaration
statement itself, but still the semicolon before the
condition is necessary.

CSC141 Introduction to Computer
Programming
print numbers from 1 to 10
void main( )
{
int i = 1 ;
for ( ; i <= 10 ; )
{
printf ( "%dn", i ) ;
i=i+1;
}
}
• Here, neither the initialisation, nor the incrementation is
done in the for statement, but still the two semicolons are
necessary.

CSC141 Introduction to Computer
Programming
print numbers from 1 to 10
void main( )
{
int i ;
for ( i = 0 ; i++ < 10 ; )
printf ( "%dn", i ) ;
}
• Here, the comparison as well as the incrementation is
done through the same statement, i++ < 10. Since the
++ operator comes after i firstly comparison is done,
followed by increment. Note that it is necessary to
initialize i to 0.

CSC141 Introduction to Computer
Programming
print numbers from 1 to 10
void main( )
{
int i ;
for ( i = 0 ; ++i <= 10 ; )
printf ( "%dn", i ) ;
}
• Here, both, the comparison and the incrementation is
done through the same statement, ++i <= 10. Since ++
precedes i firstly incrementation is done, followed by
comparison. Note that it is necessary to initialize i to 0.

CSC141 Introduction to Computer
Programming
Nesting of Loops
r = 1 c = 1 sum = 2
r = 1 c = 2 sum = 3
r = 2 c = 1 sum = 3
r = 2 c = 2 sum = 4
r = 3 c = 1 sum = 4
r = 3 c = 2 sum = 5

void

CSC141 Introduction to Computer
Programming
Multiple Initialisations in the for Loop
• The initialisation expression of the for loop can contain
more than one statement separated by a comma. For
example,
• for ( i = 1, j = 2 ; j <= 10 ; j++ )
• Multiple statements can also be used in the
incrementation expression of for loop; i.e., you can
increment (or decrement) two or more variables at the
same time.
• However, only one expression is allowed in the test
expression. This expression may contain several
conditions linked together using logical operators.

CSC141 Introduction to Computer
Programming
Solution to the last Practice Quiz
Question -1: Solve the following expressions
a).
3-8/4
b). 3 * 4 + 18 / 2

void main( )
{
printf ( “The result of the expression is %d n",
3 - 8 / 4) ;
printf ( “The result of the expression is %d n",
(3 * 4 + 18 / 2));
}

CSC141 Introduction to Computer
Programming
Solution to the last Practice Quiz
• Question - 2: If a=2, b=4, c=6, d=8, then what will be
the result of the following expression?
•
x = a * b + c * d / 4;
void main( )
{
int a =2, b=4, c=6, d=8;
printf ( “The result of the expression is %d n",
x = a * b + c * d / 4) ;
}

CSC141 Introduction to Computer
Programming

More Related Content

What's hot

Toutes les schémas de démarrage d'un moteur asynchrone par www.genie electrom...
Toutes les schémas de démarrage d'un moteur asynchrone par www.genie electrom...Toutes les schémas de démarrage d'un moteur asynchrone par www.genie electrom...
Toutes les schémas de démarrage d'un moteur asynchrone par www.genie electrom...Imed Frioukh
 
Learn Ethical Hacking With Kali Linux | Ethical Hacking Tutorial | Kali Linux...
Learn Ethical Hacking With Kali Linux | Ethical Hacking Tutorial | Kali Linux...Learn Ethical Hacking With Kali Linux | Ethical Hacking Tutorial | Kali Linux...
Learn Ethical Hacking With Kali Linux | Ethical Hacking Tutorial | Kali Linux...Edureka!
 
presentation pic 16f84.ppt
presentation pic 16f84.pptpresentation pic 16f84.ppt
presentation pic 16f84.pptsaidmahfoud2
 
Computer Wireless Network Pdf - course material 2013
Computer Wireless Network Pdf - course material 2013Computer Wireless Network Pdf - course material 2013
Computer Wireless Network Pdf - course material 2013vasanthimuniasamy
 
2 input output dan internal memori
2 input output dan internal memori2 input output dan internal memori
2 input output dan internal memoriSimon Patabang
 
Network scanning
Network scanningNetwork scanning
Network scanningoceanofwebs
 
3_TD1 +Correction.pdf
3_TD1 +Correction.pdf3_TD1 +Correction.pdf
3_TD1 +Correction.pdffatimakhdidr
 
144603938 exercices-capteur
144603938 exercices-capteur144603938 exercices-capteur
144603938 exercices-capteurMohammed moudine
 
13. Pointer and 2D array
13. Pointer  and  2D array13. Pointer  and  2D array
13. Pointer and 2D arrayGem WeBlog
 
Cours structures des données (langage c)
Cours structures des données (langage c)Cours structures des données (langage c)
Cours structures des données (langage c)rezgui mohamed
 
Circuits Base Hydraulique
Circuits Base HydrauliqueCircuits Base Hydraulique
Circuits Base Hydrauliqueyouri59490
 

What's hot (20)

Toutes les schémas de démarrage d'un moteur asynchrone par www.genie electrom...
Toutes les schémas de démarrage d'un moteur asynchrone par www.genie electrom...Toutes les schémas de démarrage d'un moteur asynchrone par www.genie electrom...
Toutes les schémas de démarrage d'un moteur asynchrone par www.genie electrom...
 
Langage vhdl
Langage vhdlLangage vhdl
Langage vhdl
 
Learn Ethical Hacking With Kali Linux | Ethical Hacking Tutorial | Kali Linux...
Learn Ethical Hacking With Kali Linux | Ethical Hacking Tutorial | Kali Linux...Learn Ethical Hacking With Kali Linux | Ethical Hacking Tutorial | Kali Linux...
Learn Ethical Hacking With Kali Linux | Ethical Hacking Tutorial | Kali Linux...
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Série révision. 2
Série révision. 2Série révision. 2
Série révision. 2
 
presentation pic 16f84.ppt
presentation pic 16f84.pptpresentation pic 16f84.ppt
presentation pic 16f84.ppt
 
Basic PLC
Basic PLCBasic PLC
Basic PLC
 
Web Server Hardening
Web Server HardeningWeb Server Hardening
Web Server Hardening
 
Computer Wireless Network Pdf - course material 2013
Computer Wireless Network Pdf - course material 2013Computer Wireless Network Pdf - course material 2013
Computer Wireless Network Pdf - course material 2013
 
2 input output dan internal memori
2 input output dan internal memori2 input output dan internal memori
2 input output dan internal memori
 
Twido guide de programmation
Twido guide de programmationTwido guide de programmation
Twido guide de programmation
 
Network scanning
Network scanningNetwork scanning
Network scanning
 
3_TD1 +Correction.pdf
3_TD1 +Correction.pdf3_TD1 +Correction.pdf
3_TD1 +Correction.pdf
 
144603938 exercices-capteur
144603938 exercices-capteur144603938 exercices-capteur
144603938 exercices-capteur
 
Operators
OperatorsOperators
Operators
 
Structure of a C program
Structure of a C programStructure of a C program
Structure of a C program
 
13. Pointer and 2D array
13. Pointer  and  2D array13. Pointer  and  2D array
13. Pointer and 2D array
 
Cours structures des données (langage c)
Cours structures des données (langage c)Cours structures des données (langage c)
Cours structures des données (langage c)
 
Process synchronization in operating system
Process synchronization in operating systemProcess synchronization in operating system
Process synchronization in operating system
 
Circuits Base Hydraulique
Circuits Base HydrauliqueCircuits Base Hydraulique
Circuits Base Hydraulique
 

Viewers also liked

C and C ++ Training in Ambala ! BATRA COMPUTER CENTRE
C and C ++ Training in Ambala ! BATRA COMPUTER CENTREC and C ++ Training in Ambala ! BATRA COMPUTER CENTRE
C and C ++ Training in Ambala ! BATRA COMPUTER CENTREjatin batra
 
C & C++ Training Centre in Ambala! BATRA COMPUTER CENTRE
C & C++ Training Centre in Ambala! BATRA COMPUTER CENTREC & C++ Training Centre in Ambala! BATRA COMPUTER CENTRE
C & C++ Training Centre in Ambala! BATRA COMPUTER CENTREjatin batra
 
C & C++ Training in Ambala ! BATRA COMPUTER CENTRE
C & C++ Training in Ambala ! BATRA COMPUTER CENTREC & C++ Training in Ambala ! BATRA COMPUTER CENTRE
C & C++ Training in Ambala ! BATRA COMPUTER CENTREjatin batra
 
Intro to C++ Basic
Intro to C++ BasicIntro to C++ Basic
Intro to C++ BasicShih Chi Lin
 
basics of C and c++ by eteaching
basics of C and c++ by eteachingbasics of C and c++ by eteaching
basics of C and c++ by eteachingeteaching
 
difference between c c++ c#
difference between c c++ c#difference between c c++ c#
difference between c c++ c#Sireesh K
 
INTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMINGINTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMINGAbhishek Dwivedi
 
Basics of C programming
Basics of C programmingBasics of C programming
Basics of C programmingavikdhupar
 

Viewers also liked (12)

C and C ++ Training in Ambala ! BATRA COMPUTER CENTRE
C and C ++ Training in Ambala ! BATRA COMPUTER CENTREC and C ++ Training in Ambala ! BATRA COMPUTER CENTRE
C and C ++ Training in Ambala ! BATRA COMPUTER CENTRE
 
C Programming
C ProgrammingC Programming
C Programming
 
C & C++ Training Centre in Ambala! BATRA COMPUTER CENTRE
C & C++ Training Centre in Ambala! BATRA COMPUTER CENTREC & C++ Training Centre in Ambala! BATRA COMPUTER CENTRE
C & C++ Training Centre in Ambala! BATRA COMPUTER CENTRE
 
C & C++ Training in Ambala ! BATRA COMPUTER CENTRE
C & C++ Training in Ambala ! BATRA COMPUTER CENTREC & C++ Training in Ambala ! BATRA COMPUTER CENTRE
C & C++ Training in Ambala ! BATRA COMPUTER CENTRE
 
Intro to C++ Basic
Intro to C++ BasicIntro to C++ Basic
Intro to C++ Basic
 
basics of C and c++ by eteaching
basics of C and c++ by eteachingbasics of C and c++ by eteaching
basics of C and c++ by eteaching
 
C vs c++
C vs c++C vs c++
C vs c++
 
C vs c++
C vs c++C vs c++
C vs c++
 
difference between c c++ c#
difference between c c++ c#difference between c c++ c#
difference between c c++ c#
 
INTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMINGINTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMING
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 
Basics of C programming
Basics of C programmingBasics of C programming
Basics of C programming
 

Similar to Discussing Fundamentals of C

cmp104 lec 8
cmp104 lec 8cmp104 lec 8
cmp104 lec 8kapil078
 
Fundamental of C Programming Language and Basic Input/Output Function
  Fundamental of C Programming Language and Basic Input/Output Function  Fundamental of C Programming Language and Basic Input/Output Function
Fundamental of C Programming Language and Basic Input/Output Functionimtiazalijoono
 
Esoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programmingEsoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programmingRasan Samarasinghe
 
Cse115 lecture04introtoc programming
Cse115 lecture04introtoc programmingCse115 lecture04introtoc programming
Cse115 lecture04introtoc programmingMd. Ashikur Rahman
 
Input and Output In C Language
Input and Output In C LanguageInput and Output In C Language
Input and Output In C LanguageAdnan Khan
 
424769021-1-First-C-Program-1-ppt (1).ppt
424769021-1-First-C-Program-1-ppt (1).ppt424769021-1-First-C-Program-1-ppt (1).ppt
424769021-1-First-C-Program-1-ppt (1).pptadvRajatSharma
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and typesimtiazalijoono
 
c_pro_introduction.pptx
c_pro_introduction.pptxc_pro_introduction.pptx
c_pro_introduction.pptxRohitRaj744272
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programmingAlpana Gupta
 
Sample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.SivakumarSample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.SivakumarSivakumar R D .
 
Datastructure notes
Datastructure notesDatastructure notes
Datastructure notesSrikanth
 

Similar to Discussing Fundamentals of C (20)

cmp104 lec 8
cmp104 lec 8cmp104 lec 8
cmp104 lec 8
 
Fundamental of C Programming Language and Basic Input/Output Function
  Fundamental of C Programming Language and Basic Input/Output Function  Fundamental of C Programming Language and Basic Input/Output Function
Fundamental of C Programming Language and Basic Input/Output Function
 
CP Handout#2
CP Handout#2CP Handout#2
CP Handout#2
 
Esoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programmingEsoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programming
 
COM1407: Input/ Output Functions
COM1407: Input/ Output FunctionsCOM1407: Input/ Output Functions
COM1407: Input/ Output Functions
 
Cse115 lecture04introtoc programming
Cse115 lecture04introtoc programmingCse115 lecture04introtoc programming
Cse115 lecture04introtoc programming
 
Input and Output In C Language
Input and Output In C LanguageInput and Output In C Language
Input and Output In C Language
 
424769021-1-First-C-Program-1-ppt (1).ppt
424769021-1-First-C-Program-1-ppt (1).ppt424769021-1-First-C-Program-1-ppt (1).ppt
424769021-1-First-C-Program-1-ppt (1).ppt
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
 
Basic of C Programming | 2022 Updated | By Shamsul H. Ansari
Basic of C Programming | 2022 Updated | By Shamsul H. AnsariBasic of C Programming | 2022 Updated | By Shamsul H. Ansari
Basic of C Programming | 2022 Updated | By Shamsul H. Ansari
 
c_pro_introduction.pptx
c_pro_introduction.pptxc_pro_introduction.pptx
c_pro_introduction.pptx
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
C programming
C programmingC programming
C programming
 
Unit-IV.pptx
Unit-IV.pptxUnit-IV.pptx
Unit-IV.pptx
 
C fundamentals
C fundamentalsC fundamentals
C fundamentals
 
Sample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.SivakumarSample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.Sivakumar
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Unit1 C
Unit1 CUnit1 C
Unit1 C
 
Unit1 C
Unit1 CUnit1 C
Unit1 C
 
Datastructure notes
Datastructure notesDatastructure notes
Datastructure notes
 

More from educationfront

Personality Development in Islam
Personality Development in IslamPersonality Development in Islam
Personality Development in Islameducationfront
 
Introduction to Programming Languages
Introduction to Programming LanguagesIntroduction to Programming Languages
Introduction to Programming Languageseducationfront
 
Fundamentals of Computer
Fundamentals of Computer Fundamentals of Computer
Fundamentals of Computer educationfront
 
Punjab Public Service Commission (PPSC)
Punjab Public Service Commission (PPSC)Punjab Public Service Commission (PPSC)
Punjab Public Service Commission (PPSC)educationfront
 

More from educationfront (6)

Personality Development in Islam
Personality Development in IslamPersonality Development in Islam
Personality Development in Islam
 
Introduction to Programming Languages
Introduction to Programming LanguagesIntroduction to Programming Languages
Introduction to Programming Languages
 
Fundamentals of Computer
Fundamentals of Computer Fundamentals of Computer
Fundamentals of Computer
 
Fcp lecture 01
Fcp lecture 01Fcp lecture 01
Fcp lecture 01
 
Punjab Public Service Commission (PPSC)
Punjab Public Service Commission (PPSC)Punjab Public Service Commission (PPSC)
Punjab Public Service Commission (PPSC)
 
Action research
Action researchAction research
Action research
 

Recently uploaded

How to Use api.constrains ( ) in Odoo 17
How to Use api.constrains ( ) in Odoo 17How to Use api.constrains ( ) in Odoo 17
How to Use api.constrains ( ) in Odoo 17Celine George
 
How to Show Error_Warning Messages in Odoo 17
How to Show Error_Warning Messages in Odoo 17How to Show Error_Warning Messages in Odoo 17
How to Show Error_Warning Messages in Odoo 17Celine George
 
Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...raviapr7
 
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...Nguyen Thanh Tu Collection
 
HED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfHED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfMohonDas
 
In - Vivo and In - Vitro Correlation.pptx
In - Vivo and In - Vitro Correlation.pptxIn - Vivo and In - Vitro Correlation.pptx
In - Vivo and In - Vitro Correlation.pptxAditiChauhan701637
 
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptxSandy Millin
 
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptx
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptxPractical Research 1: Lesson 8 Writing the Thesis Statement.pptx
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptxKatherine Villaluna
 
Benefits & Challenges of Inclusive Education
Benefits & Challenges of Inclusive EducationBenefits & Challenges of Inclusive Education
Benefits & Challenges of Inclusive EducationMJDuyan
 
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRADUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRATanmoy Mishra
 
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptxPISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptxEduSkills OECD
 
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdfP4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdfYu Kanazawa / Osaka University
 
Prescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptxPrescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptxraviapr7
 
CapTechU Doctoral Presentation -March 2024 slides.pptx
CapTechU Doctoral Presentation -March 2024 slides.pptxCapTechU Doctoral Presentation -March 2024 slides.pptx
CapTechU Doctoral Presentation -March 2024 slides.pptxCapitolTechU
 
How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17Celine George
 
3.21.24 The Origins of Black Power.pptx
3.21.24  The Origins of Black Power.pptx3.21.24  The Origins of Black Power.pptx
3.21.24 The Origins of Black Power.pptxmary850239
 
What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?TechSoup
 
UKCGE Parental Leave Discussion March 2024
UKCGE Parental Leave Discussion March 2024UKCGE Parental Leave Discussion March 2024
UKCGE Parental Leave Discussion March 2024UKCGE
 
Philosophy of Education and Educational Philosophy
Philosophy of Education  and Educational PhilosophyPhilosophy of Education  and Educational Philosophy
Philosophy of Education and Educational PhilosophyShuvankar Madhu
 

Recently uploaded (20)

How to Use api.constrains ( ) in Odoo 17
How to Use api.constrains ( ) in Odoo 17How to Use api.constrains ( ) in Odoo 17
How to Use api.constrains ( ) in Odoo 17
 
How to Show Error_Warning Messages in Odoo 17
How to Show Error_Warning Messages in Odoo 17How to Show Error_Warning Messages in Odoo 17
How to Show Error_Warning Messages in Odoo 17
 
Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...
 
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
 
HED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfHED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdf
 
In - Vivo and In - Vitro Correlation.pptx
In - Vivo and In - Vitro Correlation.pptxIn - Vivo and In - Vitro Correlation.pptx
In - Vivo and In - Vitro Correlation.pptx
 
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
 
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptx
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptxPractical Research 1: Lesson 8 Writing the Thesis Statement.pptx
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptx
 
Benefits & Challenges of Inclusive Education
Benefits & Challenges of Inclusive EducationBenefits & Challenges of Inclusive Education
Benefits & Challenges of Inclusive Education
 
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRADUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
 
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptxPISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
 
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdfP4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
 
Prescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptxPrescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptx
 
CapTechU Doctoral Presentation -March 2024 slides.pptx
CapTechU Doctoral Presentation -March 2024 slides.pptxCapTechU Doctoral Presentation -March 2024 slides.pptx
CapTechU Doctoral Presentation -March 2024 slides.pptx
 
How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17
 
Prelims of Kant get Marx 2.0: a general politics quiz
Prelims of Kant get Marx 2.0: a general politics quizPrelims of Kant get Marx 2.0: a general politics quiz
Prelims of Kant get Marx 2.0: a general politics quiz
 
3.21.24 The Origins of Black Power.pptx
3.21.24  The Origins of Black Power.pptx3.21.24  The Origins of Black Power.pptx
3.21.24 The Origins of Black Power.pptx
 
What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?
 
UKCGE Parental Leave Discussion March 2024
UKCGE Parental Leave Discussion March 2024UKCGE Parental Leave Discussion March 2024
UKCGE Parental Leave Discussion March 2024
 
Philosophy of Education and Educational Philosophy
Philosophy of Education  and Educational PhilosophyPhilosophy of Education  and Educational Philosophy
Philosophy of Education and Educational Philosophy
 

Discussing Fundamentals of C

  • 1. Do not Confuse between C and C++ • C is a structured language • C++ is an object oriented language • C++ supports most of the features of structure language hence you may say C is a subset of C++ (with few exceptions that you need not to worry at this stage) CSC141 Introduction to Computer Programming
  • 2. 1. 2. 3. 4. 5. 6. 7. 8. 9. Declaration, definition and initialization of variables User input/output functions scanf() and printf() printf () Function Format specifier Field width specifier Escape sequence Scanf( ) function Address operator For loop • • • • • • Structure of the for loop Initialization, test, increment expression Body of the for loop Operation of the for loop Multiple statements in for loop Multiple initialization in a for loop CSC141 Introduction to Computer Programming
  • 3. Defining and Declaring variables • A variable declaration only specifies the name and type of the variable without allocating memory to it • Declarations are important in multi file programs where a variable defined in one file must be referred to in a second file. • Variable definition on the other hand specifies the name and type of the variable and also sets aside memory for it. When a data is assigned first time to the variable it is defined. (We will discuss it in detail when study functions) CSC141 Introduction to Computer Programming
  • 4. Initializing variables Examples int num=2; int char=„d‟, float length=34.5677; By initializing a variable we provide: • Declaration of variable • Definition of a variable • Initial value loaded in the variable CSC141 Introduction to Computer Programming
  • 5. Practice • Declare a variable “marks” which can store floating point values. • Initialise a variable “score” of type integer with value 20. • Initialise a variable “ch” of type character with value A. CSC141 Introduction to Computer Programming
  • 6. User Input and output functions • The printf() function is used to display output on the screen • The scanf() function is used to read input from the keyboard • printf() and scanf() both are defined in stdio.h called header file which we include using: # include <stdio.h> CSC141 Introduction to Computer Programming
  • 7. The printf() function • Printf() is a function like main() • It caused the string written within the quotes “ ” inside printf() function on the screen • The string which is phrase in quotes is the function argument passed to printf() • “Hello World” is a string of characters. C compiler recognizes a string when it is enclosed within quotes. CSC141 Introduction to Computer Programming
  • 8. The definition of printf() and scanf() functions • We have called the function printf() but have not defined it anywhere • The declaration is done in the header file called stdio.h • Its definition is is provided in a standard Library which we will study later on. • The linker links our program with the ***.lib file at the time of linking. This happens for all C library functions • Remember the name of a function is like variable name also called an identifier. CSC141 Introduction to Computer Programming
  • 9. Exploring the printf() function • Printing numbers (integers) void main (void) { printf (“Printing number: %d”, 10); } Here printf took two arguments: Printing number --- argument 1 10 ( what is that ? )--- argument 2 what is %d ---- Format specifier CSC141 Introduction to Computer Programming
  • 10. Format Specifiers • Tells the compiler where to put the value in string and what format to use for printing the value • %d tells the compiler to print 10 as a decimal integer • Why not write 10 directly inside the string? Possible !! However, to give printf() more capability of handling different values, we use format specifiers and variables CSC141 Introduction to Computer Programming
  • 11. Different Format Specifiers Format specifiers used for %c %s %d %f %e %g single character string signed decimal integer floating point (decimal notation) exponential notation floating point (%f or %e whichever is shorter) unsigned decimal integer unsigned hexadecimal unsigned octal integer prefix used with %d, %u, %x %o to specify long integer e.g. %ld %u %x %o l CSC141 Introduction to Computer Programming
  • 12. Field width Specifiers Format specifier: %(-)w.nf • f is a format specifier used for float data type e.g %f • .n means number of digits after decimal point e.g %.2f 27.25 & %.3f means 27.250 • The digit (w) preceding the decimal point specifies the amount of space that number will use to get displayed: e.g printf (“Age is % 2d” 33); printf (“Age is % 4d” 33); printf (“Age is % 6d” 33); CSC141 Introduction to Computer Programming 33 - - 33 - - - - 33
  • 13. Field width Specifiers void main (void) { printf(“8.1f %8.1f %8.1fn”, 3.0, 12.5, 523.3); printf(“8.1f %8.1f %8.1fn”, 300.0, 1200.5, 5300.3); } 3.0 300.0 12.5 1200.5 „523.3 5300.3 CSC141 Introduction to Computer Programming
  • 14. Field width Specifiers void main (void) { printf(“-8.1f %-8.1f %-8.1fn”, 3.0, 12.5, 523.3); printf(“-8.1f %-8.1f %-8.1fn”, 300.0, 1200.5, 5300.3); } 3.0‟ „12.5 „ „ 523.3 300.0 „ „ 1200.5 „ „5300.3 CSC141 Introduction to Computer Programming
  • 15. Escape Sequence • n prints carriage return and linefeed combination • t tab • b backspace • r carriage return • ‟ single quote • “ double quote • backslash CSC141 Introduction to Computer Programming
  • 16. Printing Strings using printf () function void main (void) { printf(“%s is %d years old”,”Ahmed”,22); } CSC141 Introduction to Computer Programming
  • 17. Printing characters using printf () function void main (void) { printf(“%c is pronounced as %s”, ’j’, ”jay”); } • Single quotes are for character whereas the %c is format specifier • Double quotes are for strings whereas the %s is format specifier CSC141 Introduction to Computer Programming
  • 18. Scanf() function void main (void) { int userAge; printf(“Enter the the age: ”); scanf(“ %d”, &userAge); } • scanf() reads in data and stores it in one or more variables. • The first argument (the control string), contains a series of placeholders • The remaining arguments to scanf() is a variable name proceed with & sign called address operator CSC141 Introduction to Computer Programming
  • 19. scanf() can read data into multiple variables int a, b, float c; char d; scanf(“ %d %d %f %c” , &a, &b, &c, &d); • Input is stored in these variables. Each variable name is preceded by & , &a means “the memory address associated with variable a” • It uses any whitespace (space, newline or tab) character to delimit inputs in case of multiple inputs • Format specifiers are like the ones that printf() uses e.g. %d = int, %f = float, %c = char, etc. CSC141 Introduction to Computer Programming
  • 20. Comments • /* --------------------*/ multiline comments • // single line comments CSC141 Introduction to Computer Programming
  • 21. Loops • Computer has the ability to perform set of instructions repeatedly • This repetitive operation is performed using loops • It involves repeating some portion of the program either a specified number of times or until a particular condition is being satisfied CSC141 Introduction to Computer Programming
  • 22. Loops • There are three types of loops in C – For loop – While loop – Do while loop • For loop is the most popular looping instruction used by the programmers CSC141 Introduction to Computer Programming
  • 23. For loop • It specifies three things about a loop in one line – Setting a loop counter to an initial value – Testing the loop counter to determine whether its value has reached the number of desired repetitions – Increasing the value of loop counter each time the program segment within the loop has been executed CSC141 Introduction to Computer Programming
  • 24. Flowchart of the for loop CSC141 Introduction to Computer Programming
  • 25. For loop … continued The general form of for loop is: for (initialize counter; test counter; increment/decrement counter) { Do this; And this; And this; } CSC141 Introduction to Computer Programming
  • 26. Example: Print an integer number from -4 to 0 CSC141 Introduction to Computer Programming
  • 27. Sample Program void main() { int j; for(j = -4; j <= 0 ; j = j + 1) printf("%dn", j); } CSC141 Introduction to Computer Programming
  • 28. print numbers from 1 to 10 void main( ) { int i ; for ( i = 1 ; i <= 10 ; i = i + 1 ) printf ( "%dn", i ) ; } • Instead of i = i + 1, the statements i++ or i += 1 can also be used. CSC141 Introduction to Computer Programming
  • 29. Print numbers from 1 to 10 void main( ) { int i ; for ( i = 1 ; i <= 10 ; ) { printf ( "%dn", i ) ; i=i+1; } } • Here, the increment is done within the body of the for loop and not in the for statement. Note that despite of this the semicolon after the condition is necessary. CSC141 Introduction to Computer Programming
  • 30. print numbers from 1 to 10 void main( ) { int i = 1 ; for ( ; i <= 10 ; i = i + 1 ) printf ( "%dn", i ) ; } • Here the initialisation is done in the declaration statement itself, but still the semicolon before the condition is necessary. CSC141 Introduction to Computer Programming
  • 31. print numbers from 1 to 10 void main( ) { int i = 1 ; for ( ; i <= 10 ; ) { printf ( "%dn", i ) ; i=i+1; } } • Here, neither the initialisation, nor the incrementation is done in the for statement, but still the two semicolons are necessary. CSC141 Introduction to Computer Programming
  • 32. print numbers from 1 to 10 void main( ) { int i ; for ( i = 0 ; i++ < 10 ; ) printf ( "%dn", i ) ; } • Here, the comparison as well as the incrementation is done through the same statement, i++ < 10. Since the ++ operator comes after i firstly comparison is done, followed by increment. Note that it is necessary to initialize i to 0. CSC141 Introduction to Computer Programming
  • 33. print numbers from 1 to 10 void main( ) { int i ; for ( i = 0 ; ++i <= 10 ; ) printf ( "%dn", i ) ; } • Here, both, the comparison and the incrementation is done through the same statement, ++i <= 10. Since ++ precedes i firstly incrementation is done, followed by comparison. Note that it is necessary to initialize i to 0. CSC141 Introduction to Computer Programming
  • 34. Nesting of Loops r = 1 c = 1 sum = 2 r = 1 c = 2 sum = 3 r = 2 c = 1 sum = 3 r = 2 c = 2 sum = 4 r = 3 c = 1 sum = 4 r = 3 c = 2 sum = 5 void CSC141 Introduction to Computer Programming
  • 35. Multiple Initialisations in the for Loop • The initialisation expression of the for loop can contain more than one statement separated by a comma. For example, • for ( i = 1, j = 2 ; j <= 10 ; j++ ) • Multiple statements can also be used in the incrementation expression of for loop; i.e., you can increment (or decrement) two or more variables at the same time. • However, only one expression is allowed in the test expression. This expression may contain several conditions linked together using logical operators. CSC141 Introduction to Computer Programming
  • 36. Solution to the last Practice Quiz Question -1: Solve the following expressions a). 3-8/4 b). 3 * 4 + 18 / 2 void main( ) { printf ( “The result of the expression is %d n", 3 - 8 / 4) ; printf ( “The result of the expression is %d n", (3 * 4 + 18 / 2)); } CSC141 Introduction to Computer Programming
  • 37. Solution to the last Practice Quiz • Question - 2: If a=2, b=4, c=6, d=8, then what will be the result of the following expression? • x = a * b + c * d / 4; void main( ) { int a =2, b=4, c=6, d=8; printf ( “The result of the expression is %d n", x = a * b + c * d / 4) ; } CSC141 Introduction to Computer Programming

Editor's Notes

  1. Removeincrement and see if itworksgetscompiledgetsexecutedAns: infinite display of 1
  2. Condition works or notAns: yesPrintednumbersupto 9 or 10Ans: upto 10