473,893 Members | 1,588 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 #1
23 6661
Hans wrote on 05/02/05 :
Why all C/C++ guys write:
There is no such thing like 'C/C++'. There is C and there is C++. They
are different languages (e.g. in C, 'A' is an int and in C++, 'A' is a
char)
const char* str = "Hello";

or

const char[] str = "Hello";
Both are correct, but have different semantic (pointer vs array).
Why is this not a more elegant way:

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

??
Would be a type error sometimes.
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'.


A 'char', an 'unsigned' char and a 'signed char' are three different
types. The type for a string (an array of 'char' terminated by a 0) is
.... 'char'. Period.

Note that depending on the implementation, a 'char' car be signed or
unsigned. Use constants defined in <limits.h> to get the current range
values.

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

"C is a sharp tool"

Jul 23 '05 #2
Sam
> 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.
Jul 23 '05 #3
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
temp.cpp: In function `int main()':
temp.cpp:3: error: invalid conversion from `const char*' to `const unsigned
char*'
temp.cpp:4:2: warning: no newline at end of file

C:\c>cl temp.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.41013 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.

temp.cpp
temp.cpp(3) : error C2440: 'initializing' : cannot convert from 'const
char [6]'
to 'const unsigned char *'
Types pointed to are unrelated; conversion requires
reinterpret_cas t, C-
style cast or function-style cast

C:\c>


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #4

"Hans" <Ha**@replyingr ouptoavoidspam. com> wrote in message
news:cu******** **@news4.zwoll1 .ov.home.nl...
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'.


C does not require the ASCII character set.

-Mike
Jul 23 '05 #5
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.

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*).

??

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'.
You are right, but C (and associated function) still know that if you
every use that character for the purposes of dealing with a text
character, to treat the value as a positive value before comparing it
against the local character set. The very fact that your first sentence
claims it to be an ASCII character, means you will not likely use that
character value as simply an integer in computations that will be
malicious to your intents. If you want to use char storage simply to
hold integer values for computations, then specify signed/unsigned.

Of course, there are times when you want to do computations with chars
that hold text characters, and in those instances it usually never
matters whether char is equivalent to singed or unsigned. For example,
if you want to convert the text representation of a numeric character
to it's integer equivalent, you can do something like this:

char c = '7'; /* c holds value corresponding to '7' in character set */
char d = '4'; /* d holds character '4' */
int num;

num = c - d - '0'; /* num now holds integer value 3 */

In the above example, it didn't matter whether char was equivalent to
signed or unsigned, because we are using char to hold text characters
(and later use it in computations with other text characters in
computations).

Thanks!

Hans

It's true that some character sets can take up values from between 128
to 255, but that doesn't prevent an implementation from making 'char'
equivalent to 'signed char' (intead of 'unsigned char').

When dealing with text/strings, the signess of the char type has no
effect on normal use.

Say you have set up an array of ASCII characters corresponding to
decimal= 200. Will you ever use array for the sake of it's value 200,
or only as an array to represent the ASCII text characters (the latter
of course- read the first sentence of the paragraph)? Sure, the chars
have integer values of -73, but when you want deal with strings/text
their context automatically tells associated functions to treat
whatever is held in the character variables as a positive quantity.

For example, putchar(-73) <=== try that

It doesn't matter that we're sending an integer value of -73, putchar
knows that it must be treated as an unsigned positive value (puthcar
accepts integers, even when we hand it a char variable, it's promoted
an integer).

So when using a char type to represent a character, it doesn't matter
whether it is equivalent to signed or unsigned char. On the other
hand, if you want to use a char type to simple act as a small integer,
then you should specify a qualifier (signed or unsigned), because the
contexts you will use these objects in will probably rely on their
signess.

Jul 23 '05 #6

Luke Wu wrote:
Hans wrote:

Say you have set up an array of ASCII characters corresponding to
decimal= 200. Will you ever use array for the sake of it's value 200,
or only as an array to represent the ASCII text characters (the latter of course- read the first sentence of the paragraph)? Sure, the chars have integer values of -73, but when you want deal with strings/text
their context automatically tells associated functions to treat
whatever is held in the character variables as a positive quantity.

For example, putchar(-73) <=== try that

by -73 I meant -56 :-)


It doesn't matter that we're sending an integer value of -73, putchar
knows that it must be treated as an unsigned positive value (puthcar
accepts integers, even when we hand it a char variable, it's promoted
an integer).


Jul 23 '05 #7
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.
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 [])

??

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


--
Remove '.nospam' from e-mail address to reply by e-mail
Jul 23 '05 #8

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*".

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 #9
James McIninch wrote:

A string literal is of type 'const char *' in C.
No, it isn't. It has an array of char, with static storage duration.
It's "const" only in the sense that modifying it invokes undefined
behaviour; "constant" or even "read-only" would be a better
description.
Best to declare the
variable as being the same type as the data it is storing.


Then do this:

static char foo[] = "string literal";

but not everybody here will agree that this is always best!
Jul 23 '05 #10

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
2357
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
5384
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
4615
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
9830
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
10837
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...
1
10927
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9645
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...
1
8021
isladogs
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...
0
7173
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
5858
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6066
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4280
muto222
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.