top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Write a c program to modify the constant variable in c?

+1 vote
3,071 views
Write a c program to modify the constant variable in c?
posted Jun 1, 2015 by Mohammed Hussain

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
Is your question correct ?

3 Answers

+2 votes

It is not a good programming to do that.
But still if you want then using POINTER you can do that.

answer Jun 2, 2015 by Chirag Gangdev
+2 votes

It is a bad programming, because you wanted to change the property of constant itself.
Its not a good advise to change the const variable value if you wanted to change the variable value then why you are using const use a normal variable.

Anyways you can do it by using a pointer.
For example:

int main()
{
    const int i = 1;
    int *ptr = &i;

    printf("value of i before changing = %d\n", i);

    *ptr = 2;
    printf("value of i after changing =  %d\n", i);
    return 0;
}

output will be:
value of i before changing = 1
value of i after changing = 2

answer Jun 2, 2015 by Arshad Khan
+1 vote

You can modify constant variable with the help of pointers.

For example:

#include<stdio.h>
int main(){
    int i=10;
    int *ptr=&i;
    *ptr=(int *)20;
    printf("%d",i);
    return 0;
}

Output: 20

answer Jun 2, 2015 by Manikandan J
Similar Questions
+2 votes

I assume that constant must be initialized to a constant value at compile time, but following code the return value of ge_const() is assigned to x which will be collected at run time. So this must cause an error but I am getting the output as 50. Can someone clarify the detail?

main()
{
    const int x = get_const();
    printf("%d", x);
}
int get_const()
{
    return 50;
}
+7 votes

Can anybody suggest how can we create a program in C to know the last modification date of any file?

...