|
-
August 29th, 2006, 11:33 PM
#1
why "const" is not woking with "C" but working with "C++"?
Hi,
I created *.c file in VC++6.0 and pasted the follwing code. it is not compiling. but the same code compiling with *.cpp file in the same VC++.
#include <stdio.h>
const int MAX_LENGTH = 100;
struct CAR
{
char petName[MAX_LENGTH];
int maxSpeed;
int currSpeed;
};
void main()
{
struct CAR myCar;
}
:error C2057: expected constant expression
:error C2229: struct 'CAR' has an illegal zero-sized array
Regards,
Rameshkanth
BORN TO WIN
-
August 29th, 2006, 11:41 PM
#2
Re: why "const" is not woking with "C" but working with "C++"?
According to the MSDN, C has more restrictive rules for that kind of thing... here's the example they give about what does and doesn't work.
Code:
// C2057b.c
#define ArraySize1 10
int main() {
const int ArraySize2 = 10;
int h[ArraySize2]; // C2057
int h[ArraySize1]; // OK
}
So, instead of using a const variable, use the #define macro and that should work just fine. And it'll work in C++ as well.
Oni Vagrant
-
August 30th, 2006, 12:54 AM
#3
Re: why "const" is not woking with "C" but working with "C++"?
I want to avoid the use of "#define" in C also. Bcas of http://podgoretsky.pri.ee/ftp/Docs/C.../EC/EI1_FR.HTM
So How to proceed further.
Regards,
Rameshkanth
BORN TO WIN
-
August 30th, 2006, 01:03 AM
#4
Re: why "const" is not woking with "C" but working with "C++"?
If I am right, const was not a part of C. It was added as a keyword to C much later.
void main()
{
struct CAR myCar;
}
int main.. not void main
So, instead of using a const variable, use the #define macro and that should work just fine. And it'll work in C++ as well.
In C++, one should prefer to use const, althought the #define works.
C++ program ran... C++ program crashed... C++ programmer quit !!   
Regards
Shaq
-
August 30th, 2006, 01:31 AM
#5
Re: why "const" is not woking with "C" but working with "C++"?
No, in C, the main returns void not int.
-
August 30th, 2006, 01:43 AM
#6
Re: why "const" is not woking with "C" but working with "C++"?
 Originally Posted by cilu
No, in C, the main returns void not int.
Not quite right according to Stroustrup. 
Bjarne Stroustrup's C++ Style and Technique FAQ
From his FAQ's
The definition void main() { /* ... */ }
is not and never has been C++, nor has it even been C. See the ISO C++ standard 3.6.1[2] or the ISO C standard 5.1.2.2.1.
C++ program ran... C++ program crashed... C++ programmer quit !!   
Regards
Shaq
-
August 30th, 2006, 02:10 AM
#7
Re: why "const" is not woking with "C" but working with "C++"?
if you want that your code worked as in C either as in C++, that use #define. Otherwise use in code for C #define, but for code C++ - const. I often use #define in C++ and never observed bad functioning the program.
___________________________________
excuse me for my English : )
excuse me for my english 
-
August 30th, 2006, 03:21 AM
#8
Re: why "const" is not woking with "C" but working with "C++"?
This topic isn't really about the return value of main but it returns int even in C. The rationale behind this is that the return value is used by applications that begin another process via the execv group of functions, actually C functions.
Now, for the purpose of magic numbers, there is no need to use #define even in C. Use an enum. That works in C too.
Code:
enum { eArraySize = 100 };
int main()
{
char text[ eArraySize ];
}
I am not sure though whether you can enjoy the same scoping in C as you can in C++ with regard to enums. Well I know that C has no namespaces. I also think you can't scope enums in function level. Pretty certain you can't scope them in structs. So in some ways you don't gain a lot on a #define.
C99 may have addressed some of these issues, so in C99 it may be possible to scope your enums. I think though that C99 also permits const int or const size_t as array size.
Why do so many people still use VC6, by the way?
-
August 30th, 2006, 03:36 AM
#9
Re: why "const" is not woking with "C" but working with "C++"?
"enum" ????
that is not seriously ))
___________________________________
excuse me for my English : )
excuse me for my english 
-
August 30th, 2006, 03:45 AM
#10
Re: why "const" is not woking with "C" but working with "C++"?
[QUOTE=Ukrainian]"enum" ????
that is not seriously ))
Yes, seriously. It is quite common to use enums to indicate "magic" numbers. Sometimes the values of enums have no real meaning, they are just labels, but that is not always the case.
In C you often use longer names to compensate for the lack of namespaces. Sometimes they would be too wordy to spell out so loads of acronyms come into play like MDPGArraySize
-
August 30th, 2006, 08:54 AM
#11
Re: why "const" is not woking with "C" but working with "C++"?
I hadn't thought of using an enum, but that's a pretty good idea.
I saw the error code and went to check what the MSDN had to say about it, and that's what I found. Turns out the OP wants to avoid #define as well, which is up to him. Enum is a viable alternative, and it can neatly encapsulate all the magic numbers of an application.
Another suggestion could be to make a sub namespace to hold the enum and any other global scope variables, to avoid polluting the global namespace. But that might be outside the scope of the OP's concern.
Oni Vagrant
-
August 30th, 2006, 08:55 AM
#12
Re: why "const" is not woking with "C" but working with "C++"?
Are there namespaces in C? Maybe in C99 but I don't know if the programmer can use C99.
-
August 30th, 2006, 09:04 AM
#13
Re: why "const" is not woking with "C" but working with "C++"?
Either way, it was something that came to mind and I decided to mention it.
Oni Vagrant
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|