Definition and Declaration

1. Variable:

------------

int a ;

------------

This is not only a Declaration, but also a Definition.

Since it is a defintion,  it is must "Define only once".

----------------------example------------------

If you write this in a .h file, and included it everywhere...

We know that "#include" means extracted the original file totally here,  so the same var int a has been defined as many times as been included, which is invalid.

ps: compile is ok, since it is local, local in the specific cpp file. When complier is working on this file, he dosen‘t know even a bit information the world outside.

So the generated file .o is also local.

However, Link is not ok. For multi definition error of int a.

2.Function:

-------------

void f();

-------------

So you can declare it many times, everywhere. This is just a name. Comile and Link are both ok.

So when you define Variable in a .h file,  "static" "extern" "const" ... are needed.

时间: 2024-10-26 06:25:28

Definition and Declaration的相关文章

what is the difference between definition and declaration in c

A declaration introduces an identifier and describes its type, be it a type, object, or function. A declaration is what the compiler needs to accept references to that identifier. These are declarations: extern int bar; extern int g(int, int); double

Relevance Between Variable Declaration and Definition in C++

A declaration makes a name known to a programm. A definition creates the assocatied entity. A variable declaration specifies the variable type and name. In addition to specifying the variable type and name, a definition also allocates storage and may

clang format 官方文档自定义参数介绍(中英文)

英文 Configuring Style in Code When using clang::format::reformat(...) functions, the format is specified by supplying the clang::format::FormatStyle structure. Configurable Format Style Options This section lists the supported style options. Value typ

【转】Dynamics AX 2012 – Downloading a file from FTP

Back in Feb, was trying to download a file from FTP server by the conventional way using the native X++ code, though we have several approaches and free tools to achieve the same. When i did some research, couldn't able to find any references in X++

在ubuntu下使用Vim学习C++

之前就买过java,c++的书,可是由于懒惰,一直没看,丢在那里5年了.(以上废话). 现在linux下还没有好的C++ IDE.网络上的朋友都推荐eclipse和 code::block.粗略的看了下code::block,建工程,管理工程,功能很多,准备以后学习下. 学习C++,目前还是用Vim比较好,轻量.各种丰富的插件支持. 开发环境: 系统:ubuntu 14.04LTS. 工具:Vim7.4 编译,链接,运行:g++ 现在贴上Vim的配置:(这是这篇随笔的主要内容,也是为了个人保存配

C++ 之 基础回顾(一)

1  C++ program 1.1  the miminal cpp int main(){} // the minimal C++ program 1.2  Hello World #include <iostream> int main() { std::cout << "Hello, World!\n"; } 1.3  functions #include <iostream> using namespace std; double squa

c++ primer learning point.1

1.when we wanna share const variables; we use the keyword extern on both its definition and declaration(s): 2.Once initialized, a reference remains bound to its initial object. There is no way to rebind a reference to refer to a different object. Bec

在centos7(EL7.3 即 kernel-3.10.0-514.X )上安装BCM4312无线网卡驱动要注意的问题

我新装的centos7主机无法使用里面自带的网卡,查询后发现网卡型号为BCM4312.我在看资料安装的过程中遇到了些问题,纠结了好久,现在分享下要注意的点,为后来的遇到同样问题的人提供点帮助.现在开始说正事: 若要安装以 Broadcom BCM4311.BCM4312.BCM4313.BCM4321.BCM4322.BCM43224.BCM43225.BCM43227 或 BCM43228 为基础的无线网络卡,请遵照以下的步骤: 第 1 步:辨认无线网络芯片及安装时依赖的组件 首先,请确定你是

C语言 文法规则之消除左递归

<program>→<external declarations>(<program>|~) <external declarations>→<function definition>|<declaration> <function definition>→<type declaration><declarator><consistent statement> <type decl