Exercise "I do not seek. I find."
- Pablo Picasso
C Programming - Const
Why C Programming Const?
In this section you can learn and practice C Programming Questions based on "Const" and improve your skills in order to face the interview, competitive examination and various entrance test (CAT, GATE, GRE, MAT, Bank Exam, Railway Exam etc.) with full confidence.
Where can I get C Programming Const questions and answers with explanation?
IndiaBIX provides you lots of fully solved C Programming (Const) questions and answers with Explanation. Solved examples with detailed answer description, explanation are given and it would be easy to understand. View the solution for the problems with feel and good user interface, easily go through all questions and answers.
How to solve C Programming Const problems?
You can easily solve all kind of C Programming questions based on Const by practicing the objective type exercises given below, also get shortcut methods to solve C Programming Const problems.
1.
What will be the output of the program?
#include<stdio.h>
int main()
{
int y=128;
const int x=y;
printf("%d\n", x);
return 0;
}
Answer: Option A
Explanation:
Step 1 : int y=128; The variable 'y' is declared as an integer type and initialized to value "128".
Step 2 : const int x=y; The constant variable 'x' is declared as an integer and it is initialized with the variable 'y' value.
Step 3 : printf("%d\n", x); It prints the value of variable 'x' .
Hence the output of the program is "128"
2.
What will be the output of the program?
#include<stdio.h>
#include<stdlib.h>
union employee
{
char name[15];
int age;
float salary;
};
const union employee e1;
int main()
{
strcpy(e1.name, "K");
printf("%s %d %f", e1.name, e1.age, e1.salary);
return 0;
}
A.
Error: RValue required B.
Error: cannot convert from 'const int * ' to 'int *const ' C.
Error: LValue required in strcpy D.
No error
Answer: Option D
Explanation:
The output will be:
K 75 0.000000
3.
What will be the output of the program?
#include<stdio.h>
int fun(int **ptr);
int main()
{
int i=10;
const int *ptr = &i;
fun(&ptr);
return 0;
}
int fun(int **ptr)
{
int j = 223;
int *temp = &j;
printf("Before changing ptr = %5x\n", *ptr);
const *ptr = temp;
printf("After changing ptr = %5x\n", *ptr);
return 0;
}
A.
Address of i Address of j B.
10 223 C.
Error: cannot convert parameter 1 from 'const int ** ' to 'int ** '
D.
Garbage value
Answer: Option C
Explanation:
No answer description available for this question. Let us discuss .
4.
What will be the output of the program?
#include<stdio.h>
int main()
{
const int x=5;
const int *ptrx;
ptrx = &x;
*ptrx = 10;
printf("%d\n", x);
return 0;
}
Answer: Option C
Explanation:
Step 1 : const int x=5; The constant variable x is declared as an integer data type and initialized with value '5'.
Step 2 : const int *ptrx; The constant variable ptrx is declared as an integer pointer.
Step 3 : ptrx = &x; The address of the constant variable x is assigned to integer pointer variable ptrx.
Step 4 : *ptrx = 10; Here we are indirectly trying to change the value of the constant vaiable x . This will result in an error.
To change the value of const variable x we have to use *(int *)&x = 10;
5.
What will be the output of the program?
#include<stdio.h>
int fun(int **ptr);
int main()
{
int i=10, j=20;
const int *ptr = &i;
printf("i = %5X", ptr);
printf("ptr = %d", *ptr);
ptr = &j;
printf("j = %5X", ptr);
printf("ptr = %d", *ptr);
return 0;
}
A.
i= FFE2 ptr=12 j=FFE4 ptr=24 B.
i= FFE4 ptr=10 j=FFE2 ptr=20
C.
i= FFE0 ptr=20 j=FFE1 ptr=30 D.
Garbage value
Answer: Option B
Explanation:
No answer description available for this question. Let us discuss .