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. SQL Server使用证书最小粒度授权

    最近在项目中某个功能遇到了阻塞造成的Time out错误.所以需要在执行该功能的时候开启一个线程检测SQL Server中阻塞会话.但是程序本身使用的sql帐号本身不具备VIEW Sever Stat ...

  2. 背水一战 Windows 10 (77) - 控件(控件基类): ContentControl, UserControl, Page

    [源码下载] 背水一战 Windows 10 (77) - 控件(控件基类): ContentControl, UserControl, Page 作者:webabcd 介绍背水一战 Windows ...

  3. DWARF 中的 Debug Info 格式

    本周花了几天的时间来研究怎么在 breakpad [1, 2] 中加入打印函数参数的功能,以期其产生的 callstack 更具可读性,方便定位崩溃原因. 现代 ELF 中的调试信息基本是以 DWAR ...

  4. 【转】再有人问你Http协议是什么,把这篇文章发给他

    一.HTTP简介 1.HTTP协议,即超文本传输协议(Hypertext transfer protocol).是一种详细规定了浏览器和万维网(WWW = World Wide Web)服务器之间互相 ...

  5. 玩玩vs Git 中国版 Gitee

    下载vs  下载 Gitee.VisualStudio.vsix https://gitee.com/GitGroup/CodeCloud.VisualStudio/attach_files 去git ...

  6. 第二十三节:Java语言基础-详细讲解函数与数组

    函数 函数在Java中称为方法,在其他语言中可能称为函数,函数,方法就是定义在类中具有特定功能的程序.函数,在Java中可称为方法. 函数的格式: 修饰符 返回值类型 函数名(参数类型 参数1, 参数 ...

  7. AndroidStudio制作登录和注册功能的实现,界面的布局介绍

    前言 大家好,给大家带来AndroidStudio制作登录和注册功能的实现,界面的布局介绍的概述,希望你们喜欢 每日一句: Success is connecting with the world a ...

  8. MySQL备份---lvm snapshot

    正常安装(缺点要锁表) 1, 创建一个LV(逻辑卷) , 把MySQL的数据目录放到这个LV上 /var/lib/mysql 对这个LV做快照, 从快照备份数据 删除快照 非正常安装 1,创建LV 2 ...

  9. kubernetes集群搭建(2):docker私有仓库

    kubernetes集群搭建(1):环境准备 中各节点已经安装好了docker,请确认docker已启动并正常运行 1.通过命令启动私库 docker run -d -p 5000:5000 --pr ...

  10. js 解析url中search时存在中文乱码问题解决方案

    一 问题出现原因 当存在这样一种需求,前端需要通过url中search返回值进行保存使用,但如果search中存在中文解析出来会导致乱码.这个问题我找了很久原因,最后终于知道解决方案,这里和大家分享一 ...