为什么我不能在这个程序中获得输出?这种类型转换是否可行? [英] Why can't I get the output in this program? Is this type conversion posssible?

查看:52
本文介绍了为什么我不能在这个程序中获得输出?这种类型转换是否可行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
#include<string.h>
#include<math.h>
void main()
{
    int i,p;
    char y;
    char x,a[10]={4,5,6,7,8};
    p=strlen(a);
    for(i=0;i<p;i++){
        y=a[i];
        x=(char)y;
        printf("%c",x);
    }
}





我的尝试:



i无法获得输出,当我这样做时,我得到许多不可读的输出



What I have tried:

i can't get output,when i do the same i get many unreadable output

推荐答案

你的代码错了。您创建一个字符数组,然后将整数值放入其中。因此,您尝试打印的字符实际上是不可打印的值。
Your code is wrong. You create a character array and then put integer values into it. So the 'characters' you try to print are actually non-printable values.


字符是7位或8位或16位,具体取决于您要查看它们的方式以及字符设置你正在使用 - 但C是一种旧语言,所以它最有可能是7位ASCII,并且不以可打印字符开头 - 嘿从控制打印方式的字符开始,称为控制字符。最常见的是十六进制值0A和0D,称为LF或LINEFEED和CR或CARRIAGERETURN,通常分别表示为'\ n'aND'\ r'。

可打印字符开始使用SPACE在32(或20十六进制)处,然后从那里继续: Ascii表 - ASCII字符代码和html,八进制,十六进制和十进制图表转换 [ ^ ]如果要插入可打印字符,那么最简单的方法是在设置数组时使用可打印版本:

Characters are 7 bit or 8, or 16, depending on how you want to look at them, and what characters set you are using - but C is an old language so it's most likely to be 7 bit ASCII, and do not start with "printable characters" - hey start with characters that control how print occurs, called Control Characters. The most commonly seen have the hex values 0A and 0D and are known as LF or LINEFEED and CR or CARRIAGERETURN, often expressed as '\n' aND '\r' respectively.
Printable characters start at 32 (or 20 Hex) with SPACE, and then continue from there: Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion[^] If you want to insert printable characters, then the easiest way is to use the printable versions when you set up the array:
char x,a[10]={'4','5','6','7','8'};

甚至

char x,a[10]="45678";


Quote:

为什么我不能得到这个程序的输出?

Why can't I get the output in this program?



你的程序输出不可打印的字符。如果你告诉我们该怎么做这个程序,我们就能解释你的错误。

-----

你的代码没有表现出来你期望的方式,你不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它不知道你应该做什么,它没有发现错误,只是通过向你展示正在发生的事情来帮助你。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

1.11 - 调试程序(步进和断点)|学习C ++ [ ^ ]

调试器仅显示您的代码正在执行的操作,并且您的任务是与应该执行的操作进行比较。


Your program outputs non printable chars. If you tells us what is supposed to do the program, we will be able to explain you what is wrong.
-----
Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于为什么我不能在这个程序中获得输出?这种类型转换是否可行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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