GP32 Pinkspiders N00b C Help :)


PinkSpider

Shit.
Joined
Jan 1, 2004
Messages
3,507
Age
38
Location
Hull.
Website
www.giglio1.karoo.net
Anyways im trying to do a while loop. Ive done them in other languages (VB+Pascal) and I probably did them in Java hehe.

Anyways I have a string and in the tutorial its simple comparing of an integer which is easy. I wanna make it so if a string is blank (="") then it does it again.

In VB it would go: (omg its so long since I did this lol)
while x = ""

do
I think :D
 
while( !strcmp( x, "" ) ) {
...
}

or

while( x[0] == '\0' ) {
...
}
 
In C when you do a comparison the bloc of instructions will be executed if the bool value between () is different of 0 (so, if it is true) and when you don't do any comparison it automatically compare with '\0' which in fact refers to "" like '\t' to tabulations, '\n' to nextline,etc. And you can also compare your char with the ASCII value of "" which is 0 (without ')

while( x[0]==0 ) {
...
}

or

while( x[0] ) {
...
}

But I'm a noob too so may be i said something that is wrong.
 
Last edited by a moderator:
Just to be really picky, the end-of-string terminator doesn't always have to be zero, but the char '\0' is guaranteed to be the end-of-string terminator. So ReVaX's example of
Code:
while( x[0] == '\0' ) {
...
}
is technically the most correct solution.

The terminator would only change if you changed system or compiler, and nowadays it's pretty much universal to use zero, so in reality it makes no difference but just in case, it's best to do The Right Thing (tm) :D
 
Back
Top