Answers To C-Questions
Answers To C-Questions
int myshow(int);
void main()
{
int a=10;
myshow(a);
myshow(&a);
}
int myshow(int b)
{
printf("Received %d, ", b);
}
int main()
{
int a=0, b=0;
while(++a < 4)
printf("%d ", a);
while(b++ < 4)
printf("%d ", b);
return 0;
}
Answer: 1 2 3 1 2 3 4
3. What is the Priority of C Logical Operators? NOT (!), AND (&&) and OR(||)
#include <stdio.h>
#include <math.h>
const double PI = 4*atan(1.);
double ours(double);
int main() {
double (*f)(double);
f = sin;
printf("sin(PI/2) = %f\n", (*f)(PI/2));
f = &cos;
C Questions
}
double ours (double x) {
return x*x;}
#include<stdio.h>
int main()
{
printf("%x\n", -1>>1);
return 0;
}
Answer: ffffffff or 4294967295
C Questions
#include <stdio.h>
int main ()
{
union a
{
int i;
char ch[2];
};
union a u;
u.ch[0]=3;
u.ch[1]=2;
printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i);
return 0;
}
Answer: 3, 2, 515
#include<stdio.h>
int main()
{
struct emp
{
char name[25];
int age;
float sal;
};
struct emp e[2];
int i=0;
for(i=0; i<2; i++)
scanf("%s %d %f", e[i].name, &e[i].age, &e[i].sal);
Answer: C
C Questions
#include<stdio.h>
int main()
{
static int arr[] = {0, 1, 2, 3, 4};
int *p[] = {arr, arr+1, arr+2, arr+3, arr+4};
int **ptr=p;
ptr++;
printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
*ptr++;
printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
*++ptr;
printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
++*ptr;
printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
return 0;
}
Answer: 1, 1, 1
2, 2, 2
3, 3, 3
3, 4, 4
9. Write a C program to check whether the number is EVEN or ODD, without using any
arithmetic or relational operators.
Answer:
#include<stdio.h>
int main() {
int x;
printf("Enter a number:");
scanf("%d", &x);
(x&1)?printf("\nOdd"):printf("\nEven");
return 0; }
C Questions
#include<stdio.h>
int main(){
unsigned int res;
res = (64 >>(2+1-2)) & (~(1<<2));
printf("%d\n", res);
return 0;}
Answer: 32
#include <stdio.h>
int main()
{
int a = 1, b = 2, c = 3;
printf("%d", a + = a + = 3 *5 + a);
}
Answer: 34
struct f1 {
struct f5 {
char *p;
short x;
} inner;
char c;
};
Answer: 24
#include <stdio.h>
#define DefExp(x,y)
#define x ((y)+1)
#define DefExp(x) (printf("%s = %d\n", #x, (x)))
int main(int argc, char **argv){
DefExp(1*3);
return 0;}
Answer: 1*3 = 3
C Questions
#include <stdio.h>
int main()
{
union sample{
int m;
float n;
char ch;
};
union sample u;
u.m = 25;
printf("%d %f %c\n", u.m, u.n, u.ch);
u.n = 0.2;
printf("%d %f %c\n", u.m, u.n, u.ch);
u.ch = 'p';
printf("%d %f %c\n", u.m, u.n, u.ch);
printf("The size of union = %d\n", sizeof(u));
return 0;
}
Answer:
25 0.000000
1045220557 0.200000 �
1045220464 0.199999 p
C Questions
#include <stdio.h>
#include <stdlib.h>
int G = 0;
int main(int argc, char **argv)
{
static int s;
int a;
int *p;
p = malloc(sizeof(int));
free(p);
return 0;
}
Answer:
&G= 0x55ddb4e46014
&s= 0x55ddb4e46018
&a= 0x7ffc4103ba5c
&p= 0x7ffc4103ba60
p= 0x55ddb51522a0
main=0x55ddb4e431a9
C Questions
#include <stdio.h>
int main () {
int x = 10, y = 20, z, sum=7;
int *ptr1 = &x;
int *ptr2 = &y;
z = *ptr1 * *ptr2;
y=*ptr1**ptr2;
sum=sum+*ptr1;
*ptr2=*ptr2+10;
*ptr1=*ptr1+*ptr2;
*ptr1=*ptr2-*ptr1;
z=5*-*ptr2/ *ptr1;
printf("%d %d %d", x,y,z);
return 0;
}
Answer: -10 210 105
C Questions
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <string.h>
typedef struct {
char *model;
int capacity;
} Car;
int main ()
{
Car af1;
Car af2;
Car af3;
return 0;
}
Answer: Daimler BMW Daimler
C Questions
#include <stdio.h>
typedef struct tagT{
int a:4;
int b:4;
int c:8;
int d:16;
}T;
int main()
{
char data[]={0x12,0x34,0x56,0x78};
T *t = (T*)data;
printf("a =0x%x\n" ,t->a);
printf("b =0x%x\n" ,t->b);
printf("c =0x%x\n" ,t->c);
printf("d =0x%x\n" ,t->d);
return 0;
}
Answer:
a =0x2
b =0x1
c =0x34
d =0x7856
C Questions
#include <stdio.h>
int main()
{
int A=5;
int B=7;
int C;
C = A&B;
printf("1st:%d\n",C);
C = A|B;
printf("2nd:%d\n",C);
C = A^B;
printf("3rd:%d\n",C);
C = ~A;
printf("4th:%d\n",C);
C = B<<3;
printf("5th:%d\n",C);
C = B^5&A|B<<A;
printf("6th:%d\n",C);
return(0);
}
Answer:
1st:5
2nd:7
3rd:2
4th: -6
5th:56
6th:226
C Questions
#include <stdio.h>
#include <stdio.h>
int main(){
int k=8;
switch(k){
case 1==8:
printf("DELTA\t");
break;
case 1 && 2:
printf("INDIA\t");
break;
default:
printf("ENERGY\t");
}
printf("SYSTEMS");
}
Answer: ENERGY SYSTEMS