如何理解C ++中的单引号和双引号术语?

时间:2018-12-17 14:33:05

标签: c++

帮助我了解以下内容:

cout<<'a'; //prints a and it's okay but
cout<<'ab'; //prints 24930 but I was expecting an error due to term 'ab' having two character in single quote
cout<<'a'+1; //prints 98 
cout<<"ab"; // prints ab and it's okay but
cout<<"ab"+1; // prints b, why?
cout<<"a"+1; // prints nothing ?
cout<<'a'+'b'; // prints 195 ?
cout<<"a"+"b"; // gives error ?

请帮助我详细了解所有这些内容。我很困扰。我会非常感激。

1 个答案:

答案 0 :(得分:5)

'a'是C ++中的char类型。 std::cout使<<重载char来输出字符而不是字符号。

'ab'是C ++中的多字符文字。它必须具有int类型。它的值是定义的实现,但是'a' * 256 + 'b'很常见。使用ASCII编码,即24930。<<的重载int运算符输出数字。

'a' + 1是一个算术表达式。根据标准整数类型提升规则,在添加之前,'a'将转换为int类型。

"ab" + 1const char[3]类型执行指针算术,因此它等效于"b"。请记住,<<优先级+低。

"a" + 1与上面类似,但仅输出NUL终止符。

'a' + 'b'int类型。在添加之前,这两个参数都将转换为int

添加之前,"a" + "b"const char*类型的public ActionResult ReportDataView(Test[] items) { PrintingBLL oPrintingBll = new PrintingBLL(); oPrintingBll.ReportType = "1"; oPrintingBll.ReportFormat = "PDF"; string temp = ""; string temp1 = ""; foreach(var item in items) { if (item.Print4AHD == 'Y') { temp = temp + item.HBL + ","; } else { temp1 = temp1 + item.HBL + ","; } } if (temp.EndsWith(",")) { temp = temp.TrimEnd(','); } if (temp1.EndsWith(",")) { temp1 = temp1.TrimEnd(','); } oPrintingBll.Param1 = temp; oPrintingBll.Param2 = temp1; Byte[] oReportbytes = oPrintingBll.ProcessReports(); string strByte = null; System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding(); strByte = enc.GetString(oReportbytes); strByte = strByte.ToLower(); if (!(strByte.Substring(0, 5) == "error")) { Response.ContentType = "application/pdf"; Response.AppendHeader("Content-Disposition", "inline; filename=DocumentPDF.PDF"); if (oReportbytes.Length > 0) { Response.AddHeader("Content-Length", oReportbytes.Length.ToString()); } Response.BinaryWrite(oReportbytes); Response.Flush(); Response.Close(); } return Content("Print"); } decay 的参数。但这是两个指针的加法,这不是有效的C ++。