By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,982 Members | 1,932 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,982 IT Pros & Developers. It's quick & easy.

Microsoft Visual Studio, does not display unsigned char in debug, truncation of int t

P: 5
Hi, I've recently started studying C++ and in order to practice exercises I am using Microsoft Visual Studio.

I am attempting to recreate the effect of initializing -1 to an unsigned char (thus returning 255). When compiling the code however, it shows as undefined, warning that I am truncating an int into an unsigned char, as well as warning that I am truncating a constant value.

I get that an int does not promote up to an unsigned char, but shouldn't the ide recognize that -1 converts to 255 as an unsigned char? Or am I missing something that is particular to using Visual studio?

When debugging, I try to do a cout for the unsigned char, but only a blank space shows. Here is the code for cc
clarification:

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
unsigned char c = '-1';
cout << c << endl;
return 0;
}
Feb 9 '16 #1

✓ answered by weaknessforcats

This code:

Expand|Select|Wrap|Line Numbers
  1. unsigned char c = '-1';
says to take a -1 (which is an int) and then the '-1' says to make a const char out of -1. That const int -1 will never fit in a const char.

Just code:

Expand|Select|Wrap|Line Numbers
  1. unsigned char c = -1;
Here you create an unsigned char and initialize it to -1. The -1 is not a symbolic constant so you won't get these warnings.

Also 255 is not a displayable char based on the ASCII table.

Share this Question
Share on Google+
11 Replies


P: 5
edit: originally I tried to assign just -1 to the unsigned char, and after not having luck with that I tried putting the -1 in single quotes ' . This is the code with the single quotes, but I have the same issue even without quotes. Sorry for that confusion
Feb 9 '16 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
This code:

Expand|Select|Wrap|Line Numbers
  1. unsigned char c = '-1';
says to take a -1 (which is an int) and then the '-1' says to make a const char out of -1. That const int -1 will never fit in a const char.

Just code:

Expand|Select|Wrap|Line Numbers
  1. unsigned char c = -1;
Here you create an unsigned char and initialize it to -1. The -1 is not a symbolic constant so you won't get these warnings.

Also 255 is not a displayable char based on the ASCII table.
Feb 9 '16 #3

Expert 100+
P: 2,396
-1 is stored as 255 if the compiler uses two's complement format. I don't believe the C Standard mandates the use of two's complement. You would have to look very hard to find a compiler that doesn't use two's complement notation, but that's not the point. You are relying on implementation-defined behavior.
Feb 9 '16 #4

P: 5
Thank you, I think I get it now. After experimenting with it, I can see that chars of other values give a display, while most larger ones do not. I had not realized that char displays from the Ascii table, I was under the impression it stored it as a straight number.

In any case, thank you for the help everyone!
Feb 10 '16 #5

weaknessforcats
Expert Mod 5K+
P: 9,197
It is stored as a straight number. There are only two types of built-in types in C/C++: integers and floating point. A char, then, is an integer.

The ASCII tables associates a number with a display symbol for char variables only. For example, if your char contains 65 you will display an A. If your int contains 65 you will display 65.

The ASCII table is referred to as the "implementation character set". The char is the index into this table. So, a Russian implementation would not use the ASCII table for char symbols.
Feb 10 '16 #6

P: 5
So on that topic, if I wanted to use a single byte of memory to store and display a number (either unsigned or signed) what would be the best way to store and display the number? A char? I've heard of a type int8_t but I'm not sure how it works...
Feb 10 '16 #7

Expert 100+
P: 2,396
Type char (also called plain char) should only be used to hold characters.
Types signed char or unsigned char should be used to hold numbers.

The C Standard deliberately does not specify whether plain char is signed or unsigned - leaving it to the compiler implementer to choose whichever is most convenient for them.
Feb 10 '16 #8

weaknessforcats
Expert Mod 5K+
P: 9,197
The only single byte integer you have is char. I am curious as to why a one byte requirement. Especially in view of 128,000 megabyte chips that are smaller than my thumbnail.
Feb 10 '16 #9

P: 5
I'm studying C++ from the book "C++ 11 Primer Fifth Edition". In one of the first sections that I'm reviewing again, it explains how defining an unsigned char to -1 causes the char to loop around to 255. I just started using Microsoft Visual Studio with C++ and wanted to test how that would work. As I've now learned, it does store the value correctly, but when printed with cout, the command line either shows an ASCII character or a blank space. It's not as much about needing a one byte integer, but trying to learn how unsigned chars loop around and seeing how my system responds. Since it was not responding the way I was anticipating, I wanted to make sure that there wasn't something else wrong that would cause me problems with learning further down the road.
Feb 10 '16 #10

Expert 100+
P: 2,396
This book is giving you bad advice for C. The result of assigning -1 to an unsigned char variable is not certain.

Perhaps C++ has eliminated some of the wiggle room.
Feb 10 '16 #11

weaknessforcats
Expert Mod 5K+
P: 9,197
@MattDrummy Post #10:

Re-read my post #6. Then look in the ASCII table and see what is the display symbol for 255. It's not -1 because that is two display characters.
Feb 11 '16 #12

Post your reply

Sign in to post your reply or Sign up for a free account.