# incrementing

HEy Quick Question

If i say…

static int i = 1;

i++;

Then it will increase by 1… how do i increase by 2… or maybe 3… would it be?

static int i = 1;

i++2;

… maybe? lol i ono talk to me:):):)

Just i = i + 2

i++ is a shortcut, otherwise just use the number you would like to add

You can use the ordinary mathematical operators in C. The syntax is:

name_of_var = [EXPRESSION]

You can string them together:

var1 = var2 = 3

sets both var1 and var2 to 3.

You can use +, -, *, divide, everything. Please note that with integers, divide just truncates. Also, there are a few little things C does. Like ++ adds one without the equals, – subtracts without it. varname += n will add the value n to varname. same for -=, *=.

Also, you can use the symbol % for remainder in division, or for modular arithmetic. Like 7%3=1, 56%10=6, 39%17=5.

If you need any more help, ask me. ari.allynfeuer@gmail.com

Ari.

i++ is shorthand for i = i + 1 since incrementing by one is such a common task in programming. Similarly i-- is the same as i = i -1. If you want to add or subtract something different then you have to use the long version as in i = i + 3.

Not many people realize you can also go ++i and --i which is subtly different. In most cases the end effect is the same but not always. With ++i (prefixed) the value is computed before the expression is evaluated. The postfix version i++ is the opposite where the value is computed AFTER the expression is evaluated. This doesn’t make any difference if you just go:

i++;
or
++i;

However if you try:

m = ((++i)*(j++)/5;

i will increment by 1 BEFORE the rest of the expression is evaluated. j will be incremented AFTER the expression is evaluated.

Good luck!

Awesome… thanxs for the tip:) more for my programming knowledge:)

i++ and its prefix version ++i only add one. There’s no way to increment by a different value using this notation.

To add 2 there are two ways to do it.

The “classic” programming construct
i = i + 2;

and
i += 2;

The second version can be more than just a slightly shorter way of writing the expression if you have a non optimizing compiler or one that doesn’t optimize very well.

To digress a little and provide some history, there are three ways to add 1 to a variable.

The increment operator in either postfix or prefix form: i++ or ++i

An in place add: i += 1

With a non optimizing compiler and a fairly rich native machine instruction set, these three forms will all generate different machine code and some forms will be more efficient. The programmer would chose the form to use based on knowing how the compiler would translate it.

For instance on an Intel x86 chip the increment operator maps directly to the assembly instruction INC so you do the increment in one instruction.

The second form generates machine code that directly adds the number to the variable. It’s longer since it first loads 1 into the accumulator and adds that to the variable directly. Two instructions, so not as efficient.

And finally, the third form causes the processor to load the variable i into the accumulator, add 1 to it, and then store the result back into i’s memory location. Even more instructions and slower execution.

On processors like the PICs in the IFI controllers the instruction set is much more limited so it’s not as likely to make as much difference. Also, modern optimizing compilers will typically find the “best” machine instructions to use. With some you can even tell them to optimize for smaller code size or faster execution. Sometimes, paradoxically, smaller code is also faster.

Easiest way to do it is just i+=2 or i=i+2

you can just do i+=2 or i+=3 not much diff, but hey! it is the same number of keys your pressing.

Here’s something few people know about C: pointer arithmetic scales integers by the size of the type being pointed to. For example, if ptr is a pointer to an int, then ptr++ will add 2 (or whatever the native size of an int is for a particular target). If parr is a pointer to a structure of twelve bytes, parr = parr + 3 will add 36.

But don’t abuse this fact. Pointer arithmetic is not necessarily something useful for programming PIC processors to control FRC robots.

*chuckle

well yeah theres lots of ways to say it, but it all compiles the same. personally, <operator>= is my favorite. but hey, thats just me.

see you all later… i’ll be rockin out to them xm seasonal channels

-Q

That’s true, however using a pointer that way for general arithmentic and casting it back to an int type would definitely be abusive and the programmer need not apply for a job in my shop.

One place I’ve used pointer arithmetic along those lines with the PIC is to be able to treat the PWM outputs as an array. Although there the increment in address defaults back to 1.