can anyone tell me whether we can use const variables for case ...eg I wrote the following code:

const int i=2;
int j=0;
switch(1)
{
    case i:printf("hii");
}

now ...this snippet is running properly in turbo c but not in dev cpp...
likewise if we use this const variable as array index during initialisation...would it work...
can anyone explain ...at what time memory is allocated for variables...

can anyone tell me whether we can use const variables for case ...

No, case labels must be a compile time constant. In C the const keyword doesn't make the variable truly constant, it just makes it read-only under normal circumstances. Turbo C might offer a extention to support non-constant labels, but you can't rely on that outside of Turbo C.

likewise if we use this const variable as array index during initialisation...would it work...

No. This works in C++ because C++ changed the semantics of const, but not C.

can anyone explain ...at what time memory is allocated for variables...

It depends on where you declare the variable, but for the most part variables are stored on the stack at runtime.

can anyone explain ...at what time memory is allocated for variables...

Intel based computers -- All local variables are actually allocated by the compiler immediately upon function entry, even the variables that are declared later within the function. (You will see this if you let your compiler produce an assembly language version of your program) The compiler will rearrange variable declarations so that they appear as if you had declared them all at the beginning of the function. Memory for these is released as soon as the function returns to its caller.

Memory for global variables are always allocated at compile-time and stored in a data segment that is completely separate from code segments. Memory for these are not released until the program itself is terminated.

Member Avatar for I_m_rude

@james sir,

this is a really awesome question asked. Will you please make yourself more clear ? please I also wana know this thing. what exaclty u meant by compile time constant ?

secondly, again i want to ask will you explain further when memory is allocated for variables ?

Member Avatar for I_m_rude

@dragon sir,
What exaclty data segment and code segment is ?

secondly, please tell me one thing, memoery alocated to variables is at run time or compile time ? is this concept is diffrenet for gloabal and local variables ?

please Tell this, I am dam excited now.
thanks in advance.

Here is a detailed explaination of code and data segments, and how the computer's memory is laid out. In a nutshell, programs are devided into two parts -- code and data. Code segment is where the executable statements reside, data segment is there all the variables and the program's stack resides.

memoery alocated to variables is at run time or compile time

All variables are actually allocated at runtime, that is to say that very little, if any, is actually put into the executable program that resides on disk. When the program is loaded into memory the program loader allocates memory for all global variables and then calls main() function. Memory for local variables are not allocated until the function is called, as I described in my previous post.

Programmers try to avoid global variables as much as possible because they can cause a lot of confusion. You can legitimately have both local and global variables with the same name, when that happens the local variable is used instead of the global variable. If the programmer isn't aware of this he can spend hours trying to figure out why the global variable isn't changed when its supposed to be (I've been there and done that :) )

what exaclty u meant by compile time constant ?

A compile time constant in C is essentially a literal value. 123, 5.6, and "test" are all compile time constants when placed in code, but any variable (even a const qualified variable) is not.

secondly, again i want to ask will you explain further when memory is allocated for variables ?

The times differ depending on where the variable is declared and how memory is being allocated. You can typically divide things into three different times:

  1. Static data resides in the executable and is allocated when the process is loaded into memory.

  2. Local variables are stored on the stack. The stack itself is allocated in toto when the process is loaded, but individual variables are doled out from the stack as they're reached during execution. There are details where some compilers differ, as Ancient Dragon mentioned, but you really don't need to concern yourself with that just yet.

  3. Dynamic data, such as that returned by malloc(), will be allocated on request.

Member Avatar for I_m_rude

but when i write

const int =10;

int b=10;

in this, do u mean b is a compile time constant and a is not ? but sir, what const has created the difference ? i mean const is also a constant at compile time, at run time and all the time. so it is also a constant. so why is it not compile time constant ?

thanks.

in this, do u mean b is a compile time constant and a is not ?

It is not a compile time constant.

but sir, what const has created the difference ?

The const keyword gives the compiler the chance to warn you if you accidentally try to modify the value of a variable that shouldn't be modified. It's a convenience to you, the programmer, in writing robust code.

I see that you're confused, so for now just remember that const isn't really constant, it's just read-only.

Member Avatar for I_m_rude

sir, but what is diffreence read-only and not constant ? I think the thing which we can only read is constant actually. am i right ? :-O

please help.

thanks.

Constant means it cannot be changed, but you've already learned how to change a const qualified value through a pointer, right?

As pertains to this issue, compile time constant basically means that the constant can be used as the size of an array or as a case label in a switch statement. If you can't do either of those two things, it's not a compile time constant.

Member Avatar for I_m_rude

ya! but when i asked you the question abonu changing the constant through the pointer, then you said that is undefined. So this means i can't do that thing as it is undefined totally. So it's not correct to do that. So how are you now saying that "you have learnt how to change qualified value through a pointer."? If it is undefined, that means we can't do that thing. Is that was the right way to change that ?

Secondly,

int a=10;

is it compile time constant ? how ?

thanks.

but when i asked you the question abonu changing the constant through the pointer, then you said that is undefined.

Undefined means the C standard places no restrictions on behavior. This is obviously quite different from disallowed, where the standard says clearly that you aren't allowed to do something. The former means anything could happen and you need not be warned in any way, while the latter will result in a diagnostic message.

So this means i can't do that thing as it is undefined totally. So it's not correct to do that.

It's not correct, but you can still do it.

int a=10;

is it compile time constant ? how ?

10 is a compile time constant, a is not.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.