int main(){
int a = 10;
++a = 20; // works
printf("a = %d", a);
getchar();
return 0;
}
This is a c Language.
Explaining Line by line
int main() THIS line define an entry function called main which is expected to return a type integer(int)
int a = 10 declares a variable integer whose value is 10;
++a = 20; AT this point your code is incrementing the value of a by 1 before any operation is performed on a.
This means that, the value of a is incremented by 1 bfore a is assign the value of 20;....
the statement ++a = 20 is incorect in the sense that, a is 10 initially, and you increment it to 11 . Is like saying 11 = 20; this may not throw an error because the code line is not useful.
printf() is a c-method to print file to screen, passing a string "a = %d" tells the compiler to print a to a decimal number(%d).
getchar() is use to terminate the running program and return 0 is to forcefully tell the operating system that the code run successfully and return integer value 0.
int main(){
int a = 10;
a++ = 20; // error
printf("a = %d", a);
getchar();
return 0;
}
This didn't at a++ = 20 because , right value increment perform the operation before it increment unlike ++a who increment before performing operation.
So this isn't possible and it never works because a is already 10, and you are saying the value ++ and assign 20, you can't assign a value to it, it should be the one calculating its own value. So the compiler will want to interprete is as variable a++ = 20, and ++ can not be a vaiable name. That's why it never work.
What is the essence of incrementation and decrementation in c....its useful for creating conditional statement e.g for loop, while statement etc. for example:
for(int i = 0; i < 4; i++)
{
printf('THis is c-language');
}
or
int i = 0;
while(i < 4){
printf('THis is c-Language');
i++;
}
so therefore, following the programming rules and regulations, you are not allow to assign value to either a++ or ++a
they are for the compilers to manipulate, so never assign value to them.
Thank you.