Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

heretiic

macrumors newbie
Original poster
May 24, 2009
5
0
Hi there I am trying to print out all ASCII signs on my macbook.

This is the code I made:

Code:
#include "test.h"
#include <stdlib.h>
#include <stdio.h>
 
int main() 
{
	unsigned int i;
	for(i = 0; i <= 255; i++){
		unsigned char c = i;
 	printf("HEJSAN %c = %d \n",c,c);
 	
	}

  
  return;
}

When i gcc it and then runs it, this is the prints i get:
(ill just show the last)
HEJSAN ? = 244
HEJSAN ? = 245
HEJSAN ? = 246
HEJSAN ? = 247
HEJSAN ? = 248
HEJSAN ? = 249
HEJSAN ? = 250
HEJSAN ? = 251
HEJSAN ? = 252
HEJSAN ? = 253
HEJSAN ? = 254
HEJSAN ? = 255
Signs up until 127 works but everything after that is printed as "?", why is that? Asked a couple of friends and none of them has this problem with their macbook.

Best regards
Alex
 

heretiic

macrumors newbie
Original poster
May 24, 2009
5
0
SilentPanda: maybe. but if i print a line with something like a å ä or ö it works for me ( printf("åäö"); ) works... Why is that? And I made another program

Code:
#include <stdlib.h>
#include <stdio.h>

int main(void)
{
    int i;
    unsigned char c;

    while(1)
    {
        c = getchar();

        if (c != (char)10)
        {
            printf("%3d = %c\n", (int) c, c);
        }
    }
}

It tell me what ascii value that a char has. for ex: a = 97 but if i enter ö it tells me:
195 = ?
182 = ?
This is wierd :S
 

SilentPanda

Moderator emeritus
Oct 8, 2002
9,992
31
The Bamboo Forest
I'm guessing (gonna look into it more in a bit) that %c only prints signed chars? So when you pass a number hgiher than 127 to %c it goofs it up. I'm not really a C programmer... :(
 

heretiic

macrumors newbie
Original poster
May 24, 2009
5
0
Ok, thank you :)
Is there an alternative to %c for printing unsigned chars?
 

lloyddean

macrumors 65816
May 10, 2009
1,047
19
Des Moines, WA
Hi there I am trying to print out all ASCII signs on my macbook.

<- snipped ->

Best regards
Alex

This is as it should be.

From the wikipedia article at <http://en.wikipedia.org/wiki/ASCII>.


ASCII includes definitions for 128 characters: 33 are
non-printing, mostly obsolete control characters that affect
how text is processed;[6] 94 are printable characters, and the
space is considered an invisible graphic.

Anything beyond character 127 is NOT part of the standard and
would result in output that varies from platform to platform.
dependent.

With that said:

Code:
#include <stdlib.h>
#include <stdio.h>

int main(void)
{
    int c;
	
    printf("Control-D to stop!\n");
    while ( (c = getchar()) != EOF )
    {
        if ( c != (char)10 )
        {
            printf("%3d = %c\n", (int) c, c);
        }
    }
}
 

heretiic

macrumors newbie
Original poster
May 24, 2009
5
0
Ok. Is it possible to print all "wierd" signs in any other way? I mean besides ascii if that doesnt work.
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
Ok. Is it possible to print all "wierd" signs in any other way? I mean besides ascii if that doesnt work.

Most likely your compiler and the console assume that you are using UTF8, not ASCII. Try printing strlen ("ö"). Lookup UTF8 on Google, wikipedia has a decent article.
 

Guiyon

macrumors 6502a
Mar 19, 2008
771
4
Cambridge, MA
Your shell and terminal app would need to support Unicode but here is an example using wprintf which will print some Unicode charsets:

Code:
#include <stdio.h>
#include <wchar.h>
#include <locale.h>

int main( void ) {
  wchar_t val = 0x20;
  
  setlocale( LC_ALL, "en_US.UTF-8" );
  
  printf( "Printing the (printable) US-ASCII charset:\n" );
  for( ; val < 0x7F; val++ ) {
    wprintf( L"Character 0x%3.3X is %lc\n", val, val );
  }
  
  printf( "Printing Latin-1 Supplement charset:\n" );
  for( val = 0x80; val <= 0xFF; val++ ) {
    wprintf( L"Character 0x%3.3X is %lc\n", val, val );
  }
  
  printf( "Printing Latin Extended-A charset:\n" );
  for( val = 0x100; val <= 0x17F; val++ ) {
    wprintf( L"Character 0x%3.3X is %lc\n", val, val );
  }

  printf( "Printing Latin Extended-B charset:\n" );
  for( val = 0x180; val <= 0x24F; val++ ) {
    wprintf( L"Character 0x%3.3X is %lc\n", val, val );
  }
  
  printf( "Printing Cyrillic charset:\n" );
  for( val = 0x400; val <= 0x4FF; val++ ) {
    wprintf( L"Character 0x%3.3X is %lc\n", val, val );
  }
  
  return 0;
}
 

ChrisA

macrumors G5
Jan 5, 2006
12,576
1,691
Redondo Beach, California
Hi there I am trying to print out all ASCII signs on my macbook.

....
Signs up until 127 works but everything after that is printed as "?", why is that? Asked a couple of friends and none of them has this problem with their macbook.

ASCII is a 7-bit code. There are only 127 character defined.

However you may not be printing ASCII. From the terminal type the command "locale" and see what character set in in use.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.