简体   繁体   中英

Print out character in c

I'd like to be able to print out the value of a charcter in c as follows:

fprintf("%c", alphabet[val]);

, where alphabet is initialized as

for(i = 0; i < 26; i++){
    alphabet[i] = i + 65;
}

However, this line gives the following errors:

encode.c: In function ‘main’:
encode.c:76:13: warning: passing argument 1 of ‘fprintf’ from incompatible pointer type [-Wincompatible-pointer-types]
     fprintf("%c", alphabet[val]);
             ^
In file included from encode.c:1:0:
/usr/include/stdio.h:356:12: note: expected ‘FILE * restrict {aka struct _IO_FILE * restrict}’ but argument is of type ‘char *’
 extern int fprintf (FILE *__restrict __stream,
            ^
encode.c:76:19: warning: passing argument 2 of ‘fprintf’ makes pointer from integer without a cast [-Wint-conversion]
     fprintf("%c", alphabet[val]);
                   ^
In file included from encode.c:1:0:
/usr/include/stdio.h:356:12: note: expected ‘const char * restrict’ but argument is of type ‘char’
 extern int fprintf (FILE *__restrict __stream,
            ^
encode.c:76:5: warning: format not a string literal and no format arguments [-Wformat-security]
     fprintf("%c", alphabet[val]);
     ^

How can I print out a character like this?

Check your code closely. For the statement

 fprintf("%c", alphabet[val]);

where is the file pointer? It's missing. You have to provide the file pointer, to which you want to see the output, something like

      fprintf(fp, "%c", alphabet[val]);  

where, fp is the file pointer (of type FILE * ), usually returned by fopen() , check the man page for more details on this.

In case, you want the prints to arrive on stdout , ie, the standard output, use printf() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM