3. 3
#include <stdio.h>
void main (void)
{
printf ("Testing...n..1n...2n....3n");
}
Output?
#include <stdio.h>
void main (void)
{
int value1, value2, sum;
value1 = 50;
value2 = 25;
sum = value1 + value2;
printf("The sum of %d and %d is %dn", value1, value2, sum);
}
Output?
1
2
4. Arithmetic Instructions in C
4
int ad ;
float kot, deta, alpha, beta, gamma;
ad = 3200 ;
kot = 0.0056 ;
deta = alpha * beta / gamma + 3.2 * 2 / 5 ;
---------------------------------------------------------------------------------
Here,
*, /, -, + are the arithmetic operators.
= is the assignment operator.
2, 5 and 3200 are integer constants.
3.2 and 0.0056 are real constants.
ad is an integer variable.
kot, deta, alpha, beta, gamma are real variables.
5. Variable Declaration and Use
5
Correct Incorrect
float a = 1.5, b = a + 3.1; float b = a + 3.1, a = 1.5;
int a, b, c, d ;
a = b = c = 10 ;
int a = b = c = d = 10;
int x, y, z;
x = y * z;
int x, y, z;
y * z = x;
Cannot Use aVariable before declaring it!
6. Integer and Float Conversion
6
Implicit and Explicit Conversions
An arithmetic operation between an integer and integer
always yields an integer result.
An operation between a real and real always yields a real
result.
An operation between an integer and real always yields a real
result. In this operation the integer is first promoted to a real
and then the operation is performed. Hence the result is real.
7. 7
int i ;
float b ;
i = 3.5 ;
b = 30 ;
What would be the value of i and b?
Think of Integer and Float Conversion
8. 8
k is an integer variable
a is a real/float variable
9. Receiving User Input
To get the input from the user we use a library function called
scanf()
The format of scanf() is;
scanf(“<format specifier>”, &<variable name>);
format specifier shows what type of data you want to input or get
from user
& before the variable name is must
& is an “Address of Operator”
It gives the location number used by the variable in memory
&num tells the scanf() function at which memory location should
it store the value supplied by the user
a blank, a tab or a new line (pressing enter) must separate the values
supplied to scanf()
Example: scanf(“%d %d %f”, &p,&n,&r);
9
12. Evaluate yourself!
12
Point out the errors, if any, in the following C statements:
(a) int = 314.562 * 150 ;
(b) name = ‘Ajay’ ;
(c) varchar = ‘3’ ;
(d) 3.14 * r * r * h = vol_of_cyl ;
(e) k = ( a * b ) ( c + ( 2.5a + b ) ( d + e ) ;
(f) m_inst = rate of interest * amount in rs ;