Click to See Complete Forum and Search --> : New to C, printf() problem.


aaronk42
01-22-2004, 01:19 PM
Hi. I'm writing a program that's reading characters from a file, formatting them, and just outputting them to the screen. (Like I said, new to C.)

Here's a piece of code:

while (ch != EOF)
{
printf("%s", &ch);
ch = fgetc(infile);
}

My program works perfectly EXCEPT that it's printing a space after every character. So rather than

*Blue pencil*

as output, I get

* B l u e p e n c i l *

Annoying. Suggestions? Thanks.

Stuka
01-22-2004, 01:25 PM
Your format string (%s) is for a string, so the space is likely being added by printf intentionally. Try %c, for a character.

mwinterberg
01-22-2004, 04:42 PM
In addition to what Stuka said, you'll also want to change the second parameter of printf to just "ch", so printf doesn't print the character value of (part of) the address of ch.

-Michael

bwkaz
01-22-2004, 08:39 PM
But you will want to make sure the type of "ch" is still an int, so that you can detect EOF. If it's a char and not an int, then EOF is the same as the char value 255 (which can be held in your file, and will screw up your loop if it is).

Which also means you'll probably have to cast ch to a char before passing it to printf.