2

This snippet is supposed to (according a textbook) print a 10 x 10 block of smiley faces.

#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;
}

Taken from Sams teach yourself C in one hour a day. All I'm getting is empty spaces (most probably a 10 x 10 block of spaces). How can I print a smiley face correctly?

I'm a using Cloud9 IDE workspace, which I believe creates a Linux environment.

Sagar Pandya
  • 9,323
  • 2
  • 24
  • 35
  • 10
    Maybe this textbook snippet is supposed to be running on DOS, using [Code page 437](https://en.wikipedia.org/wiki/Code_page_437), which indeed had a smiley face at position 1 (and 2). But this will not work on current OSes. – Karsten Koop Jul 05 '18 at 12:09
  • @KarstenKoop CP437 is a common setting for the cmd.exe console on Windows 10. Works fine for what it is. – Tom Blodget Jul 05 '18 at 16:37
  • It could depend on your source file character encoding (stated to your compiler as source-charset or similar), the execution charcter encoding (exec-charset or similar), the operating system, the console program and the user's configuration for it, the compiler vendor's standard C library support of encodings, and the compiler startup code (that what calls your `main`) and how it senses and sets up the C standard library to some of the preceeding. So, please [edit] to fill in some of these details. Yes, in CP437 character code 1 is displayed as ☺. Try `chcp` to see your console's encoding. – Tom Blodget Jul 05 '18 at 16:47
  • All of the above always applies, but due to similarities between character encodings, many get by without knowing and controlling them. So, unless you have a burning desire for ☺, you could just change 1 to 65, observe and understand (Google what `chcp` tells you), then the move on to the next exercise. – Tom Blodget Jul 05 '18 at 16:54
  • you might want to look at: [wideCharOutput](https://stackoverflow.com/questions/40590207/displaying-wide-chars-with-printf) – user3629249 Jul 05 '18 at 22:20

1 Answers1

2

You are trying to print the ascii value 1 which is SOH (start of heading)

If you want to print '1' try using printf("%c", '1');

Killian G.
  • 370
  • 2
  • 15