全局变量

  -全局变量即在函数之外定义的变量

  -全局变量保存在静态存储区

注意:

  -全局变量只能声明和初始化

  -全局变量不能进行运算、赋值(非初始化)、调用函数

    -否则会出现编译错误

      -error: expected constructor, destructor, or type conversion before '.' token

      -initializer element is not constant

eg:

  

#include <stdio.h>
;
;
int c = a+b;// error 不能运算  

int main() {
    printf("c is %d\n", c);
    ;
}  
#include <stdio.h>

;

a = ; // error 不能赋值

int main(){

  ;
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

srand((unsigned int)time(NULL)); // error 不能调用函数

int sum = add(); // error 不能调用函数

int add(){
  ;
}

int main(){
  ;
}
#include <iostream>

using namespace std;

;

int a = b; // sucess 

int main(){

    ;
}
#include <iostream>

using namespace std;

;

const int add(){
    ;
}

int a = add(); // success

int main(){

    ;
}
#include <iostream>

using namespace std;

;

int a;

a = ; // error ??

int main(){

    ;
}

总结:

  -全局变量保存在静态存储区,其值必须在编译时确定,不能在执行时确定

  -所以定义一个全局变量时必须使用常量。

C++_知识点_全局变量的更多相关文章

  1. C++_知识点_指针类型转换

    #include <iostream> using namespace std; int main(){ ] = {, , , , , , , , , }; int* p = (int*) ...

  2. C++_知识点_结构体/枚举/联合

    //C++中结构体的不同之处 #include <iostream> #include <string> using namespace std; int main(void) ...

  3. Lock锁_线程_线程域

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  4. 『TensorFlow』分布式训练_其三_多机分布式

    本节中的代码大量使用『TensorFlow』分布式训练_其一_逻辑梳理中介绍的概念,是成熟的多机分布式训练样例 一.基本概念 Cluster.Job.task概念:三者可以简单的看成是层次关系,tas ...

  5. Oracle学习总结_day03_day04_条件查询_排序_函数_子查询

    本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 谢谢配合! day03_条件查询_排序_函数 清空回收站: PUR ...

  6. C Primer Plus_第6章_循环_编程练习

    1.题略 #include int main(void) { int i; char ch[26]; for (i = 97; i <= (97+25); i++) { ch[i-97] = i ...

  7. 转:HIBERNATE一些_方法_@注解_代码示例---写的非常好

    HIBERNATE一些_方法_@注解_代码示例操作数据库7步骤 : 1 创建一个SessionFactory对象 2 创建Session对象 3 开启事务Transaction : hibernate ...

  8. 迅为4412开发板Linux驱动教程——总线_设备_驱动注册流程详解

    本文转自:http://www.topeetboard.com 视频下载地址: 驱动注册:http://pan.baidu.com/s/1i34HcDB 设备注册:http://pan.baidu.c ...

  9. 3.IP地址分类_规划_子网掩码

    IP地址分类_规划_子网掩码 3.1MAC地址 网卡的身份证号———MAC地址 MAC地址的长度为48位(6个字节),通常表示为12个16进制数,每2个16进制数之间用冒号隔开,如:08:00:20: ...

随机推荐

  1. foj 2150 Fire Game(bfs暴力)

         Problem Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M ...

  2. C++隐式转换

    #include <iostream> using namespace std; class A { int a; public: A(int n):a(n) { cout << ...

  3. SharePoint 计时器服务无法启动

    摘要: Microsoft SharePoint Server 2010 使用 Windows SharePoint Services 定时 V4 (SPTimerV4) 服务运行大多数系统任务.服务 ...

  4. window.onload()与$(document).ready()区别

    浏览器加载完DOM后,会通过javascript为DOM元素添加事件,在javascript中,通常使用window.onload()方法. 在jquery中,则使用$(document).ready ...

  5. 说说VS 2015 RC最新开发工具的体验

    有两个我感觉是提高效率的地方: 1.智能提示的改进,鼠标只要移动到代码上面的类型.字段,就会显示相应的提示,这大大提高我们开发时候需要按F12才能看到定义的内容.下面上图,给大家形象化: 2.管理Nu ...

  6. 在webstrorm中配置好es6 babel

    第一步,新建一个项目,我这里建立了基于express 的node项目 第二步:将JavaScript语言版本切换为ECMAScript6 点击File —>settings,弹出设置框.把js的 ...

  7. BZOJ 3875: [Ahoi2014]骑士游戏

    d[i]表示消灭i所需的最小体力值, d[i] = min(S[i], K[i]+Σd[x]), Σd[x]表示普通攻击而产生的其他怪兽. 因为不是DAG, 所以用个队列类似SPFA来更新答案. -- ...

  8. OC语法4——自定义构造方法,description方法

    自定义构造方法: 我们已经知道创建对象分两步,1:在内存中开辟存储空间,并把地址存储在指针变量里,2:调用指针变量的初始化方法init初始化该对象. Student * stu = [Student ...

  9. vijos 1047 送给圣诞夜的礼品 矩阵

    题目链接 描述 当小精灵们把贺卡都书写好了之后.礼品准备部的小精灵们已经把所有的礼品都制作好了.可是由于精神消耗的缘故,他们所做的礼品的质量越来越小,也就是说越来越不让圣诞老人很满意.可是这又是没有办 ...

  10. Oracle EBS-SQL (BOM-6):检查物料失效但BOM中未失效的数据.sql

    select msi.segment1                   装配件编码 , msi.description                  装配件描述 , msi.item_type ...