Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > unsigned char ---- a special type of integer

Reply
Thread Tools

unsigned char ---- a special type of integer

 
 
Generic Usenet Account
Guest
Posts: n/a
 
      08-03-2007
On Aug 3, 11:01 am, Rolf Magnus <(E-Mail Removed)> wrote:
> Well, that's due to an overloaded version of the C++ stream input operator.
>
> > MY APOLOGIES TO comp.lang.c READERS THAT THIS SAMPLE IS C++, but the
> > issue that I am describing pertains to C as well.

>
> Actually, it doesn't.


I must admit that I was sceptical of Rolf's claim that this issue does
not pertain to the stdio library used in C. However, Rolf is exactly
right, as the following code snippet (modified version of OP's code
snippet) shows.

Are there any other hidden pitfalls with using switching from stdio to
stream libraries?

Song

/****************/

#include <stdio.h>

int
main()
{
unsigned char first;
unsigned short second;

unsigned int firstInt, secondInt;

printf("\nEnter first value: ");
scanf("%uc", &first);

firstInt = first;

printf("\nEnter second value: ");
scanf("%uhd", &second);

secondInt = second;

printf("Your values are %d and %d\n", firstInt, secondInt);
}


 
Reply With Quote
 
 
 
 
Army1987
Guest
Posts: n/a
 
      08-03-2007
On Fri, 03 Aug 2007 11:57:01 -0700, Generic Usenet Account wrote:

> On Aug 3, 11:01 am, Rolf Magnus <(E-Mail Removed)> wrote:
>> Well, that's due to an overloaded version of the C++ stream input operator.
>>
>> > MY APOLOGIES TO comp.lang.c READERS THAT THIS SAMPLE IS C++, but the
>> > issue that I am describing pertains to C as well.

>>
>> Actually, it doesn't.

>
> I must admit that I was sceptical of Rolf's claim that this issue does
> not pertain to the stdio library used in C. However, Rolf is exactly
> right, as the following code snippet (modified version of OP's code
> snippet) shows.
>
> Are there any other hidden pitfalls with using switching from stdio to
> stream libraries?
>
> Song
>
> /****************/
>
> #include <stdio.h>
>
> int
> main()
> {
> unsigned char first;
> unsigned short second;
>
> unsigned int firstInt, secondInt;
>
> printf("\nEnter first value: ");
> scanf("%uc", &first);

The fact is the meaning of %c.
If you used "%hhu" (in C99) it would store a number in decimal,
not the value of a character.
Also, there is no modifier u in standard C.
> firstInt = first;
>
> printf("\nEnter second value: ");
> scanf("%uhd", &second);

You meant "%hu"?
>
> secondInt = second;
>
> printf("Your values are %d and %d\n", firstInt, secondInt);
> }

Try this:
#include <stdio.h>
int main(void)
{
unsigned int a = 'A';
unsigned int b = 65;
unsigned char c = 'A';
unsigned char d = 65;
printf("%u %c\n", a, a);
printf("%u %c\n", b, b);
printf("%u %c\n", c, c);
printf("%u %c\n", d, d);
return 0;
}
--
Army1987 (Replace "NOSPAM" with "email")
"Never attribute to malice that which can be adequately explained
by stupidity." -- R. J. Hanlon (?)

 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      08-03-2007
Zahid Faizal wrote:
> All this time I mindlessly thought that unsigned char is just like
> other unsigned members of the integer family (integer, short, long,
> long long) with a more constrained range of values ---- from 0 to
> 255. That is what I had read somewhere. Imagine my surprise when I
> realized that the way unsigned char is read from stdin or a file is
> completely different from other entities in the integer family! I did
> not expect that in the case of unsigned char, the value that would be
> assigned to a variable would be its ASCII equivalent. I knew that
> this is the behavior for char, but I did not expect unsigned char to
> do that. I was badly bitten by this revelation today.
>
> Kindly see the source snippet below, where I was able to recreate the
> problem. MY APOLOGIES TO comp.lang.c READERS THAT THIS SAMPLE IS C++,
> but the issue that I am describing pertains to C as well.


Your problem is C++ specific. It is a result of the C++ <iostream>
functions trying to figure out what you mean to do with
cin >> whatever;
This is a price you pay for overloading.

In C you do not have this problem, since reading a char as an integer
value uses specifiers for integer values (%d, %i, %o, %x, %u, with
whatever modifiers are appropriate).

The C++ functions assume that reading a char is equivalent to using the
"%c" specifier, which is incorrect.

Since your problem is entirely with the assumptions C++ forces on you,
and has nothing at all to do with C, it was inappropriate to post to
comp.lang.c. I have removed it from the Follow-ups.

Nor is it all all clear why in the world you should think comp.sources.d
should give a flip. It, too, has been removed from the Follow-ups.
Your crossposting to irrelevant newsgroups is dangerously close to
newgroup abuse.

>
> Thanks,
> Zahid
>
>
>
> ////////////////////////
> #include <iostream>
>
> using namespace std;
>
> int
> main()
> {
> unsigned char first;
> unsigned short second;
>
> unsigned int firstInt, secondInt;
>
> cout << "\nEnter first value: ";
> cin >> first;
>
> firstInt = first;
>
> cout << "\nEnter second value: ";
> cin >> second;
>
> secondInt = second;
>
> cout << "Your values are " << firstInt << " and " << secondInt <<
> endl;
> }
>
> ////////////////////////
>
>
> I entered 0 and 0 and the output was as follows:
> Your values are 48 and 0
>

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      08-04-2007
On Aug 3, 8:57 pm, Generic Usenet Account <(E-Mail Removed)>
wrote:
> On Aug 3, 11:01 am, Rolf Magnus <(E-Mail Removed)> wrote:
> > Well, that's due to an overloaded version of the C++ stream
> > input operator.


> > > MY APOLOGIES TO comp.lang.c READERS THAT THIS SAMPLE IS
> > > C++, but the issue that I am describing pertains to C as
> > > well.


> > Actually, it doesn't.


> I must admit that I was sceptical of Rolf's claim that this
> issue does not pertain to the stdio library used in C.
> However, Rolf is exactly right, as the following code snippet
> (modified version of OP's code snippet) shows.


Rolf is right, but your example doesn't show it.

> Are there any other hidden pitfalls with using switching from
> stdio to stream libraries?


> /****************/


> #include <stdio.h>


> int
> main()
> {
> unsigned char first;
> unsigned short second;


> unsigned int firstInt, secondInt;


> printf("\nEnter first value: ");
> scanf("%uc", &first);


This line has undefined behavior, which means that your program
can'd show us anything. You tell the library to read an
unsigned int, followed by the character 'c', and you give it the
address of an unsigned char in which to store it.

> firstInt = first;


> printf("\nEnter second value: ");
> scanf("%uhd", &second);


Same problem as above (except that you give the library the
address of an unsigned short).

> secondInt = second;


> printf("Your values are %d and %d\n", firstInt, secondInt);
> }


--
James Kanze (GABI Software) email:james.kanze:gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      08-06-2007
Generic Usenet Account wrote:

> On Aug 3, 11:01 am, Rolf Magnus <(E-Mail Removed)> wrote:
>> Well, that's due to an overloaded version of the C++ stream input
>> operator.
>>
>> > MY APOLOGIES TO comp.lang.c READERS THAT THIS SAMPLE IS C++, but the
>> > issue that I am describing pertains to C as well.

>>
>> Actually, it doesn't.

>
> I must admit that I was sceptical of Rolf's claim that this issue does
> not pertain to the stdio library used in C. However, Rolf is exactly
> right, as the following code snippet (modified version of OP's code
> snippet) shows.
>
> Are there any other hidden pitfalls with using switching from stdio to
> stream libraries?
>
> Song
>
> /****************/
>
> #include <stdio.h>
>
> int
> main()
> {
> unsigned char first;
> unsigned short second;
>
> unsigned int firstInt, secondInt;
>
> printf("\nEnter first value: ");


End output with a newline to ensure that buffers are flushed.

> scanf("%uc", &first);


ITYM %hhu.

> firstInt = first;
>
> printf("\nEnter second value: ");
> scanf("%uhd", &second);


Again %hu

> secondInt = second;
>
> printf("Your values are %d and %d\n", firstInt, secondInt);


return 0;

> }


And what does your program prove?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Casting from const pair<const unsigned char*, size_t>* to constpair<unsigned char*, size_t>* Alex Vinokur C++ 9 10-13-2008 05:05 PM
unsigned char ---- a special type of integer Zahid Faizal C++ 39 08-11-2007 01:34 PM
Linking error LNK2001 - "__declspec(dllimport) private: void __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::_Tidy(unsigned short)" (__imp_?_Tidy@?$basic_string@DU?$char_ sharmadeep1980@gmail.com C++ 1 07-07-2006 07:27 AM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
void*, char*, unsigned char*, signed char* Steffen Fiksdal C Programming 1 05-09-2005 02:33 AM



Advertisments