Sorry, forgot, was it possible to declare but not define local (inside function) variable in C/C++?
Looks like it is not possible, since it is impossible to access local variable from somewhere else, except this function.
Then, what is it correct to say: variable should be "declared" before use or "defined" before use?
Sorry, forgot, was it possible to declare but not define local (inside function) variable in C/C++?
No, local (block-scope) variables only have declarations. They are instantiated when the program reaches the declaration, with no need for a separate definition to control instantiation.
Then, what is it correct to say: variable should be "declared" before use or "defined" before use?
Variables, and named entities in general, must be declared before use. Not all variables have separate definitions; if they do, then the definition doesn't usually need to be available to use the variable.
Global (namespace-scope) and static member variables (depending on use) need definitions, to determine which translation unit is responsible for instantiating them. Global variables can also be declared separately from their definition, in their namespace or in functions inside that namespace.
For local variables, there is no concept of definition. They are just declared and are conditionally instantiated according to the flow of program.
Separate declaration and definition are used for global variables and functions.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I know the keyword in c has two usage:
modify variables
modify global variables
this usage limited the scope of global variable range from the point that defined to the end of the file.
modify local variables
this usage limited the scope of local variable in the function that defined, but also remained in the static area of memory
modify functions
this means can only invoke the function in the file it's defined.
and in c++, beside the usage in c, static also used to modify the data member and function member of class. This usage limited the member belong to the class instead of the objects of the class.
I want to know are there anything else usage of static in c/c++?
static is probably the most confusingly overloaded keyword in both C and C++. It means different things in different places.
Within functions, static is a storage class, denoting variables which exist for the lifetime of the programme. So saying
void f() {
static int i = 0;
}
says that the value of i will be preserved between calls to f(). Other storage classes are the default auto (but beware the change in meaning in C++11), extern, and register, plus thread_local in C11/C++11.
At file scope (or namespace scope in C++), static is a linkage specifier. Functions and variables marked static in this way have internal linkage, and so are local to the current translation unit. What this means is that a function like
static int f() {
return 3;
}
can only be referenced by other functions inside the same .c file. This usage of static was deprecated in C++03 in favour of unnamed namespaces. I read somewhere it was undeprecated again in C++11.
In C++, when applied to a member function or member variable of a class, it means that the function or variable does not need a class instance in order to be accessed. There is little different between "class static" member functions/variables and global functions/variable in terms of implementation, except that C++ class access specifiers apply to members.
One last one: in C99 (but not C++), static can be used within an array function parameter, like so:
void f(int a[static 4]) {
}
this specifies that the parameter a must by an integer array of size at least 4.
I think that's all of them, but let me know in the comments if there are any I've forgotten!
Static In the C language family, a static variable is one that exists for the lifetime of a compilation
unit (a source file or module). A static variable can be declared module-wide, and thus be
accessed by all functions defined within the same source file. Such a static variable cannot
be directly accessed from other modules, but inner-module API can pass pointers to static variables and
modify those through pointers. A static variable can also be declared within a function body, where the
usual scope rules apply. A static variable declared within a function is only initialized when
the module is initialized (typically when the application loads), and preserves its values over multiple
invocations of the function that contains the definition.
In C++, a static variable can also be a member of a class definition. Access to a static member
variable is governed by the standard access modifiers (private, public, protected), but all instances of
this class share the same static variable, and share the same value. Modifying the value of this variable
affects all objects of the class. VolatileThe volatile keyword is something all together different, and not in any way
an opposite to static. A static variable may or may not be declared volatile, just as a global or local variable
can be. The volatile keyword is a hint informing the compiler that the variable's value
might change without the compiler's knowledge. Therefore, the compiler's code optimizer cannot make assumptions
about the variable's current value, and must always (re-) read the variable's content.
In reference to c++ -> The static keyword can be used to declare variables, functions, class data members and class functions.
Here are the common usages in different scenarios ( ref from MSDN )
When you declare a variable or function at file scope (global and/or namespace scope), the static keyword specifies that the variable or function has internal linkage. When you declare a variable, the variable has static duration and the compiler initializes it to 0 unless you specify another value.
When you declare a variable in a function, the static keyword specifies that the variable retains its state between calls to that function.
When you declare a data member in a class declaration, the static keyword specifies that one copy of the member is shared by all instances of the class. A static data member must be defined at file scope. An integral data member that you declare as const static can have an initializer.
When you declare a member function in a class declaration, the static keyword specifies that the function is shared by all instances of the class. A static member function cannot access an instance member because the function does not have an implicit this pointer. To access an instance member, declare the function with a parameter that is an instance pointer or reference.
You cannot declare the members of a union as static. However, a globally declared anonymous union must be explicitly declared static.
check following also:
The static keyword and its various uses in C++
static keyword usage
I have some global variables in my program and I tried to avoid them but I don't know how..
Here is my program:
Here
Is there any alternative to avoid those variables?
And any other remark is appriciated too
I think you have multiple possibilities.
Create a class and put your functions and your global variables inside (preferred way)
If you just care about name conflicts you can use unnamed namespaces
It your global variable is used in only one function, declare it as a static variable inside that function (its value will be kept between calls)
What about to use a STATIC for i_mouse and depthimagemouse inside the function onMouse (you use it in just 1 function) and a #define for the constant variables?
I think it can be a good solution in your case.
So, I was trying to make a list of the different types of variables and wanted to confirm if my classification was correct or not.
Three types, I say:
1.Static/Class
2.Instance
3.Global
I understand that global variables are declared outside the class definition but static variables must be declared (not necessarily instantiated) within the class definition. Are there any more important differences between the class and global variables?
A class is not synonymous with a static variable. Any variable can be declared static. Where it's declared will impact what it actually means.
What Does static Mean?
A class really isn't a variable type, it's how you define a new variable type. int is a type of variable, and Foo is a type of variable once you've defined it with the class keyword.
Instance really makes no sense. You have an instance of a variable, (e.g. an instance of an integer) but that's not a variable type.
In the terms of "types of variables", as you're looking at them, I would say there are two types: global and local. Static just has too many meanings to be included in that list.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I know the keyword in c has two usage:
modify variables
modify global variables
this usage limited the scope of global variable range from the point that defined to the end of the file.
modify local variables
this usage limited the scope of local variable in the function that defined, but also remained in the static area of memory
modify functions
this means can only invoke the function in the file it's defined.
and in c++, beside the usage in c, static also used to modify the data member and function member of class. This usage limited the member belong to the class instead of the objects of the class.
I want to know are there anything else usage of static in c/c++?
static is probably the most confusingly overloaded keyword in both C and C++. It means different things in different places.
Within functions, static is a storage class, denoting variables which exist for the lifetime of the programme. So saying
void f() {
static int i = 0;
}
says that the value of i will be preserved between calls to f(). Other storage classes are the default auto (but beware the change in meaning in C++11), extern, and register, plus thread_local in C11/C++11.
At file scope (or namespace scope in C++), static is a linkage specifier. Functions and variables marked static in this way have internal linkage, and so are local to the current translation unit. What this means is that a function like
static int f() {
return 3;
}
can only be referenced by other functions inside the same .c file. This usage of static was deprecated in C++03 in favour of unnamed namespaces. I read somewhere it was undeprecated again in C++11.
In C++, when applied to a member function or member variable of a class, it means that the function or variable does not need a class instance in order to be accessed. There is little different between "class static" member functions/variables and global functions/variable in terms of implementation, except that C++ class access specifiers apply to members.
One last one: in C99 (but not C++), static can be used within an array function parameter, like so:
void f(int a[static 4]) {
}
this specifies that the parameter a must by an integer array of size at least 4.
I think that's all of them, but let me know in the comments if there are any I've forgotten!
Static In the C language family, a static variable is one that exists for the lifetime of a compilation
unit (a source file or module). A static variable can be declared module-wide, and thus be
accessed by all functions defined within the same source file. Such a static variable cannot
be directly accessed from other modules, but inner-module API can pass pointers to static variables and
modify those through pointers. A static variable can also be declared within a function body, where the
usual scope rules apply. A static variable declared within a function is only initialized when
the module is initialized (typically when the application loads), and preserves its values over multiple
invocations of the function that contains the definition.
In C++, a static variable can also be a member of a class definition. Access to a static member
variable is governed by the standard access modifiers (private, public, protected), but all instances of
this class share the same static variable, and share the same value. Modifying the value of this variable
affects all objects of the class. VolatileThe volatile keyword is something all together different, and not in any way
an opposite to static. A static variable may or may not be declared volatile, just as a global or local variable
can be. The volatile keyword is a hint informing the compiler that the variable's value
might change without the compiler's knowledge. Therefore, the compiler's code optimizer cannot make assumptions
about the variable's current value, and must always (re-) read the variable's content.
In reference to c++ -> The static keyword can be used to declare variables, functions, class data members and class functions.
Here are the common usages in different scenarios ( ref from MSDN )
When you declare a variable or function at file scope (global and/or namespace scope), the static keyword specifies that the variable or function has internal linkage. When you declare a variable, the variable has static duration and the compiler initializes it to 0 unless you specify another value.
When you declare a variable in a function, the static keyword specifies that the variable retains its state between calls to that function.
When you declare a data member in a class declaration, the static keyword specifies that one copy of the member is shared by all instances of the class. A static data member must be defined at file scope. An integral data member that you declare as const static can have an initializer.
When you declare a member function in a class declaration, the static keyword specifies that the function is shared by all instances of the class. A static member function cannot access an instance member because the function does not have an implicit this pointer. To access an instance member, declare the function with a parameter that is an instance pointer or reference.
You cannot declare the members of a union as static. However, a globally declared anonymous union must be explicitly declared static.
check following also:
The static keyword and its various uses in C++
static keyword usage
I have a requirement where a (const) variable should be available throughout an entire cpp which consists of several classes. I have decided to use a namespace to solve the problem, but unsure about the following:
Do I need to define this variable as static?
Is it true that I can avoid making the variable static only if I go with an unnamed namespace?
You don't need to define the variable as static, or in an anonymous namespace. However, if you're not using this object outside of the file it's defined in, it's a good idea, to reduce namespace pollution and speed up links (by reducing how many symbols need to be considered by the linker).
If you declare a variable in an anonymous namespace, it will be effectively static. There's no need to actually make it static as well (although you can if you like). The advantage of anonymous namespaces is you can also define types (classes, structs, enums, typedefs) as well as static variables and functions.