Questions tagged [variable-declaration]

In computer programming, variable declaration specifies properties of a variable (such as its type).

In computer programming, variable declaration specifies properties of a variable:

  • It associates a type and an identifier (or name) with the variable.
  • It allows the compiler to decide how much storage space to allocate for storage of the value associated with the identifier and to assign an address for each variable which can be used in code generation.
663 questions
26
votes
3 answers

Confusion about declaration and definition of static const data memebers

Scott Meyers writes in Effective Modern C++ (Item 30, at page 210) that there's no need to define integral static const data members in classes; declarations alone suffice, then the sample code is class Widget { public: static const…
Enlico
  • 23,259
  • 6
  • 48
  • 102
26
votes
3 answers

C# suffix behind numeric literal

I am new to C# and want to understand how values work. If I look at a normal integer value, it has 3 important parts in it: the type, name and value. int testInt = 3; | | | Type Name Value But when I see a float value it confuses me a…
user3772108
  • 854
  • 2
  • 14
  • 32
25
votes
5 answers

Can I declare variables of different types in the initialization of a for loop?

Why does this C++ code not compile under VS2010: for ( int a = 0, short b = 0; a < 10; ++a, ++b ) {} while this one does: short b = 0; for ( int a = 0; a < 10; ++a, ++b ) {} Is the declaration of two variables of different types inside the…
grzkv
  • 2,599
  • 3
  • 26
  • 37
25
votes
2 answers

Does C have One Definition Rule like C++?

Recently, I found out there are some cases that will absolutely violate the ODR of C++ but will be compiled OK in C compiler. For example, this wierd scenario (with me): Source 1 int var_global=-3; Source 2 #include #include…
Van Tr
  • 5,889
  • 2
  • 20
  • 44
21
votes
5 answers

Why does this C code compile?

#include int main() { int c = c; printf("c is %i\n", c); return 0; } I'm defining an integer variable called c, and I'm assigning its value to itself. But how can this even compile? c hasn't been initialized, so how can its…
Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
21
votes
9 answers

Declaring vs Initializing a variable?

I'm curious to know the difference between declaring a variable, and initializing a variable. e.g. var example; // this is declaring var example = "hi" // initializing? Or just "adding a value"? I don't think I'm right there, but what exactly is…
20
votes
2 answers

VB.NET equivalent for the C# 7 is operator declaration pattern

Is there a VB.NET equivalent to the C# 7 is operator declaration pattern? Note in particular the bmp in the following code sample: public void MyMethod(Object obj) { if (obj is Bitmap bmp) { // ... } } Or the short pattern…
VBobCat
  • 2,527
  • 4
  • 29
  • 56
20
votes
3 answers

declare extern variable within a C function?

I define a variable in a C file: int x, and I know I should use extern int x to declare it in other files if I want to use it in other files. My question is: where should I declare it in other files? Outside of all functions, // in file a.c: int…
Wang Tuma
  • 893
  • 5
  • 14
  • 24
18
votes
1 answer

Is long long a type in C?

I know the title seems quite stupid, but I think it's worth asking. Take this declaration(or definition, maybe) for example: _Thread_local long volatile static int _Atomic const long unsigned x = 10; I used to consider long long as a type, but if…
nalzok
  • 14,965
  • 21
  • 72
  • 139
17
votes
4 answers

what's wrong with declaring a variable inside if's condition?

Perhaps I am getting rusty (have been writing in Python recently). Why does this not compile? if ( (int i=f()) == 0) without the () around the int i=f() I get another, much more reasonable error of i is not being boolean. But that's why I wanted…
davka
  • 13,974
  • 11
  • 61
  • 86
17
votes
3 answers

How can I tell Visual Studio/Microsoft's C compiler to allow variable declarations after the first statement?

I have code that compiles on the GNUARM compiler, but Visual Studio 2010 issues errors. The issue involves declaring variables after the first statement in a C language file: main.c #include #include int main(void) { int i =…
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
17
votes
2 answers

Is jumping over a variable initialization ill-formed or does it cause undefined behaviour?

Consider this code: void foo() { goto bar; int x = 0; bar: ; } GCC and Clang reject it, because the jump to bar: bypasses variable initialization. MSVC doesn't complain at all (except using x after bar: causes a warning). We can do a…
HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
17
votes
2 answers

How to declare multiple variables with specifying type using As in VBA?

According to this documentation by Microsoft, the following code can be used to make sure; a, b, and c are all Single; x and y are both Double Dim a, b, c As Single, x, y As Double, i As Integer > a, b, and c are all Single; x and y are both…
Kouichi C. Nakamura
  • 850
  • 3
  • 10
  • 25
17
votes
3 answers

MYSQL Stored Procedures: Variable Declaration and Conditional Statements

I have looked over numerous tutorials, manuals and documentations, but I still can not get this to work. I am trying to create a stored procedure using phpMyAdmin. I cant seem to find the errors here, the sql errors are so vague... CREATE…
Vigs
  • 1,286
  • 3
  • 13
  • 30
15
votes
1 answer

Is "int (x), 1;" an ambiguous statement?

void f(int x) { int (x), 1; } Clang compiles it, GCC doesn't. Which compiler is correct?
geza
  • 28,403
  • 6
  • 61
  • 135
1 2
3
44 45