int x,y; int main( void ) { for ( x =...">

Why printf ("% c", 1) returns a smiley instead of an encoded char for 1

This is my code.

#include <stdio.h> int x,y; int main( void ) { for ( x = 0; x < 10; x++, printf( "\n" ) ) for ( y = 0; y < 10; y++ ) printf( "%c", 1 ); return 0; } 

It returns emoticons. I searched everywhere for emoticon code or code for 1, but I could not find any links or any explanation why the char value for 1 returns the emoticon when the ascii code for 1 is SOH. I researched the answers to this question, but I did not find the answers explaining why this is happening.

+6
source share
3 answers

The output depends on different terminals. For example, on my terminal, by default, OS X does not display any characters.

In your case, is inferred presumably due to some historical reasons . In short, this is because codepage 437, which maps byte 0x01 to U+263A , is an MS-DOS character set.

+3
source

Because 1 not a printed character code. If you want '1' , you need to write it with a character literal:

  printf( "%c", '1' ); // ^^^ 
+2
source

If you use a number, you will select the character number from the ASCII table; if you use char , you will find char.

Example: this code prints the ASCII character number 65:

 printf( "%c", 65 ); // Outputs: A 

This code prints the letter A:

 printf( "%c", 'A' ); // Outputs: A 
+2
source

All Articles