Try these C aptitude
Go for these C questions and tel the answer and errors if any:
1. void main()
{
int const * p=5;
printf("%d",++(*p));
}
2. main()
{
int c[ ]={2.8,3.4,4,6.7,5};
int j,*p=c,*q=c;
for(j=0;j<5;j++) {
printf(" %d ",*c);
++q; }
for(j=0;j<5;j++){
printf(" %d ",*p);
++p; }
}
3. main()
{
extern int i;
i=20;
printf("%d",i);
}
4. main()
{
int i=-1,j=-1,k=0,l=2,m;
m=i++&&j++&&k++||l++;
printf("%d %d %d %d %d",i,j,k,l,m);
}
5. main()
{
printf("%x",-1<<4);
}
1. void main()
{
int const * p=5;
printf("%d",++(*p));
}
2. main()
{
int c[ ]={2.8,3.4,4,6.7,5};
int j,*p=c,*q=c;
for(j=0;j<5;j++) {
printf(" %d ",*c);
++q; }
for(j=0;j<5;j++){
printf(" %d ",*p);
++p; }
}
3. main()
{
extern int i;
i=20;
printf("%d",i);
}
4. main()
{
int i=-1,j=-1,k=0,l=2,m;
m=i++&&j++&&k++||l++;
printf("%d %d %d %d %d",i,j,k,l,m);
}
5. main()
{
printf("%x",-1<<4);
}
Replies
-
Neha Kochhar
Answer of 1st questionmoonfalshGo for these C questions and tel the answer and errors if any:
1. void main()
{
int const * p=5;
printf("%d",++(*p));
}
According to me it will result in an error as we cannot change the value the pointer is pointing as pointer is pointing to constant integer .
Tell me where I am wrong? -
gaurav.bhorkar
Output: 20moonfalsh3. main()
{
extern int i;
i=20;
printf("%d",i);
}
Reason: A local variable is always preferred over global variable if their names are same. -
Manish Goyal@ Gaurav which compiler are you using? Actually it is giving error in my system
-
gaurav.bhorkar
I didn't compile that.goyal420@ Gaurav which compiler are you using? Actually it is giving error in my system
You are getting error because the variable 'i' is not defined outside main ().
Maybe my answer was wrong. -
Mangesh6688
Its giving error as:moonfalsh
3. main()
{
extern int i;
i=20;
printf("%d",i);
}
"Undefined symbol _i in module" -
Mangesh6688
The %x format specifier represents the integer value in a hexadecimal form.moonfalsh
5. main()
{
printf("%x",-1<<4);
}
-1 is represented as all 1's and when it is four times left shifted the least significant 4 bits are filled with 0's.
Output: fff0 -
gaurav.bhorkar
Thats what I said. The variable should also be defined outside main ().Mangesh6688Its giving error as:
"Undefined symbol _i in module" -
Mangesh6688
As c[] is declared as integer, c will contain {2,3,4,6,5}.moonfalsh
2. main()
{
int c[ ]={2.8,3.4,4,6.7,5};
int j,*p=c,*q=c;
for(j=0;j<5;j++) {
printf(" %d ",*c);
++q; }
for(j=0;j<5;j++){
printf(" %d ",*p);
++p; }
}
In first 'for' loop we are printing *c, as *c contains 2 it will be printed 5 times.
In second 'for' loop we are printing *p, initially *p contains 2 then it is incremented.
So, 2,3,4,6,5 will be printed.
Final Output: 2 2 2 2 2 2 3 4 6 5 -
moonfalshSorry for late reply guys.
Answer for the questions are:
1. Compilation error - cannot modify a constant value.
2. 2222223465
3. linker error- undefined symbol i
4. 00131
5. fffo -
sherya mathuranswers are
ans1. compilation error
explanation
because p is a constant integer but here we are going to change the value of a constant integer
ans2: the out will be
2 2 2 2 2 2 3 4 6 5
Explanation:
Initially pointer c is assigned to both p and q. In the first loop,
since
only q is incremented and not c , the value 2 will be printed 5 times.
In
second loop p itself is incremented. So the values 2 3 4 6 5 will be
printed.
ans3: Linker Error
explaination: Undefined symbol '_i'
Explanation:
extern storage class in the following declaration,
extern int i;
specifies to the compiler that the memory for i is allocated in some
other
program and that address will be given to the current program at the
time
of linking. But linker finds that no other variable of name i is
available
in any other program with memory space allocated for it. Hence a linker
error has occurred
ans 4:00131
ans5: fff0
You are reading an archived discussion.
Related Posts
i am e&tc student and i want idea for mini project based on microcontroller or communication
please help me
So, old ICQ fans. Here's some good news for you. ICQ 7 is now available for download.
ICQ 7 - ICQ.com
Increasing efficiency of gas turbine powerplant
i am taking part in a line follower event wherein the track contains a few inclined planes(that will include going up and going down)
will DC MOTORS suffice for this...
I am new member of this forum.
i am a computer engineer.