What will be the output of the following program?
#include<stdio.h>
int main()
{
int i = 10;
printf("%d", ++(-i));
return 0;
}
(a) 11
(b) 10
(c) -9
(d) None
Ans: (d)
Explanation: In C/C++ the pre-increment (decrement) and the post-increment (decrement) operators require an L-value expression as operand. Providing an R-value or a const qualified variable results in compilation error.
In the above program, the expression -i results in R-value which is operand of pre-increment operator. The pre-increment operator requires an L-value as operand, hence the compiler throws an error.
The increment/decrement operators needs to update the operand after the sequence point, so they need an L-value. The unary operators such as -, +, won’t need L-value as operand. The expression -(++i) is valid.
In C++ the rules are little complicated because of references. We can apply these pre/post increment (decrement) operators on references variables that are not qualified by const. References can also be returned from functions.
In C/C++, precedence of Prefix ++ (or Prefix –) has higher priority than dereference (*) operator, and precedence of Postfix ++ (or Postfix –) is higher than both Prefix ++ and *.
If p is a pointer then *p++ is equivalent to *(p++) and ++*p is equivalent to ++(*p) (both Prefix ++ and * are right associative).
For example, program 1 prints ‘h’ and program 2 prints ‘e’.
// Program 1
#include<stdio.h>
int main()
{
char arr[] = "geeksforgeeks";
char *p = arr;
++*p;
printf(" %c", *p);
getchar();
return 0;
}
Output: h
// Program 2
#include<stdio.h>
int main()
{
char arr[] = "geeksforgeeks";
char *p = arr;
*p++;
printf(" %c", *p);
getchar();
return 0;
}
Output: e
What will be the output of the following C program?
#include <stdio.h>
int main()
{
int a = 3, b = -8, c = 2;
printf("%d", a % b / c);
return 0;
}
Output: 1
% and / have same precedence and left to right associativity. So % is performed first which results in 3 and / is performed next resulting in 1. The emphasis is, sign of left operand is appended to result in case of modulus operator in C.
#include <stdio.h>
int main()
{
// a positive and b negative.
int a = 3, b = -8;
printf("%d", a % b);
return 0;
}
Output: 3
#include <stdio.h>
int main()
{
// a negative and b positive
int a = -3, b = 8;
printf("%d", a % b);
return 0;
}
Output: -3
#include <stdio.h>
int main()
{
// a and b both negative
int a = -3, b = -8;
printf("%d", a % b);
return 0;
}
Output: -3
Predict the output of following C++ program.
#include <iostream>
using namespace std;
int main()
{
int test = 0;
cout << "First character " << '1' << endl;
cout << "Second character " << (test ? 3 : '1') << endl;
return 0;
}
One would expect the output will be same in both the print statements. However, the output will be,
First character 1
Second character 49
Why the second statement printing 49? Read on the ternary expression.
A ternary operator has the following form,
exp1 ? exp2 : exp3
The expression exp1 will be evaluated always. Execution of exp2 and exp3 depends on the outcome of exp1. If the outcome of exp1 is non zero exp2 will be evaluated, otherwise exp3 will be evaluated.
Predict the output of following C programs.
// Program 1
#include <stdio.h>
int main(void)
{
int arr[] = {10, 20};
int *p = arr;
++*p;
printf("arr[0] = %d, arr[1] = %d, *p = %d",
arr[0], arr[1], *p);
return 0;
}
// PROGRAM 2
#include <stdio.h>
int main(void)
{
int arr[] = {10, 20};
int *p = arr;
*p++;
printf("arr[0] = %d, arr[1] = %d, *p = %d",
arr[0], arr[1], *p);
return 0;
}
// Program 3
#include <stdio.h>
int main(void)
{
int arr[] = {10, 20};
int *p = arr;
*++p;
printf("arr[0] = %d, arr[1] = %d, *p = %d",
arr[0], arr[1], *p);
return 0;
}
The output of above programs and all such programs can be easily guessed by remembering following simple rules about postfix ++, prefix ++ and * (dereference) operators
In C++, pre-increment (or pre-decrement) can be used as l-value, but post-increment (or post-decrement) can not be used as l-value.
For example, following program prints a = 20 (++a is used as l-value)
// CPP program to illustrate
// Pre-increment (or pre-decrement)
#include <cstdio>
int main()
{
int a = 10;
++a = 20; // works
printf("a = %d", a);
getchar();
return 0;
}
a = 20
The above program works whereas the following program fails in compilation with error “non-lvalue in assignment” (a++ is used as l-value)
// CPP program to illustrate
// Post-increment (or post-decrement)
#include <cstdio>
int main()
{
int a = 10;
a++ = 20; // error
printf("a = %d", a);
getchar();
return 0;
}
prog.cpp: In function 'int main()':
prog.cpp:6:5: error: lvalue required as left operand of assignment
a++ = 20; // error
^
How ++a is different from a++ as lvalue?
It is because ++a returns an lvalue, which is basically a reference to the variable to which we can further assign — just like an ordinary variable. It could also be assigned to a reference as follows:
int &ref = ++a; // valid
int &ref = a++; // invalid
Whereas if you recall how a++ works, it doesn’t immediately increment the value it holds. For brevity, you can think of it as getting incremented in the next statement. So what basically happens is that a++ returns an rvalue, which is basically just a value like the value of an expression which is not stored. You can think of a++ = 20; as follows after being processed:
int a = 10;
// On compilation, a++ is replaced by the value of a which is an rvalue:
10 = 20; // Invalid
// Value of a is incremented
a = a + 1;
That should help to understand why a++ = 20; won’t work.
119 docs|30 tests
|
1. What is the eligibility criteria for appearing in the C-3 GATE exam? |
2. How can I apply for the C-3 GATE exam? |
3. What is the exam pattern of the C-3 GATE exam? |
4. Are there any negative marks in the C-3 GATE exam? |
5. How can I prepare for the C-3 GATE exam effectively? |
|
Explore Courses for Computer Science Engineering (CSE) exam
|