Professional Documents
Culture Documents
d = ++b;
printf("\n%d", d);
printf("\n%d",b);
getchar();
return 0;
}
Dr. Muhammad Yousaf Hamza
Data Types
#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.
• Examples:
int count, x, y, z;
float a, b, m;
double percent, total, average;
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.
printf("\n%f",c); // 5.75
getchar();
return 0;
}
Dr. Muhammad Yousaf Hamza
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; #include<stdio.h>
const int val=(3*7+6)*5; int main()
•(scientific) notation {
(mantissa/exponent) const float n = 6.18e2;
onst double n = 6.18e2; printf("%f",n); 618.00
getchar();
return 0;
• 6.18e2 = 6.18x10^2 }
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
circum = 2*pi*radius;
getchar();
return 0;
}
radius = 7.3;
pi = 2.9; // Error
circum = 2*pi*radius;
printf("\n%f", circum);
getchar();
return 0;
} Dr. Muhammad Yousaf Hamza
Decisions
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);
else
printf("\nThe number %d is an odd number",num);
getchar(); getchar();
return 0;
}
Dr. Muhammad Yousaf Hamza
Control Statements
if (age<=12)
printf("Please go to Child Specialist in Room 10\n\n");
else
printf("Please go to Medical Specialist in Room 15\n");
printf(“Allah Hafiz");
getchar();
return 0;
}
Dr. Yousaf, PIEAS
The if Statement
• Form 1:
if (expression)
statement1;
next statement;
• Form 2:
if (expression)
statement1;
else
statement2;
next statement;
false true
age < = 12
print “15” print “10”