The C Standard Library

  • Thread starter Frederick Gotham
  • Start date
F

Frederick Gotham

I gather that, rather than the C++ Standard defining the functionality of
the Standard Library features which it has in common with C, it makes
reference to a C Standard which defines their functionality.

Is the C Standard in question C89 (or C90 as some call it)?

Does the C++ Standard make any changes to the C Standard Library which it
inherits? For example, in C, the "toupper" function takes an argument of
int, and the value passed to it must be within range of an unsigned char.
Are things the same in C++? If so, the following program can invoke
undefined behaviour:

#include <cctype>

int main()
{
toupper('a');
}


Why? Because 'a' might be a negative number.

A better example would be the use of extended characters, which are far
more likely to be a negative number:

toupper( 'German sharp s' );


To remedy this, many C programmers always cast the argument:

toupper( (unsigned char)'German sharp s' );
 
T

Tom Widmer

Frederick said:
I gather that, rather than the C++ Standard defining the functionality of
the Standard Library features which it has in common with C, it makes
reference to a C Standard which defines their functionality.

Is the C Standard in question C89 (or C90 as some call it)?

Yes, with the 1995 amendment AM1.
Does the C++ Standard make any changes to the C Standard Library which it
inherits?

Yes, Annex C of the C++ standard describes the various changes, such as
the fact that wchar_t is no longer a typedef (which was a mistake, IMHO.
It would have been more consistent to call the new type wchar, and to
have kept wchar_t as a typedef (which is what _t usually designates)).
Other major changes are:
typedefs and macros from C that are now C++ keywords are removed from
the headers
<cstring> includes some const overloads
atexit, exit, abort and longjmp have different semantics
offsetof only works for a subset of types - POD types
malloc and friends are forbidden from calling ::eek:perator new.

For example, in C, the "toupper" function takes an argument of
int, and the value passed to it must be within range of an unsigned char.
Are things the same in C++?

Yes, the vast majority is unchanged.

Tom
 
V

Victor Bazarov

Frederick said:
I gather that, rather than the C++ Standard defining the
functionality of the Standard Library features which it has in common
with C, it makes reference to a C Standard which defines their
functionality.

Is the C Standard in question C89 (or C90 as some call it)?
Yes.

Does the C++ Standard make any changes to the C Standard Library
which it inherits?

Yes, some. But they don't concern interfaces. Mostly macros and
namespace 'std'.
For example, in C, the "toupper" function takes an
argument of int, and the value passed to it must be within range of
an unsigned char. Are things the same in C++?

Yes. No change is mentioned in the C++ Standard.
If so, the following
program can invoke undefined behaviour:

#include <cctype>

int main()
{
toupper('a');
}


Why? Because 'a' might be a negative number.

Really? Any real-world example of a character set where 'a' is in fact
a negative number?
A better example would be the use of extended characters, which are
far more likely to be a negative number:

toupper( 'German sharp s' );


To remedy this, many C programmers always cast the argument:

toupper( (unsigned char)'German sharp s' );

But the Standard explicitly prohibits 'German sharp s' to be in the
source character set. Read 2.2.

V
 
D

Daniel T.

Frederick Gotham said:
I gather that, rather than the C++ Standard defining the functionality of
the Standard Library features which it has in common with C, it makes
reference to a C Standard which defines their functionality.

Is the C Standard in question C89 (or C90 as some call it)?

Does the C++ Standard make any changes to the C Standard Library which it
inherits? For example, in C, the "toupper" function takes an argument of
int, and the value passed to it must be within range of an unsigned char.
Are things the same in C++? If so, the following program can invoke
undefined behaviour:

#include <cctype>

int main()
{
toupper('a');
}


Why? Because 'a' might be a negative number.

As I understand it, 'toupper' only looks at the first 7 bits or EOF. So
no undefined behavior there.
A better example would be the use of extended characters, which are far
more likely to be a negative number:

toupper( 'German sharp s' );


To remedy this, many C programmers always cast the argument:

toupper( (unsigned char)'German sharp s' );

Nether of the above are defined to work. 'German sharp s' is not a value
in the ASCII standard.
 
V

Victor Bazarov

Daniel said:
As I understand it, 'toupper' only looks at the first 7 bits or EOF.
So no undefined behavior there.

Where did you get the "first 7 bits"?
Nether of the above are defined to work. 'German sharp s' is not a
value in the ASCII standard.

Who said anything about ASCII? ASCII is just one supported encoding.
There are several others that are commonly used, and C++ has to work
fine with them. That is not to say that 'German sharp s' is going to
be accepted, though.

V
 
J

Jack Klein

I gather that, rather than the C++ Standard defining the functionality of
the Standard Library features which it has in common with C, it makes
reference to a C Standard which defines their functionality.

Is the C Standard in question C89 (or C90 as some call it)?

Does the C++ Standard make any changes to the C Standard Library which it
inherits? For example, in C, the "toupper" function takes an argument of
int, and the value passed to it must be within range of an unsigned char.
Are things the same in C++? If so, the following program can invoke
undefined behaviour:

#include <cctype>

int main()
{
toupper('a');
}


Why? Because 'a' might be a negative number.

No, it can't. Try reading the standard. Particularly paragraph 3 of
2.2 Character sets in this particular case.
 
J

Jerry Coffin

[ ... ]
Why? Because 'a' might be a negative number.

$2.2/3: "For each basic execution character set, the values of the
members shall be non-negative and distinct from
one another."
A better example would be the use of extended characters, which are far
more likely to be a negative number:

Read "far more likely" as "possible"...
toupper( 'German sharp s' );


To remedy this, many C programmers always cast the argument:

toupper( (unsigned char)'German sharp s' );

That's still necessary if the character isn't certain to fall within
a basic execution character set. If memory serves, locale
<char>::toupper takes a char as its parameter though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top