Why in the code “456”+1, output is “56”
Question:
#include <iostream>
int main()
{
std::cout << "25"+1;
return 0;
}
I am getting "5" as output.when I use "5"+1,output is blank;"456"+1 output is "56".confused what is going behind the scenes.
Answer:
The string literal "25"
is really a char array of type const char[3]
with values {'2', '5', '\0'}
(the two characters you see and a null-terminator.) In C and C++, arrays can easily decay to pointers to their first element. This is what happens in this expression:
"25" + 1
where "25"
decays to &"25"[0]
, or a pointer to the first character. Adding 1
to that gives you a pointer to 5
.
On top of that, std::ostream
, of which std::cout
is an instance, prints a const char*
(note that char*
would also work) by assuming it is a null-terminated string. So in this case, it prints only 5
.
Why in the code “456”+1, output is “56”的更多相关文章
- svn Error: post-commit hook failed (exit code 127) with output
Command: Commit Modified: C:\Users\xsdff\Desktop\project\index.html Sending content: C:\Users\xsdff\ ...
- svn 提交报错post-commit hook failed (exit code 23) with output
svn 提交文件,hook同步更新报权限错误 排查后可能原因是被同步的服务器 selinux 已开启. 查看状态命令:/usr/sbin/sestatus -v #如果SELinux status参 ...
- SVN提交时报错:Commit blocked by pre-commit hook (exit code 1) with no output.
可能的原因: 提交代码的SVN命令中,Comment长度短了.参考:http://tortoisesvn.net/docs/nightly/TortoiseSVN_en/tsvn-howto-minl ...
- java io读书笔记(5) Writing Bytes to Output Streams
outputstream类是所有的字符输出类的父类,他是一个抽象类. 对于OutputStream类来说,其最基础的方法就是:write(). public abstract void write(i ...
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) A B C D 水 模拟 并查集 优先队列
A. Broken Clock time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- The secret code
The secret code Input file: stdinOutput file: stTime limit: 1 sec Memory limit: 256 MbAfter returnin ...
- MySQL下perror工具查看System Error Code信息
在MySQL数据库的维护过程中,我们有时候会在MySQL的错误日志文件中看到一些关于Operating system error的错误信息,例如在MySQL的错误日志里面,有时候会看到关于 Inn ...
- Hadoop Exit Code 含义
经常遇到的exception是: 1. PipeMapRed.waitOutputThreads(): subprocess failed with code N ............ 2. T ...
- hive subprocess failed with code X 的错误码对应信息
PipeMapRed.waitOutputThreads(): subprocess failed with code X ,这里code X对应的信息如下:error code 1: Operati ...
随机推荐
- 前端vue框架 父组件与子组件之间的相互调用
子组件调用父组件东西: 1.在父组件与子组件契合的标签的的template模板中绑定 v-bind:自定义一个名字=“要调用的名字” 2.在子组件的script中props:["自定义的名字 ...
- 【C#】 使用Gsof.Native 动态调用 C动态库
[C#] 使用Gsof.Native 动态调用 C动态库 一.背景 使用C# 开发客户端时候,我们经常会调用一些标准的动态库或是C的类库.虽然C# 提供的PInvoke的方式,但因为使用的场景的多变, ...
- 可在广域网部署运行的即时通讯系统 -- GGTalk总览(附源码下载)
(最新版本:V6.2,2019.01.03 .Xamarin移动端版本已经推出,包括 Android 和 iOS) GGTalk开源即时通讯系统(简称GG)是QQ的高仿版,同时支持局域网和广域网, ...
- 夜谈Java类的定义
女孩:谈Java了,好耶? 男孩:夜谈一下,Java的类的定义~ 女孩:那谈Java的类的什么呢? 男孩:类的定义,对象的定义,类中的方法,构造方法,this关键字,方法的重载,Java中的类的访问权 ...
- MySQL:explain 和 慢查询日志
1. 执行SQL时显示执行情况 explain + SQL语句 2. 强制使用索引 select * from t force index (a) where a between 1 ...
- 使用autogen生成应用程序遇到问题及解决方法
从github上下载的代码,运行autogen.sh的时候,上报错误: $ ./autogen.sh --prefix=/usr./autogen.sh: 10: ./autogen.sh: auto ...
- Linux学习笔记之四————Linux常用命令之文件管理
Linux命令——文件管理相关命令 <1>查看文件信息:ls ls是英文单词list的简写,其功能为列出目录的内容,是用户最常用的命令之一,它类似于DOS下的dir命令. Linux文件或 ...
- Xpath定位方法深入探讨及元素定位失败常见情况
一.Xpath定位方法深入探讨 (1)常用的Xpath定位方法及其特点 使用绝对路径定位元素. 例如: driver.findElement(By.xpath("/html/body/div ...
- SQL Server性能优化(14)索引碎片
一. 外部碎片和内部碎片的概念 碎片的概念和检测,参考MSDN:https://msdn.microsoft.com/zh-cn/library/ms189858.aspx 二.对于碎片的解决办法 解 ...
- Java读取Properties的几种方法
本文转载自:http://blog.csdn.net/chjttony/article/details/5927613 每次用完每次忘相对与绝对...