为什么在C ++中不将char当作数字? [英] Why char is not taken as numeral in C++?

查看:101
本文介绍了为什么在C ++中不将char当作数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用C编写了一个代码,该代码运行良好,然后将其翻译为C ++。在那里它给出了错误的输出。我进行了一次迭代,将输入和迭代器变量都用作char来节省空间。

 未签名的字符重复,i;但未达到预期的效果。 
cin>>重复;
for(i = 0; i< repeat; i ++)

等价物是什么

  scanf(%hhi,& repeat)
行pre>

解决方案


为什么




因为C和C ++是不同的编程语言。



例如参见 n3337 ,一种C ++标准草案,实际上等同于 C ++ 11 标准。对于 C11 编程语言,请查看 n1570 CompCert 项目。



编程语言是规范,通常写在某些文档中。它的语义与它的语法



如果使用最近的 GCC变体编译C ++源代码,我建议调用 g ++ -Wall -Wextra -g 可以启用很多警告和一些调试信息。



还考虑使用一些静态程序分析工具,例如 Frama-C Clang静态分析器 Coverity 等...。



另请阅读 如何调试小型程序 以及有关未定义行为



另请参见有关C和C ++的网站,并浏览 RefPerSys (有些有趣的 C ++ 免费软件项目,我为此做出了积极贡献,与AGI 相关)。



至少要从灵感中研究现有C ++ 开源项目的源代码。 Qt 鱼壳立刻浮现在脑海(但 github gitlab 或其他地方,例如 Ghudi ,< a href = https://www.bugseng.com/parma-polyhedra-library rel = noreferrer> PPL , Fox工具包 FLTK )。通过积极地为其中一个做出贡献,您会学到很多东西(甚至 RefPerSys



BTW,我强烈建议明确地初始化C或C ++中的每个数字变量(因此代码 unsigned char repeat = 0,i = 0; )。任何足够好的优化编译器都会在无用的情况下将其删除,并调试程序(例如,使用< a href = https://sourceware.org/gdb/ rel = noreferrer> gdb ,特别是在GNU / Linux 系统,例如 Ubuntu Debian )的行为变得更具确定性时会更容易。


I wrote a code in C, which ran perfectly, I translated it to C++ . There it was giving wrong output. I had an iteration where I used both the input and the iterator variable as char to save space. But didn't behaved as expected.

unsigned char repeat, i;  
cin >> repeat;
for(i= 0; i < repeat; i++)

What is the equivalent line for

scanf("%hhi", &repeat)

?

解决方案

Why char is not taken as numeral in C++?

Because C and C++ are different programming languages.

See for example n3337, a draft C++ standard, practically equivalent to C++11 standard. For the C11 programming language, look into n1570 and the CompCert project.

A programming language is a specification, usually written in some documentation. Its semantics matters as much as its syntax.

If you compile your C++ source code with some recent variant of GCC, I suggest to invoke it as g++ -Wall -Wextra -g that is to enable a lot of warnings and some debug info.

Consider also using some static program analysis tools, e.g. Frama-C or Clang static analyzer or Coverity etc....

Read also How to debug small programs and about Undefined behavior

Refer also to this site about C and C++ and look into RefPerSys (some "interesting" C++ free software project to which I actively contribute, AGI related).

Study, at least for inspiration, the source code of existing C++ open source projects. Qt or the fish shell comes immediately to mind (but thousands of others exist on github or on gitlab or elsewhere, e.g. Ghudi, PPL, Fox toolkit, FLTK). You'll learn a lot by actively contributing to one of them (perhaps even RefPerSys)

BTW, I strongly recommend explicitly initializing every numeric variable in C or in C++ (so code unsigned char repeat=0, i=0;in your case). Any good enough optimizing compiler would remove them when they are useless, and debugging your program (e.g. with gdb, notably on GNU/Linux systems such as Ubuntu or Debian) would be easier when its behavior becomes more deterministic.

这篇关于为什么在C ++中不将char当作数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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