Professional Documents
Culture Documents
1. Ques: Write a program to read three floating point numbers and find their sum
and average.
Ans: #include<stdio.h>
#include<conio.h>
void main()
clrscr();
float x,y,z,avg,sum;
scanf("%f%f%f",&x,&y,&z);
sum=x+y+z;
avg=sum/3;
printf("sum=%5.2f,avg=%5.2f\n",sum,avg,(x+y+z)/3);
getch();
}
2. Ques: What are precedence of operators? How expressions are evaluated using
precedences?
(m + n) (x + y) (m + n) * (x + y)
(ab / c) a*b/c
(x / y) + c x/y+c
Evaluation of Expressions
Variable = expression;
Variable is any valid C variable name. When the statement is encountered, the expression is
evaluated first and then replaces the previous value of the variable on the left hand side. All
variables used in the expression must be assigned values before evaluation is attempted.
x = a * b – c
y = b / c * a
z = a – b / c + d;
.
main ()
{
float a, b, c x, y, z;
a = 9;
b = 12;
c = 3;
x = a – b / 3 + c * 2 – 1;
y = a – b / (3 + c) * (2 – 1);
z = a – ( b / (3 + c) * 2) – 1;
printf (“x = %fn”,x);
printf (“y = %fn”,y);
printf (“z = %fn”,z);
}
.
output
x = 10.00
y = 7.00
z = 4.00
An arithmetic expression without parentheses will be evaluated from left to right using the rules
of prescedence of operators. There are two distinct priority levels of arithmetic operators in C:
High priority * / %
Low priority + -
Each operator in C has a precedence associated with it. The precedence is used to determine how
an expression involving more than one operator is evaluated. There are distinct levels of
precedence and an operator may belong to one of these levels. The operators of higher
precedence are evaluated first.
The operators of same precedence are evaluated from right to left or from left to right depending
on the level. This is known as associativity property of an operator.
This table shows the priority and associativity of each operator in Pike, with the highest priority
at the top:
Operators Associativity
::a a::b left to right
a() a[b] a->b a[b..c] ({}) ([]) (<>) left to right
a++ a-- left to right
!a ~a (type)a ++a --a right to left!
a*b a/b a%b left to right
a+b a-b left to right
a>>b a<<b left to right
a>b a>=b a<b a<=b left to right
a==b a!=b left to right
a&b left to right
a^b left to right
a|b left to right
&& left to right
|| left to right
a?b:c right to left!
= right to left!
+=
-=
*=
/=
%=
<<=
>>=
&=
|=
^=
@a right to left!
, left to right
Examples:
Ans: Constants
Constants in C refer to fixed values that do not change during the execution of a program. C
supports several types of constants as illustrated in Fig 1
Fig 1
Integer constants
An integer constant refers to a sequence of digits. There are three types of integers, namely
decimal, octal and hexadecimal.
Decimal integers consist of a set of digits, 0 through 9, preceded by an optional – or +.
An octal integer constant consists of any combination of digits from the set 0 through 7, with a
leading 0.
Real constants
The numbers containing fractional parts like 67.45 are called real(or floating point) constants.
Examples: 0.0045, -8.5, +345.678
A real number may also be expressed in exponential(scientific) notation. The general form is:
mantissa e exponent
The mantissa is either a real number expressed in decimal notation or an integer. The exponent is
an integer number with an optional plus or minus sign. The letter e separating the mantissa and
the exponent can be written in either lowercase or uppercase.
Floating point constants are normally represented as double-precision quantities. However, the
suffixes f or F may be used to force single precision and l or L to extend double-precision
further.
Character constants
A single character constant( or simple character constant) contains a single character enclosed
within a pair of single quote marks.
Character constants have integer values known as ASCII values. For example, the statement
printf(“%d”, ‘a’);
would print the number 97, the ASCII value of the letter a. Similarly, the statement
printf(“%c”, 97);
A string constant is a sequence of characters enclosed within double quotes. The characters may
be letters, numbers, special characters and blank space.
C supports some special backslash character constants that are used in output functions. A list of
such backslash character constants is given in Table 2.1. Note that each one of them represents
one character, although they consist of two characters. These character combinations are called
escape sequences.
Constant Meaning
‘\\’ backslash
‘\0’ null
4. Ques: Explain pre increment and post increment operator with an example.
Ans: When you use a post-increment (n++) you are first making a copy of the variable. This is
the value used in your statement. The variable will be incremented, but not until the current line
is done executing. When it finishes, the incremented value will replace n.
When you use a pre-increment (++n) you are first making a copy of the variable, as in post-
increment. The difference is that the value you copied will be incremented right away. This value
will then be copied back into n after the current line is done executing
Just remember that post-increment happens AFTER you use it, and pre-increment happens right
when you use it. Try running the following code to see it in action.
int main(int argc, char** argv) {
int n = 0;
printf("%d\n", n++); // this would print out 0, since n isn't incremented until after this line
printf("%d\n", n); // now n would print out as 1
printf("%d\t%d\n", ++n, n); // and now we see that the first n is printed as a 4
// while the second is still a 3
printf("%d\n", n); // and once again n is set to 4 after the last line
return 0;
}
// end condition
if (start > end) {
return 0;
}
// actual work
return start + sumEvens(start + 2, end);
printf("\nMatrix 1:\n");
print_matrix(p);
printf("\nMatrix 2:\n");
print_matrix(q);
printf("\nResult:\n");
print_matrix(r);
}
void add_matrices(int a[][3], int b[][3], int result[][3])
{
int i, j;
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
result[i][j] = a[i][j] + b[i][j];
}
}
}
void print_matrix(int a[][3])
{
int i, j;
for (i=0; i<3; i++)
{
for (j=0; j<3; j++)
{
printf("%d\t", a[i][j]);
}
printf("\n");
}
getch();
}
8. Ques: What is the difference between pointer variable and simple variable? What are the
advantages of pointer variable.
Ans:
A variable has 3 things when you make them :
int <- type
x <- name
=3 <- data
int x = 3;
int *x = x