gpt4 book ai didi

c - 如何打印字符串的字符值?

转载 作者:行者123 更新时间:2023-11-30 17:55:05 24 4
gpt4 key购买 nike

我正在尝试以这种格式打印字符串中每个字符的值:

Code 2 in set 0 has value: 41 42

Code 6 in set 0 has value: 41 42 43 44 45 46

我正在尝试通过以下方式做到这一点:

printf("Code %d in set %d has value: %.*s\n", ndx, set, getCode(cs[set],ndx).size, getCode(cs[set],ndx).data);

但是当我这样做时,它会打印与该值关联的字符而不是值本身。我应该如何以指定的格式打印数据?

最佳答案

您正在将数组打印为字符串。相反,循环遍历每个字符并打印其十进制值:

int i;
printf("Code %d in set %d has value: ", ndx, set);
for (i = 0; i < getCode(cs[set],ndx).size ; i++) {
printf("%d ", getCode(cs[set],ndx).data[i]);
}
puts(""); //print a newline

关于c - 如何打印字符串的字符值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14551774/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com