Go Down

Topic: student C question (Read 1 time) previous topic - next topic


Jan 08, 2010, 02:17 pm Last Edit: Jan 08, 2010, 02:18 pm by jscott13 Reason: 1
Good day friends!

As promised I have begun my studies in earnest. I have begun to study C and have had a great deal of fun learning. It is much easier to understand what I am doing with the Arduino now that I've gotten a little background. Enough blabber...

For all I can determine the operators +1 and ++ are interchangeable. Is that a correct assumption? I have not found an answer in two days of study. The code follows:

Code: [Select]
#include <stdio.h>

#define START 65
#define END 90

int main()

long int humongous;
char chuck;

    chuck = (char)humongous;

As the code stands it prints the English alphabet to the screen (holly cow!) This is what it was intended to do (I hope). However, if you change the ++ after humongous to a +1 you get the letter A printed continuously. Can someone help me understand why?

Thanks in advance to all of you kind folks that come to my rescue!



Jan 08, 2010, 02:23 pm Last Edit: Jan 08, 2010, 02:24 pm by GrooveFlotilla Reason: 1
++ has an implied assignment, so "a++" takes the value of "a" increments it, and puts the result back in "a".

"a+1" simply adds one to the value of "a", evaluates the result as either zero or non-zero, and does absolutely nothing, because the result is not assigned back to "a"

What you want is "a+=1;" == "a = a + 1;" == "a++;" and mostly == "++a;"
Per Arduino ad Astra


Jan 08, 2010, 02:26 pm Last Edit: Jan 08, 2010, 02:27 pm by Grumpy_Mike Reason: 1
The +1 does not store the value back in the variable.
is the same as
humongous = humongous +1
is the same as
humongous += 1

humongous +1
evaluates to the value plus one but the value of humongous is left unchanged and so next time evaluates to the same value.


Jan 08, 2010, 03:14 pm Last Edit: Jan 08, 2010, 03:18 pm by stephen_t Reason: 1
is the same as
humongous = humongous +1

I thought so too, but I spent 2 days chasing a bug which involved getting wierd outputs from incrementing a byte variable with the ++ bit.  When finally out of desperation I switched it to the latter, it sprang to life and worked as expected.

When I unhooked the arduinos from the sensors it was wired to it gave expected results either way.  Complete baffleball......



Thank you all very much!  :D

It drove me crazy for a while. It's these little things that get ya sometimes.

I can't tell you how thankful I am that you guys are out there for all of us. Teaching yourself can be difficult. Without you guys, maybe for me, impossible.

Thanks for the answer, and being there to make this form of education possible.


getting wierd outputs from incrementing a byte variable with the ++ bit

Bytes will wrap round to negative values at values greater than 127 was that it?


Jan 10, 2010, 10:48 am Last Edit: Jan 10, 2010, 10:50 am by stephen_t Reason: 1
Bytes will wrap round to negative values at values greater than 127 was that it?

Nope, the byte was set to 0, and then incremented in a 20 iteration for loop with and an 'if' directive inside it.  Worst case senario was it would get to 20.  It would often return a value of 255.  Just changing 'variable ++' to 'variable = variable + 1' cured it.  It displayed exactly the same problem if I used unsigned long instead of byte (but not if I used float). I've put it down to an obscure bug somewhere.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131