Search code examples
ccompiler-errors

how to overwrite the value of a variable in c


Here I want to change the change the value of i but getting error as: error: redefinition of 'i' int i=4; ^ exp.c:5:9: note: previous definition is here int i=4;

#include<stdio.h>
#include<math.h>

int main(void){
    int i=4;
    printf("before:%i",i);
    
    int i=5;
    printf("after:%i",i);
    
    
}

So my question is how to overwrite the value of a variable which already value assigned.


Solution

  • You may define a variable only once.

    You can (usually!) (re)assign a value to a variable as many times as you want:

    int main(void){
        int i=4;     // Declare as "int" and assign value "4"
        printf("before:%i",i);
        
        i=5;  // Assign a different value
        printf("after:%i",i);
    }