Wednesday, 26 October 2011

NOW SOME QUESTIONS ON POINTERS

1)What will be output of following program?

#include
#include
int main(){
char *ptr1 = NULL;
char *ptr2 = 0;
strcpy(ptr1,"jay");
strcpy(ptr2,"sanghvi");
printf("\n%s %s",ptr1,ptr2);
return 0;
}

Explanation:

Turbo C++ 3.0: (null) (null)

Turbo C ++4.5: Run time error

Linux GCC: Run time error

Visual C++: Run time error



We cannot assign any string constant in null pointer by strcpy function.

2)What will be output of following program?

#include
int main(){
int a = 10;
void *p = &a;
int *ptr = p;
printf("%u",*ptr);
return 0;
}
ANS:-10

Void pointer can hold address of any data type without type casting. Any pointer can hold void pointer without type casting.

Wednesday, 20 April 2011

Excellent questions with answers

1)
#include<stdio.h>
void main()
{
char arr[6]="jaynam" ;
printf("%s",arr);
}

output:-  Garbage Value

Explanation:-
Size of  a character array should one greater than total number of characters in any string which it stores.In c every string has one terminating null character.this represents end of string.
so in the string "jaynam" there are 6 characters and they are 'j','a','y','n','a','m' and '/o'.size of array six.so array will only store first 6 characters and it will not store null character.
As we know %s in printf statement prints stream of characters until it doesn't get first null characters.since array arr has not store any null character so it will print garbage value...

2)
#include<stdio.h>
void main()
{
int goto=4;
printf("%d",goto);
}

output-Compilation error

Explanation:-
goto is a keyword in c.variable name cannot be any keyword of c language..

3).
#include<stdio.h>
int main(){
     int i=1;
     i=2+2*i++;
     printf("%d",i);
     return 0;
}

output-5

Explanation:-
i++ i.e. when postfix increment operator is used any expression the it first assign the value in the expression the it increments the value of variable by one. So,
i = 2 + 2 * 1
i = 4
Now i will be incremented by one so i = 4 + 1 = 5

4)
#include<stdio.h>
int main(){
     int a=1,b=2,c=3;
     c=a==b;
     printf("%d",c);
     return 0;
}

output-0

Explanation:-
 == is relational operator which returns only two values.
0: If a == b is false
1: If a == b is true
Since
a=2
b=7
So, a == b is false hence c=0

5)
#include<stdio.h>
void main(){
      int a=5;
      a=a>=4;
      switch(2){
          case 0:int a=8;
          case 1:int a=10;
          case 2:++a;
          case 3:printf("%d",a);
      }
}

output-error

Explanation:-
We can not declare any variable in any case of switch case statement.

6)
#include<stdio.h>
#include<string.h>
void main(){
  int i=0;
  for(;i<=2;)
    printf(" %d",++i);
}

output-1 2 3

Explanation:-
In for loop each part is optional.

7)Program for Subtract two integer numbers without using subtraction operator
#include<stdio.h>
void main(){
     int a,b,d;
     printf("enter two numbers u want to subtract");
     scanf("%d%d",&a,&b);
     d=a+~b+1;
     printf("ans=%d",d);
}

Output:-enter two numbers u want to subtract
5
4
ans=1

Explanation:-
 ~ means ones complement
ones complement of 4=1011
add 1 to this that is 1100
binary number for 5=0101
nw add 0101+1100=0001
that is equal to 1 

8)
Write a c program without using any semicolon whose output is: jaynam.
#include<stdio.h>
void main(){
     switch(printf("jaynam")){
     }
}

Output:-jaynam

9)what is the output of the following program??
#include<stdio.h>
void main(){
char arr[10];
arr = "jaynam";
printf("%s",arr);
}

Output:-Compilation error

Explanation:-
Compilation error Lvalue required
Array name is constant pointer and we cannot assign any value in constant data type after declaration.

Translate this page on 8 other languages also



























Monday, 18 April 2011

nice concept behind this

what will be the output of this program?
#include<stdio.h>
int main()
{
int x=011,i;
for(i=0;i<x;i+=3)
{
printf("start");
continue;
printf("end");
}
return 0;
}

output:-start start start 

Explanation:-
011 is octal number. Its equivalent decimal value is 9.
So, x = 9