-1

I'm using the Ch C compiler/environment to learn C. I note that when I want to redeclare variables with the same name I get an error:

/Users/srm> int c = 1
/Users/srm> c++
/Users/srm> c
/Users/srm> 2
/Users/srm> int c = 3
ERROR: identifier 'c' redeclared
ERROR: invalid lvalue of assignment operation

Is this is a restriction imposed by Ch or a C language feature?

srm
  • 548
  • 1
  • 4
  • 19
  • 1
    Just use `c = 3`... – l'L'l Feb 01 '17 at 19:02
  • http://stackoverflow.com/questions/21275992/redeclaration-of-global-variable-vs-local-variable – greedy52 Feb 01 '17 at 19:03
  • If you want to learn C, use a C compiler. It is not clear if ch actually supports standard C or just some older version, possibly the long time outdated C90 only. And if you want to know if something is standard, get a good C book and have the standard under your pillow. C is not a language for trial&error learning. – too honest for this site Feb 01 '17 at 19:37

1 Answers1

1

You need to write just c = 3, as far as I understand.

Asalle
  • 1,239
  • 19
  • 41
  • So this means I cannot change the data type of a variable, once declared. – srm Feb 01 '17 at 19:03
  • You can if you use a language that allows that. – Xofo Feb 01 '17 at 19:04
  • 1
    @srm If you have a nested scope (you introduce a new scope with braces), you can shadow a variable - essentially, declare a new variable with the same name, and it can have a different type. But the shadowed variable will be inaccessible until you return to the outer scope. – Kevin Feb 01 '17 at 19:07
  • as @Kevin explained, it's all about variable scopes in C. Google it or visit [this page](https://en.wikibooks.org/wiki/C%2B%2B_Programming/Scope/Examples) – Asalle Feb 01 '17 at 19:10
  • @Kevin: Shadowing any name is bad practice and should never be done intentionally. – too honest for this site Feb 01 '17 at 19:39
  • @Olaf Definitely agreed, but shadowing is the closest thing to what the question was asking about. I'm just saying it's possible, not that it's a good thing to do. – Kevin Feb 01 '17 at 19:46
  • @Kevin: It is never wrong to be very explicit about bad practice. Remember this is read mistly by beginners (experience programmers should be aware of it already). – too honest for this site Feb 02 '17 at 19:10