0

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.

paulsm4
  • 114,292
  • 17
  • 138
  • 190
Omkar
  • 59
  • 1
  • 9
  • 6
    I would suggest you to find [a good book about C programming](https://en.wikipedia.org/wiki/The_C_Programming_Language) and start studying the language before attempting to write any code. – Marco Bonelli Aug 12 '21 at 02:56
  • You're welcome, another good list of books is here: https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list – Marco Bonelli Aug 12 '21 at 02:59
  • I wonder how helpful coding community is and once again Thank you very much – Omkar Aug 12 '21 at 04:52

1 Answers1

2

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);
}
paulsm4
  • 114,292
  • 17
  • 138
  • 190