constants in C/C++


 
Thread Tools Search this Thread
Top Forums Programming constants in C/C++
# 1  
Old 08-22-2002
constants in C/C++

Hi all
My question is related to following sample code which tries to change consant value by pointers.(I know it is wrong practice but i am surprised by mis-behaviour)
The code:

#include <stdio.h>

int main()
{
const int x = 10;
int *y;
const int * const z = &x;

y = (int *)&x;
*y=11;
printf('%p %d ', y, *y);
printf('%p %d ', &x, x);
printf('%p %d ', z, *z);
return 0;
}

The output:

0012FF7C 11
0012FF7C 10
0012FF7C 11

The above output is when the program was compiled as cpp file and got
11
11
11
when compiled as "c" file.
Also
const int x=10;
int arr[x];
works with c++ compiler and not C.

My question is why is this mis-behaviour.
# 2  
Old 08-22-2002
Actually, you are misbehaving, not the compiler. When you violate the rules of a language any behavior by the compiler becomes legal. As for why this particular result happens, code like:
const int x = 10;
printf("%p %d \n", &x, x);

can be rewritten by the compiler as something like:
const int x = 10;
printf("%p 10 \n", &x);

Whether or not this happens is up the compiler. Most compilers have options to control optimizations. By fiddling with these options you may be able to induce both behaviors from both compilers. Or maybe not. "Garbage in, garbage out" applys heavily to compilers.
# 3  
Old 08-23-2002
I agree that compilers are free to optimise the way they like.But i was surprised to see exactly similar behaviour for c and c++ respectively on VC++ ,Turbo,gnu,aCC compilers.
All changed value of constant in C compilation but the value remained same in C++ compilation.

similarly for array index being declared with constant.

Why all the vendors opted for such behaviour made me suspect if their is some standard.

shobhit
Login or Register to Ask a Question

Previous Thread | Next Thread

3 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Operation with real variables and constants

Hello there, I'd like to define a variable b equal to 0.5/a where a=0.001, so I wrote something like that: a=0.001; let 'b=0.5/$a'; but it doesn't work... maybe because the variable a has a real value??? Any help will be appreciated!!!:D (1 Reply)
Discussion started by: Giordano Bruno
1 Replies

2. Programming

char constants vs. hard-coding

This might be a silly question, but I thought I'd ask anyway. If I'm writing in C, isn't it more efficient to, for instance, use constant character variable set to 'A' instead of hard-coding a character 'A'? Since it's only a single character instead of a string, it might not matter much. (10 Replies)
Discussion started by: cleopard
10 Replies

3. Shell Programming and Scripting

perl: eval and constants

is it possible to use eval to create constants in perl? i cannot seem to get anything to work, and my searches are turning up little to nothing. an example of what i am trying to do is this: 2 arrays: array 1: 'FOOD','NUMBER','OS' array 2: 'pizza','two','unix' loop through the arrays and... (5 Replies)
Discussion started by: effigy
5 Replies
Login or Register to Ask a Question