473,657 Members | 2,721 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 2641
Conan1st <me*********@db forums.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*********@db forums.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*********@db forums.com> wrote in message
news:35******** ********@dbforu ms.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.c om> Conan1st <me*********@db forums.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.stanfor d.edu> writes:
Conan1st <me*********@db forums.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*********@db forums.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

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

Similar topics

3
5135
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 starting at position 0. I thought that char was 2 bytes, but this makes it seem as though it is just 1 when I write to a file. Why is this? I imagine the extra bit is just a null bit (correct me if I'm wrong). I don't know if this helps but when I...
7
10622
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 namespace std; : : vector<string>* getTyphoon()
7
3173
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 libraries. Here's my situation. My system is Red Hat Linux 9, using the Borland C++BuilderX Personal IDE (which uses the g++ compiler).
11
3348
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
5704
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. ================================================================= Error 19: "CORBAManagerMessages.h", line 4 # Unexpected 'std'. using std::string; ^^^
6
17173
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, process and output to the screen in my application when a message arrived. But the problem is how do I read the SMS message immediately when it arrived without my handphone BeEPINg for new message ? I read up the AT commands, but when getting down...
13
6237
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 get me, which brings me to... I'm trying to create a program that gets a string from standard input and then manipulates it a little bit. It has to be a char array and not use string from the library. Here are my prototypes:
11
2695
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 receiving a char buffer into a string? Reason I ask is otherwise I have to guess at what size buffer to create? And then copy buffer to a string. Doesn't seem ideal.
9
1772
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
5049
by: Amera | last post by:
hello , I have written these codes : Mydll file : Mydll.h #ifndef MYDLL_H
0
8382
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
8297
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
8717
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
8498
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
7311
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
6162
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
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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
2
1930
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.