C Programming Question and Answer - WORD 5
C Programming Question and Answer - WORD 5
C Programming Question and Answer - WORD 5
any semicolon.
Explanation:
Solution: 1
void main(){
if(printf("Hello world")){
}
}
Solution: 2
void main(){
while(!printf("Hello world")){
}
}
Solution: 3
void main(){
switch(printf("Hello world")){
}
}
Explanation:
#include<stdio.h>
int main(){
int a=5,b=10;
//process one
a=b+a;
b=a-b;
a=a-b;
printf("a= %d b= %d",a,b);
//process two
a=5;
b=10;
a=a+b-(b=a);
printf("\na= %d b= %d",a,b);
//process three
a=5;
b=10;
a=a^b;
b=a^b;
a=b^a;
printf("\na= %d b= %d",a,b);
//process four
a=5;
b=10;
a=b-~a-1;
b=a+~b+1;
a=a+~b+1;
printf("\na= %d b= %d",a,b);
//process five
a=5,
b=10;
a=b+a,b=a-b,a=a-b;
printf("\na= %d b= %d",a,b);
return 0;
}
Dangling pointer:
Initially:
Later:
For example:
#include<stdio.h>
int *call();
int main(){
int *ptr;
ptr=call();
fflush(stdin);
printf("%d",*ptr);
return 0;
}
int * call(){
int x=25;
++x;
return &x;
}
Output: Garbage value
Note: In some compiler you may get warning
message returning address of local variable or
temporary
#include<stdio.h>
int *call();
int main(){
int *ptr;
ptr=call();
fflush(stdin);
printf("%d",*ptr);
return 0;
}
int * call(){
return &x;
}
Output: 26
Explanation:
A pointer in c which has not been initialized is
known as wild pointer.
Example:
int main(){
int *ptr;
printf("%u\n",ptr);
printf("%d",*ptr);
return 0;
}
Explanation:
Merits:
Demerit:
Explanation:
An array is derived data type in c programming
language which can store similar type of data in
continuous memory location. Data may be primitive
type (int, char, float, double…), address of union,
structure, pointer, function or another array.
Example of array declaration:
int arr[5];
char arr[5];
float arr[5];
long double arr[5];
char * arr[5];
int (arr[])();
double ** arr[5];
//PROCESS ONE
int main(){
int ax=1;
int b=2;
int cg=5;
int dff=7;
int am=8;
int raja=0;
int rani=11;
int xxx=5;
int yyy=90;
int p;
int q;
int r;
int avg;
avg=(ax+b+cg+dff+am+raja+rani+xxx+yyy+p+q+r)/12;
printf("%d",avg);
return 0;
}
If we will use array then above program can be
written as:
//PROCESS TWO
int main(){
int arr[]={1,2,5,7,8,0,11,5,50};
int i,avg;
for(int i=0;i<12;i++){
avg=avg+arr[i];
}
printf("%d",avg/12);
return 0;
}
Question: Write a C program to find out average of
200 integer number using process one and two.
int main(){
int arr[]={0,10,20,30,40};
char *ptr=arr;
arr=arr+2;
printf("%d",*arr);
return 0;
}
Array of pointers in c:
int main(){
float a=0.0f,b=1.0f,c=2.0f;
float * arr[]={&a,&b,&c};
b=a+c;
printf("%f",arr[1]);
return 0;
}
do {
Loop body
} while (Expression);
Example:
int main(){
int num,i=0;
do{
printf("To enter press 1\n");
printf("To exit press 2");
scanf("%d",&num);
++i;
switch(num){
case 1:printf("You are welcome\
n");break;
default : exit(0);
}
}
while(i<=10);
return 0;
}
Output: 3 3 4 4
(a)
int main(){
double i=5.5678;
do
printf("hi");
while(!i);
return 0;
}
Output: 3 3 4 4
(b)
int main(){
double i=5.63333;
do
printf("hi");
while(!i);
return 0;
}
Output: hi
(c)
int main(){
int x=25,y=1;
do
if(x>5)
printf(" ONE");
else if(x>10)
printf(" TWO");
else if(x==25)
printf(" THREE");
else
printf(" FOUR");
while(y--);
return 0;
}
#include<stdio.h>
int main(){
int i=10;
int *ptr=&i;
*ptr=(int *)20;
printf("%d",i);
return 0;
}
Output: 20
Explanation:
(1) What will be output if you will execute following
code?
int * function();
int main(){
auto int *x;
int *(*ptr)();
ptr=&function;
x=(*ptr)();
printf("%d",*x);
}
int *function(){
static int a=10;
return &a;
}
Output: 10
Explanation: Here function is function whose
parameter is void data type and return type is
pointer to int data type.
x=(*ptr)()
=> x=(*&functyion)() //ptr=&function
=> x=function() //From rule *&p=p
=> x=&a
So, *x = *&a = a =10
int find(char);
int(*function())(char);
int main(){
int x;
int(*ptr)(char);
ptr=function();
x=(*ptr)('A');
printf("%d",x);
return 0;
}
int find(char c){
return c;
}
int(*function())(char){
return find;
}
Output: 65
Explanation: Here function whose name is function
which passing void data type and returning another
function whose parameter is char data type and return
type is int data type.
x=(*ptr)(‘A’)
=> x= (*function ()) (‘A’) //ptr=function ()
//&find=function () i.e. return type of function ()
=> x= (* &find) (‘A’)
=> x= find (‘A’) //From rule*&p=p
=> x= 65
Output: inter.blogspot.com
Explanation: Here call is function whose return type
is pointer to character and one parameter is pointer
to int data type and second parameter is pointer to
float data type and ptr is pointer to such function.
str= str+*i+ (int) (*j)
=”c-pointer.blogspot.com” + *&a+ (int) (*&b)
//i=&a, j=&b
=”c-pointer.blogspot.com” + a+ (int) (b)
=”c-pointer.blogspot.com” +2 + (int) (2.0)
=”c-pointer.blogspot.com” +4
=”inter.blogspot.com”
Output: cquestionbak
Explanation: Here display is function whose parameter
is pointer to character and return type is also
pointer to character and ptr is its pointer.
Explanation:
struct ABC{
int a;
float b;
char c;
};
int main(){
struct ABC *ptr=(struct ABC *)0;
ptr++;
printf("Size of structure is: %d",*ptr);
return 0;
}
What is NULL pointer?
Explanation:
Literal meaning of NULL pointer is a pointer which is
pointing to nothing. NULL pointer points the base
address of segment.
Examples:
#include "stdio.h"
int main(){
if(!NULL)
printf("I know preprocessor");
else
printf("I don't know preprocessor");
}
Explanation:
!NULL = !0 = 1
In if condition any non zero number mean true.
(2)What will be output of following c program?
#include "stdio.h"
int main(){
int i;
static int count;
for(i=NULL;i<=5;){
count++;
i+=2;
}
printf("%d",count);
}
Output: 3
#include "stdio.h"
int main(){
#ifndef NULL
#define NULL 5
#endif
printf("%d",NULL+sizeof(NULL));
}
Output: 2
Explanation:
NULL + sizeof(NULL)
=0 + sizeoof(0)
=0+2 //size of int data type is two byte.
Example:
#include "string.h"
int main(){
char *str=NULL;
strcpy(str,"c-pointer.blogspot.com");
printf("%s",str);
return 0;
}
Output: (null)
Explanation:
In c we can pass the parameters in a function in two
different ways.
#include<stdio.h>
int main(){
int a=5,b=10;
swap(a,b);
printf("%d %d",a,b);
return 0;
}
void swap(int a,int b){
int temp;
temp =a;
a=b;
b=temp;
}
Output: 5 10
#incude<stdio.h>
int main(){
int a=5,b=10;
swap(&a,&b);
printf("%d %d",a,b);
return 0;
}
void swap(int *a,int *b){
int *temp;
*temp =*a;
*a=*b;
*b=*temp;
}
Output: 10 5
Explanation:
Size of any type of pointer in c is independent of
data type which is pointer is pointing i.e. size of
all type of pointer (near) in c is two byte either it
is char pointer, double pointer, function pointer or
null pointer. Void pointer is not exception of this
rule and size of void pointer is also two byte.
Explanation:
An uninitialized pointer is a pointer which points
unknown memory location while null pointer is pointer
which points a null value or base address of segment.
For example:
printf("%s %s",p,q);
return 0;
}
Explanation:
Rule 1. Assign the priority to the pointer
declaration considering precedence and associative
according to following table.
char (* ptr)[3]
Answer:
Step 1: () and [] enjoys equal precedence. So rule of
associative will decide the priority. Its associative
is left to right so first priority goes to ().
float (* ptr)(int)
Answer:
Assign the priority considering precedence and
associative.
int ( * ( * ptr ) [ 5 ] ) ( )
Answer:
Assign the priority considering rule of precedence
and associative.
double*(*(*ptr)(int))(double **,char c)
Answer:
Assign the priority considering rule of precedence
and associative.
Answer:
Assign the priority considering rule of precedence
and associative.
Explanation:
1. pascal: In this style function name should (not
necessary ) in the uppercase .First parameter of
function call is passed to the first parameter of
function definition and so on.
int main(){
static int a=25;
void cdecl conv1() ;
void pascal conv2();
conv1(a);
conv2(a);
return 0;;
}
void cdecl conv1(int a,int b)
{
printf("%d %d",a,b);
}
void pascal conv2(int a,int b)
{
printf("\n%d %d",a,b);
}
Output: 25 0
0 25
(2) What will be output of following program?
fun1(a,++a);
fun2(b,++b);
return 0;
}
void cdecl fun1(int p,int q){
printf("cdecl: %d %d \n",p,q);
}
void pascal fun2(int p,int q){
printf("pascal: %d %d",p,q);
}
Output:
cdecl: 6 6
pascal: 5 6
fun1(a,++a);
fun2(b,++b);
return 0;
}
void cdecl fun1(int p,int q){
printf("cdecl: %d %d \n",p,q);
}
void pascal fun2(int p,int q){
printf("pascal: %d %d",p,q);
}
Output:
cdecl: 6 6
pascal: 5 6
void convention(int,int,int);
int main(){
int a=5;
convention(a,++a,a++);
return 0;
}
void convention(int p,int q,int r){
printf("%d %d %d",p,q,r);
}
Output: 7 7 5
(5) What will be output of following program?
convention(a,++a,a++);
return 0;}
void pascal convention(int p,int q,int r){
printf("%d %d %d",p,q,r);
}
Output: 5 6 6
convention(a,++a);
return 0;
}
void pascal convention(int a,int b){
printf("%d %d",a,b);
}
Output: 1 2
(7) What will be output of following program?
void convention(int,int);
int main(){
int a=1;
convention(a,++a);
return 0;}
void convention(int a,int b){
printf("%d %d",a,b);
}
Output: 2 2
int main(){
int x=10;
int far *ptr;
ptr=&x;
printf("%d",sizeof ptr);
return 0;
}
Output: 4
int main(){
int far *near*ptr;
printf("%d %d",sizeof(ptr) ,sizeof(*ptr));
return 0;
}
Output: 4 2
Explanation: ptr is far pointer while *ptr is near
pointer.
int main(){
int far *p,far *q;
printf("%d %d",sizeof(p) ,sizeof(q));
}
Output: 4 4
Example:
int main(){
int x=100;
int far *ptr;
ptr=&x;
printf("%Fp",ptr);
return 0;
}
Output: 8FD8:FFF4
Here 8FD8 is segment address and FFF4 is offset
address in hexadecimal number format.
Examples:
(1)What will be output of following c program?
#include "dos.h"
int main(){
int i=25;
int far*ptr=&i;
printf("%X %X",FP_SEG(ptr),FP_OFF(ptr));
}
#include "dos.h"
int main(){
int i=25;
int far*ptr=&i;
unsigned int s,o;
s=FP_SEG(ptr);
o=FP_OFF(ptr);
printf("%Fp",MK_FP(s,o));
return 0;
}
Output: 8FD9:FFF4 (Assume)
Note: We cannot guess what will be offset address;
segment address and far address of any far
pointer .These address are decided by operating
system.
Example:
int main(){
int i;
char far *ptr=(char *)0xB800FFFA;
for(i=0;i<=10;i++){
printf("%Fp \n",ptr);
ptr++;
}
return 0;
}
Output:
B800:FFFA
B800:FFFB
B800:FFFC
B800:FFFD
B800:FFFE
B800:FFFF
B800:0000
B800:0001
B800:0002
B800:0003
B800:0004
This property of far pointer is called cyclic nature
of far pointer within same segment.
Examples:
int main(){
int far *p=(int *)0X70230000;
int far *q=(int *)0XB0210000;
if(p==q)
printf("Both pointers are equal");
else
printf("Both pointers are not equal");
return 0;
}
int main(){
int far *p=(int *)0X70230000;
int far *q=(int *)0XB0210000;
int near *x,near*y;
x=(int near *)p;
y=(int near *)q;
if(x==y)
printf("Both pointer are equal");
else
printf("Both pointer are not equal");
return 0;
}