As far as i know, keyword "const" is invented to replace "define" identifier.
But ...
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_SUCCE SS);
}
test2.c
-----------------------------
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
enum width {MAX_CHAR_NUM=1 0, OTHER};
char name[MAX_CHAR_NUM]="Computer";
printf("My name is %s.\n", name);
exit(EXIT_SUCCE SS);
}
why test2.c can be compiled and test1.c not?
And what's the difference between c const and c++ const?
Thanks. 2 4081 sh***@133sh.com (sh***@133sh.co m) wrote: As far as i know, keyword "const" is invented to replace "define" identifier.
It isn't. const is used to make an actual, in-memory object read-only,
while #define is used to create a compile-time, source code only, text
replacement.
test1.c
const int MAX_CHAR_NUM=10 ; char name[MAX_CHAR_NUM]="Computer";
test2.c
enum width {MAX_CHAR_NUM=1 0, OTHER}; char name[MAX_CHAR_NUM]="Computer";
why test2.c can be compiled and test1.c not?
Because enum constants are compile-time constants, whereas const objects
are read-only, but not, strictly speaking, constant. The name of the
qualifier is confusing, it's true.
And what's the difference between c const and c++ const?
C++ consts _are_ compile-time constant, AFAIK, but I'm not a C++ expert
so ICBW.
Richard
On 20 Jan 2004 03:59:47 -0800, sh***@133sh.com (sh***@133sh.co m)
wrote: As far as i know, keyword "const" is invented to replace "define" identifier.
No.
But ...
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_SUCCE SS); }
test2.c ----------------------------- #include <stdio.h> #include <stdlib.h>
int main(int argc, char **argv) { enum width {MAX_CHAR_NUM=1 0, OTHER}; char name[MAX_CHAR_NUM]="Computer"; printf("My name is %s.\n", name); exit(EXIT_SUCCE SS); }
why test2.c can be compiled and test1.c not?
Because in C89 a "const int" doesn't qualify as an expression for
array dimensioning while an enum does. An enum is an alias for some
int value known to the compiler from the definition of this enum while
a const int is not - even if the compiler "sees" the value to which it
is initialized.
And what's the difference between c const and c++ const?
One of the differences is that in C++ a const int may be used for
array dimensioning (if its value is "visible" to the compiler
according to a definition of "visible" given by the C++ Standard).
Specifically
const int MAX_CHAR_NUM=10 ;
char name[MAX_CHAR_NUM]="Computer";
would be OK in C++.
--
Horst This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Kelly Mandrake |
last post by:
Iv'e been reading tutorials and articles describeing operator
overloading as both member functions and friend functions. I don't
understand however the purpose of the friend on an operator when the
operator already has access to the private data of a class since it is
a member. So why add the keyword friend.
What I do understand is that declareing a class or function as a friend
alows the friend to access private data without...
|
by: Peggy Go |
last post by:
Hi! I've downloaded postgreqsql-base-7.2.4.tar.gz but it says in the
readme file that
"This distribution also contains several language bindings, including
C, Perl, Python, and Tcl, as well as a JDBC driver.
The ODBC and C++ interfaces have been moved to the PostgreSQL
Projects Web Site at http://gborg.postgresql.org for separate
maintenance. "
|
by: agentxx04 |
last post by:
Hi. Our assignment was to creat a program that can find the average, median
& mode of a #of integers. Here's my program:
#include<stdio.h>
int main()
{
int item;
int a, b, t, mode;
int median_index;
|
by: Bit byte |
last post by:
I have the following methods:
static void Foo::setBar(const Bar*) ; //store a copy of Bar
static const Bar* Foo::getBar(void) const ; //return an UNMODIFIABLE ptr
to our internal copy
In another part of my code , I retrieved and used Bar as follows:
....
const Bar* temp = NULL ;
|
by: porky008 |
last post by:
We are still going over pseudo code and we are working on arrays right
now. I am having some difficulty with the bubble sort algorithm. Can
some one give me a better example than this one? I do understand that
each pass the larger values are to drop to the bottom but the algorithm
is still throwing me off.
While the array A is not sorted
For K = 1 Step 1 To N - 1
If A A Then
Interchange A and A
End If
| |
by: eric |
last post by:
hello
i'm confused by an example in the book "Effective C++ Third Edition"
and would be grateful for some help. here's the code:
class Person {
public:
Person();
virtual ~Person(); // see item 7 for why this is virtual
...
|
by: Yin Zhu |
last post by:
why the answer is the second
?
#include <iostream.h>
void fn(const char *a)
{
cout<<"const"<<endl;
cout<<a<<endl;
}
|
by: arnuld |
last post by:
i am trying to understand arrays and char. Stroustrup says, this a
string literal:
"this is a string literal"
he says it is of type /const char/. he also says, because of keeping
compatibilities with previous definitions of C & C++, it is also a
CHAR POINTER a.k.a /char*/. however it is an error, which can not be
caught at untill runtime, to modify a string literal using such
pointer:
|
by: StephQ |
last post by:
According to:
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.4
, if my understanding is correct, in
template<typename T>
class Foo {
friend void func (const Foo<T>& foo);
};
template<typename T>
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |