SlideShare a Scribd company logo
Computing Fundamentals
Dr. Muhammad Yousaf Hamza
#include <stdio.h>
int main( )
{
int x, y, z;
x = 5;
y = 7;
z = x + y;
printf(“%d", z);
getchar();
return 0;
}
//Our First C Program
Dr. Muhammad Yousaf Hamza
scanf()
Dr. Muhammad Yousaf Hamza
Example
#include <stdio.h>
int main ()
{
int num, result_square;
printf ("Enter an integer value please: ");
scanf ( "%d", &num);
result_square = num*num;
printf ("Square of your entered number is %dn",
result_square);
getchar();
return 0;
}
Dr. Muhammad Yousaf Hamza
Reading Numeric Data with scanf
• Reading input from keyboard
• scanf can be used like printf but to read instead of write.
• The scanf function is the input equivalent of printf
– A C library function in the <stdio.h> library
– Takes a format string and parameters, much like printf
– The format string specifiers are nearly the same as those used in
printf
• Examples:
scanf ("%d", &x); /* reads a decimal integer */
• The ampersand (&) is used to get the “address” of the
variable (Later)
– If we use scanf("%d",x) instead, the value of x is passed. As a
result, scanf will not know where to put the number it reads.
Dr. Muhammad Yousaf Hamza
Reading Numeric Data with scanf
• Reading more than one variable at a time:
– For example:
int n1, n2, n3;
scanf("%d%d%d",&n1,&n2,&n3);
– Use white spaces to separate numbers when input.
5 10 22
• In the format string:
– You can use other characters to separate the numbers
int no_students, no_chairs;
scanf(“%d%d", &no_students, &no_chairs);
Dr. Muhammad Yousaf Hamza
#include <stdio.h>
int main( void )
{
int value1, value2, sum, product ;
printf(“Enter two integer values: ”) ;
scanf(“%d %d”, &value1, &value2) ;
sum = value1 + value2 ;
product = value1 * value2 ;
printf(“Sum is = %d nnProduct = %dn”, sum, product) ;
getchar();
return 0 ;
}
Example
Dr. Muhammad Yousaf Hamza
The scanf statement
int number, check;
scanf ("%d",&number);
check= number;
//Correct
int number, check;
check= scanf ("%d",&number);
/*The program may run without error.
However, on printing the value of check,
it would not be same as number */
Dr. Muhammad Yousaf Hamza
#include <stdio.h>
int main(void)
{
int x, y, z;
x = scanf("%d %d", &y, &z);
printf("%d", x);
getchar();
return 0;
}
Ans: 2
// x will tell how many values scanned.
Dr. Muhammad Yousaf Hamza
Expressions and Operators
Dr. Muhammad Yousaf Hamza
Expressions and Operators
• In the most general sense, a statement is a part of your
program that can be executed.
• An expression is a statement.
• Examples:
x = 4;
x = x + 1;
printf("%d",x);
• The expressions are formed by data and operators
• An expression in C usually has a value
– except for the function call that returns void. (later)
Dr. Muhammad Yousaf Hamza
Arithmetic Operators
Operator Symbol Action
Addition + Adds operands x + y
Subtraction - Subtracts from first x - y
Negation - Negates operand -x
Multiplication * Multiplies operands x * y
Division / Divides first by second x / y
(integer quotient)
Modulus % Remainder of divide op x % y
• (x % y) gives the remainder when x is divided by y
• remainder= x%y; (ints only)
Dr. Muhammad Yousaf Hamza
The Use of Modulus
Dr. Muhammad Yousaf Hamza
• int x;
• // Various cases
• x = 6%2 // x =
• x = 7%2 // x =
• Suppose num is any even number then
• x = num%2 // x =
• Suppose num is any odd number then
• x = num%2 // x =
• // Some other examples
• x = 63%10 // x =
• x = 100 %7 // x =
Dr. Muhammad Yousaf Hamza
• int x;
• // Various cases
• x = 6%2 // x = 0
• x = 7%2 // x = 1
• Suppose num is any even number then
• x = num%2 // x = 0
• Suppose num is any odd number then
• x = num%2 // x = 1
• // Some other examples
• x = 63%10 // x = 3
• x = 100 %7 // x = 2
The Use of Modulus
Dr. Muhammad Yousaf Hamza
include<stdio.h>
int main()
{
int num=12;
int digit1,digit2;
digit1=num%10; // digit1 = 2
digit2=num/10; // digit2 = 1
printf(“First digit is = %d ”,digit1);
printf(“nSecond digit is =%d”,digit2);
getchar();
return 0;
}
The Use of Modulus
Assignment Operator
• The assignment operator =
x = 3
– It assigns the value of the right hand side (rhs) to
the left hand side (lhs).
– The value is the value of the rhs.
• For example:
x = ( y = 3 ) +1; /* y is assigned 3 */
/* the value of (y=3) is 3 */
/* x is assigned 4 */
Dr. Muhammad Yousaf Hamza
Compound Assignment Operator
• Often we use “update” forms of operators
x=x+1, x=x*2, ...
• C offers a short form for this:
Operator Equivalent to:
x + = y x = x + y
x *= y x = x * y
y -= z + 1 y = y - (z + 1)
a /= b a = a / b
x += y / 8 x = x + (y / 8)
y %= 3 y = y % 3
Dr. Muhammad Yousaf Hamza
// demonstrates arithmetic assignement operators
#include <stdio.h>
int main()
{
int ans = 27;
ans += 10; //same as: ans = ans + 10;
printf(" %d, ",ans);
ans -= 7; //same as: ans = ans - 7;
printf(" %d, ",ans);
ans *= 2; //same as: ans = ans * 2;
printf(" %d, ",ans);
ans /= 3; //same as: ans = ans / 3;
printf(" %d, ",ans);
ans %= 3; //same as: ans = ans % 3;
printf(" %d, n",ans);
getchar(); return 0;
} Dr. Muhammad Yousaf Hamza
Increment and Decrement Operators
Dr. Muhammad Yousaf Hamza
Increment and Decrement
• Increment and decrement operators.
– Increment: ++ It increases the value by 1
i = 7; // i is a variable name
++i; // (i = i + 1 or i + = 1). It increases the value of i by 1
i = 7;
i++;
–Decrement: -- (similar to ++) It decreases the value by 1
i = 8;
--i; // --i is the same as : (i = i – 1 or i - = 1).
i = 8;
i--;
Dr. Muhammad Yousaf Hamza
• ++i means increment i then use it
• i++ means use i then increment it
int i= 6;
printf ("%dn",i++); /* Prints 6 sets i to 7 */
int i= 6;
printf ("%dn",++i); /* prints 7 and sets i to 7 */
Note this important difference
All of the above also applies to --.
Increment and Decrement
Pre-fix and Post-fix
Dr. Muhammad Yousaf Hamza
#include<stdio.h>
int main()
{
int a = 7, b = 20, c, d;
c = a++;
printf("%d", c);
printf("n%d",a);
d = ++b;
printf("n%d", d);
printf("n%d",b);
getchar();
return 0;
}
Dr. Muhammad Yousaf Hamza
Increment and Decrement
Pre-fix and Post-fix
Data Types
Dr. Muhammad Yousaf Hamza
Data Types in C
• We must declare the type of every variable we
use in C.
• Every variable has a type (e.g. int) and a name
(e.g. no_students), i.e. int no_students
• Basic data types in C
– char: a single byte, capable of holding one
character
– int: an integer of fixed length, typically reflecting
the natural size of integers on the host
machine (i.e., 32 or 64 bits)
– float: single-precision floating point
– double: double precision floating point
Dr. Muhammad Yousaf Hamza
• Floating-point variables represent numbers
with a decimal place—like 9.3, 3.1415927,
0.0000625, and –10.2.
• They have both an integer part to the left of
the decimal point, and a fractional part to the
right.
• Floating-point variables represent what
mathematicians call real numbers.
Data Types in C
Dr. Muhammad Yousaf Hamza
Conversion Specifiers
#include <stdio.h>
int main( )
{
int x = 5;
printf(“n x is %d", x); // %d is format specifier
getchar();
return 0;
}
Format specifiers are used in printf function for printing
numbers and characters. A format specifier acts like a place
holder, it reserves a place in a string for numbers and
characters.
Dr. Muhammad Yousaf Hamza
Conversion Specifiers
Specifier Meaning
%c Single character
%d Decimal integer
%f Decimal floating point number
%lf Decimal floating point number (double)
There must be one conversion specifier for each argument
being printed out.
• Ensure you use the correct specifier for the type of data you are
printing.
• Format specifiers are used in printf function for printing
numbers and characters. A format specifier acts like a place
holder, it reserves a place in a string for numbers and
characters.
Dr. Muhammad Yousaf Hamza
Variable Declaration
• Generic Form
typename varname1, varname2, ...;
• Examples:
int count, x, y, z;
float a, b, m;
double percent, total, average;
Dr. Muhammad Yousaf Hamza
Variable Declaration
Initialization
• ALWAYS initialize a variable before using it
– Failure to do so in C is asking for trouble
– The value of an uninitialized variables is undefined in
the C standards
• Examples:
int count; /* Set aside storage space for count */
count = 0; /* Store 0 in count */
• This can be done at definition:
int count = 0;
double percent = 10.0, rate = 0.56;
Dr. Muhammad Yousaf Hamza
Example
#include <stdio.h>
int main ()
{
double radius, area;
printf ("Enter the value of radius ");
scanf ( "%lf", &radius);
area = 3.14159 * radius * radius;
printf ("nArea = %lfnn", area);
getchar();
return 0;
} Dr. Muhammad Yousaf Hamza
Reading Numeric Data with scanf
– For example:
int n1, n2,x;
float f, rate;
scanf ("%d",&x); /*reads a decimal integer */
scanf ("%f",&rate); /*reads a floating point value*/
scanf("%d%d%f",&n1,&n2,&f);
• Use white spaces to separate numbers when input.
5 10 20.3 then press Enter Key
OR, you can also enter these values one by one by
pressing enter Key. Both are OK.
Dr. Muhammad Yousaf Hamza
Type Conversion
Dr. Muhammad Yousaf Hamza
#include<stdio.h>
int main()
{
int a= 23, b= 4;
float c;
c = a/b; // 5.00
printf("n%f",c); // 5.00
getchar();
return 0;
}
Actually 23/4 = 5.75, but here output is 5.00 In order to have
correct answer (with decimal value), we need type casting.
Type Conversion
• C allows for conversions between the basic types, implicitly or
explicitly. It is also called casting.
• A cast is a way of telling one variable type to temporarily look
like another.
• Explicit conversion uses the cast operator.
• Example :
int x=10;
float y, z=3.14;
y=(float) x; /* y=10.0 */
x=(int) z; /* x=3 */
x=(int) (-z); /* x=-3 */
Dr. Muhammad Yousaf Hamza
By using (type) in front of a variable we tell
the variable to act like another type of variable.
We can cast between any type usually. However,
the only reason to cast is to stop
ints being rounded by division.
Dr. Muhammad Yousaf Hamza
Casting of Variables
#include<stdio.h>
int main()
{
int a= 23, b= 4;
float c;
c = a/b;
printf("n%f",c); // 5.00
c= (float)a/(float)b;
// c = (float)a/b; or c= a/(float)b; are also same.
printf("n%f",c); // 5.75
getchar();
return 0;
}
Dr. Muhammad Yousaf Hamza
Casting of Variables
Cast ints a and b to be float
More types: const
const means a variable which doesn't vary – useful for
physical constants or things like pi or e
– You can also declare variables as being constants
– Use the const qualifier:
const double pi=3.1415926;
const long double e = 2.718281828;
const int maxlength=2356;
const int val=(3*7+6)*5;
•(scientific) notation
(mantissa/exponent)
onst double n = 6.18e2;
• 6.18e2 = 6.18x10^2
Dr. Muhammad Yousaf Hamza
#include<stdio.h>
int main()
{
const float n = 6.18e2;
printf("%f",n); 618.00
getchar();
return 0;
}
Constants
– Constants are useful for a number of reasons
• Tells the reader of the code that a value does not
change
• Tells the compiler that a value does not change
– The compiler can potentially compile faster code
• Use constants where appropriate
Dr. Muhammad Yousaf Hamza
More types: const
#include<stdio.h>
int main()
{
const double pi=3.1415926;
float radius = 4.5,circum ;
circum = 2*pi*radius;
printf("n%f", circum); // 28.274334
getchar();
return 0;
}
Dr. Muhammad Yousaf Hamza
More types: const
Dr. Muhammad Yousaf Hamza
#include<stdio.h>
int main()
{
const double pi=3.1415926;
float radius = 4.5,circum ;
circum = 2*pi*radius;
printf("n%f", circum);
radius = 7.3;
pi = 2.9; // Error
circum = 2*pi*radius;
printf("n%f", circum);
getchar();
return 0;
}
More types: const
Decisions
Dr. Muhammad Yousaf Hamza
To decide even/odd
#include<stdio.h>
int main()
{
int num;
printf("Please enter an integer number:n");
scanf("%d",&num);
if(num%2==0)
printf("nThe number %d is an even number",num);
if(num%2!=0)
printf("nThe number %d is an odd number",num);
getchar(); getchar();
return 0;
}
Dr. Muhammad Yousaf Hamza
To decide even/odd
#include<stdio.h>
int main()
{
int num;
printf("Please enter an integer number:n");
scanf("%d",&num);
if(num%2==0) // Here only one check.
printf("nThe number %d is an even number",num);
else
printf("nThe number %d is an odd number",num);
getchar(); getchar();
return 0;
}
Dr. Muhammad Yousaf Hamza

More Related Content

What's hot (20)

DOC
Slide07 repetitions
altwirqi
 
PPTX
C Programming Language Part 11
Rumman Ansari
 
PPT
Lecture 6- Intorduction to C Programming
Md. Imran Hossain Showrov
 
PDF
8 arrays and pointers
MomenMostafa
 
PPTX
C if else
Ritwik Das
 
PDF
7 functions
MomenMostafa
 
PPTX
Conditional Statement in C Language
Shaina Arora
 
DOCX
Core programming in c
Rahul Pandit
 
PPT
Lecture 10 - Control Structures 2
Md. Imran Hossain Showrov
 
PDF
Chapter 5 exercises Balagurusamy Programming ANSI in c
BUBT
 
PDF
Chapter 5 Balagurusamy Programming ANSI in c
BUBT
 
PPT
Structure in programming in c or c++ or c# or java
Samsil Arefin
 
PDF
[ITP - Lecture 15] Arrays & its Types
Muhammad Hammad Waseem
 
PPTX
C Programming Language Part 8
Rumman Ansari
 
PPTX
C Programming Language Part 9
Rumman Ansari
 
PDF
Chapter 3 : Balagurusamy Programming ANSI in C
BUBT
 
PPT
Lecture 8- Data Input and Output
Md. Imran Hossain Showrov
 
PPTX
Input output functions
hyderali123
 
PPT
Lecture 7- Operators and Expressions
Md. Imran Hossain Showrov
 
PDF
Chapter 4 : Balagurusamy Programming ANSI in C
BUBT
 
Slide07 repetitions
altwirqi
 
C Programming Language Part 11
Rumman Ansari
 
Lecture 6- Intorduction to C Programming
Md. Imran Hossain Showrov
 
8 arrays and pointers
MomenMostafa
 
C if else
Ritwik Das
 
7 functions
MomenMostafa
 
Conditional Statement in C Language
Shaina Arora
 
Core programming in c
Rahul Pandit
 
Lecture 10 - Control Structures 2
Md. Imran Hossain Showrov
 
Chapter 5 exercises Balagurusamy Programming ANSI in c
BUBT
 
Chapter 5 Balagurusamy Programming ANSI in c
BUBT
 
Structure in programming in c or c++ or c# or java
Samsil Arefin
 
[ITP - Lecture 15] Arrays & its Types
Muhammad Hammad Waseem
 
C Programming Language Part 8
Rumman Ansari
 
C Programming Language Part 9
Rumman Ansari
 
Chapter 3 : Balagurusamy Programming ANSI in C
BUBT
 
Lecture 8- Data Input and Output
Md. Imran Hossain Showrov
 
Input output functions
hyderali123
 
Lecture 7- Operators and Expressions
Md. Imran Hossain Showrov
 
Chapter 4 : Balagurusamy Programming ANSI in C
BUBT
 

Similar to C Language Lecture 3 (20)

DOCX
C Programming
Raj vardhan
 
PPTX
presentation_c_basics_1589366177_381682.pptx
KrishanPalSingh39
 
PDF
Module 2_PPT_P1 POP Notes module 2 fdfd.pdf
anilcsbs
 
PPTX
Each n Every topic of C Programming.pptx
snnbarot
 
PPTX
presentation_data_types_and_operators_1513499834_241350.pptx
KrishanPalSingh39
 
PPT
c_tutorial_2.ppt
gitesh_nagar
 
PPTX
Basic of C Programming | 2022 Updated | By Shamsul H. Ansari
G. H. Raisoni Academy of Engineering & Technology, Nagpur
 
PDF
2 EPT 162 Lecture 2
Don Dooley
 
PPTX
the refernce of programming C notes ppt.pptx
AnkitaVerma776806
 
PPTX
Chapter 2: Elementary Programming
Eric Chou
 
PPT
Introduction to c
sunila tharagaturi
 
PPT
C language Unit 2 Slides, UPTU C language
Anurag University Hyderabad
 
PPTX
Fundamentals of computers - C Programming
MSridhar18
 
PPTX
Variable declaration
Mark Leo Tarectecan
 
PPT
Unit i intro-operators
HINAPARVEENAlXC
 
PPTX
introduction to c programming and C History.pptx
ManojKhadilkar1
 
PPT
Token and operators
Samsil Arefin
 
PPT
c-programming
Zulhazmi Harith
 
C Programming
Raj vardhan
 
presentation_c_basics_1589366177_381682.pptx
KrishanPalSingh39
 
Module 2_PPT_P1 POP Notes module 2 fdfd.pdf
anilcsbs
 
Each n Every topic of C Programming.pptx
snnbarot
 
presentation_data_types_and_operators_1513499834_241350.pptx
KrishanPalSingh39
 
c_tutorial_2.ppt
gitesh_nagar
 
Basic of C Programming | 2022 Updated | By Shamsul H. Ansari
G. H. Raisoni Academy of Engineering & Technology, Nagpur
 
2 EPT 162 Lecture 2
Don Dooley
 
the refernce of programming C notes ppt.pptx
AnkitaVerma776806
 
Chapter 2: Elementary Programming
Eric Chou
 
Introduction to c
sunila tharagaturi
 
C language Unit 2 Slides, UPTU C language
Anurag University Hyderabad
 
Fundamentals of computers - C Programming
MSridhar18
 
Variable declaration
Mark Leo Tarectecan
 
Unit i intro-operators
HINAPARVEENAlXC
 
introduction to c programming and C History.pptx
ManojKhadilkar1
 
Token and operators
Samsil Arefin
 
c-programming
Zulhazmi Harith
 
Ad

More from Shahzaib Ajmal (20)

PDF
C Language Lecture 22
Shahzaib Ajmal
 
PDF
C Language Lecture 21
Shahzaib Ajmal
 
PDF
C Language Lecture 20
Shahzaib Ajmal
 
PDF
C Language Lecture 18
Shahzaib Ajmal
 
PDF
C Language Lecture 17
Shahzaib Ajmal
 
PDF
C Language Lecture 16
Shahzaib Ajmal
 
PDF
C Language Lecture 15
Shahzaib Ajmal
 
PDF
C Language Lecture 14
Shahzaib Ajmal
 
PDF
C Language Lecture 13
Shahzaib Ajmal
 
PDF
C Language Lecture 12
Shahzaib Ajmal
 
PDF
C Language Lecture 11
Shahzaib Ajmal
 
PDF
C Language Lecture 10
Shahzaib Ajmal
 
PDF
C Language Lecture 9
Shahzaib Ajmal
 
PDF
C Language Lecture 8
Shahzaib Ajmal
 
PDF
C Language Lecture 7
Shahzaib Ajmal
 
PDF
C Language Lecture 6
Shahzaib Ajmal
 
PDF
C Language Lecture 5
Shahzaib Ajmal
 
PDF
C Language Lecture 4
Shahzaib Ajmal
 
PDF
C Language Lecture 2
Shahzaib Ajmal
 
PDF
C Language Lecture 1
Shahzaib Ajmal
 
C Language Lecture 22
Shahzaib Ajmal
 
C Language Lecture 21
Shahzaib Ajmal
 
C Language Lecture 20
Shahzaib Ajmal
 
C Language Lecture 18
Shahzaib Ajmal
 
C Language Lecture 17
Shahzaib Ajmal
 
C Language Lecture 16
Shahzaib Ajmal
 
C Language Lecture 15
Shahzaib Ajmal
 
C Language Lecture 14
Shahzaib Ajmal
 
C Language Lecture 13
Shahzaib Ajmal
 
C Language Lecture 12
Shahzaib Ajmal
 
C Language Lecture 11
Shahzaib Ajmal
 
C Language Lecture 10
Shahzaib Ajmal
 
C Language Lecture 9
Shahzaib Ajmal
 
C Language Lecture 8
Shahzaib Ajmal
 
C Language Lecture 7
Shahzaib Ajmal
 
C Language Lecture 6
Shahzaib Ajmal
 
C Language Lecture 5
Shahzaib Ajmal
 
C Language Lecture 4
Shahzaib Ajmal
 
C Language Lecture 2
Shahzaib Ajmal
 
C Language Lecture 1
Shahzaib Ajmal
 
Ad

Recently uploaded (20)

PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
PDF
1, 2, 3… E MAIS UM CICLO CHEGA AO FIM!.pdf
Colégio Santa Teresinha
 
PDF
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPSX
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PDF
Zoology (Animal Physiology) practical Manual
raviralanaresh2
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PDF
IMP NAAC REFORMS 2024 - 10 Attributes.pdf
BHARTIWADEKAR
 
PPTX
Gall bladder, Small intestine and Large intestine.pptx
rekhapositivity
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
DOCX
A summary of SPRING SILKWORMS by Mao Dun.docx
maryjosie1
 
PPTX
How to Manage Promotions in Odoo 18 Sales
Celine George
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PPTX
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PPTX
Quarter1-English3-W4-Identifying Elements of the Story
FLORRACHELSANTOS
 
PDF
Federal dollars withheld by district, charter, grant recipient
Mebane Rash
 
PPTX
How to Configure Access Rights of Manufacturing Orders in Odoo 18 Manufacturing
Celine George
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
1, 2, 3… E MAIS UM CICLO CHEGA AO FIM!.pdf
Colégio Santa Teresinha
 
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
Dimensions of Societal Planning in Commonism
StefanMz
 
Zoology (Animal Physiology) practical Manual
raviralanaresh2
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
IMP NAAC REFORMS 2024 - 10 Attributes.pdf
BHARTIWADEKAR
 
Gall bladder, Small intestine and Large intestine.pptx
rekhapositivity
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
A summary of SPRING SILKWORMS by Mao Dun.docx
maryjosie1
 
How to Manage Promotions in Odoo 18 Sales
Celine George
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
Quarter1-English3-W4-Identifying Elements of the Story
FLORRACHELSANTOS
 
Federal dollars withheld by district, charter, grant recipient
Mebane Rash
 
How to Configure Access Rights of Manufacturing Orders in Odoo 18 Manufacturing
Celine George
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 

C Language Lecture 3

  • 2. #include <stdio.h> int main( ) { int x, y, z; x = 5; y = 7; z = x + y; printf(“%d", z); getchar(); return 0; } //Our First C Program Dr. Muhammad Yousaf Hamza
  • 4. Example #include <stdio.h> int main () { int num, result_square; printf ("Enter an integer value please: "); scanf ( "%d", &num); result_square = num*num; printf ("Square of your entered number is %dn", result_square); getchar(); return 0; } Dr. Muhammad Yousaf Hamza
  • 5. Reading Numeric Data with scanf • Reading input from keyboard • scanf can be used like printf but to read instead of write. • The scanf function is the input equivalent of printf – A C library function in the <stdio.h> library – Takes a format string and parameters, much like printf – The format string specifiers are nearly the same as those used in printf • Examples: scanf ("%d", &x); /* reads a decimal integer */ • The ampersand (&) is used to get the “address” of the variable (Later) – If we use scanf("%d",x) instead, the value of x is passed. As a result, scanf will not know where to put the number it reads. Dr. Muhammad Yousaf Hamza
  • 6. Reading Numeric Data with scanf • Reading more than one variable at a time: – For example: int n1, n2, n3; scanf("%d%d%d",&n1,&n2,&n3); – Use white spaces to separate numbers when input. 5 10 22 • In the format string: – You can use other characters to separate the numbers int no_students, no_chairs; scanf(“%d%d", &no_students, &no_chairs); Dr. Muhammad Yousaf Hamza
  • 7. #include <stdio.h> int main( void ) { int value1, value2, sum, product ; printf(“Enter two integer values: ”) ; scanf(“%d %d”, &value1, &value2) ; sum = value1 + value2 ; product = value1 * value2 ; printf(“Sum is = %d nnProduct = %dn”, sum, product) ; getchar(); return 0 ; } Example Dr. Muhammad Yousaf Hamza
  • 8. The scanf statement int number, check; scanf ("%d",&number); check= number; //Correct int number, check; check= scanf ("%d",&number); /*The program may run without error. However, on printing the value of check, it would not be same as number */ Dr. Muhammad Yousaf Hamza
  • 9. #include <stdio.h> int main(void) { int x, y, z; x = scanf("%d %d", &y, &z); printf("%d", x); getchar(); return 0; } Ans: 2 // x will tell how many values scanned. Dr. Muhammad Yousaf Hamza
  • 10. Expressions and Operators Dr. Muhammad Yousaf Hamza
  • 11. Expressions and Operators • In the most general sense, a statement is a part of your program that can be executed. • An expression is a statement. • Examples: x = 4; x = x + 1; printf("%d",x); • The expressions are formed by data and operators • An expression in C usually has a value – except for the function call that returns void. (later) Dr. Muhammad Yousaf Hamza
  • 12. Arithmetic Operators Operator Symbol Action Addition + Adds operands x + y Subtraction - Subtracts from first x - y Negation - Negates operand -x Multiplication * Multiplies operands x * y Division / Divides first by second x / y (integer quotient) Modulus % Remainder of divide op x % y • (x % y) gives the remainder when x is divided by y • remainder= x%y; (ints only) Dr. Muhammad Yousaf Hamza
  • 13. The Use of Modulus Dr. Muhammad Yousaf Hamza • int x; • // Various cases • x = 6%2 // x = • x = 7%2 // x = • Suppose num is any even number then • x = num%2 // x = • Suppose num is any odd number then • x = num%2 // x = • // Some other examples • x = 63%10 // x = • x = 100 %7 // x =
  • 14. Dr. Muhammad Yousaf Hamza • int x; • // Various cases • x = 6%2 // x = 0 • x = 7%2 // x = 1 • Suppose num is any even number then • x = num%2 // x = 0 • Suppose num is any odd number then • x = num%2 // x = 1 • // Some other examples • x = 63%10 // x = 3 • x = 100 %7 // x = 2 The Use of Modulus
  • 15. Dr. Muhammad Yousaf Hamza include<stdio.h> int main() { int num=12; int digit1,digit2; digit1=num%10; // digit1 = 2 digit2=num/10; // digit2 = 1 printf(“First digit is = %d ”,digit1); printf(“nSecond digit is =%d”,digit2); getchar(); return 0; } The Use of Modulus
  • 16. Assignment Operator • The assignment operator = x = 3 – It assigns the value of the right hand side (rhs) to the left hand side (lhs). – The value is the value of the rhs. • For example: x = ( y = 3 ) +1; /* y is assigned 3 */ /* the value of (y=3) is 3 */ /* x is assigned 4 */ Dr. Muhammad Yousaf Hamza
  • 17. Compound Assignment Operator • Often we use “update” forms of operators x=x+1, x=x*2, ... • C offers a short form for this: Operator Equivalent to: x + = y x = x + y x *= y x = x * y y -= z + 1 y = y - (z + 1) a /= b a = a / b x += y / 8 x = x + (y / 8) y %= 3 y = y % 3 Dr. Muhammad Yousaf Hamza
  • 18. // demonstrates arithmetic assignement operators #include <stdio.h> int main() { int ans = 27; ans += 10; //same as: ans = ans + 10; printf(" %d, ",ans); ans -= 7; //same as: ans = ans - 7; printf(" %d, ",ans); ans *= 2; //same as: ans = ans * 2; printf(" %d, ",ans); ans /= 3; //same as: ans = ans / 3; printf(" %d, ",ans); ans %= 3; //same as: ans = ans % 3; printf(" %d, n",ans); getchar(); return 0; } Dr. Muhammad Yousaf Hamza
  • 19. Increment and Decrement Operators Dr. Muhammad Yousaf Hamza
  • 20. Increment and Decrement • Increment and decrement operators. – Increment: ++ It increases the value by 1 i = 7; // i is a variable name ++i; // (i = i + 1 or i + = 1). It increases the value of i by 1 i = 7; i++; –Decrement: -- (similar to ++) It decreases the value by 1 i = 8; --i; // --i is the same as : (i = i – 1 or i - = 1). i = 8; i--; Dr. Muhammad Yousaf Hamza
  • 21. • ++i means increment i then use it • i++ means use i then increment it int i= 6; printf ("%dn",i++); /* Prints 6 sets i to 7 */ int i= 6; printf ("%dn",++i); /* prints 7 and sets i to 7 */ Note this important difference All of the above also applies to --. Increment and Decrement Pre-fix and Post-fix Dr. Muhammad Yousaf Hamza
  • 22. #include<stdio.h> int main() { int a = 7, b = 20, c, d; c = a++; printf("%d", c); printf("n%d",a); d = ++b; printf("n%d", d); printf("n%d",b); getchar(); return 0; } Dr. Muhammad Yousaf Hamza Increment and Decrement Pre-fix and Post-fix
  • 23. Data Types Dr. Muhammad Yousaf Hamza
  • 24. Data Types in C • We must declare the type of every variable we use in C. • Every variable has a type (e.g. int) and a name (e.g. no_students), i.e. int no_students • Basic data types in C – char: a single byte, capable of holding one character – int: an integer of fixed length, typically reflecting the natural size of integers on the host machine (i.e., 32 or 64 bits) – float: single-precision floating point – double: double precision floating point Dr. Muhammad Yousaf Hamza
  • 25. • Floating-point variables represent numbers with a decimal place—like 9.3, 3.1415927, 0.0000625, and –10.2. • They have both an integer part to the left of the decimal point, and a fractional part to the right. • Floating-point variables represent what mathematicians call real numbers. Data Types in C Dr. Muhammad Yousaf Hamza
  • 26. Conversion Specifiers #include <stdio.h> int main( ) { int x = 5; printf(“n x is %d", x); // %d is format specifier getchar(); return 0; } Format specifiers are used in printf function for printing numbers and characters. A format specifier acts like a place holder, it reserves a place in a string for numbers and characters. Dr. Muhammad Yousaf Hamza
  • 27. Conversion Specifiers Specifier Meaning %c Single character %d Decimal integer %f Decimal floating point number %lf Decimal floating point number (double) There must be one conversion specifier for each argument being printed out. • Ensure you use the correct specifier for the type of data you are printing. • Format specifiers are used in printf function for printing numbers and characters. A format specifier acts like a place holder, it reserves a place in a string for numbers and characters. Dr. Muhammad Yousaf Hamza
  • 28. Variable Declaration • Generic Form typename varname1, varname2, ...; • Examples: int count, x, y, z; float a, b, m; double percent, total, average; Dr. Muhammad Yousaf Hamza
  • 29. Variable Declaration Initialization • ALWAYS initialize a variable before using it – Failure to do so in C is asking for trouble – The value of an uninitialized variables is undefined in the C standards • Examples: int count; /* Set aside storage space for count */ count = 0; /* Store 0 in count */ • This can be done at definition: int count = 0; double percent = 10.0, rate = 0.56; Dr. Muhammad Yousaf Hamza
  • 30. Example #include <stdio.h> int main () { double radius, area; printf ("Enter the value of radius "); scanf ( "%lf", &radius); area = 3.14159 * radius * radius; printf ("nArea = %lfnn", area); getchar(); return 0; } Dr. Muhammad Yousaf Hamza
  • 31. Reading Numeric Data with scanf – For example: int n1, n2,x; float f, rate; scanf ("%d",&x); /*reads a decimal integer */ scanf ("%f",&rate); /*reads a floating point value*/ scanf("%d%d%f",&n1,&n2,&f); • Use white spaces to separate numbers when input. 5 10 20.3 then press Enter Key OR, you can also enter these values one by one by pressing enter Key. Both are OK. Dr. Muhammad Yousaf Hamza
  • 32. Type Conversion Dr. Muhammad Yousaf Hamza #include<stdio.h> int main() { int a= 23, b= 4; float c; c = a/b; // 5.00 printf("n%f",c); // 5.00 getchar(); return 0; } Actually 23/4 = 5.75, but here output is 5.00 In order to have correct answer (with decimal value), we need type casting.
  • 33. Type Conversion • C allows for conversions between the basic types, implicitly or explicitly. It is also called casting. • A cast is a way of telling one variable type to temporarily look like another. • Explicit conversion uses the cast operator. • Example : int x=10; float y, z=3.14; y=(float) x; /* y=10.0 */ x=(int) z; /* x=3 */ x=(int) (-z); /* x=-3 */ Dr. Muhammad Yousaf Hamza
  • 34. By using (type) in front of a variable we tell the variable to act like another type of variable. We can cast between any type usually. However, the only reason to cast is to stop ints being rounded by division. Dr. Muhammad Yousaf Hamza Casting of Variables
  • 35. #include<stdio.h> int main() { int a= 23, b= 4; float c; c = a/b; printf("n%f",c); // 5.00 c= (float)a/(float)b; // c = (float)a/b; or c= a/(float)b; are also same. printf("n%f",c); // 5.75 getchar(); return 0; } Dr. Muhammad Yousaf Hamza Casting of Variables Cast ints a and b to be float
  • 36. More types: const const means a variable which doesn't vary – useful for physical constants or things like pi or e – You can also declare variables as being constants – Use the const qualifier: const double pi=3.1415926; const long double e = 2.718281828; const int maxlength=2356; const int val=(3*7+6)*5; •(scientific) notation (mantissa/exponent) onst double n = 6.18e2; • 6.18e2 = 6.18x10^2 Dr. Muhammad Yousaf Hamza #include<stdio.h> int main() { const float n = 6.18e2; printf("%f",n); 618.00 getchar(); return 0; }
  • 37. Constants – Constants are useful for a number of reasons • Tells the reader of the code that a value does not change • Tells the compiler that a value does not change – The compiler can potentially compile faster code • Use constants where appropriate Dr. Muhammad Yousaf Hamza More types: const
  • 38. #include<stdio.h> int main() { const double pi=3.1415926; float radius = 4.5,circum ; circum = 2*pi*radius; printf("n%f", circum); // 28.274334 getchar(); return 0; } Dr. Muhammad Yousaf Hamza More types: const
  • 39. Dr. Muhammad Yousaf Hamza #include<stdio.h> int main() { const double pi=3.1415926; float radius = 4.5,circum ; circum = 2*pi*radius; printf("n%f", circum); radius = 7.3; pi = 2.9; // Error circum = 2*pi*radius; printf("n%f", circum); getchar(); return 0; } More types: const
  • 41. To decide even/odd #include<stdio.h> int main() { int num; printf("Please enter an integer number:n"); scanf("%d",&num); if(num%2==0) printf("nThe number %d is an even number",num); if(num%2!=0) printf("nThe number %d is an odd number",num); getchar(); getchar(); return 0; } Dr. Muhammad Yousaf Hamza
  • 42. To decide even/odd #include<stdio.h> int main() { int num; printf("Please enter an integer number:n"); scanf("%d",&num); if(num%2==0) // Here only one check. printf("nThe number %d is an even number",num); else printf("nThe number %d is an odd number",num); getchar(); getchar(); return 0; } Dr. Muhammad Yousaf Hamza