What will output when you compile and run the following code? #include struct student { int roll; int cgpa; int sgpa[8]; }; void main() { struct student s = { 12,8,7,2,5,9 }; int *ptr; ptr = (int *)&s; clrscr(); printf( "%d", *(ptr+3) ); getch(); }
Options
A. 8
B. 7
C. 2
D. Compiler error
Correct Answer
2
Programming problems
Search Results
1. What will be output of following program ? #include int main() { int a = 10; void *p = &a; int *ptr = p; printf("%u",*ptr); return 0; }
Correct Answer: #include "memh" #include "alloch" main() { int area; char *dest; char src[] = "Life is the camera and you are the target" "so keep smiling always"; area = sizeof (src); dest = malloc (area); memmove (dest, src, area); printf("\n%s", dest); printf("\n%s",src); }
3. What would be the output of the following program? main() { char huge * near * far *ptr1; char near * far * huge *ptr2; char far * huge * near *ptr3; printf ("%d%d%d", sizeof (ptr1), sizeof (ptr2), sizeof (ptr3)); }
Correct Answer: While extracting a float argument using va_arg we should have useed c = va_arg (ptr, double)
5. Point out the error, if any, in the following program. #include "stdio.h" main() { unsigned char; FILE *fp; fp = fopen ("trail", "r"); while (( ch = getc (fp)) ! = EOF) printf ("%c", ch); fclose (fp); }
Correct Answer: EOF has been defined as #define EOF -1 n the file "stdioh" and an unsigned char ranges from 0 to 255 hence when EOF is read from the file it cannot be accommodated in ch Solution is to declare ch as an int
6. C program to find whether a number is palindrome or not.
Correct Answer: #include int main(){ int num,r,sum=0,temp; printf("Enter a number: "); scanf("%d",&num); temp=num; while(num){ r=num%10; num=num/10; sum=sum*10+r; } if(temp==sum) printf("%d is a palindrome",temp); else printf("%d is not a palindrome",temp); return 0; } Sample output: Enter a number: 131 131 is a palindrome
7. Write a c program to find out sum of diagonal element of a matrix.
Correct Answer: #include int main(){ int a[10][10],i,j,sum=0,m,n; printf("\nEnter the row and column of matrix: "); scanf("%d %d",&m,&n); printf("\nEnter the elements of matrix: "); for(i=0;i
8. The output of the code below is #include void main() { int i = 0, k; if ( i == 0 ) goto label; for ( k = 0;k < 3; k++ ) { printf( "hin" ); label: k = printf( "%03d", i ); } }
10. What would be the output of the following program? main() { int i = -3, j =2, k =0, m ; m = ++j && ++i || ++k ; Printf ( "\n%d%d%d%d", i , j , k , m ); }