C Questions
C Questions
C Questions
Search
Search
Solution: 1
void main(){
if(printf("Hello world")){
}
}
Solution: 2
void main(){
while(!printf("Hello world")){
}
}
Solution: 3
void main(){
switch(printf("Hello world")){
}
}
Hide
#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);
getch();
Hide
Initially:
Later:
For example:
int *call();
void main(){
int *ptr;
ptr=call();
clrscr();
printf("%d",*ptr);
}
int * call(){
int x=25;
++x;
return &x;
}
Hide
Example:
void main(){
int *ptr;
printf("%u\n",ptr);
printf("%d",*ptr);
Demerit:
(a) Wastage of memory space. We cannot change size of array
at the run time.
(b) It can store only similar type of data.
Hide
First eight bit of data bit from right side i.e. 00000111
will store in the leftmost byte from right to left side and
rest seven bit of data bit i.e. 0000000 will store in
rightmost byte from right to left side as shown in the
following figure:
Hide
//PROCESS ONE
void 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);
}
If we will use array then above program can be written as:
//PROCESS TWO
void 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);
}
(q) What will be output when you will execute the following
program?
void main(){
int arr[]={0,10,20,30,40};
char *ptr=arr;
arr=arr+2;
printf("%d",*arr);
}
void main(){
float a=0.0f,b=1.0f,c=2.0f;
float * arr[]={&a,&b,&c};
b=a+c;
printf("%f",arr[1]);
}
Hide
Example:
void main(){
int num,i=0;
clrscr();
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);
getch();
}
Output: 3 3 4 4
(a)
void main(){
double i=5.5678;
clrscr();
do
printf("hi");
while(!i);
getch();
}
Output: 3 3 4 4
(b)
void main(){
double i=5.63333;
clrscr();
do
printf("hi");
while(!i);
getch();
}
Output: hi
(c)
void 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--);
getch();
}
Hide
(a)
#include<stdio.h>
int a;
int main(){
printf("%d",a);
return 0;
}
Output: 0
(b)
#include<stdio.h>
static int a;
int main(){
printf("%d",a);
return 0;
}
Output: 0
(c)
#include<stdio.h>
extern int a;
int main(){
printf("%d",a);
return 0;
}
Output: Compilation error
#include <stdio.h>
static char c;
static int i;
static float f;
static char *str;
int main(){
printf("%d %d %f %s",c,i,f,str);
return 0;
}
(a)
#include <stdio.h>
static int i; //Declaring the variable i.
static int i=25; //Initializing the variable.
static int i; //Again declaring the variable i.
int main(){
static int i; //Again declaring the variable i.
printf("%d",i);
return 0;
}
Output: 25
(b)
#include <stdio.h>
static int i; //Declaring the variable
static int i=25; //Initializing the variable
int main(){
printf("%d",i);
return 0;
}
static int i=20; //Again initializing the variable
#include <stdio.h>
static int i=10; //Initialization statement
i=25; //Assignment statement
int main(){
printf("%d",i);
return 0;
}
(b)
#include <stdio.h>
static int i=10;
int main(){
i=25; //Assignment statement
printf("%d",i);
return 0;
}
Output: 25
#include <stdio.h>
static int i=10;
int main(){
i=5;
for(i=0;i<5;i++){
static int a=10; //This statement will execute
//only time.
printf("%d",a++);//This statement will execute
//five times.
}
return 0;
}
Output: 10 11 12 13 14
(6)If we declared static variable locally then its
visibility will within a block where it has declared. For
example:
#include<stdio.h>
int main(){
{
static int a=5;
printf("%d",a);
}
//printf("%d",a); variable a is not visible here.
return 0;
}
Output: 5
(a)
#include<stdio.h>
static float a=144.0f; //global to all function
int main(){
{
printf("%d",a); //variable a is visible here.
//printf("%d",b); variable b is not visible here.
}
printf("%d",a); //variable a is visible here.
//printf("%d",b); variable b is not visible here.
return 0;
}
static int b=5; //Global to only calculation function
void calculation(){
printf("%d",a); //variable a is visible here.
printf("%d",b); //variable b is visible here.
}
(b) Consider a c program which has written in two files
named as one.c and two.c:
//one.c
#include<conio.h>
static int i=25;
static int j=5;
void main(){
clrscr();
sum();
getch();
}
//two.c
#include<stdio.h>
extern int i; //Declaration of variable i.
extern int j; //Declaration of variable j.
/**
Above two lines will search the initialization statement of
variable i and j either in two.c (if initialized variable
is static or extern) or one.c (if initialized variable is
extern)
*/
extern void sum(){
int s;
s=i+j;
printf("%d",s);
}
Compile and execute above two file one.c and two.c at the
same time:
In Turbo c compiler
In Open project File text field write any project name with
.prj extension. In this example I am writing project name
as CProject.PRJ. Now press OK button.
In the name text field write down all c source code file
one by one i.e. first write one.c and click on Add button
Then write two.c and click on Add button and so on
At the lower part of window you can see project name, list
of files you have added etc.
Step7: To compile the two files press Alt+F9 and to run the
above program press Ctrl+F9
Note: To close the project click on Project ->
Closeproject.
#include<stdio.h>
void visit();
int main(){
int i=0;
{ //Opening inner block
static int a=5; //locally declaration
XYZ:; //Label of goto statement
printf("%d ",a);
a++;
i++;
} //closing inner block.
visit();
/* printf("%d",a); Variable a is not visible here but
it is alive. */
if(i<5)
goto XYZ;
return 0;
}
void visit(){
}
Output: 5 6 7 8 9
Hide
Prototype of a function
Answer: Declaration of function is known as prototype of
a function. Prototype of a function means
(1) What is return type of function?
(2) What parameters are we passing?
(3) For example prototype of printf function is:
int printf(const char *, …);
I.e. its return type is int data type, its first
parameter constant character pointer and second parameter
is ellipsis i.e. variable number of arguments.
Hide
#include<stdio.h>
int main(){
int i=10;
int *ptr=&i;
*ptr=(int *)20;
printf("%d",i);
}
Output: 20
Hide
Hide
Hide
struct ABC{
int a;
float b;
char c;
};
void main(){
struct ABC *ptr=(struct ABC *)0;
ptr++;
printf("Size of structure is: %d",*ptr);
}
Hide
Hide
Hide
Hide
#include<string.h>
#include<stdio.h>
void main(){
char *p; //Uninitialized pointer
char *q=NULL; //Null pointer;
strcpy(p,"cquestionbank");
strcpy(q,"cquestionbank");
clrscr();
printf("%s %s",p,q);
getch();
}
Hide
Hide
(): This operator behaves as bracket operator or function
operator.
[]: This operator behaves as array subscription operator.
*: This operator behaves as pointer operator not as
multiplication operator.
Identifier: It is not an operator but it is name of pointer
variable. You will always find the first priority will be
assigned to the name of pointer.
Data type: It is also not an operator. Data types also
includes modifier (like signed int, long double etc.)
Output: 25 0
0 25
(2) What will be output of following program?
void cdecl fun1(int,int);
void pascal fun2(int,int);
void main(){
int a=5,b=5;
clrscr();
fun1(a,++a);
fun2(b,++b);
getch();
}
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
(3) What will be output of following program?
void cdecl fun1(int,int);
void pascal fun2(int,int);
void main(){
int a=5,b=5;
clrscr();
fun1(a,++a);
fun2(b,++b);
getch();
}
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
(4) What will be output of following program?
void convention(int,int,int);
void main(){
int a=5;
clrscr();
convention(a,++a,a++);
getch();
}
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?
void pascal convention(int,int,int);
void main(){
int a=5;
clrscr();
convention(a,++a,a++);
getch();
}
void pascal convention(int p,int q,int r){
printf("%d %d %d",p,q,r);
}
Output: 5 6 6
(6) What will be output of following program?
void pascal convention(int,int);
void main(){
int a=1;
clrscr();
convention(a,++a);
getch();
}
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);
void main(){
int a=1;
clrscr();
convention(a,++a);
getch();
}
void convention(int a,int b){
printf("%d %d",a,b);
}
Output: 2 2
Hide
Far pointer:
Examples:
(1) What will be output of following c program?
void main(){
int x=10;
int far *ptr;
ptr=&x;
printf("%d",sizeof ptr);
Output: 4
void main(){
int far *near*ptr;
printf("%d %d",sizeof(ptr) ,sizeof(*ptr));
Output: 4 2
Explanation: ptr is far pointer while *ptr is near pointer.
void main(){
int far *p,far *q;
printf("%d %d",sizeof(p) ,sizeof(q));
Output: 4 4
Example:
void main(){
int x=100;
int far *ptr;
ptr=&x;
printf("%Fp",ptr);
}
Output: 8FD8:FFF4
Examples:
#include "dos.h"
void main(){
int i=25;
int far*ptr=&i;
printf("%X %X",FP_SEG(ptr),FP_OFF(ptr));
}
Output: Any segment and offset address in hexadecimal
number format respectively.
Example:
void main(){
int i;
char far *ptr=(char *)0xB800FFFA;
for(i=0;i<=10;i++){
printf("%Fp \n",ptr);
ptr++;
}
Output:
B800:FFFA
B800:FFFB
B800:FFFC
B800:FFFD
B800:FFFE
B800:FFFF
B800:0000
B800:0001
B800:0002
B800:0003
B800:0004
void 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");
void 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");
Hide
int * function();
int *(*ptr)();
ptr=&function;
int * function();
void 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
Hide
(a)
void main(){
int i=5;
clrscr();
do{
printf("%d",i);
continue;
i++;
}
while(i<=10);
getch();
}
(b)
void main(){
int i=5;
clrscr();
do{
printf("%d",i);
continue;
i++;
}
while(i<=10);
getch();
}
void main(){
int x;
scanf("%d",&x);
clrscr();
switch(x){
case 1:printf("1");break;
case 2:printf("2");continue;
default:printf("3");
}
getch();
}
Hide
}
printf(“%d”,a); //Accessing outer local variable a.
getch();
}
Output: 30 15
Value of variale:
Data which any variable keeps is known as value of
variable. For example:
int a=5;
Here value of variable a is five. Name of variable always
returns value of the variable.
How to assign any value to a variable:
C supports eleven type of assignment operator to
assign any value to operator. Those are:
(a) = (b) += (c) -= (d) v*= (e) /= (f) %=
(g) <<= (h) >>= (i) |= (j) &= (k) ^=
In this chapter will discuss only first operator i.e. =
Assignment statement in c:
Hide
const int i;
Hide
While loop:
while (Expression){
Loop body
}
(a)
void main(){
int x=3,y=2;
clrscr();
while(x+y-1){
printf("%d ",x--+y);
}
getch();
}
Output: 5 4 3 2
(b)
void main(){
float a=1.5f;
clrscr();
while(a){
printf("%.f ",a);
a-=.5f;
}
getch();
}
Output: 2 1 0
void main(){
clrscr();
while(){
printf("Hello world");
}
getch();
}
Output: Compilation error
void main(){
int i=0;
clrscr();
while(i++,i<=8);
printf("%d ",i);
getch();
}
Output: 9
void main(){
int x=2,y=2;
clrscr();
while(x<=5,y<=3)
printf("%d %d ",++x, ++y);
getch();
}
Output: 3 3 4 4
void main(){
clrscr();
while(!printf("Hello world"));
getch();
}
Hide
Hide
void main(){
int i,j,k;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf(" %d",i+j);
}
}
getch();
}
(b)
void main(){
int i,j,k;
do
while(0)
for(;0;)
printf("cbyexample");
while(0);
getch();
}
Hide
Hide
12 comments:
blogsbyalo said...
great job!!
10/29/10 12:17 PM
Anonymous said...
10/31/10 11:14 PM
vichy said...
11/9/10 7:59 PM
Anonymous said...
Superb collection..thanks !!
just one bug to notify in Q.14..printf("Size of structure is: %d",*ptr); *ptr
should be changed to ptr
12/8/10 1:24 AM
Anonymous said...
12/8/10 11:36 AM
Anonymous said...
12/14/10 9:50 AM
Anonymous said...
superr collectionnnnnnn.............
1/9/11 12:21 AM
Anonymous said...
1/29/11 2:17 PM
Anonymous said...
2/9/11 10:14 PM
Anonymous said...
2/12/11 9:11 PM
Anonymous said...
great post!!
2/17/11 11:24 PM
Anonymous said...
#include
void main()
{
}
}
}
SAMPLE OUTPUT:
A B C DEDCBA
A B C DDCBA
A B C CBA
A B B A
A A
2/22/11 12:00 AM
Post a Comment
Click me go to forum page to ask questions in C
Links to this post
Create a Link
INDEX
• Home
• C tutorial
• C Programming
• Interview c questions
• C programming pdf
• C projects
• C language
• Index
• Java questions
Subscribe To
Posts
Atom
Posts
Comments
Atom
Comments
Standard of questions ?
C Questions and Explanation
Search