473,471 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

What's wrong about const?

test1.c
------------------------------
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
const int MAX_CHAR_NUM=10;
char name[MAX_CHAR_NUM]="Computer";
printf("My name is %s.\n", name);
exit(EXIT_SUCCESS);
}

test2.c
-----------------------------
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
enum width {MAX_CHAR_NUM=10, OTHER};
char name[MAX_CHAR_NUM]="Computer";
printf("My name is %s.\n", name);
exit(EXIT_SUCCESS);
}

Test1 can not be compiled.
And test2.c can.
Why?
Nov 14 '05 #1
6 1478
sh***@133sh.com wrote:
test1.c
------------------------------
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
const int MAX_CHAR_NUM=10;
char name[MAX_CHAR_NUM]="Computer";
printf("My name is %s.\n", name);
exit(EXIT_SUCCESS);
}

Test1 can not be compiled.


Does this answer your question?
http://www.eskimo.com/~scs/C-faq/q11.8.html

Nov 14 '05 #2
Enum const is compilation time entity where us const variable is runtime
entity

I mean const variable value can take at the time of code is running not at
compilation and array size is calculated at the time of compilation

but in ENUM const compiler can get the value at compilation time

I hope it may help
<sh***@133sh.com> wrote in message
news:86**************************@posting.google.c om...
test1.c
------------------------------
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
const int MAX_CHAR_NUM=10;
char name[MAX_CHAR_NUM]="Computer";
printf("My name is %s.\n", name);
exit(EXIT_SUCCESS);
}

test2.c
-----------------------------
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
enum width {MAX_CHAR_NUM=10, OTHER};
char name[MAX_CHAR_NUM]="Computer";
printf("My name is %s.\n", name);
exit(EXIT_SUCCESS);
}

Test1 can not be compiled.
And test2.c can.
Why?

Nov 14 '05 #3
sh***@133sh.com (sh***@133sh.com) wrote in message news:<86**************************@posting.google. com>...
test1.c
------------------------------
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
const int MAX_CHAR_NUM=10;
char name[MAX_CHAR_NUM]="Computer";
printf("My name is %s.\n", name);
exit(EXIT_SUCCESS);
}

test2.c
-----------------------------
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
enum width {MAX_CHAR_NUM=10, OTHER};
char name[MAX_CHAR_NUM]="Computer";
printf("My name is %s.\n", name);
exit(EXIT_SUCCESS);
}

Test1 can not be compiled.
And test2.c can.
Why?


const in C does not create a compile time const.
Don't confuse this with C++. This is one of the
many areas in which they vary.
Nov 14 '05 #4
On 21 Jan 2004 02:42:55 -0800, sh***@133sh.com (sh***@133sh.com) wrote
in comp.lang.c:
test1.c
------------------------------
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
const int MAX_CHAR_NUM=10;
char name[MAX_CHAR_NUM]="Computer";
printf("My name is %s.\n", name);
exit(EXIT_SUCCESS);
}

test2.c
-----------------------------
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
enum width {MAX_CHAR_NUM=10, OTHER};
char name[MAX_CHAR_NUM]="Computer";
printf("My name is %s.\n", name);
exit(EXIT_SUCCESS);
}

Test1 can not be compiled.
And test2.c can.
Why?


What's wrong with you reading the answers to the same question when
you asked it yesterday. The first program can't be compiled because
the C language standard says it can't.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #5
sh***@133sh.com <sh***@133sh.com> wrote:
test1.c
------------------------------
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
const int MAX_CHAR_NUM=10;
char name[MAX_CHAR_NUM]="Computer";
printf("My name is %s.\n", name);
exit(EXIT_SUCCESS);
}

test2.c
-----------------------------
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
enum width {MAX_CHAR_NUM=10, OTHER};
char name[MAX_CHAR_NUM]="Computer";
printf("My name is %s.\n", name);
exit(EXIT_SUCCESS);
}

Test1 can not be compiled.
And test2.c can.
Why?


Although one would think that using the keyword const makes the object
a constant that is not essentially true. In an array declaration
(definition) you must use a constant that can be evaluated at compile
time, but not at run time. An enumeration value is such a constant, but
const is just qualifying the object such that it cannot (should not) be
changed directly or indirectly, but there will be an object. The value
of an object cannot be evaluated at runtime.

As a rule, if any of the operands of an expression can be pointed to
with a pointer (or the address can be evaluated) the expression cannot
be constant.
--
Z (Zo**********@daimlerchrysler.com)
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond
Nov 14 '05 #6
Zoran Cutura <zo**********@daimlerchrysler.com> wrote:
changed directly or indirectly, but there will be an object. The value
of an object cannot be evaluated at runtime.


That should be "compiletime"

--
Z (Zo**********@daimlerchrysler.com)
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

72
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
1
by: Tony Johansson | last post by:
This class template and main works perfectly fine. But could be better. I have this class template called Handle that has a pointer declared as T* body; As you can see I have a reference counter...
4
by: Oliver | last post by:
Hello ! I am trying to get the unrar.dll working in C#... it seems that I correctly imported the functions as the first 2 function work without problem (RAROpenArchive & RARGetDLLVersion)......
10
by: Protoman | last post by:
Could you tell me what's wrong with this program, it doesn't compile: #include <iostream> #include <cstdlib> using namespace std; class Everything { public: static Everything* Instance()
15
by: David White | last post by:
The size of a struct can be affected by compiler packing. Suppose you need it to be a specific value for some reason (e.g., in firmware). How can you get the compiler to generate an error for the...
9
by: Jacek Dziedzic | last post by:
Hi! I often find that my programs need to store information on "current mode of something" with two or at most several mutually exclusive "modes" to choose from, e.g. - datafile: is it in a)...
4
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader =...
10
by: Jim Langston | last post by:
I use a game engine using MSVC++ .net 2003 and have no problems. Some users of DevC++ who use the same engine crash at times when a copy of this structure is the return variable. I don't have...
7
by: fl | last post by:
Hi, I am learning C++ with C++ primer written by Lippman. The first output line: cout << s1 << endl; works right. The second is wrong. I find the problem is that s1 is destroyed in the call from...
16
by: John Doe | last post by:
Hi, I wrote a small class to enumerate available networks on a smartphone : class CNetwork { public: CNetwork() {}; CNetwork(CString& netName, GUID netguid): _netname(netName),...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.