473,503 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using char


Hello everyone!

I made a variable under unsigned short but displaying it using char the
value that doesnt change in fact is shown changed...

unsigned short a

a = 321;

printf("a = %d", (char)a);

the display is 'a = 63'...

why is that?

how can I have the right display for a?
--
Posted via http://dbforums.com
Nov 13 '05 #1
32 2605
Conan1st <me*********@dbforums.com> writes:
I made a variable under unsigned short but displaying it using char the
value that doesnt change in fact is shown changed...

unsigned short a

a = 321;

printf("a = %d", (char)a);


Obviously, 321 doesn't fit in `char' on your platform. Use a
wider type.

(Also, %d is for displaying `int's, not `char's, but that's
unlikely to be the real problem.)
Nov 13 '05 #2
Mac
On Mon, 27 Oct 2003 23:22:45 +0000, Conan1st wrote:

Hello everyone!

I made a variable under unsigned short but displaying it using char the
value that doesnt change in fact is shown changed...

unsigned short a

a = 321;

printf("a = %d", (char)a);

the display is 'a = 63'...

why is that?

how can I have the right display for a?


printf("a = %hu", a);

When you use a %d with printf, you need to pass an int. %u Is for unsigned
int, and %hu is for a short unsigned int.

If you are determined to use '%d,' then cast 'a' to 'int':

printf("a = %d", (int)a);

Don't forget to add a '\n' at the end of the string, or fflush(stdout),
otherwise you may not ever see the output.

HTH

Mac
--

Nov 13 '05 #3
Conan1st <me*********@dbforums.com> wrote:
unsigned short a
a = 321;
printf("a = %d", (char)a); the display is 'a = 63'...
why is that?


Like someone else said, 321 doesn't fit in 8 bits.
a is a short, on x86 it occupies 16 bits. You're
trying to print after casting it to a char. When
you cast it to a char the c compiler puts in logic
mask out all but the lowest byte ( I actually just
told a lie since x86 is little-endian curse them ).
When you print the result as an int it is
effectively doing this:

unsigned short a = 321;
unsigned char b = 0;
b = ( a & 0x00ff );
printf("%d\n", (int)b);

--
Harrison Caudill | .^ www.hypersphere.org
Computer Science & Physics Double Major | | Me*Me=1
Georgia Institute of Technology | '/ I'm just a normal guy
Nov 13 '05 #4
"Conan1st" <me*********@dbforums.com> wrote in message
news:35****************@dbforums.com...

Hello everyone!

I made a variable under unsigned short but displaying it using char the
value that doesnt change in fact is shown changed...

unsigned short a Missing semicolon.

a = 321;

printf("a = %d", (char)a);

the display is 'a = 63'... Are you sure? Unless it is UB (of which I'm not sure), it should output
a = 65
why is that? because 321 % 256 == 65 (or 321 & 0xff == 65, if you prefer).
how can I have the right display for a?

Mac already answered ...
Nov 13 '05 #5
In <35****************@dbforums.com> Conan1st <me*********@dbforums.com> writes:
I made a variable under unsigned short but displaying it using char the
value that doesnt change in fact is shown changed...

unsigned short a
a = 321;
printf("a = %d", (char)a);

the display is 'a = 63'...
why is that?
If you can't engage your brain, C programming is not for you.

Isn't it obvious, from your result, that 321 cannot be represented by the
type char on your implementation?

On the vast majority of hosted implementations, char is an 8-bit type.
One of these 8 bits may or may not be used as a sign bit. How many bits
do you need to represent the value 321?
how can I have the right display for a?


By not converting it to a narrower type before displaying it. Isn't it
obvious? What's less obvious is the right printf invocation, to get the
right answer in a portable way:

printf("a = %u\n", (unsigned)a);

The cast to unsigned is needed because, in a portable programming context,
you don't know to what type a is going to be promoted by the default
argument promotions: int or unsigned int. Since no conversion descriptor
can accept either int or unsigned int, you have to bypass the default
argument promotions by converting the value to a type that is no longer
subject to the default argument promotions.

In standard C, this is an issue only with variadic functions, like
printf. For other functions you can always provide a prototype, so that
the default argument promotions are no longer performed by the compiler,
when the function is called.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #6
In <87************@pfaff.stanford.edu> Ben Pfaff <bl*@cs.stanford.edu> writes:
Conan1st <me*********@dbforums.com> writes:
I made a variable under unsigned short but displaying it using char the
value that doesnt change in fact is shown changed...

unsigned short a

a = 321;

printf("a = %d", (char)a);


Obviously, 321 doesn't fit in `char' on your platform. Use a
wider type.

(Also, %d is for displaying `int's, not `char's, but that's
unlikely to be the real problem.)


Huh? What *exactly* is wrong with passing a char to %d, except for the
pathological case when char gets promoted to unsigned int?

Have they dropped the default argument promotions from the C standard
behind my back?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #7
Mac wrote:

On Mon, 27 Oct 2003 23:22:45 +0000, Conan1st wrote:
unsigned short a printf("a = %d", (char)a); how can I have the right display for a?

printf("a = %hu", a);


That's the best way, in my opinion.

--
pete
Nov 13 '05 #8
Ben Pfaff wrote:
Conan1st <me*********@dbforums.com> writes:

I made a variable under unsigned short but displaying it using char the
value that doesnt change in fact is shown changed...

unsigned short a

a = 321;

printf("a = %d", (char)a);

Obviously, 321 doesn't fit in `char' on your platform. Use a
wider type.

(Also, %d is for displaying `int's, not `char's, but that's
unlikely to be the real problem.)


But, Ben, in this case, the char _is_ an int. ;-)

Since printf()'s variadic parameters go through standard function call type
promotion, the char data item is promoted to int as part of the call, before
the call is made.

But you knew that already.

--

Lew Pitcher, IT Consultant, Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)

Nov 13 '05 #9
Lew Pitcher wrote:
Ben Pfaff wrote:

....
(Also, %d is for displaying `int's, not `char's, but that's
unlikely to be the real problem.)


But, Ben, in this case, the char _is_ an int. ;-)


I think he meant "character", as in printf("%c", a);

Jirka

Nov 13 '05 #10
Jirka Klaue wrote:
Lew Pitcher wrote:
Ben Pfaff wrote:


...
(Also, %d is for displaying `int's, not `char's, but that's
unlikely to be the real problem.)

But, Ben, in this case, the char _is_ an int. ;-)

I think he meant "character", as in printf("%c", a);


Assuming that a is a char or an int, then the above is legal and proper. If
a is a char, it will be promoted to int as part of the call (variadic
functions), and if a is an int, it remains an int. In either case, the
resulting int will be explicitly truncated into a character when printed
using the %c format.

In any case, Ben's comment was directed at the OP's code fragment
printf("a = %d", (char)a);

--

Lew Pitcher, IT Consultant, Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)

Nov 13 '05 #11
Lew Pitcher wrote:
Jirka Klaue wrote:
Lew Pitcher wrote:
Ben Pfaff wrote: ...
(Also, %d is for displaying `int's, not `char's, but that's
unlikely to be the real problem.)

But, Ben, in this case, the char _is_ an int. ;-)


I think he meant "character", as in printf("%c", a);


Assuming that a is a char or an int, then the above is legal and proper.
If a is a char, it will be promoted to int as part of the call (variadic
functions), and if a is an int, it remains an int. In either case, the
resulting int will be explicitly truncated into a character when printed
using the %c format.


What is your understanding of "truncated" in this case?
In any case, Ben's comment was directed at the OP's code fragment
printf("a = %d", (char)a);


Yes, and my comment was directed to you. Did you consider that the
printf("%c", a) is what the OP actually wanted and that Ben wanted
to express exactly that?

Jirka

Nov 13 '05 #12
In <3F***********@mindspring.com> pete <pf******@mindspring.com> writes:
Mac wrote:

On Mon, 27 Oct 2003 23:22:45 +0000, Conan1st wrote:

> unsigned short a > printf("a = %d", (char)a); > how can I have the right display for a?

printf("a = %hu", a);


That's the best way, in my opinion.


Except that it's not clear what is the *actual* type expected by %hu: is
it int or unsigned int?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #13
In <bn**********@mamenchi.zrz.TU-Berlin.DE> Jirka Klaue <jk****@ee.tu-berlin.de> writes:
Lew Pitcher wrote:
Ben Pfaff wrote:

...
(Also, %d is for displaying `int's, not `char's, but that's
unlikely to be the real problem.)


But, Ben, in this case, the char _is_ an int. ;-)


I think he meant "character", as in printf("%c", a);


The OP was obviously expecting a numeric value, not some random character.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #14
In <bn**********@mamenchi.zrz.TU-Berlin.DE> Jirka Klaue <jk****@ee.tu-berlin.de> writes:
Lew Pitcher wrote:
Jirka Klaue wrote:
Lew Pitcher wrote:
Ben Pfaff wrote:
...

> (Also, %d is for displaying `int's, not `char's, but that's
> unlikely to be the real problem.)

But, Ben, in this case, the char _is_ an int. ;-)

I think he meant "character", as in printf("%c", a);


Assuming that a is a char or an int, then the above is legal and proper.
If a is a char, it will be promoted to int as part of the call (variadic
functions), and if a is an int, it remains an int. In either case, the
resulting int will be explicitly truncated into a character when printed
using the %c format.


What is your understanding of "truncated" in this case?
In any case, Ben's comment was directed at the OP's code fragment
printf("a = %d", (char)a);


Yes, and my comment was directed to you. Did you consider that the
printf("%c", a) is what the OP actually wanted and that Ben wanted
to express exactly that?


This hypothesis is not worth considering, since the OP was expecting to
see 321 as the output and didn't understand why he got another value.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #15
In <bn**********@mamenchi.zrz.TU-Berlin.DE> Jirka Klaue
<jk****@ee.tu-berlin.de> writes:
Lew Pitcher wrote:
Jirka Klaue wrote:
Lew Pitcher wrote:

Ben Pfaff wrote:

...
>(Also, %d is for displaying `int's, not `char's, but that's
>unlikely to be the real problem.)

But, Ben, in this case, the char _is_ an int. ;-)

I think he meant "character", as in printf("%c", a);


Assuming that a is a char or an int, then the above is legal and proper.
If a is a char, it will be promoted to int as part of the call (variadic
functions), and if a is an int, it remains an int. In either case, the
resulting int will be explicitly truncated into a character when printed
using the %c format.


What is your understanding of "truncated" in this case?


truncated: past part of "truncate": to shorten by cutting off the top or end

New Websters Dictionary and Thesaurus

--

Lew Pitcher, IT Consultant, Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)

Nov 13 '05 #16
Lew Pitcher wrote:
In <bn**********@mamenchi.zrz.TU-Berlin.DE> Jirka Klaue writes:
Lew Pitcher wrote: ....
In
either case, the resulting int will be explicitly truncated into a
character when printed using the %c format.


What is your understanding of "truncated" in this case?


truncated: past part of "truncate": to shorten by cutting off the top or
end


The standard says:

7.19.6.1#8
c If no l length modifier is present, the int argument is converted to an
unsigned char, and the resulting character is written.
...

"Converted" does not necessarily mean "truncated".

Jirka

Nov 13 '05 #17
Dan Pop wrote:

In <3F***********@mindspring.com> pete <pf******@mindspring.com> writes:
Mac wrote:

On Mon, 27 Oct 2003 23:22:45 +0000, Conan1st wrote:

> unsigned short a

> printf("a = %d", (char)a);

> how can I have the right display for a?

printf("a = %hu", a);


That's the best way, in my opinion.


Except that it's not clear what is the
*actual* type expected by %hu: is
it int or unsigned int?


I would expect that %hu would *actually* expect
whatever type it is that unsigned short promotes to,
be it int or be it unsigned.

--
pete
Nov 13 '05 #18
In <3F***********@mindspring.com> pete <pf*****@mindspring.com> writes:
Dan Pop wrote:

In <3F***********@mindspring.com> pete <pf******@mindspring.com> writes:
>Mac wrote:
>>
>> On Mon, 27 Oct 2003 23:22:45 +0000, Conan1st wrote:
>
>> > unsigned short a
>
>> > printf("a = %d", (char)a);
>
>> > how can I have the right display for a?
>
>> printf("a = %hu", a);
>
>That's the best way, in my opinion.


Except that it's not clear what is the
*actual* type expected by %hu: is
it int or unsigned int?


I would expect that %hu would *actually* expect
whatever type it is that unsigned short promotes to,
be it int or be it unsigned.


But the standard doesn't explicitly say so.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #19
Dan Pop wrote:

In <3F***********@mindspring.com> pete <pf*****@mindspring.com> writes:
Dan Pop wrote:

In <3F***********@mindspring.com> pete <pf******@mindspring.com> writes:

>Mac wrote:
>>
>> On Mon, 27 Oct 2003 23:22:45 +0000, Conan1st wrote:
>
>> > unsigned short a
>
>> > printf("a = %d", (char)a);
>
>> > how can I have the right display for a?
>
>> printf("a = %hu", a);
>
>That's the best way, in my opinion.

Except that it's not clear what is the
*actual* type expected by %hu: is
it int or unsigned int?


I would expect that %hu would *actually* expect
whatever type it is that unsigned short promotes to,
be it int or be it unsigned.


But the standard doesn't explicitly say so.


I'm starting from the premise that
printf("a = %hu", a);
is defined behavior for the case in question,
and from there I conclude that
something that would cause undefined behavior
(like printf not getting what it's expecting),
isn't happening.

--
pete
Nov 13 '05 #20
In <3F***********@mindspring.com> pete <pf*****@mindspring.com> writes:
Dan Pop wrote:

In <3F***********@mindspring.com> pete <pf*****@mindspring.com> writes:
>Dan Pop wrote:
>>
>> In <3F***********@mindspring.com> pete <pf******@mindspring.com> writes:
>>
>> >Mac wrote:
>> >>
>> >> On Mon, 27 Oct 2003 23:22:45 +0000, Conan1st wrote:
>> >
>> >> > unsigned short a
>> >
>> >> > printf("a = %d", (char)a);
>> >
>> >> > how can I have the right display for a?
>> >
>> >> printf("a = %hu", a);
>> >
>> >That's the best way, in my opinion.
>>
>> Except that it's not clear what is the
>> *actual* type expected by %hu: is
>> it int or unsigned int?
>
>I would expect that %hu would *actually* expect
>whatever type it is that unsigned short promotes to,
>be it int or be it unsigned.


But the standard doesn't explicitly say so.


I'm starting from the premise that
printf("a = %hu", a);
is defined behavior for the case in question,


Where does the standard say that?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #21
Dan Pop wrote:

In <3F***********@mindspring.com> pete <pf*****@mindspring.com> writes:
Dan Pop wrote:

In <3F***********@mindspring.com> pete <pf*****@mindspring.com> writes:

>Dan Pop wrote:
>>
>> In <3F***********@mindspring.com> pete <pf******@mindspring.com> writes:
>>
>> >Mac wrote:
>> >>
>> >> On Mon, 27 Oct 2003 23:22:45 +0000, Conan1st wrote:
>> >
>> >> > unsigned short a
>> >
>> >> > printf("a = %d", (char)a);
>> >
>> >> > how can I have the right display for a?
>> >
>> >> printf("a = %hu", a);
>> >
>> >That's the best way, in my opinion.
>>
>> Except that it's not clear what is the
>> *actual* type expected by %hu: is
>> it int or unsigned int?
>
>I would expect that %hu would *actually* expect
>whatever type it is that unsigned short promotes to,
>be it int or be it unsigned.

But the standard doesn't explicitly say so.


I'm starting from the premise that
printf("a = %hu", a);
is defined behavior for the case in question,


Where does the standard say that?


The standard doesn't actually refer to
an unsigned short variable named "a".

N869
7.19.6.1 The fprintf function
[#6]
h Specifies that a following d, i, o, u, x, or X
conversion specifier applies to a short int or
unsigned short int argument (the argument will
have been promoted according to the integer
promotions, but its value shall be converted to
short int or unsigned short int before
printing); or that a following n conversion
specifier applies to a pointer to a short int
argument.

--
pete
Nov 13 '05 #22
In <3F***********@mindspring.com> pete <pf*****@mindspring.com> writes:
Dan Pop wrote:

In <3F***********@mindspring.com> pete <pf*****@mindspring.com> writes:
>Dan Pop wrote:
>>
>> In <3F***********@mindspring.com> pete <pf*****@mindspring.com> writes:
>>
>> >Dan Pop wrote:
>> >>
>> >> In <3F***********@mindspring.com> pete <pf******@mindspring.com> writes:
>> >>
>> >> >Mac wrote:
>> >> >>
>> >> >> On Mon, 27 Oct 2003 23:22:45 +0000, Conan1st wrote:
>> >> >
>> >> >> > unsigned short a
>> >> >
>> >> >> > printf("a = %d", (char)a);
>> >> >
>> >> >> > how can I have the right display for a?
>> >> >
>> >> >> printf("a = %hu", a);
>> >> >
>> >> >That's the best way, in my opinion.
>> >>
>> >> Except that it's not clear what is the
>> >> *actual* type expected by %hu: is
>> >> it int or unsigned int?
>> >
>> >I would expect that %hu would *actually* expect
>> >whatever type it is that unsigned short promotes to,
>> >be it int or be it unsigned.
>>
>> But the standard doesn't explicitly say so.
>
>I'm starting from the premise that
> printf("a = %hu", a);
>is defined behavior for the case in question,


Where does the standard say that?


The standard doesn't actually refer to
an unsigned short variable named "a".


If you read the standard carefully, you'll see that all conversion
descriptors require a well specified argument type and it's not clear
at all that %h[ouxX] is an exception.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #23
>>a = 321;

printf("a = %d", (char)a);

the display is 'a = 63'...


Are you sure? Unless it is UB (of which I'm not sure), it should output
a = 65


Please forgive my stupidity. But what does UB stand for and mean?

Thanks

Nov 13 '05 #24
Johnny wrote:
[...] what does UB stand for and mean?


UB means "undefined behaviour", and refers to code whose result is not laid
down by the C standard. Examples include void main, fflush(stdin),
gets(NULL), and a trillion other examples of bad programming.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #25
Johnny <aw****@hotmail.com> wrote in message news:<AM*********************@news-server.bigpond.net.au>...
a = 321;

printf("a = %d", (char)a);

the display is 'a = 63'...


Are you sure? Unless it is UB (of which I'm not sure), it should output
a = 65


Please forgive my stupidity. But what does UB stand for and mean?

UB -- > Undefined Behaviour
Here is an exact definition

http://dspace.dial.pipex.com/town/gr...t/welcome.html

Or on a light note here is a definition from standard
<quote>

undefined behavior
behavior, upon use of a nonportable or erroneous program construct, of
erroneous data, or of indeterminately valued objects, for which this
International Standard imposes no requirements
2 NOTE Possible undefined behavior ranges from ignoring the situation
completely with unpredictable
results, to behaving during translation or program execution in a
documented manner characteristic of the
environment (with or without the issuance of a diagnostic message), to
terminating a translation or
execution (with the issuance of a diagnostic message).

3 EXAMPLE An example of undefined behavior is the behavior on integer
overflow.

</quote>
--
Imanpreet Singh Arora
imanomaniac AT acm DOT org
Nov 13 '05 #26

"Johnny" <aw****@hotmail.com> wrote in message

Please forgive my stupidity. But what does UB stand for and mean?

UB is "undefined behaviour", like writing to an uninitialised pointer.

The point is that UB means a program is always incorrect. It may produce
correct output, or what the programmer "means", but it is not correct as a C
program.

It shouldn't be confused with implementation-defined behaviour, which is
correct but not portable.
Nov 13 '05 #27
Richard Heathfield <do******@address.co.uk.invalid> wrote in message news:<bo**********@titan.btinternet.com>...
Johnny wrote:
[...] what does UB stand for and mean?


UB means "undefined behaviour", and refers to code whose result is not laid
down by the C standard. Examples include void main, fflush(stdin),
gets(NULL), and a trillion other examples of bad programming.


Just a curious, irrelevant question, if there are trillion examples of
bad programming, how many examples are there of good programming.
Certainly not a trillion.

--
Imanpreet Singh Arora
isingh AT acm DOT org
Nov 13 '05 #28
In <bo**********@titan.btinternet.com> Richard Heathfield <do******@address.co.uk.invalid> writes:
Johnny wrote:
[...] what does UB stand for and mean?


UB means "undefined behaviour",


How do you know? What is preventing UB from meaning "unspecified
behaviour", which, in the context of the C standard, is a completely
different beast?

Because of this ambiguity, UB should be avoided, unless the context
provides enough information for disambiguation.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #29
Minti wrote:
Richard Heathfield <do******@address.co.uk.invalid> wrote in message
news:<bo**********@titan.btinternet.com>...
Johnny wrote:
> [...] what does UB stand for and mean?


UB means "undefined behaviour", and refers to code whose result is not
laid down by the C standard. Examples include void main, fflush(stdin),
gets(NULL), and a trillion other examples of bad programming.


Just a curious, irrelevant question, if there are trillion examples of
bad programming, how many examples are there of good programming.
Certainly not a trillion.

It's trivial to write code to generate a trillion examples of good
programming.

Might take up a fair bit of storage, though.
--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #30
Dan Pop wrote:
In <bo**********@titan.btinternet.com> Richard Heathfield
<do******@address.co.uk.invalid> writes:
Johnny wrote:
[...] what does UB stand for and mean?
UB means "undefined behaviour",


How do you know? What is preventing UB from meaning "unspecified
behaviour", which, in the context of the C standard, is a completely
different beast?


Nothing, which is why I suggested the alternatives UDB and USB, some time
ago. In message <3D***************@eton.powernet.co.uk> I said: "Others in
this newsgroup call undefined behaviour "UB", but there is nothing to
disambiguate that from "unspecified behaviour", so I think it a poor
abbreviation. UDB and USB, *within the context of this newsgroup*, are not
ambiguous."
Because of this ambiguity, UB should be avoided, unless the context
provides enough information for disambiguation.


UB should /always/ be avoided.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #31
In <bo**********@sparta.btinternet.com> Richard Heathfield <do******@address.co.uk.invalid> writes:
Dan Pop wrote:
In <bo**********@titan.btinternet.com> Richard Heathfield
<do******@address.co.uk.invalid> writes:
Johnny wrote:

[...] what does UB stand for and mean?

UB means "undefined behaviour",
How do you know? What is preventing UB from meaning "unspecified
behaviour", which, in the context of the C standard, is a completely
different beast?


Nothing, which is why I suggested the alternatives UDB and USB, some time
ago.


Bad idea, since USB already has a well defined meaning and because we
can't realistically expect people who have just joined the newsgroup
(regardless of their experience as C programmers) to be able to figure
out the meaning of such abbreviations.
In message <3D***************@eton.powernet.co.uk> I said: "Others in
this newsgroup call undefined behaviour "UB", but there is nothing to
disambiguate that from "unspecified behaviour", so I think it a poor
abbreviation. UDB and USB, *within the context of this newsgroup*, are not
ambiguous."


Imagine that right after you joined c.l.c you saw UDBs and USBs all over
the place. Would you have been able to figure out their meaning?

Even if one doesn't fully understand the meanings of "unspecified
behaviour" and "undefined behaviour" (and, especially, the difference
between them), at least one can figure out what the poster is talking
about.

So, if we don't want to turn c.l.c into a closed club for the regulars,
there is no place for such abbreviations here.
Because of this ambiguity, UB should be avoided, unless the context
provides enough information for disambiguation.


UB should /always/ be avoided.


OK, I'll bite: how do you avoid the unspecified behaviour in

printf("Hello world\n");

?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #32
Richard Heathfield <do******@address.co.uk.invalid> wrote in message news:<bo**********@sparta.btinternet.com>...
Minti wrote:
Richard Heathfield <do******@address.co.uk.invalid> wrote in message
news:<bo**********@titan.btinternet.com>...
Johnny wrote:

> [...] what does UB stand for and mean?

UB means "undefined behaviour", and refers to code whose result is not
laid down by the C standard. Examples include void main, fflush(stdin),
gets(NULL), and a trillion other examples of bad programming.


Just a curious, irrelevant question, if there are trillion examples of
bad programming, how many examples are there of good programming.
Certainly not a trillion.

It's trivial to write code to generate a trillion examples of good
programming.

Might take up a fair bit of storage, though.

Maybe it could scan all the USENET c.l.c and get a trillion examples
of good programming.
Might have to filter out fair bit of bad programs though.

--
Imanpreet Singh Arora
Nov 13 '05 #33

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

Similar topics

3
5114
by: Mark Miller | last post by:
I have a char array and when I write it to a file using BinaryWriter the position of the pointer is the size of the array + 1. For example: writing char leaves the pointer at position 26 after...
7
10612
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using...
7
3163
by: sbobrows | last post by:
{Whilst I think much of this is OT for this newsgroup, I think the issue of understanding diagnostics just about gets under the door. -mod} Hi, I'm a C++ newbie trying to use the Boost regex...
11
3330
by: TheDD | last post by:
Hello, i don't manage to use the tolower() function: #include <algorithm> #include <iostream> #include <locale> #include <string> using std::cout;
5
5686
by: Enos Meroka | last post by:
Hallo, I am a student doing my project in the university.. I have been trying to compile the program using HP -UX aCC compiler, however I keep on getting the following errors. ...
6
17139
by: ransoma22 | last post by:
I developing an application that receive SMS from a connected GSM handphone, e.g Siemens M55, Nokia 6230,etc through the data cable. The application(VB.NET) will receive the SMS automatically,...
13
6218
by: Superman859 | last post by:
Hello everyone. Heads up - c++ syntax is killing me. I do quite well in creating a Java program with very few syntax errors, but I get them all over the place in c++. The smallest little things...
11
2671
by: Angus | last post by:
I am working with a C API which often requires a char* or char buffer. If a C function returns a char* I can't use string? Or can I? I realise I can pass a char* using c_str() but what about...
9
1763
by: chikkubhai | last post by:
Why is the result different for the following set of two code snippets Code without using this pointer #include <string> #include <iostream> using namespace std; struct X { private:
4
5016
by: Amera | last post by:
hello , I have written these codes : Mydll file : Mydll.h #ifndef MYDLL_H
0
7201
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
7278
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,...
1
6988
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...
0
7456
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
4672
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
3166
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
3153
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1510
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 ...
1
734
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.