views:

2089

answers:

7

When printing a single character in a C program, must I use "%1s" in the format string? Can I use something like "%c"?

+9  A: 

yea, %c will print a single char:

printf("%c\n", 'h');

also, putchar/putc will work too. From "man putchar":

#include <stdio.h>

int fputc(int c, FILE *stream);
int putc(int c, FILE *stream);
int putchar(int c);

* fputc() writes the character c, cast to an unsigned char, to stream.
* putc() is equivalent to fputc() except that it may be implemented as a macro which evaluates stream more than once.
* putchar(c); is equivalent to putc(c,stdout).

EDIT:

also note, that if you have a string, to output a single char, you need get the character in the string that you want to output. For example:

const char *h = "hello world";
printf("%c\n", h[4]); /* output's and 'o' character */
Evan Teran
+1  A: 

Yes, %c should work http://linux.die.net/man/3/printf

Dana the Sane
A: 

I am using MS C 6.0 and it does not work. Gives no error either.

Aydya
Could you post some non-working sample code? Make sure the 'c' is lowercase in the '%c'.
Adam Rosenfield
did you try copy and pasting my example. %c should work given that the parameter is of the correct type (a char, not a string).
Evan Teran
+5  A: 

As mentioned in one of the other answers, you can use putc(int c, FILE *stream), putchar(int c) or fputc(int c, FILE *stream) for this purpose.

What's important to note is that using any of the above functions is from some to signicantly faster than using any of the format-parsing functions like printf.

Using printf is like using a machine gun to fire one bullet.

Roalt
+2  A: 

Be careful of difference between 'c' and "c"

'c' is a char suitable for formatting with %c

"c" is a char* pointing to a memory block with a length of 2 (with the null terminator).

Douglas Leeder
Technically, "c" is a char* with a length of 4 (or whatever your pointer size is) that points to a block of memory with 2 characters in it ('c' and '\0'). But that's just being pedantic.
paxdiablo
Added a brief note that "c" is a pointer.
Douglas Leeder
A: 
char variable = 'x';  // the variable is a char whose value is lowercase x

printf("<%c>", variable); // print it with angle brackets around the character
EvilTeach
A: 

hmmm i'm really stuck i'm trying to find lf/cr character in a string in a file. here's the section of code: as u can see there's a lot of code comented out. fgetc was causing a stack overflow for some reason am using cygwin to compile and run. sourceFile and destinationFile have already been opened. the bit that's not working is the for loop, c[] is the pointer to the string i'm grabbing and i'm trying to look for new line characters within it but can't work out how to do it. i've not done any C for about 10 years and i think i need to output the pointer's contents to an array then search that but i also am not exactly sure what the lf/cr character is in C think it is an \r or a \n. an help would be really really appreciated as i wanna get this functionality in the program. the putc line will just add a line number to the start of each line so i can identify the lines i need to change as i think i will have issues doing a string comparison as the line i need to idenify isn't unique. eventually this will write config files for WAPs :)

 else {
printf("File opened successfully. Contents:\n\n");
  char c[34];   
  while(fgets(c, 34, sourceFile)!=NULL) { 
   /* keep looping until NULL pointer... */
   printf("String: %s \n", c);
   fputs(c, destinationFile);
   /* print the file one line at a time  */
    for(i=0; i!=34; i++){
     printf("checking string %c\n",c[i]);
     if (c[i]="\n"){
      printf("found it!\n");
      fputc(line, destinationFile);
      line++;
     }
    }    
  }   
 printf("text grabbed\n");
 printf("text written\n");   
  fclose(sourceFile);  
  fclose(destinationFile);
 }