Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Assignment operator += usage with earlier declared vs declared variable?

I am new to C and got a little confused. I have code where I am using += operator with a variable when declared but it is giving me an error for this operator, but working fine when used inversely i.e. =+. Please explain why?

Here is the code:

  int i = 0;
  int g = 99;
  do
  {
    int  f += i;   //Here += is throwing error and =+ is working fine why?
    printf("%-6s = %d ","This is",f);
    i++;
  } while(i<10);
Error as 
ff.c:16:11: error: invalid '+=' at end of declaration; did you mean '='?
    int f += i;
          ^~
          =
1 error generated.

It’s working this way:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  int i = 0;
  int g = 99;
  do
  {
    int  f =+ i;   //Here += is `your text throwing error and =+ is working fine why?
    printf("%-6s = %d ","This is",f);
    i++;
  } while(i<10);

>Solution :

The definition

int f =+ 1;

is really the same as

int f = +1;

That is, you’re initializing f with the value +1.

When initializing a variable on definition, you can only initialize it, not modify it.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading