我正在尝试打印我的char数组,但它只是打印数字为什么? [英] I am trying to print my char array but it just prints number why?

查看:189
本文介绍了我正在尝试打印我的char数组,但它只是打印数字为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[])
{
int counter;
char car[3]={'a','b','c',};
counter = 0; 
 while(counter < 3)  
{

   printf("%d\n",car[counter]); 
   counter++; 
    } 
 
 

	
	return 0;

What I have tried:

i have tried changing the values and trying different codes but nothing comes up it just gives me ramdon numbers on the output which i find annoying cuz the values are ABC

推荐答案


改变
Change from
Quote:

printf(%d \ n,car [counter]);

printf("%d\n",car[counter]);

to

printf("%c\n",car[counter]);

查看 printf文档 [ ^ ]。



顺便说一下,你得到的数字不是随机的。它们是 ASCII代码 [ ^ ]对应字符'a''b''c'

Have a look at printf documentation[^].

By the way, the numbers you get are not random ones. They are the ASCII codes[^] corresponding to the characters 'a', 'b', 'c'.


因为您使用的是%d格式说明符 - 其中说打印我整数。它打印的数字是每个字符的ASCII表示。

试试这个:

Because you are using the "%d" format specifier - which says "print me an integer number". The number it is printing is the ASCII representation of each character.
Try this:
printf("%c\n",car[counter]);


这篇关于我正在尝试打印我的char数组,但它只是打印数字为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆