2014.12.26 注释,预编译,命名空间,变量规范

本文介绍了C++编程的基础知识,包括单行和多行注释的使用,预编译指令的介绍,命名空间的作用,变量定义及其存储方式,变量命名规则,并讨论了不同命名规范的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<h4>注释:</h4> <blockquote> <p>传统C语言都是多行 C++和有些C语言编译器已经支持单行 </p> <pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"><span style="color: #008000">//这是一个单行注释</span> </pre></pre>

<pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"><span style="color: #008000">/* </pre></pre>

<pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"> 这是一个多行注释 多行注释是不可以嵌套多行注释 </pre></pre>

<pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; margin: 0em; width: 100%; background-color: #ffffff">*/</span></pre></pre>

</blockquote>

<h4>预编译:</h4>

<blockquote> <p>&#160;&#160;&#160;&#160;&#160; 预编译指令 #include&lt;iostream&gt; </p>

<p>使用visual C++时,总是包含了头文件stdafx.h&#160; 把通用的预编译指令放在stdafx.h里 那每次只要 #include &quot;stdafx.h&quot;就可以了</p>

<p>&#160;</p>

<h4></h4> </blockquote>

<h4>命名空间</h4>

<blockquote> <p>命名空间是为了避免重名把不同的成员放在不同的namespace下</p>

<pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"><span style="color: #0000ff">using</span> <span style="color: #0000ff">namespace</span> std; </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; margin: 0em; width: 100%; background-color: #ffffff">cout&lt;&lt;&quot;<span style="color: #8b0000">hello world \n</span>&quot;; <span style="color: #008000">//使用命名空间后直接使用cout</span>

</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"><span style="color: #008000">//如果不引入命名空间就必须</span> </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; margin: 0em; width: 100%; background-color: #ffffff">std::cout<<"<span style="color: #8b0000">hello world \n</span>"; </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"><span style="color: #008000">// \n是传统C语言的换行 也可以如下使用</span> </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; margin: 0em; width: 100%; background-color: #ffffff">cout<<"<span style="color: #8b0000">hello world</span>"<<endl; </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"><span style="color: #008000">//cout是一个输出流 把文字放在里面变可以输出 <<这个符号表示把文字放进去</span></pre></pre>

</blockquote>

<h4>变量定义</h4>

<pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"><blockquote><p><span style="color: #0000ff">int</span> num = 5;<span style="color: #008000">//定义一个整型变量</span></p></blockquote> </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"></pre></pre>

<blockquote> <p>变量的存储分别存在堆或栈里,为函数或者方法分配的一个内存空间这个是栈,堆是在函数方法外的任意一个地方分配的一个指针类型空间。</p> </blockquote>

<blockquote> <p>在函数或方法结束之后栈里面的所有数据都会清空,如果是堆的话,函数结束如果不手动释放是一直存在的.</p> </blockquote>

<h5>变量命名规则</h5>

<blockquote> <p>可以使用26个大写字母,26个小写字母,10个数字,下划线。数字不能作为变量的开头,运算符和关键字也不能作为变量名,但可以包含关键字。</p>

<p>现在C++很多编译器已经支持Unicode做变量名,可以直接使用中文和其它语言的文字作为变量名.</p> </blockquote>

<h5>题外话</h5>

<blockquote> <p><font style="background-color: #a5a5a5">Visual studio为什么生成的main函数是带下划线的_tmain?</font></p>

<p><font style="background-color: #a5a5a5"><font style="style">_tmain是一个宏不是一个函数。 <br />main是C++标准。函数签名为

    <br />int main(int argc, char* argv[]); (这两个参数省略的话也没什么问题)

    <br />为了支持Unicode,微软定义了一个扩展

    <br />int wmain(int argc, wchar_t* argv[]);

    <br />和一个宏_tmain,这个根据项目的Unicode设置被定义成main或者wmain。

    <br />一般说来没有必要不必在代码中写死Unicode或者ANSI,而是用_tmain、_T、TCHAR这样的宏使得代码兼容Unicode和ANSI</font>。</font></p>

</blockquote>

<h4></h4>

<h4>命名规范</h4>

<ol> <ol> <li>大驼峰(pascal)</li>

<ul>
  <li>每个单词首字符都大写 比如EatApple 一般用来取复合型类型的 类名</li>
</ul>

<li>小驼峰</li>

<ul>
  <li>开头单词小写,后面大写。 比如 eatApple 在C语言中一般用来取变量名和函数名</li>
</ul>

<li>匈牙利命名法</li>

<ul>
  <li>变量名=属性 + 类型 + 描述 </li>

  <li>如果变量定义在函数中就不需要属性了 </li>

  <li>比如 hwnd h:句柄 wnd:描述了windows&#160;&#160; 或 pfnBuyApple 里面出现两个类型 表示这是一个 函数指针变量 </li>

  <ol>
    <li>属性 : </li>

    <ul>
      <li>全局变量:g_</li>

      <li>常量:c_</li>

      <li>类的成员变量:m_</li>

      <li>静态变量:s_</li>
    </ul>

    <li>类型:</li>

    <ul>
      <li>指针:p</li>

      <li>函数:fn</li>

      <li>句柄:h</li>

      <li>整型:i</li>

      <li>长整型:l</li>

      <li>布尔:b</li>

      <li>浮点(文件):f</li>
    </ul>

    <li>描述</li>

    <ul>
      <li>取有实际意义的单词 </li>
    </ul>
  </ol>
</ul>

<li>下划线命名法(动态语言中用的多)</li>

<ul>
  <li>比如eat_apple</li>
</ul>

</ol> </ol>

转载于:https://my.oschina.net/u/2297730/blog/360909

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值