473,795 Members | 3,457 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why is "Hello World" const char* ?

Hello,

Why all C/C++ guys write:

const char* str = "Hello";

or

const char[] str = "Hello";

Why is this not a more elegant way:

const unsigned char* str = "Hello"; (or [])

??

My Reasson: have you ever seen ASCII-Code: -50? The extended ASCII Set
(>127) would be represented by a negative number if you use 'char'
instead of 'unsigned char'.

Thanks!

Hans
Jul 23 '05
23 6641

jo*******@my-deja.com wrote:
Hans wrote:
Hello,

Why all C/C++ guys write:

const char* str = "Hello";

C++ guys don't use char arrays or pointers to char for text data if
they can help it; that's what the std::string datatype is for.
or

const char[] str = "Hello";

Why is this not a more elegant way:

const unsigned char* str = "Hello"; (or [])


Because the type of a string literal is "const char *", not "const
unsigned char*".


Your statement is incorrect. Neve A string literal is an array of
(const char) representing each of the characters within quotation " "
and an extra element thereafter to terminate it: NUL. rtheless, when
using a string literal in quotations " " in value contexts, like
assignments, C automatically replaces the string literal (which at this
point in runtime or compile time is guaranteed to be allocated already)
with a pointer value pointing to its first character (to take part in
the expression). Since the first character is of type (const char) as
we mentioned earlier, the type of the pointer value is (const char *)-
pointer to const char.

Try not to say "a string literal is a pointer to (const) char," because
it is an incorrect statement (even though people will understand it,
they might think less of you).

Secondly, the plain, unqualified char type may be signed or unsigned,
or a mixture of both, depending on the implementation. The char type
is not treated exactly the same as other signed and unsigned integral
types.
??

My Reasson: have you ever seen ASCII-Code: -50? The extended ASCII

Set
(>127) would be represented by a negative number if you use 'char'
instead of 'unsigned char'.

Thanks!

Hans


Jul 23 '05 #11
James McIninch <ja************ *******@comcast .net> writes:
A string literal is of type 'const char *' in C.


No, it isn't, it's of type 'char *' (but attempting to modify the
content invokes undefined behavior). This may be different in C++ --
which is one of the reasons cross-posting to comp.lang.c and
comp.lang.c++ is rarely a good idea.

I haven't set followups on this reply, but think carefully when you
post.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 23 '05 #12
Sam wrote:
.... snip ...
It is not important whether char is signed or unsigned here. If
you do some maths things with char like
char a;
a = 127;
a++;
if(a > 0)....
whether it is signed or not may be a problem. But when you just
wanna handle text, it makes no difference.


For just one example, it matters when classifying chars with the
routines in ctype.h. Use of signed chars can cause UB.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

Jul 23 '05 #13
On 5 Feb 2005 09:49:26 -0800, "Luke Wu" <Lo***********@ gmail.com>
wrote:
Hans wrote:
Hello,

Why all C/C++ guys write:

const char* str = "Hello";

or

const char[] str = "Hello";

Why is this not a more elegant way:

const unsigned char* str = "Hello"; (or [])

In C, a string literal (like "Hello") in value context (like the right
side of an assignment= operator) evaluates to a pointer value of type
(const char*) that points to the first character.


Even though the string literal is not modifiable, the pointer is of
type char*, without the const.

snip
<<Remove the del for email>>
Jul 23 '05 #14
On Sat, 05 Feb 2005 14:45:11 -0500, James McIninch
<ja************ *******@comcast .net> wrote:
A string literal is of type 'const char *' in C. Best to declare the
variable as being the same type as the data it is storing.


A string literal is of type array of char. You can verify this by
applying the sizeof operator to it. In most expressions, a string
literal evaluates to the address of the first char with type char*,
but still no const.
snip
<<Remove the del for email>>
Jul 23 '05 #15
On 5 Feb 2005 09:49:26 -0800, "Luke Wu" <Lo***********@ gmail.com>
wrote in comp.lang.c:
Hans wrote:
Hello,

Why all C/C++ guys write:

const char* str = "Hello";

or

const char[] str = "Hello";

Why is this not a more elegant way:

const unsigned char* str = "Hello"; (or [])
In C, a string literal (like "Hello") in value context (like the right
side of an assignment= operator) evaluates to a pointer value of type
(const char*) that points to the first character.


No, in C a string literal in ANY context is an array of char, not
const char, and the address of a string literal has the type "pointer
to char" and NOT "pointer to const char".

In C attempting to modify a string literal produces undefined behavior
because the C standard specifically says so, not because the literal
has the type "array of const char".
So you are mixing types here. You are trying to assign a VALUE of type
(const char*) to an LVALUE/OBJECT of type (const unsigned char*).


A pointer to a non-const object of any type 'T', or the address of a
non-const object of any type 'T', can always be assigned directly to a
pointer to const 'T', in both C and C++. So the issue is not the
const, it is the fact that a pointer to plain char is incompatible
with a pointer to unsigned char, regardless of whether plain char is
signed or not. That is what requires a cast.

--
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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 23 '05 #16
On 5 Feb 2005 12:55:31 -0800, "Luke Wu" <Lo***********@ gmail.com>
wrote in comp.lang.c:

jo*******@my-deja.com wrote:
Hans wrote:
Hello,

Why all C/C++ guys write:

const char* str = "Hello";

C++ guys don't use char arrays or pointers to char for text data if
they can help it; that's what the std::string datatype is for.
or

const char[] str = "Hello";

Why is this not a more elegant way:

const unsigned char* str = "Hello"; (or [])


Because the type of a string literal is "const char *", not "const
unsigned char*".


Your statement is incorrect. Neve A string literal is an array of
(const char) representing each of the characters within quotation " "
and an extra element thereafter to terminate it: NUL. rtheless, when
using a string literal in quotations " " in value contexts, like
assignments, C automatically replaces the string literal (which at this
point in runtime or compile time is guaranteed to be allocated already)
with a pointer value pointing to its first character (to take part in
the expression). Since the first character is of type (const char) as
we mentioned earlier, the type of the pointer value is (const char *)-
pointer to const char.


Since you are talking about C specifically here, you are just plain
wrong. The type of a string literal is "array of char", NOT "array of
const char". The address of a string literal has the type "pointer to
char", NOT "pointer to const char".

Either you think the rules of some other language apply to C, or you
just don't know C as well as you think you do.
Try not to say "a string literal is a pointer to (const) char," because
it is an incorrect statement (even though people will understand it,
they might think less of you).


Right, both because a string literal is not a pointer, and because its
address is a pointer to char, not a pointer to const char.

--
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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 23 '05 #17
Sam wrote:
Why is this not a more elegant way:

const unsigned char* str = "Hello"; (or [])

??

It is not important whether char is signed or unsigned here. If you do some
maths things with char like
char a;
a = 127;
a++;
if(a > 0)....
whether it is signed or not may be a problem. But when you just wanna handle
text, it makes no difference.


Really? Is the character é (130) within your definition of text?
--
Joe Wright mailto:jo****** **@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jul 23 '05 #18
Ioannis Vranos wrote on 05/02/05 :
Hans wrote:
Why is this not a more elegant way:

const unsigned char* str = "Hello"; (or [])


int main()
{
const unsigned char* str = "Hello";
}

The error messages say it all:

C:\c>g++ temp.cpp -o temp.exe


Wait a minute... We are talking C here. C++ is next door...

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++

Jul 23 '05 #19
Luke Wu wrote:
In C, a string literal (like "Hello") in value context (like the right
side of an assignment= operator) evaluates to a pointer value of type
(const char*) that points to the first character.


Nope, it evaluates to an array of char, which converts to pointer to
char. The characters are effectively const (it is undefined behvaior
to attempt to change them), but the type of the expression is not
const char*. (except when sizeof or & is applied to the array value).

It's effectively the same in C++, except C++ defines a formal array-to-pointer
conversion.
Jul 23 '05 #20

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

Similar topics

31
630
by: Hans | last post by:
Hello, Why all C/C++ guys write: const char* str = "Hello"; or const char str = "Hello";
3
2354
by: lovecreatesbeauty | last post by:
Both `K&R C, 2nd' and `C: A reference manual, 5th' introduce the "hello, world" thing using the name "string-constant". But `ISO/IEC 9899:TC2' does not include this kind of thing in section `A.1.5 Constants'.
10
5361
by: fei.liu | last post by:
Consider the following sample code char * ptr = "hello"; char carray = "hello"; int main(void){ } What does the standard have to say about the storage requirement about ptr and carray? Is it a fair statement that char *ptr will take 4 more bytes (on 32bit platform) in DATA segment? I have found
0
4612
by: devito | last post by:
hi there, for some days i try to build the boost.python tutorial "hello world" without bjam on winxp by using mingw. so i wrote a *.bat-file like the following: // --- snip -------------------------------------------------------------------------------------- @echo off SETLOCAL
0
9672
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, 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...
0
9519
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,...
0
10437
Oralloy
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...
0
10214
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 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...
0
10001
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9042
agi2029
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...
0
6780
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
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 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.