Note: All Learned From Here

C和Objective-C的function前面都有个隐含的extern,对于function来说,有没有extern都无所谓,但变量不一样。

declaration 声明可以无数次但定义只能有一次

声明但不定义:no memory allocation for it

extern int var;

// This could be done as many times as you want

// If we prepend extern in variable as default,then the memory for them will not be allocated ever.

definition 定义且声明一个变量:memory allocated

int var;

Examples that work

int var;

int main(void) {

var = 10;

return 0;

}

extern int var;

int main(void) {

return 0;

}

extern int var = 0;  //如果有初始化 那么内存是被分配了的

int main() {

var = 10;

return 0;

}

#include "somefile.h"

extern int var;

int main() {

var = 10;

return 0;

}

// 如果somefile.h里有var的definition,那么是可行的

Examples that don't work

extern int var;

int main(void) {

var = 10;

return 0;

}

// var 被声明了但没有被定义,也就是没有被分配内存,所以赋值行为是错误的

1. Declaration can be done any number of times but definition only once.

2. “extern” keyword is used to extend the visibility of variables/functions().

3. Since functions are visible through out the program by default. The use of extern is not needed in function declaration/definition. Its use is redundant.

4. When extern is used with a variable, it’s only declared not defined.

5. As an exception, when an extern variable is declared with initialization, it is taken as definition of the variable as well.

Things About 'extern'的更多相关文章

  1. const,static,extern 简介

    const,static,extern 简介 一.const与宏的区别: const简介:之前常用的字符串常量,一般是抽成宏,但是苹果不推荐我们抽成宏,推荐我们使用const常量. 执行时刻:宏是预编 ...

  2. const extern static 终极指南

    const extern static 终极指南 不管是从事哪种语言的开发工作,const extern static 这三个关键字的用法和原理都是我们必须明白的.本文将对此做出非常详细的讲解. co ...

  3. 16-static和extern关键字2-对变量的作用

    上一讲介绍了static和extern对函数的作用,static用来定义一个内部函数,不允许其他文件访问:extern用来定义和声明一个外部函数,允许其他文件访问.static和extern对变量也有 ...

  4. 15-static和extern关键字1-对函数的作用

    一.extern与函数 如果一个程序中有多个源文件(.c),编译成功会生成对应的多个目标文件(.obj),这些目标文件还不能单独运行,因为这些目标文件之间可能会有关联,比如a.obj可能会调用c.ob ...

  5. 转:C++项目中的extern "C" {}

    引言 在用C++的项目源码中,经常会不可避免的会看到下面的代码: #ifdef __cplusplus extern "C" { #endif /*...*/ #ifdef __c ...

  6. extern "c"用法解析

    转自: extern "c"用法解析 - 简书 引言 C++保留了一部分过程式语言的特点,因而它可以定义不属于任何类的全局变量和函数.但是,C++毕竟是一种面向对象的程序设计语言, ...

  7. extern

    gcc编译器编译程序有四个阶段,预处理.编译.汇编.链接.预处理阶段会将源代码中的包含的头文件如stdio.h编译进来:编译阶段,gcc首先要检查代码的规范性.是否有语法错误等,以确定代码的实际要做的 ...

  8. 李洪强iOS经典面试题155 - const,static,extern详解(面试必备)

    李洪强iOS经典面试题155 - const,static,extern详解(面试必备) 一.const与宏的区别(面试题): const简介:之前常用的字符串常量,一般是抽成宏,但是苹果不推荐我们抽 ...

  9. extern用法总结

    在C语言中,修饰符extern用在变量或者函数的声明前,用来说明"此变量/函数是在别处定义的,要在此处引用".1. extern修饰变量的声明. 如果文件a.c需要引用b.c中变量 ...

  10. 链接(extern、static关键词\头文件\静态库\共享库)

    原文链接:http://www.orlion.ga/781/ 一. 多目标文件的链接 假设有两个文件:stack.c: /* stack.c */ char stack[512]; int top = ...

随机推荐

  1. linux中shell变量$#,$@,$0,$1,$2的含义解释

    linux中shell变量$#,$@,$0,$1,$2的含义解释 linux中shell变量$#,$@,$0,$1,$2的含义解释:  变量说明:  $$  Shell本身的PID(ProcessID ...

  2. [Javascript] Implement zip function

    1. Use a for loop to traverse the videos and bookmarks array at the same time. For each video and bo ...

  3. [React] React Fundamentals: transferPropsTo

    the transferPropsTo method lets you easily push properties into your components to easily customize ...

  4. 垂死挣扎还是涅槃重生 -- Delphi XE5 公布会归来感想

    Delphi 是一个基本上被我遗忘的工具, 要不是在使用RapidSql , 我是收不到Embarcadero 公司发出的邀请来參加Delphi XE5的公布会的. 有人可能要问为什么是Embarca ...

  5. System Address Map Initialization in x86/x64 Architecture Part 2: PCI Express-Based Systems

      原文  http://resources.infosecinstitute.com/system-address-map-initialization-x86x64-architecture-pa ...

  6. Hadoop Oozie 学习笔记

    Oozie是一个工作流引擎服务器,用于运行Hadoop Map/Reduce和Pig 任务工作流.同时Oozie还是一个Java Web程序,运行在Java Servlet容器中,如Tomcat. O ...

  7. Network Load Balancing Technical Overview--reference

    http://technet.microsoft.com/en-us/library/bb742455.aspx Abstract Network Load Balancing, a clusteri ...

  8. Adobe Flash Platform产品介绍

    解释 adobe flash platform flex是一个免费的开源的框架 该应用程序通常是用在 浏览器和 桌面 AIR中的 了解 flex框架 ,Flex sdk 和adobe flash bu ...

  9. start mysqld on Mac server

    #!/bin/sh # Source the common setup functions for startup scripts test -r /etc/rc.common || exit 1 . ...

  10. linux安装java环境

    在linux下安装JDK如下: 第一步:查看Linux自带的JDK是否已安装 (1)查看已经安装的jdk: [root@web-server ~]# rpm -qa|grep jdk ← 查看jdk的 ...