Pages: [1]   Go Down
 Author Topic: student C question  (Read 735 times) 0 Members and 1 Guest are viewing this topic.
Massachusetts, USA
Offline
Jr. Member
Karma: 0
Posts: 53
Things fall apart, it's scientific
 « on: January 08, 2010, 08:17:15 am » Bigger Smaller Reset

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:
#include <stdio.h>

#define START 65
#define END 90

int main()

{
long int humongous;
char chuck;

for(humongous=START;humongous<=END;humongous++)
{
chuck = (char)humongous;
putchar(chuck);
}
putchar('\n');
return(0);
}

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!

Scott
 « Last Edit: January 08, 2010, 08:18:32 am by jscott13 » Logged

UK
Offline
Karma: 17
Posts: 2884
Gorm deficient
 « Reply #1 on: January 08, 2010, 08:23:16 am » Bigger Smaller Reset

++ 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;"
 « Last Edit: January 08, 2010, 08:24:24 am by GrooveFlotilla » Logged

Manchester (England England)
Online
Brattain Member
Karma: 602
Posts: 33373
Solder is electric glue
 « Reply #2 on: January 08, 2010, 08:26:55 am » Bigger Smaller Reset

The +1 does not store the value back in the variable.
So
humongous++
is the same as
humongous = humongous +1
is the same as
humongous += 1

but:-
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.
 « Last Edit: January 08, 2010, 08:27:53 am by Grumpy_Mike » Logged

Lancashire, UK
Offline
Edison Member
Karma: 9
Posts: 1991
 « Reply #3 on: January 08, 2010, 09:14:17 am » Bigger Smaller Reset

Quote
humongous++
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......

 « Last Edit: January 08, 2010, 09:18:01 am by stephen_t » Logged

Massachusetts, USA
Offline
Jr. Member
Karma: 0
Posts: 53
Things fall apart, it's scientific
 « Reply #4 on: January 08, 2010, 09:38:37 am » Bigger Smaller Reset

Thank you all very much!

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.
 Logged

Manchester (England England)
Online
Brattain Member
Karma: 602
Posts: 33373
Solder is electric glue
 « Reply #5 on: January 08, 2010, 06:44:10 pm » Bigger Smaller Reset

Quote
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?
 Logged

Lancashire, UK
Offline
Edison Member
Karma: 9
Posts: 1991
 « Reply #6 on: January 10, 2010, 04:48:10 am » Bigger Smaller Reset

Quote
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.
 « Last Edit: January 10, 2010, 04:50:25 am by stephen_t » Logged

 Pages: [1]   Go Up