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
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
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
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