C /C++ Questions
1. Write a c program without using any semicolon which output will : Hello word.
7. Write a c program to find size of structure without using sizeof operator ?
Answer
2. What is dangling pointer in c?
Solution: 1
void main(){
if(printf("Hello world")){
}
}
Solution: 2
void main(){
while(!printf("Hello world")){
}
}
Solution: 3
void main(){
switch(printf("Hello world")){
}
}
Answer


3. Why we use do-while loop in c? Also tell any properties which you know ?
If any pointer is pointing the memory address of any variable but after some variable has deleted from that memory location while pointer is still pointing such memory location. Such pointer is known as dangling pointer and this problem is known as dangling pointer problem.
Initially:

Later:

For example:
(q)What will be output of following c program?
int *call();
void main(){
int *ptr;
ptr=call();
clrscr();
printf("%d",*ptr);
}
int * call(){
int x=25;
++x;
return &x;
}
Output: Garbage value
Explanation: variable x is local variable. Its scope and lifetime is within the function call hence after returning address of x variable x became dead and pointer is still pointing ptr is still pointing to that location.
Solution of this problem: Make the variable x is as static variable.
In other word we can say a pointer whose pointing object has been deleted is called dangling pointer.
Answer
void main(){
void main(){
4. Why we use static variable in c? Tell me all properties of static variables which you know?
It is also called as post tested loop. It is used when it is necessary to execute the loop at least one time.
Syntax:
do {
Loop body
} while (Expression);
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
If there is only one statement in the loop body then braces is optional. For example:
(a)
double i=5.5678;
clrscr();
do
printf("hi");
while(!i);
getch();
}
Output: 3 3 4 4
(b)
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();
}
Output: ONE ONE
Answer
5. Write a c program to modify the constant variable in c?
Keyword static is used for declaring static variables in c. This modifier is used with all data types like int, float, double, array, pointer, structure, function etc.Important points about static keyword:
1. It is not default storage class of global variables. For example, analyze the following three programs and its output.
(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
At first glance if you will observe the output of above three codes you can say default storage class of global variable is static. But it is not true. Why? Read extern storage class.
2. Default initial value of static integral type variables are zero otherwise null. For example:
#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;
}
Output: 0 0 0.000000 (null)
3. A same static variable can be declared many times but we can initialize at only one time. For example:
(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
Output: Compilation error: Multiple initialization variable i.
4. We cannot write any assignment statement globally. For example:
#include <stdio.h>
static int i=10; //Initialization statement
i=25; //Assignment statement
int main(){
printf("%d",i);
return 0;
}
Output: Compilation error
Note: Assigning any value to the variable at the time of declaration is known as initialization while assigning any value to variable not at the time of declaration is known assignment.
(b)
#include <stdio.h>
static int i=10;
int main(){
i=25; //Assignment statement
printf("%d",i);
return 0;
}
Output: 25
(5) A static variable initializes only one time in whole program. For example:
#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
7. If declared a static variable or function globally then its visibility will only the file in which it has declared not in the other files. For example:
(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
Step 1: Write above two codes in the file named as one.c and two.c (You can give any name as you like) and save it.
Step 2: In Turbo c++ IDE click on Project -> Open projectmenu as shown in following screen dump.
Step 3: After Clicking on open project you will get following screen:
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.
Step 4: After pressing OK button you will get following screen:
Now click on Project -> Add item menu.
Step 5: After clicking Add item you will get following screen:
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
Step 6: At the end click on Done button. After clicking on done button you will get following screen:
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.
Output: Compilation error: Unknown symbol i and j.
Hence we can say variable i and j which has initialized into two.c is not visible in file one.c. This example proves visibility of globally declared static variable is file.
Note: In the above example function sum which was declared and defined in two.c has also storage class extern. So we can call from other file (one.c).If it will static then we cannot call function sum since static storage class is only visible to the file where it has declared.
(8)If we static variable has declared locally or globally its scope will always whole the program. For example:
(a) //locally declaration of static variable
#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
Explanation: When program control will come out of inner block where variable a has declared then outside of inner block variable a is not visible but its scope is outside the program i.e. variable a hasn’t dead .If with help of goto statement control again comes inside the inner block it prints previous incremented values which was not possible in case of auto or register variables.
(b)
//Locally declarations of variable
There are two c source code files:
//one.c
#include<stdio.h>
#include<conio.h>
void main(){
int i;
for(i=0;i<3;i++){
{
static int a=5;
printf("%d\n",a);
a++;
}
visit();
}
getch();
}
//two.c
#include<stdio.h>
void visit(){
printf("Don’t disturb, I am learning storage class");
/* printf("%d",a); Variable a is not visible here but
It is alive. */
}
Now compile and execute both files together:
Output:
5
disturb, I am learning storage class
6
disturb, I am learning storage class
7
disturb, I am learning storage class
Explanation: When control goes to another file and comes even that variable didn’t dead and it prints previous incremented value.
Note: In both examples if you will declare static variable globally you will get same output.
9. A static variables or functions have internal linkage. An internal linkage variables or functions are visible to the file where it has declared.
Answer
You can modify constant variable with the help of pointers. For example:
6. What is the meaning of scope of a variable?
#include<stdio.h>
int main(){
int i=10;
int *ptr=&i;
*ptr=(int *)20;
printf("%d",i);
}
Output: 20
Answer
Meaning of scope is to check either variable is alive or dead. Alive means data of a variable has not destroyed from memory. Up to which part or area of the program a variable is alive, that area or part is known as scope of a variable.
Note: If any variable is not visible it may have scope i.e. it is alive or may not have scope. But if any variable has not scope i.e. it is dead then variable must not to be visible.
There are four type of scope in c:
1. Block scope.
2. Function scope.
3. File scope.
3. Program scope.
Answer
8. What is NULL pointer?
struct ABC{
int a;
float b;
char c;
};
void main(){
struct ABC *ptr=(struct ABC *)0;
ptr++;
printf("Size of structure is: %d",*ptr);
}
Answer
9. What is size of void pointer ?
Literal meaning of NULL pointer is a pointer which is pointing to nothing. NULL pointer points the base address of segment.
Examples of NULL pointer:
1. int *ptr=(char *)0;
2. float *ptr=(float *)0;
3. char *ptr=(char *)0;
4. double *ptr=(double *)0;
5. char *ptr=’\0’;
6. int *ptr=NULL;
(q) What is meaning of NULL?
Answer:
NULL is macro constant which has been defined in the header file stdio.h, alloc.h, mem.h, stddef.h and stdlib.h as
#define NULL 0
Examples:
(1)What will be output of following c program?
#include "stdio.h"
void main(){
if(!NULL)
printf("I know preprocessor");
else
printf("I don't know preprocessor");
}
Output: I 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"
void main(){
int i;
static int count;
for(i=NULL;i<=5;){
count++;
i+=2;
}
printf("%d",count);
}
Output: 3
(3)What will be output of following c program?
#include "stdio.h"
void 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.
We cannot copy any thing in the NULL pointer.
Example:
(q)What will be output of following c program?
#include "string.h"
void main(){
char *str=NULL;
strcpy(str,"c-pointer");
printf("%s",str);
}
Output: (null)
Answer
10. What is difference between pass by value and pass by reference ?
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.
Answer
In c we can pass the parameters in a function in two different ways.
(a)Pass by value: In this approach we pass copy of actual variables in function as a parameter. Hence any modification on parameters inside the function will no reflect in the actual variable. For example:
#include<stdio.h>
void main(){
int a=5,b=10;
swap(a,b);
printf("%d %d",a,b);
}
void swap(int a,int b){
int temp;
temp =a;
a=b;
b=temp;
}
Output: 5 10
(b)Pass by reference: In this approach we pass memory address actual variables in function as a parameter. Hence any modification on parameters inside the function will reflect in the actual variable. For example:
#include<stdio.h>
void main(){
int a=5,b=10;
swap(&a,&b);
printf("%d %d",a,b);
}
void swap(int *a,int *b){
int *temp;
*temp =*a;
*a=*b;
*b=*temp;
}
Output: 10 5
No comments:
Post a Comment