开发者

my program prints some arbit ascii string

开发者 https://www.devze.com 2023-03-13 19:22 出处:网络
I wrote following program int main 开发者_如何学Python() { char a=0xf; a=a+1; printf(\"%c\\n\",a); } the output of above program is what I am not able to understand.It is giving me some character w

I wrote following program

int main 开发者_如何学Python()
{
char a=0xf;
a=a+1;
printf("%c\n",a);
}

the output of above program is what I am not able to understand.It is giving me some character which I am not able to understand.Is it possible to find out ASCII code of the character that I am getting in my above program so that I understand what is it printing.

EDIT

Based on the replies I read I am adding further to my confusion

if I write a statement as following

char ch='Z';

then what would be stored in ch,

1) The character Z

2) ASCII value of Z

3) Z along with single inverted commas

4) Both (1) and (2)


ASCII value for 16(0x0f + 1 = 0x10) is DLE (data link escape) which is non-printable character. Just Print as integer like this.

printf("%d\n",a);


The characters from 0 to 31 are non-printing characters (in your case, you've chosen 0xF, which is 15 in decimal). Many of the obscure ones were designed for teletypes and other ancient equipment. Try a character from 32 to 126 instead. See http://www.asciitable.com for details.

In response to your second question, the character stores the decimal value 90 (as characters are really 1-byte integers). 'Z' is just notation that Z is meant to be taken as a character and not a variable.


You can modify your program like that:

int main ()
{
  char a=0xf;
  a=a+1;
  printf("Decimal:%u Hexa:%x Actual Char:|%c|\n",a,a,a);
}

Printf can use different formatting for a character.


Its printing the character 0x10 (16).

If you want the output, change your print to output the values (in this case, character, hex value, decimal value):

printf("%c - %x - %d\n", a, a, a);


#include<stdio.h>

int main ()    
{

    char a='z';                 \\\ascii value of z is stored in a i.e 122

    a=a+1;   \\\a now becomes 123

    printf("%c",a);   \\\ 123 corresponds to character '{' 

}
0

精彩评论

暂无评论...
验证码 换一张
取 消