Arithmetic Expressions and Printf() Function 35. Arithmetic Expression & Printf() Function

11  Download (0)

Full text

(1)

Like any other languages, in C the plus sign (+) is used to add two values; the minus sign(-) to subtract; the asterisk(*) to multiply and the slash (/) to divide two values or terms. There is no exponent operator in C, hence the function pow() is used for this purpose. The modulus operator(%) gives the remainder of the division.

It is allowed only with integers. During modulus division the sign of the result is always the sign of the first operand. For e.g.

17 % 5 = 2 -17 % 5 = -2 17 % -5 = 2 -17 % -5 = -2

ThefollowingchartdescribestheassociativityofdifferentoperatorsusedinC :

Operator Description Associativity Arithmetic Operators

* Multiplication Left to right / Division Left to right % Remainder(modulus) Left to right + Addition Left to right - Subtraction Left to right

Relational Operators

< Less than Left to right <= Less than or equal to Left to right > Greater than’ Left to right >= Greater than or equal to Left to right == Equal to Left to right != Not equal to Left to right

Logical Operators

&& Logical AND Left to right || Logical OR Left to right ! Logical NOT Left to right

Assignment Operator

= Assignment Right to left

Shorthand Operators

+= Addition Assignment Right to left -= Subtraction assignment Right to left /= Division assignment Right to left *= Multiplication assignment Right to left %= Modulus assignment Right to left

Unit

Arithmetic Expression &

Printf() Function

(2)

Assignment Operator ( = ) : (Note : There is a == sign also in C which means comparison)

The equal to (=) operator is used to assign values to variables. It is called the assignment operator.

Syntax : variable = expression; e.g. int x = 9; or int. x;

x = 9;

This will assign the value 9 to the variable x.

e.g. int a, b; (or int a = 10, b = 6;) a = 10;

b = 6;

printf(“%d %d\n”, a, b); output: 10 6 a =a + b;

printf(“%d %d\n”,a,b); output: 16 6 Auto Conversion

This is done when the values of one data type are assigned to another. If we declare variable as int. x; If we declare variable as float y;

Assigned Actual Value stored Assigned Actual Value stored

x = 9; 9 y = 9; 9.000000 (Auto conversion) x = 9.0; 9 (auto conversion) y = 9.0 9.000000

x = 9.56 9 (auto conversion) y = 9.56 9.560000

Arithmetic Operators:

e.g. Consider integers variables a = 14 , b = 4, float variables x = 12.2, y = 3 and char variable q = ‘A’

Expression Value Result

a/b 14/4 3 (integer division)

a%b 14%4 2

x/y 12.2/3 4.066667(floatdivision)

q 65 (ascii value) 65

q+’1’ 65+42 (ascii value of ‘1’ is 42) 107 Shorthand Operators

Statement Meaning Shorthand original new x = x + 9 Increment the value of x by 9 x += 9 x=18 x=27 x = x - 9 Decrements the value of x by 9 x -= 9 x=18 x=9 x = x * 9 Multiply x by 9 x *= 9 x=18 x=162 x = x / 9 Divide x by 9 x /= 9 x=18 x=2 x = x % 9 Divide x by 9 and assign

remainder to x

x %= 9 x=18 x=0 x = x + 1 Increments the value of x by 1 x += 1 or x++ or ++x x=18 x=19 x = x -1 Decrements the value of x by 1 x -= 1 or x- - or - - x x=18 x=17

Note : % is called the modulus operator and gives the remainder, it works only with integers.

(3)

INCREMENT AND DECREMENT OPERATORS

Increment operator ++y or y++ Decrement operator - - y or y - -

Means y = y + 1 i.e. increases y by 1 Means y = y – 1 i.e. decreases y by 1

++y called pre-increment, first increases y by

1 and then this increased value can be used. --y called pre-decrement, first decreases y by 1 and then this decreased value can be used. y++ called post-increment, first the current

value of y is used and then its value is

increased by 1

y-- called post-decrement, first the current

value of y is used and then its value is decreased

by 1

e.g. 1. x = y++ (post increment)

First the value of y is assigned to x (x=y)and then y increments by 1(y++). e.g. 2. x = ++y (pre increment)

First the value of y is incremented by 1(++y) and then it is assigned to x (x = y).

Value Before Statement meaning Value After

a = 6 b = 9 a = b++ a=b then b=b+1 (post) a = 9 b = 10 a= 6 b = 9 a = ++b b=b+1 then a=b (pre) a = 10 b = 10 a = 6 b = 9 a = b-- a=b then b=b-1(post) a = 9 b = 8 a = 6 b = 9 a = --b b=b-1 then a=b (pre) a = 8 b = 8 x=2, y=7 and z=0 z = ++x + y++ x=x+1(pre) then z=x+y then

y=y+1(post) x = 3, y = 8 and z = 10 x=2, y=7 and z=0 z = --x + y++ x=x-1(pre) then z=x+y then

y=y+1(post) x = 1, y = 8, z = 8

Note : Pre-increment/decrement operators(++x or --x) are evaluated first, then

Arithmetic operators are evaluated as per their hierarchy, then assignments will be done. Last post increment/decrement will be done.

Unary Operators

Operators that act upon a single operand to produce a result are known as unary operators.

The minus (-) sign is a unary operator, ++ and -- are also unary operators. e.g. –a Here the minus (-) sign is the unary operator

e.g. ++a Here the increment operator (++) is the unary operator. The printf() function

printf() is a formatted output function defined in the header file stdio.h and can be used to output any combination of numbers characters and strings. e.g. printf(“hello there\n”);

For printing numbers characters and strings format(conversion) characters are used. Several of the more frequently used conversion characters are listed below :

Commonly Used Conversion Characters for Data Output :

Conversion Character Meaning c d e f g i o s u x

data item is displayed as a single character data item is displayed as a signed decimal integer

data item is displayed as a floating-point value with an exponent data item is displayed as a floating-point value without an exponent data item is displayed as a floating point value using either e-type or f-type conversion, depending on value, trailing zeros, trailing decimal point will not be displayed

data item is displayed as a signed decimal integer

data item is displayed as an octal integer, without a leading zero data item is displayed as a string

data item is displayed as an unsigned decimal integer

(4)

e.g. int x = 9;

printf(“x = %d\n”,x); will print x = 9

Remember that what has to be printed including the format characters is always written in quotes in the printf() function, while the variables or data values are written outside the quotes.

Printing Integer Numbers

The format characters for integer numbers is [ %wd ]

“w” is the field width or number of columns in which the integer is printed. “d” represents integers.

1. If the integer is greater than the field width then the full integer is printed and the value of “w” is ignored.

2. If “w” is positive the integer is printed right justified, if “w” is negative the printing is left justified.

3. If a 0 is placed before “w” then the leading blank spaces, if any, will be filled with zeros.

4. If a plus (+) sign is placed before “w” then the + will be printed before the integer only if it is positive.

Program 3 #include<stdio.h> void main() { int x=5678; OUTPUT printf(“%d\n”,x); 5678 printf(“%2d\n”,x); 5678 printf(“%3d\n”,x); 5678 printf(“%4d\n”,x); 5678 printf(“%5d\n”,x); b5678 printf(“%6d\n”,x); bb5678 printf(“%06d\n”,x); 005678 printf(“%-6d\n”,x); 5678bb printf(“%+6d\n”,x); b+5678 printf(“%+06d\n”,x); +05678 printf(“%+-06d\n”,x); +5678b }

Note : If x = – 4832 then a minus sign would be printed before each number. Printing Real Numbers

The format for real [ %w.pf ] numbers is where “w” is the field width as explained earlier, “p” denotes the digits after the decimal point and “f” represents float. The value displayed is rounded to “p” decimal places. If “p” is not specified then the value is printed with 6 decimal places by default. If the field width “w” is less than required then it is ignored. Hence in float type, if we write % 8.2f it means we are allotting 8 positions in all of which 2 are used for decimal places. The decimal will be rounded.

(5)

Program 4

#include<stdio.h> void main(){

float x=3412.65879; OUTPUT

printf(“%5.2f\n”,x) left justified 3412.66 printf(“%10.2f\n”,x); right justified bbb3412.66

printf(“%010.2f\n”,x); 0003412.66

printf(“%-10.2f\n”,x); left justified 3412.66bbb

printf(“%.2f\n”,x); 3412.66

printf(“%.6f\n”,x); 3412.658790

printf(“%10f\n”,x); 3412.658790

printf(“%10.0f\n”,x); right justified bbbbbb3413 printf(“%#10.0f\n”,x); } bbbbb3413. Note : Normally float always gives 6 decimal places by default

Printing of a single Character

The format for printing [ %wc ]single characters is “w” is the number of columns to be used for printing and “c” represents character.

In C characters are right justified when “w” is positive, and left justified when “w” is negative, just as for numbers. Use of “0” before “w” is meaningless here Program 5

What is the output of the following C program? #include<stdio.h>

void main()

{char m = ‘A’; output

printf(“%c\n”,m); A printf(“%4c\n”,m); bbbA printf(“%-4c\n”,m); Abbb printf(“%04c\n”,m); bbbA printf(“%-04c\n”,m);} Abbb Printing of strings

The format for printing [ %w.ps ] strings is “w” is the number of columns for printing, “p” denotes that only the first “p” characters will be printed, and “s” represents format for printing of strings. Again use of “0” before “w” is meaningless.

Program 6

What is the output of the following C program? #include<stdio.h>

main()

{char m[ ] = “HOW ARE YOU”;

printf(“%s\n”,m); H O W A R E Y O U

(6)

printf(“%14.6s\n”.m); H O W A R printf(“%-14s\n”.m); H O W A R E Y O U

printf(“%-14.6s\n”.m); H O W A R

printf(“%4s\n”.m);} H O W A R E Y O U Program 7

What is the output of the following C program? #include<stdio.h> void main() { int a=300,b=-22,c=2345; printf("%6d%8d%5d\n",a,b,a+b); printf("%+-8d%+-8d\n",b,c); } output bbb300bbbbb-22bb278

-22 bbbbb+2345bbb where b denotes a blank space. Program 8

What is the output of the following C program? #include <stdio.h> void main() {float p=-31.446,q=434.987,r=12.5; printf("%.2f %.0f %.2f\n",p,q,r); printf("%-8.3f %+10.1f\n",p,q); } output -31.45b435b12.50

-31.446bbbbbb+435.0 where b denotes a blank space. Program 9

What is the output of the following C program? #include<stdio.h> void main() { int t=400,m=200; float x= 125.8, y=35.76; printf("%8d %8d %5d\n",t,m,t-m); printf("%+10.3f %10.3f\n",x,y); } output bbbbb400bbbbbb200bbb200

(7)

Program 10

What is the output of the following C program? #include<stdio.h> void main() { char x = ‘&’; float y = 21344.627; printf(“%3c%+5.2f\n”,x,y); printf(“%-2c%-7.0f%2c\n”,x,y,x); } Output & + 2 1 3 4 4 . 6 3 & 2 1 3 4 5 & Program 11

What is the output of the following C program? #include<stdio.h> void main() {int x=4,y=10; x=x++; y+=++x; y--; x-=2; printf("%d %d\n",x,y); } output 4 15 Reasoning x=x++; (x increases by 1,now x=5)

y+=++x; (x increases by 1,now x=6,then x is added to y, now y=16)

y--; (y decreases by 1, now y=15)

x-=2; (x decreases by 2, now x=4)

Program 12

What is the output of the following C program? #include <stdio.h> void main() { int a=10,b=5,c,d; a+=-b*3; b*=7; c=b%a; d=++c+2*b printf("%d %d %d %d\n",a,b,c,d); } output -5 35 1 71

(8)

Reasoning :

a+=-b*3; (-15 is added to a, now a=-5) b*=7; (b is multiplied by 7, now b=35) c=b%a; (Remainder of b(35) divided by a(-5) is 0, now c=0) d=++c+2*b (First c increases by 1, now c=1,then 1+2*35 i.e. 70

is assigned to d, now d=71) Program 13

What is the output of the following C program? #include <stdio.h> void main() {int x=10,y=15,z=300,t; z%=--y; t=4*x--; printf("%d %d %d %d\n",x,y,z,t); } output 9 14 6 40 Reasoning :

z%=--y; (First y decreases by 1, now y=14,then remainder

of 300 div by 14 gives 6 which is assigned to z. Now z=6) t=4*x--; (first 4*10, i.e. 40 is assigned to t, now t=40, then x

decreased by 1,now x=9) Comments or Remarks

We can give comments or remarks in the program to improve its readability by placing them between /* and */. E.g. /* Tax Calculatons */ . There is no semicolon at the end of a comment line.

Comments can be spilt over more than one line /* this is

Multiline Comment */ Program 14

Write a program to find and print the sum and average of 8,11,5 and 10 /* Sum and Averages*/

#include<stdio.h> void main() { int i,j,l,m,s,a; i=8; j=11; l=5; m=10; s=i+j+l+m;

(9)

a=s/4;

printf(“sum of the 4 numbers=%d\n”,s); printf(“average of the 4 numbers=%d\n”,a); }

Output

sum of the 4 numbers=34 average of the 4 number=8

Note 1 : Fraction is dropped and not rounded

Note 2 : In the above program integer division has been performed as both s and 4 are integers. Hence s/4 results in 8 and not 8.5. In C int/int gives int. However if even one of them i.e. either the numerator or denominator is a float then the answer is a float. Therefore the above program if we write a=s/4. answer will be a float

Output

average of the 4 numbers = 8.500000 (change %d to %f in printf() Program 15

/*Converting Fahrenheit into Celsius*/ #include<stdio.h>

void main()

{ float c,f=110; c=5./9*(f-32);

printf(“%f in Farenheit is equal to %f in Centigrade \n”,f,c); }

Note :

c=(5/9)*(f-32) will be wrong since int/int gives int hence in this case is 5/9=0(fraction dropped). Therefore Output will be 0.

QUESTIONS

I. Using for each evaluation the declaration given, evaluate the value of the

expressions given below :

int x=5,y=-10,z=4.5,a=2,b=5,c=0

(1) x=y++; (2) b=++a; (3 ) z=x%y ; (4) z=y%x; (5) c=a*b/b+5; (6) x=z--;

II. What is the output of the following C program?

#include<stdio.h> void main() {int a=1235,b=-1888; float x=2233.44, y=4455.866; printf("%6d %6d %6d\n",a,b,a+b); printf("%+8.2f %+10.1f\n",x,y); }

III. What is the output of the following C program?

#include<stdio.h> void main()

{ float p=-210.6278; int q=2719, r=101;

(10)

printf("%+8d %-8d %8d\n",q,q,q+r); }

IV. What is the output of the following C program?

#include <stdio.h> void main() { int a=5,b=10,c,d,e; a+=b++; c=a*2+b; d=++a-4; e=d++; printf("%d %d %d %d %d\n",a,b,c,d,e); }

V. What is the output of the following C program?

#include <stdio.h> void main() { int x=1,y=10,z=5; float s=0; x++; y+=x++*z; z--; s+=x/y; printf("\n%d %d %d\n",x,y,z); printf("\n%f\n",s); }

VI. What is the output of the following C program?

#include <stdio.h> void main() { float a=-3517.276, b=48.99, c=531.6; printf("%+010.2f %-6.1f %06.2f\n”,a,b,c); printf("%+08.1f %+-5.0f\n",a,b); }

VII. What is the output of the following C program?

#include <stdio.h> void main() { float a=527.671, b=348.29, c=12.2; printf("%09.2f %-4.1f %+5.2f\n”,a,b,c,); printf("%+07.1f %+04.0f\n",a,b); }

VIII. What is the output of the following C program?

#include <stdio.h> void main() { int b = 786, c=100; float a=-9263.65; printf("%+010.1f %05d %-5d\n”,a,b,c); printf("%+09.3f %+06d\n",a,b); }

IX. Give the difference between the following in C language. i) float and double

ii) ++x and x++ iii) = and = =

(11)

OBJECTIVE QUESTIONS

I. Select the appropriate option from the following :

1. ++y is called _______

(a) post increment (b) pre increment (c) pre decrement (d) post decrement 2. The modulus operator (%) gives the _______ of the division.

(a) Quotient (b) Numerator (c) denominator (d) remainder 3. y++ is called _______

(a) post increment (b) pre increment (c) pre decrement (d) post decrement 4. A formatted output function is _______

(a) scan(f) (b) print(f) (c) getchar( ) (d) puts( ) 5. To increase the value of y by one we can use ______.

(a) y-- (b) --y

(c) y+1 (d) ++y or y++

6. The format characters “%8f” will contain _______ decimals. (a) 6 (b) 8

(c) 4 (d) 5

7. If integer variable x contains 18, then x+=9 will change the value of x to ____. (a) 9 (b) 27

(c) 23 (d) 18

8. If integer variable x contains 18, then x-=9 will change the value of x to ____. (a) 9 (b) 27

(c) 23 (d) 18

9. If integer variable x contains 8, then x*=9 will change the value of x to ____. (a) 9 (b) 27

(c) 23 (d) 72

10. If integer variable x contains 72, then x/=9 will change the value of x to ____. (a) 9 (b) 27

(c) 8 (d) 18

11. If integer variable x contains 18, then x%=9 will change the value of x to ____. (a) 9 (b) 2

(c) 23 (d) 18

ANSWERS

Figure

Updating...

References

Related subjects :