SlideShare a Scribd company logo
1 of 42
Download to read offline
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

Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitionsaltwirqi
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11Rumman Ansari
 
8 arrays and pointers
8  arrays and pointers8  arrays and pointers
8 arrays and pointersMomenMostafa
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C LanguageShaina Arora
 
Core programming in c
Core programming in cCore programming in c
Core programming in cRahul Pandit
 
Chapter 5 exercises Balagurusamy Programming ANSI in c
Chapter 5 exercises Balagurusamy Programming ANSI  in cChapter 5 exercises Balagurusamy Programming ANSI  in c
Chapter 5 exercises Balagurusamy Programming ANSI in cBUBT
 
Chapter 5 Balagurusamy Programming ANSI in c
Chapter 5 Balagurusamy Programming ANSI  in cChapter 5 Balagurusamy Programming ANSI  in c
Chapter 5 Balagurusamy Programming ANSI in cBUBT
 
Structure in programming in c or c++ or c# or java
Structure in programming  in c or c++ or c# or javaStructure in programming  in c or c++ or c# or java
Structure in programming in c or c++ or c# or javaSamsil Arefin
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8Rumman Ansari
 
C Programming Language Part 9
C Programming Language Part 9C Programming Language Part 9
C Programming Language Part 9Rumman Ansari
 
Chapter 3 : Balagurusamy Programming ANSI in C
Chapter 3 : Balagurusamy Programming ANSI in C Chapter 3 : Balagurusamy Programming ANSI in C
Chapter 3 : Balagurusamy Programming ANSI in C BUBT
 
Input output functions
Input output functionsInput output functions
Input output functionshyderali123
 
Chapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in CChapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in CBUBT
 

What's hot (20)

Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitions
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11
 
Lecture 6- Intorduction to C Programming
Lecture 6- Intorduction to C ProgrammingLecture 6- Intorduction to C Programming
Lecture 6- Intorduction to C Programming
 
8 arrays and pointers
8  arrays and pointers8  arrays and pointers
8 arrays and pointers
 
C if else
C if elseC if else
C if else
 
7 functions
7  functions7  functions
7 functions
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
 
Core programming in c
Core programming in cCore programming in c
Core programming in c
 
Lecture 10 - Control Structures 2
Lecture 10 - Control Structures 2Lecture 10 - Control Structures 2
Lecture 10 - Control Structures 2
 
Chapter 5 exercises Balagurusamy Programming ANSI in c
Chapter 5 exercises Balagurusamy Programming ANSI  in cChapter 5 exercises Balagurusamy Programming ANSI  in c
Chapter 5 exercises Balagurusamy Programming ANSI in c
 
Chapter 5 Balagurusamy Programming ANSI in c
Chapter 5 Balagurusamy Programming ANSI  in cChapter 5 Balagurusamy Programming ANSI  in c
Chapter 5 Balagurusamy Programming ANSI in c
 
Structure in programming in c or c++ or c# or java
Structure in programming  in c or c++ or c# or javaStructure in programming  in c or c++ or c# or java
Structure in programming in c or c++ or c# or java
 
[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8
 
C Programming Language Part 9
C Programming Language Part 9C Programming Language Part 9
C Programming Language Part 9
 
Chapter 3 : Balagurusamy Programming ANSI in C
Chapter 3 : Balagurusamy Programming ANSI in C Chapter 3 : Balagurusamy Programming ANSI in C
Chapter 3 : Balagurusamy Programming ANSI in C
 
Lecture 8- Data Input and Output
Lecture 8- Data Input and OutputLecture 8- Data Input and Output
Lecture 8- Data Input and Output
 
Input output functions
Input output functionsInput output functions
Input output functions
 
Lecture 7- Operators and Expressions
Lecture 7- Operators and Expressions Lecture 7- Operators and Expressions
Lecture 7- Operators and Expressions
 
Chapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in CChapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in C
 

Similar to C Language Lecture 3

Cs291 assignment solution
Cs291 assignment solutionCs291 assignment solution
Cs291 assignment solutionKuntal Bhowmick
 
C programming Control Structure.pptx
C programming Control Structure.pptxC programming Control Structure.pptx
C programming Control Structure.pptxDEEPAK948083
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control StructureSokngim Sa
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CSowmya Jyothi
 
introduction to c programming and C History.pptx
introduction to c programming and C History.pptxintroduction to c programming and C History.pptx
introduction to c programming and C History.pptxManojKhadilkar1
 
Branching statements
Branching statementsBranching statements
Branching statementsArunMK17
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02Wingston
 
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTESUnit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTESLeahRachael
 
The solution manual of programming in ansi by Robin
The solution manual of programming in ansi by RobinThe solution manual of programming in ansi by Robin
The solution manual of programming in ansi by RobinShariful Haque Robin
 
Control structure of c
Control structure of cControl structure of c
Control structure of cKomal Kotak
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4MKalpanaDevi
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.Haard Shah
 

Similar to C Language Lecture 3 (20)

C Language Lecture 17
C Language Lecture 17C Language Lecture 17
C Language Lecture 17
 
C Language Lecture 16
C Language Lecture 16C Language Lecture 16
C Language Lecture 16
 
Cs291 assignment solution
Cs291 assignment solutionCs291 assignment solution
Cs291 assignment solution
 
C faq pdf
C faq pdfC faq pdf
C faq pdf
 
C programming Control Structure.pptx
C programming Control Structure.pptxC programming Control Structure.pptx
C programming Control Structure.pptx
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in C
 
introduction to c programming and C History.pptx
introduction to c programming and C History.pptxintroduction to c programming and C History.pptx
introduction to c programming and C History.pptx
 
C Language Lecture 18
C Language Lecture 18C Language Lecture 18
C Language Lecture 18
 
C Language Lecture 5
C Language Lecture  5C Language Lecture  5
C Language Lecture 5
 
Branching statements
Branching statementsBranching statements
Branching statements
 
Operators1.pptx
Operators1.pptxOperators1.pptx
Operators1.pptx
 
C programs
C programsC programs
C programs
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
 
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTESUnit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
 
The solution manual of programming in ansi by Robin
The solution manual of programming in ansi by RobinThe solution manual of programming in ansi by Robin
The solution manual of programming in ansi by Robin
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
 
C Language Lecture 2
C Language Lecture  2C Language Lecture  2
C Language Lecture 2
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.
 

More from Shahzaib Ajmal

More from Shahzaib Ajmal (15)

C Language Lecture 22
C Language Lecture 22C Language Lecture 22
C Language Lecture 22
 
C Language Lecture 21
C Language Lecture 21C Language Lecture 21
C Language Lecture 21
 
C Language Lecture 20
C Language Lecture 20C Language Lecture 20
C Language Lecture 20
 
C Language Lecture 15
C Language Lecture 15C Language Lecture 15
C Language Lecture 15
 
C Language Lecture 14
C Language Lecture 14C Language Lecture 14
C Language Lecture 14
 
C Language Lecture 13
C Language Lecture 13C Language Lecture 13
C Language Lecture 13
 
C Language Lecture 12
C Language Lecture 12C Language Lecture 12
C Language Lecture 12
 
C Language Lecture 11
C Language Lecture  11C Language Lecture  11
C Language Lecture 11
 
C Language Lecture 10
C Language Lecture 10C Language Lecture 10
C Language Lecture 10
 
C Language Lecture 9
C Language Lecture 9C Language Lecture 9
C Language Lecture 9
 
C Language Lecture 8
C Language Lecture 8C Language Lecture 8
C Language Lecture 8
 
C Language Lecture 7
C Language Lecture 7C Language Lecture 7
C Language Lecture 7
 
C Language Lecture 6
C Language Lecture 6C Language Lecture 6
C Language Lecture 6
 
C Language Lecture 4
C Language Lecture  4C Language Lecture  4
C Language Lecture 4
 
C Language Lecture 1
C Language Lecture  1C Language Lecture  1
C Language Lecture 1
 

Recently uploaded

AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptNishitharanjan Rout
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111GangaMaiya1
 
latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answersdalebeck957
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptxJoelynRubio1
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactisticshameyhk98
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsNbelano25
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxPooja Bhuva
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17Celine George
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningMarc Dusseiller Dusjagr
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSAnaAcapella
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationNeilDeclaro1
 

Recently uploaded (20)

AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answers
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactistics
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health Education
 

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