C Programming - Const - Discussion
Discussion Forum : Const - Find Output of Program (Q.No. 8)
8.
What will be the output of the program (in Turbo C)?
#include<stdio.h>
int fun(int *f)
{
*f = 10;
return 0;
}
int main()
{
const int arr[5] = {1, 2, 3, 4, 5};
printf("Before modification arr[3] = %d", arr[3]);
fun(&arr[3]);
printf("\nAfter modification arr[3] = %d", arr[3]);
return 0;
}
Answer: Option
Explanation:
Step 1: const int arr[5] = {1, 2, 3, 4, 5}; The constant variable arr is declared as an integer array and initialized to
arr[0] = 1, arr[1] = 2, arr[2] = 3, arr[3] = 4, arr[4] = 5
Step 2: printf("Before modification arr[3] = %d", arr[3]); It prints the value of arr[3] (ie. 4).
Step 3: fun(&arr[3]); The memory location of the arr[3] is passed to fun() and arr[3] value is modified to 10.
A const variable can be indirectly modified by a pointer.
Step 4: printf("After modification arr[3] = %d", arr[3]); It prints the value of arr[3] (ie. 10).
Hence the output of the program is
Before modification arr[3] = 4
After modification arr[3] = 10
Discussion:
11 comments Page 1 of 2.
Neha_rao@yahoo.com said:
7 years ago
I executed this program. And I found error as type mismatch and type convert.
And if I was putting construction keyword with function fun () then it didn't give error. And my idea is turbo C.
And if I was putting construction keyword with function fun () then it didn't give error. And my idea is turbo C.
Ratan Lal said:
9 years ago
In GCC, it will run with following two warnings
1. Passing argument 1 of 'fun' discards 'const' qualifier from pointer target type [enabled by default]
fun(&arr[3]);
^
2. Expected 'int *' but argument is of type 'const int *'
int fun(int *f)
^
The output as follow:
Before modification arr[3] = 4.
After modification arr[3] = 10.
1. Passing argument 1 of 'fun' discards 'const' qualifier from pointer target type [enabled by default]
fun(&arr[3]);
^
2. Expected 'int *' but argument is of type 'const int *'
int fun(int *f)
^
The output as follow:
Before modification arr[3] = 4.
After modification arr[3] = 10.
Naveen said:
9 years ago
Question mentioned in turbo C. So A is the correct answer.
Sandy said:
9 years ago
Guys what's the use of const int arr[] in this program?
The value is anyway changes strangely.
The value is anyway changes strangely.
Dmitriy said:
1 decade ago
MS VS 2010 compiler:
Error 1 error C2664: 'fun' : cannot convert parameter 1 from 'const int *' to 'int *'.
Error 1 error C2664: 'fun' : cannot convert parameter 1 from 'const int *' to 'int *'.
Siva said:
1 decade ago
Yes you are correct friends. It gives error. The conversion from "const int" to "int" not supt in linux c compiler. It depends on compiler definition.
Parmanand Soni said:
1 decade ago
Its Gives error like-cannot convert parameter from "const int *" to "int *"
so ans. is B.
so ans. is B.
Keerthi said:
1 decade ago
The const int cannot modified by pointer. so, the option B is correct.
Error:
cannot convert parameter from "const int *" to "int *"
Error:
cannot convert parameter from "const int *" to "int *"
Sundar said:
1 decade ago
It gives the following output in Turbo C (under DOS)
Before modification arr[3] = 4
After modification arr[3] = 10
It may give 'Segmentation fault' error in GCC (under Linux).
Before modification arr[3] = 4
After modification arr[3] = 10
It may give 'Segmentation fault' error in GCC (under Linux).
Himanshu said:
1 decade ago
You are right mohini it is not strange but wrong cause when we execute it, it gives two errors.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers