TCS
Company
Programming
Program
What is the output of following program?
#include
int main()
{
void *ptr1, *ptr2, x;
int y;
x = 100;
y = 200;
ptr1 = &x;
ptr2 = &y;
printf("%d", *ptr1);
pritnf("%d", *ptr2);
return 0;
}
Read Solution (Total 22)
-
- the program causes compile time error showing the "variable or field declares as void" or invalid use of void because variables can't be declared as void..though void pointer will be working fine..
- 10 years agoHelpfull: Yes(23) No(2)
- Output : 100, 200
Since ptr1 nd ptr2 are pointer variables, it ll always hold the address of another variable...
Assume address of x=1000, y=1002
Then ptr1=&x; ptr2=&y; will store ptr1=1000, ptr2 = 1002
*ptr1 ll print *1000 (ie value of address 1000 => 100
*ptr2 ll print *1002 (ie value of address 1000 => 200
- 10 years agoHelpfull: Yes(21) No(16)
- This will show error in programs as variable x is also declared as void,wrong way of printing values of ptr1 and ptr2.
program should be like this
#include
int main()
{
void *ptr1;
void *ptr2;
int y,x;
x = 100;
y = 200;
ptr1 = &x;
ptr2 = &y;
printf("%dn",*((int *)ptr1));
printf("%dn",*((int *)ptr2));
return 0;
} - 10 years agoHelpfull: Yes(10) No(0)
- the data type of x,*ptr1,*ptr2 cannot be void
- 10 years agoHelpfull: Yes(3) No(1)
- return type of ptr*1,ptr*2 and x are null so it it impossible to implement this.
- 10 years agoHelpfull: Yes(1) No(0)
- ptri=&x
*ptr1=*&x=x=200
similarly *ptr2=200
output=100 200
- 10 years agoHelpfull: Yes(1) No(1)
- There is an error..
ptr1 and ptr2 are void so they are not allocated any space to store any value..
Therefore it is not allowed and it will not compile.. - 10 years agoHelpfull: Yes(1) No(0)
- Give the Compile error like..
Not an allowed type - 10 years agoHelpfull: Yes(1) No(0)
- ptri=&x
*ptr1=*&x=x=200
similarly *ptr2=200
output=100 200 - 10 years agoHelpfull: Yes(1) No(0)
- error because "X" a variable cant be of void data type , only pointer can be of void data type
- 10 years agoHelpfull: Yes(1) No(0)
- we will get compile time errors.
- 7 years agoHelpfull: Yes(1) No(0)
- 100,200 is answer
- 10 years agoHelpfull: Yes(0) No(1)
- it displays the actual values of x and y............i.e. 100,200
- 10 years agoHelpfull: Yes(0) No(2)
- ans:100200
- 10 years agoHelpfull: Yes(0) No(1)
- this will be an error
- 10 years agoHelpfull: Yes(0) No(0)
- i think its a compile time error
- 9 years agoHelpfull: Yes(0) No(0)
- output will be 100 200
because ptr1=&x means address of x will be stored in ptr1. similarly for pttr2=address of y.
and in printf it is *ptr1 means value that is present in the pt1.
- 9 years agoHelpfull: Yes(0) No(0)
- frnds this concept is known as null pointer or generic pointer ,by this we can use pointer to any data type but we should type cast with the respective data type while retriving
As there is no type casting this will result an error - 9 years agoHelpfull: Yes(0) No(0)
- *ptr1=100,*ptr2=200
- 9 years agoHelpfull: Yes(0) No(0)
- ptr1 will hold address of x and ptr2 will hold address of y
print("%d", *ptr1) will print value at address ptr1
and print("%d", *ptr2) will print value at address ptr2
tthus, 100, 200 will be answer - 9 years agoHelpfull: Yes(0) No(0)
- 100
200
because ptr1 and ptr2 is a pointer variable, it is assigned to the address of x and y.In printf statement once again pointer is used thus,it display the value of the address location of x and y. - 8 years agoHelpfull: Yes(0) No(0)
- it provides an error bcoz, *ptr1,*ptr2 is declared as void so it can't return anything
when we declare those pointer variables and x to int the ans is 100 and 200. - 7 years agoHelpfull: Yes(0) No(0)
TCS Other Question
Which of the following is the correct output of the program?
#include
int main()
{
int a[5]={9,10};
printf("%d,%d",a[1],a[2]);
return 0;
}
o/p of the program
#include
int main()
{
enum status { pass, fail, atkt};
enum status stud1, stud2, stud3;
stud1 = pass;
stud2 = atkt;
stud3 = fail;
printf("%d, %d, %dn", stud1, stud2, stud3);
return 0;
}