I'm brand new to C and am trying to learn how to take a string and print it using a function. I see examples everywhere using while(ch = getchar(), ch >= 0)
, but as soon as I put it into a function (instead of main()), it ceases to work. Right now, it's stuck in an endless loop... why is that?
// from main():
// printString("hello");
void printString(char *ch)
{
while (*ch = getchar(), *ch >= 0)
putchar(*ch);
}
Based on your description, you just want:
void printString(char *ch)
{
while(*ch) {
putchar(*ch);
ch++;
}
}
Your original function:
void printString(char *ch)
{
while (*ch = getchar(), *ch >= 0)
putchar(*ch);
}
Does a lot of stuff:
ch
(this might not even work if you pass in a string literal.