473,386 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

scope, linkage and storage duration

Hello,

To learn the details of C, I've build the following example,
could you check if it is correct and if it miss some important
cases?

Are there some useful (real cases) examples of:
- "function prototype scope" for structures and unions?
- "extern" for internal linkage ?

ciao
cate
-------------------------
/* Scope, linkage and storage duration in C
* by Giacomo Catenazzi <ca**@cateee.net>
*/

/***************************************
* scope, linkage and storage duration *
***************************************/

/* Kind of identifier [C99:TC3 6.2.1]:
* - objects
* - functions
* - tags or members of a structure, union, or enumeration
* - typedef names
* - label names
* - macro name
* - macro parameter
*
* scope of identifiers [C99:TC3 6.2.1] (where the symbol is visible):
* - file scope,
* - function scope (only for labels),
* - block scope,
* - function prototype scope
*
* linkage (whenether two symbols are the same) [C99:TC3 6.2.2]:
* - external: across translation unit
* - internal (static): within translation unit
* - none: unique
*
* storage duration (lifetime of an object) [C99:TC3 6.2.4]:
* - static storage duration (it exists before to start up the program)
* - automatic storage duration
* - allocated storage duration
*/

/* objects scope linkage duration
* ******* ---------------------------*/

/* Note: here the same identifier name define the same entity */

int i; // i: file external static
extern int ei; // ei: file external static
static int si; // si: file internal static
extern int si; // si: file internal static //re-declaration

const int ci; // ci: file external static
extern const int eci; //eci: file external static
static const int sci; //sci: file internal static

int x1
(int j) { // j: block none automatic

int k; // k: block none automatic
static int sk; // sk: block none static
extern int ek; // ek: block external static

extern int i; // i: block external static //re-declaration
extern int ei; // ei: block external static //re-declaration
extern int si; // si: block internal static //re-declaration
return j+k+ek;
}
int x2
(int l); // l: fproto none automatic
/* functions scope linkage
* ********* ---------------*/

/* Note: same identifier name is the same function */

int f(int); // f: file external
extern int ef(int); // ef: file external
static int sf(int); // ef: file internal

int x2(int j) {
int g(int);// g: block external
extern int eg(int);// eg: block external

extern int ef(int);// ef: block external //re-declaration

extern int f(int);// f: block external //re-declaration
int ef(int);// ef: block external //re-declaration
int sf(int);// ef: block internal //re-declaration
return eg(j)+f(j);
}

/* tags scope linkage
* **** ---------------*/

struct t1 { // t1: file none
struct t2 { // t2: file none
int y1;
} y2;
} y3;

void x3(
struct t3 { // t3: block none
struct t4 { // t4: block none
int y4;}
y5;} y6) {
struct t5 { // t5: block none
struct t6 { // t6: block none
int y7;}
y8;
} y9;
struct t3 y10;
}
void x4(
struct t7 { // t7: fproto none
struct t8 { // t8: fproto none
int y11;}
y12;} y13);

struct t9 { // t9: block none
struct t10 { // t10: block none
int y12;}
y13;}
x5 (struct t9 y14) {
struct t9 y15;
}

struct t11 { // t11: fproto none
struct t12 { // t12: fproto none
int y16;}
y17;}
x6 (struct t11 y18) {
struct t11 y15;
}

/* note: tag could be declared more that once, but it is not
* considered as "linkage" */
struct t13;
struct t13* y19; /* note: this is not a declaration */
struct t13 {
int y20;
};
struct t13 y21; /* note: this is not a declaration */
/* labels scope linkage
* ****** ---------------*/

void x7(void) {
goto lab;
lab: ; // lab: function none
}
/* note: label is the only identifier with function scope,
* and it can be used before the definition. */
/* macro name and macro parameter
* ******************************/

/* the macro are defined and expanded in earlier phases,
* with different scope rules (e.g. macro can be undefined)
*/
Nov 4 '08 #1
1 3731
Hi

On Tue, 04 Nov 2008 10:58:02 +0100, Giacomo Catenazzi wrote:
/* Kind of identifier [C99:TC3 6.2.1]:
* - tags or members of a structure, union, or enumeration
Why group tags with members? they don't share a namespace.

I would also separate enum members since there syntax is different.
* linkage (whenether two symbols are the same) [C99:TC3 6.2.2]:
* - external: across translation unit
I wouldn't normally nit-pick grammar so strictly, but missing the 's' off
the plural makes this ambiguous. "across translation units"

HTH
Nov 4 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Vipul Jain | last post by:
Can any one please tell me what is the difference between global scope of an variable and file scope of an variable. Vipul
1
by: Cplusplus | last post by:
as C++ defaults to external linkage for non-consts and non-static(right?): Qn) why is it then : a object declared (*without* static keyword)outside main() and outside all functions ,...
3
by: Tony Johansson | last post by:
Hello Experts!! When you instansiate varaibles(object) you can do so in four different scops which are. Within a block which is called Local or block scope . Within a function which is called...
5
by: pembed2003 | last post by:
Hi all, I am reading the book "C How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope...
4
by: Peter Ammon | last post by:
I would like to share a variable between two functions defined in two separate source files; no other functions will need the global variable so I'd prefer to not give it file scope. Thus, I want...
8
by: TTroy | last post by:
I have a few questions about "scope" and "visibility," which seem like two different things. To me "visibility" of the name of a function or object is the actual code that can use it in an...
7
by: Kobu | last post by:
The code below isn't compiling for me (error message: conflicting types for 'total' - pointing to the extern declaration). Why wouldn't this work, since the types are different, the extern...
7
by: Christian Christmann | last post by:
Hi, I've a a question on the specifier extern. Code example: void func( void ) { extern int e; //...
1
by: Kurt | last post by:
The C++ standard (in 3.5:6, p42Example) said: static int i = 0; // 1 void g() { int i; //2: i has no linkage { extern int i; //3: external linkage
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.