storage-class-specifier

Why can't I specify the storage class for formal parameters of a function?

萝らか妹 提交于 2019-12-03 14:02:57
When I do as below the code works fine : #include <stdio.h> void test( int a) { printf("a=%d\n",a); } int main() { test(10); return 1; } But when I do #include <stdio.h> void test( auto int a) // Or static int a Or extern int a { printf("a=%d\n",a); } int main() { test(10); return 1; } It generates an error, error: storage class specified for parameter 'a' Why is that error? What happens internally(memory management)? But it works fine without any error when I do: void test( register int a) { printf("a=%d\n",a); } Why is that? First,quoting C11 , chapter 6.7.6.3 The only storage-class

What is the difference between “File scope” and “program scope”

坚强是说给别人听的谎言 提交于 2019-12-02 18:12:33
A variable declared globally is said to having program scope A variable declared globally with static keyword is said to have file scope. For example: int x = 0; // **program scope** static int y = 0; // **file scope** static float z = 0.0; // **file scope** int main() { int i; /* block scope */ /* . . . */ return 0; } What is the difference between these two? In C99, there's nothing called "program scope". In your example variable x has a file scope which terminates at the end of translation unit. Variables y and z which are declared static also have the file scope but with internal linkage.

Why 'extern' storage class works differently in functions?

爱⌒轻易说出口 提交于 2019-12-02 05:47:13
问题 The following snippet works fine extern int i; int i; int main(){ return 0; } Here what I got is, 'i' is declared and then defined. Since there is only one definition so thats perfectly fine. int main(){ extern int i; int i; return 0; } Now, the above one gives the following error new.cpp: In function ‘int main()’: new.cpp:5:6: error: redeclaration of ‘int i’ int i; ^ new.cpp:4:13: note: previous declaration ‘int i’ extern int i; Whats the problem here? Here also there is single definition of

Using extern keyword to call functions

和自甴很熟 提交于 2019-12-02 03:41:49
I want to call functions defined in test.c from other.c. Can I extern the function1 to call it? Also, do I have to use extern in function2 and function3 , which are being called by function1 ? other.c extern function1(); function1(); test.c void function1() { function2(); function3(); } void function2() { } void function3() { } Function declarations are "by-default" extern . Quoting C11 , chapter §6.2.2, ( emphasis mine ) If the declaration of an identifier for a function has no storage-class specifier , its linkage is determined exactly as if it were declared with the storage-class specifier

Using extern keyword to call functions

旧巷老猫 提交于 2019-12-02 02:26:55
问题 I want to call functions defined in test.c from other.c. Can I extern the function1 to call it? Also, do I have to use extern in function2 and function3 , which are being called by function1 ? other.c extern function1(); function1(); test.c void function1() { function2(); function3(); } void function2() { } void function3() { } 回答1: Function declarations are "by-default" extern . Quoting C11 , chapter §6.2.2, ( emphasis mine ) If the declaration of an identifier for a function has no storage

Why 'extern' storage class works differently in functions?

南楼画角 提交于 2019-12-02 02:21:35
The following snippet works fine extern int i; int i; int main(){ return 0; } Here what I got is, 'i' is declared and then defined. Since there is only one definition so thats perfectly fine. int main(){ extern int i; int i; return 0; } Now, the above one gives the following error new.cpp: In function ‘int main()’: new.cpp:5:6: error: redeclaration of ‘int i’ int i; ^ new.cpp:4:13: note: previous declaration ‘int i’ extern int i; Whats the problem here? Here also there is single definition of 'i'. To understand the difference, you need to get familiar with a concept called tentative definition

_Thread_local storage class specifier in C?

五迷三道 提交于 2019-12-02 01:28:48
问题 I read a note in the book C How to Program 7th about some new standard C storage class named _Thread_local : The new C standard adds storage class specifier _Thread_local , which is beyond this book's scope. I looked for it in Google and here but nothing show up. Could someone please provide me some link about it? 回答1: Variables marked with _Thread_local are given "thread" storage duration -- that is, they are allocated when a thread begins, and deallocated when the thread ends. Such

Why BSS segment is “16” by default?

泄露秘密 提交于 2019-12-01 18:25:59
As per my knowledge, segmentation for c program is: High address |---------------------------| |env/cmd line args vars | |---------------------------| | stack segment |--> uninitialized auto vars |---------------------------| |---------------------------| |---------------------------| | heap segment |--> dynamic allocated memory |---------------------------| | BSS segment |--> uninitialized static/global vars |---------------------------| | data segment |--> initialized static/global vars |---------------------------| | text segment |--> initialized auto vars/exec instructions |---------------

Can the 'auto' keyword be used as a storage class specifier in C++11?

别说谁变了你拦得住时间么 提交于 2019-12-01 15:32:43
Can the auto keyword be used as a storage class specifier in C++11? Is the following code legal in C++11? int main() { auto int x; } Prasoon Saurav No the code is ill-formed in C++11. auto in C++11 would be used to deduce the type of a variable from its initializer and it can't be used as a storage class specifier. Correct Usage int main() { auto x = 12; // x is an int auto y = 12.3; // y is a double } auto int x; is circular - you are literally declaring the type as an int . given that you had this information - there is no reason to not simply use: int x; if you wanted to declare x the type

Can the 'auto' keyword be used as a storage class specifier in C++11?

北慕城南 提交于 2019-12-01 14:32:38
问题 Can the auto keyword be used as a storage class specifier in C++11? Is the following code legal in C++11? int main() { auto int x; } 回答1: No the code is ill-formed in C++11. auto in C++11 would be used to deduce the type of a variable from its initializer and it can't be used as a storage class specifier. Correct Usage int main() { auto x = 12; // x is an int auto y = 12.3; // y is a double } 回答2: auto int x; is circular - you are literally declaring the type as an int . given that you had