answersLogoWhite

0


Best Answer

A variable is declared by declaring its name and type. Once a variable is declared you can use it in expressions, whether to assign a value or use the value. The name is also a reference to the memory address allocated to the variable, thus you can take the address of the name if required.

User Avatar

Wiki User

8y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

7y ago

There are two types of variable in C Programming: named variables and anonymous variables. A named variable is a memory address that we can refer to by a user-defined name. This is useful because we cannot guarantee that a given variable will be allocated the exact same memory address on every execution, particularly if that variable is allocated on the stack (local variables). By referring to the variable by name in our code, the compiler can calculate the actual offset address at compile time (for static variables) or inject code to compute the offset address at runtime (for local variables). The name also makes it much easier for the programmer as they don't have to remember which variable is allocated to which offset address; the compiler can do that for us.

However, in order for the compiler to calculate the offsets for each named variable, it has to know how much memory is allocated to each of them. It also has to know what the value stored at that memory address signifies because the same representation can have infinite meanings. For instance, the value 65 is a decimal value, but it's also the ASCII character code for the letter 'A'. Both have the same binary representation (0100001), but that binary representation is only meaningful if we know what it actually represents. Perhaps the first four bits should be treated separately from the last four, in which case it may represent two completely separate decimal values: 4 and 1.

In order for the compiler to know which operations are valid for a given name it must know the name's type. The type not only determines how the memory should be interpreted it also determines its length. This is achieved by declaring the name to the compiler. For example:

int x;

Here we've declared the name x to be of type int. Note that no memory is actually allocated at this point; memory is only allocated when we actually use the name. However, the compiler now knows what x means and how much memory to allocate to it.

A variable's typename (such as int) is also a name. As with variable names, user-defined typenames must also be declared before they can be used. An int is a built-in type (already known to the compiler) so we don't require a declaration for it, but consider the following declaration:

T y; // error - T is undefined

Here we have declared y to be of type T but we haven't actually told the compiler what T represents. So we must declare it before we can use it:

struct T { char c;

double d;

};

T y; // error - T is undefined

This is one of the more annoying aspects of C because we "forgot" that T is not the typename, the actual typename is struct T.

struct T y; // ok

To avoid this annoyance, we typically use an alias:

typedef struct Tx {

char c;

double d;

} T;

Here we've declared T to be an alias for the type struct Tx. Although we've introduced yet another name (struct Tx), typenames don't consume any memory (only instances of the type use memory) and we can now use the alias more intuitively:

T y; // ok -- y is of type struct Tx

Declarations should always be placed at the top of the block of code in which they are used. this is determined by the names scope. For instance:

int a; // global scope

void f () {

int b; // local scope

// ...

}

Note that f is also a name; the name of a function. Like variable names, function names must also be declared before they are used (invoked). However, a definition is also a declaration so as long as the function definition is above any invocations of that function, we don't need a separate declaration (known as a forward declaration).

In cases where two functions have a circular dependency upon each other, at least one of them must be forward-declared. Consider the following:

void x (bool b) {

if (!b) y (b); // error: the name y is undefined

}

void y (bool b) {

if (b) x (b); // ok

}

To fix the above error we must forward declare the y name:

void y (bool); // forward declaration (required)

void x (bool b) {

if (!b) y (b); // ok

}

void y (bool b) {

if (b) x (b); // ok

}

We could also forward-declare the x function but it is not necessary in this case. However, it is good etiquette to declare all functions before defining them. For functions that are used by more than one translation unit, their declarations should be placed in header files that can then be included wherever required. this ensures the declarations are consistent across all translation units.

At the start of this answer I mentioned anonymous variables. Anonymous variables are variables that are allocated at runtime (as opposed to compile time). These variables are always allocated on the heap. As such, their offsets cannot be known in advance and cannot be calculated by the compiler; they could literally be allocated anywhere outside the process' address space. Since thy have no name, the only way we can refer to them is by their runtime addresses. To keep track of these addresses we use pointer variables:

in* p = malloc (1000 * sizeof (int));

Here we've allocated an array of one thousand integers. Imagine naming each and every one of them individually! The only variable name we actually use here is p, however that is the name of the pointer that stores the address of the allocation, not the name of the allocation itself. However, given that the point refers to the start address of the allocation, and that allocation contains integers (as denoted by the pointer's type, int), we can use array suffix notation on the pointer just as if it really were the name of the array:

p[100] = 42; // assign the value 42 to the anonymous variable at offset 100

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

you have to give a statement in the following syntax

datatype variable;

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

we can declare a variable using the following syntax

datatype variablename;

actually variable is the name given to the memory location which store values.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

Simplest case:

the;

A more difficult case:

the [];

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the syntax to declare the variable in c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Why can you not declare void variables in c?

All names must be declared in C so that the compiler knows what each name represents even if the definition of that name has not yet been compiled. Without a declaration, the compiler cannot know what operations are permitted upon the name and therefore cannot notify the programmer of syntax errors.


Why all variables are declared with in the function?

It is not necessary to to declare variables inside the function in C. If you declare a variable inside a function, the variable becomes local for the function and another variable of same name can be declared in any other function, but you can not use the variable declared in other function. When you declare any variable outside the function body then the variable becomes global and can be used in any function of the program. Note: errno is an example for a variable declared outside any function.


Why you need to declare variable 1st in turbo c plus plus?

In C++ all names (including variables) must be declared before they can be used.


What are the functions of a storage class?

There are four storage class specifiers in C and C++. These are - 1. auto : The storage specifier auto refers to automatic variable declaration. The life of an automatic variable is the time during which its parent function is running. The scope of an auto variable is the function scope. They can be accessed only from their parent functions. Syntax : auto int a; 2. register : A register variable has all the characteristics of an auto variable. The only difference is that auto variable uses the main memory to store data and register uses the CPU registers. 3. extern : This storage specifier is used to declare a global variable. The life of these variables is the time during which the program runs.


What is the syntax of foreach in c?

There is no 'foreach' in C

Related questions

How you declare a variable in c language?

you have to give a statement in the following syntax datatype variable;


What is syntax in c plus plus for declaring a variable?

type variable {[optional array size]} {= optional initializer};


How do you increment hex value in c?

First a variable in numeric data type is to be defined. Then increment the number using the ++ command syntax of C,


How do you use define in C?

Just type declare then the variable that you desire to assigned a certain constant value on it. Just type declare then the variable that you desire to assigned a certain constant value on it.


Why variable name is not start with digit in programming language?

Because the compilers do not allow that, you would get a 'Syntax error' if you tried.(In FORTH, mind you, variable names can start with digits, for example:VARIABLE 0A ( declare variable )10 0A ! ( assign value )0A @ ( fetch value ))


Why can you not declare void variables in c?

All names must be declared in C so that the compiler knows what each name represents even if the definition of that name has not yet been compiled. Without a declaration, the compiler cannot know what operations are permitted upon the name and therefore cannot notify the programmer of syntax errors.


What is syntax in declaring a variable?

type your answer here


Is it corrrect to declare a variable in c or c plus plus as two or three words for eg int seg no?

No., If you want to declare you jus use _ in between so the declaration will be like int seg_no;


Why all variables are declared with in the function?

It is not necessary to to declare variables inside the function in C. If you declare a variable inside a function, the variable becomes local for the function and another variable of same name can be declared in any other function, but you can not use the variable declared in other function. When you declare any variable outside the function body then the variable becomes global and can be used in any function of the program. Note: errno is an example for a variable declared outside any function.


What are the32 key words in c turbo?

key words are the specfic command of the program like variable name, syntax, name of loops.


Why you need to declare variable 1st in turbo c plus plus?

In C++ all names (including variables) must be declared before they can be used.


What is the difference between normal variable and register variable in c?

In C/C++ when we declare a variable; e.g int var; for this variable (i.e. var) memory is being reserved in RAM (i.e out side processor). If we declare variable like that; register int var2; for this variable memory is being reserved in register of CPU (i.e. withing processor) But register variables are discouraged because processor has to work with registers..... Note: strictly speaking, storage class 'register' means: dear compiler, you might optimize this variable into register, as I won't ever request its address. But of course, it's up to you to decide.