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”的更多相关文章

  1. 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\ ...

  2. svn 提交报错post-commit hook failed (exit code 23) with output

    svn 提交文件,hook同步更新报权限错误 排查后可能原因是被同步的服务器 selinux 已开启. 查看状态命令:/usr/sbin/sestatus -v  #如果SELinux status参 ...

  3. 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 ...

  4. java io读书笔记(5) Writing Bytes to Output Streams

    outputstream类是所有的字符输出类的父类,他是一个抽象类. 对于OutputStream类来说,其最基础的方法就是:write(). public abstract void write(i ...

  5. 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 ...

  6. The secret code

    The secret code Input file: stdinOutput file: stTime limit: 1 sec Memory limit: 256 MbAfter returnin ...

  7. MySQL下perror工具查看System Error Code信息

      在MySQL数据库的维护过程中,我们有时候会在MySQL的错误日志文件中看到一些关于Operating system error的错误信息,例如在MySQL的错误日志里面,有时候会看到关于 Inn ...

  8. Hadoop Exit Code 含义

    经常遇到的exception是: 1. PipeMapRed.waitOutputThreads(): subprocess failed with code N  ............ 2. T ...

  9. hive subprocess failed with code X 的错误码对应信息

    PipeMapRed.waitOutputThreads(): subprocess failed with code X ,这里code X对应的信息如下:error code 1: Operati ...

随机推荐

  1. Hadoop 系列文章(一) Hadoop 的安装,以及 Standalone Operation 的启动模式测试

    以前都是玩 java,没搞过 hadoop,所以以此系列文章来记录下学习过程 安装的文件版本.操作系统说明 centos-6.5-x86_64 [bamboo@hadoop-senior opt]$ ...

  2. 一行代码实现自定义转场动画--iOS自定义转场动画集

    WXSTransition 这款非常不错,力推 这是作者源码简书地址: http://www.jianshu.com/p/fd3154946919 这是作者源码github地址 https://git ...

  3. 牛客JS编程大题(二)

    11.统计数组 arr 中值等于 item 的元素出现的次数 function count(arr, item) { var num = 0; for(var i = 0;i < arr.len ...

  4. Math.round(),Math.ceil(),Math.floor()

    Math.round() :round周围,求一个附近的 整数 小数点后第一位 < 5 正数:Math.round(10.48)       //  10 负数:Math.round(-10.4 ...

  5. maya2019卸载/安装失败/如何彻底卸载清除干净maya2019注册表和文件的方法

    maya2019提示安装未完成,某些产品无法安装该怎样解决呢?一些朋友在win7或者win10系统下安装maya2019失败提示maya2019安装未完成,某些产品无法安装,也有时候想重新安装maya ...

  6. 代码的重构(Refactor-Extract)

    1.vs中的代码重构快捷方式:Refactor-Extract: 选中两个需要重构的部分完整代码,右击,选中Refactoe-Extract-Extract Method: 该选中的代码会自动形成一个 ...

  7. koa-static与react-create-app搭配的路径

    概述 前端路由与后端路由的结合一直是一个难题.koa-static这个中间件能够把静态资源"搬到"后端路由上面去,react-create-app在不解构的情况下只会把资源打包到b ...

  8. Testing - 软件测试知识梳理 - 软件性能测试

    软件性能测试的基本概念 软件的性能是软件的一种非功能特性,它关注的不是软件是否能够完成特定的功能,而是软件在完成该功能时展示出来的及时性. 软件性能的指标 响应时间:是指系统对请求作出响应的时间,并且 ...

  9. LeetCode:21_Merge Two Sorted Lists | 合并两个排序列表 | Easy

    题目:Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list sh ...

  10. JAVA发送http get/post请求,调用http接口、方法

    import java.io.BufferedReader; import java.io.IOException;import java.io.InputStream; import java.io ...