2021-01-28

A constant being assigned to another constant without errors

I'm currently learning about pointers and the concept of pass-by-value, and I have this C code block:

void doSomething(int b){
    b = 6;
    printf("%d", b);
}
    
int a = 5;

int main(void){
    doSomething(a);
    printf("%d",a);
    return 0;
}

I should get the output 65 with no errors on compilation nor on execution. By tracing the code, here is how I'm seeing it:

  • Integer a is assigned the value 5.
  • Since C is strictly pass-by-value, doSomething(a) == doSomething(5).

Now prior to running the line b = 6;, I'm fairly certain that b == 5. So by running the line, the program is effectively reading:

5 = 6;

A constant (for a lack of a better term on my part) is being assigned to another constant. In Python this would have failed with a syntax error, and it makes sense to have an error. Why doesn't it raise a syntax error?



from Recent Questions - Stack Overflow https://ift.tt/3agmJSO
https://ift.tt/eA8V8J

No comments:

Post a Comment