C++_知识点_全局变量
全局变量
-全局变量即在函数之外定义的变量
-全局变量保存在静态存储区
注意:
-全局变量只能声明和初始化
-全局变量不能进行运算、赋值(非初始化)、调用函数
-否则会出现编译错误
-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++_知识点_全局变量的更多相关文章
- C++_知识点_指针类型转换
#include <iostream> using namespace std; int main(){ ] = {, , , , , , , , , }; int* p = (int*) ...
- C++_知识点_结构体/枚举/联合
//C++中结构体的不同之处 #include <iostream> #include <string> using namespace std; int main(void) ...
- Lock锁_线程_线程域
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- 『TensorFlow』分布式训练_其三_多机分布式
本节中的代码大量使用『TensorFlow』分布式训练_其一_逻辑梳理中介绍的概念,是成熟的多机分布式训练样例 一.基本概念 Cluster.Job.task概念:三者可以简单的看成是层次关系,tas ...
- Oracle学习总结_day03_day04_条件查询_排序_函数_子查询
本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 谢谢配合! day03_条件查询_排序_函数 清空回收站: PUR ...
- 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 ...
- 转:HIBERNATE一些_方法_@注解_代码示例---写的非常好
HIBERNATE一些_方法_@注解_代码示例操作数据库7步骤 : 1 创建一个SessionFactory对象 2 创建Session对象 3 开启事务Transaction : hibernate ...
- 迅为4412开发板Linux驱动教程——总线_设备_驱动注册流程详解
本文转自:http://www.topeetboard.com 视频下载地址: 驱动注册:http://pan.baidu.com/s/1i34HcDB 设备注册:http://pan.baidu.c ...
- 3.IP地址分类_规划_子网掩码
IP地址分类_规划_子网掩码 3.1MAC地址 网卡的身份证号———MAC地址 MAC地址的长度为48位(6个字节),通常表示为12个16进制数,每2个16进制数之间用冒号隔开,如:08:00:20: ...
随机推荐
- uva 10004 Bicoloring(dfs二分染色,和hdu 4751代码差不多)
Description In the ``Four Color Map Theorem" was proven with the assistance of a computer. This ...
- C++Primer 中间Sales_items.h头文件
#ifndef SALESITEM_H #define SALESITEM_H #include <iostream> #include <string> class Sale ...
- vs快捷键
Ctrl+E,D ----格式化全部代码 Ctrl+A+K+FCtrl+E,F ----格式化选中的代码 Ctrl+K+FCTRL + SHIFT + B生成解决方案 Alt+B+B 或 F6 生成当 ...
- sql2008R2数据库备份--双机备份
二.解决SQL2008代理作业出现错误: c001f011维护计划创建失败的解决方法 SQL2008数据库总会出现从 IClassFactory 为 CLSID 为 {17BCA6E8-A95D-49 ...
- tmux 命令
创建并指定session名字tmux new -s $session_name 删除sessionCtrl+b :kill-session 临时退出sessionCtrl+b d 列出sessiont ...
- 为什么用户主目录下.bash_profile没有自动执行
请注意 一般登录当前用户后 .bash_profile会自动运行 如果不自动运行请查看是不是切换用户的时候使用的su 用户名而不是su - 用户名 添加中间这个 - 后 会除了切换用户外还会加载该用户 ...
- jQuery滚动广告 解决子div绝对定位与父div重叠引起的闪烁问题
这两天做了一个滚动广告栏的demo 功能有自动轮播 左右箭头移动 导航点选中图片移动效果 模仿的是新浪体育的广告 最难的问题就是子div绝对定位于父div 鼠标移入子div 系统会判定鼠标移出了父di ...
- 无缝滚动 jQuery经典代码 (收藏)
<script language="javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4 ...
- requirejs + vue 项目搭建
以前都是支持 司徒正美 的,毕竟咱们也是跟着 司徒正美 一起走进了前端的世界.所以一般MVVM都是用avalon的,当然也是考虑到项目需要支持IE6,7,8的考虑.当然在用的时候也有一些小坑和bug, ...
- ThinkPHP中ajax提交数据
最近在做项目时遇到了一些需要从页面用ajax提交数据到后台的操作,无奈本人技术有限,网上苦寻,研究了一下ajax和thinkPHP的结合,黄天不负苦心人,终于搞定了. 闲话少叙,进入正题:我需要从页面 ...