Question regarding char in Visual C++ 2013

There's something going on in Visual C++ 2013 (also in Visual C++ 2012, I believe) regarding the char type that I don't understand and is striking me as positively baffling. First, as a preamble, Ivor Horton, of Beginning Visual C++ fame, states in his book that, to run the console programs in the book, we have to disable the Unicode Character set (Project Properties -> Configuration Properties -> General and then setting the Character Set flag to Not Set ... it's set to Unicode by default). Not doing this supposedly will result in the programs not compiling.

However, I have never had a problem getting my console programs to run even though I leave the Character Set in its default (Use Unicode) state.

for example the following snippet:
1
2
3
   char c = 'm';
   cout << "\nchar is " << sizeof (char) << " byte(s)." << endl;
   cout << "c is " << sizeof c << " byte(s)." << endl;


displays 1 byte as the size of char and c. Of course that's not surprising because char is supposed to be 1 byte, but what's baffling is that the code runs at all, since Unicode is set, and Ivor Horton states that char will not work unless Unicode is not set. And I'm running the same compiler as he is, for Pete's sake!

If you're using Visual C++ 2013 or 2012, you're probably experiencing the same disconnect. What's happening here? Why do console programs that use char build successfully in Visual Studio C++ 2013 (or 2012) even though the Unicode Character set is selected and wchar_t is not being used as the character type?
The character set in the project settings has nothing to do with the char type. It affects which versions of Windows functions you call (ASCII or wide character).

This isn't exactly your question, but it is related:
http://www.cplusplus.com/articles/2w6AC542/
Thanks for that link. The only reason I had a question about this at all is because of this paragraph in Ivor Horton's Beginning Visual C++ 2012 book (I have the ebook, so cutting and pasting is easy) The guilty sentences are bolded by me:

TRY IT OUT Creating an Empty Console Project
The previous project contained a certain amount of excess baggage that you don’t need when working
with simple C++ language examples. The precompiled headers option chosen by default resulted in the
stdafx.h fi le being created in the project. This is a mechanism for making the compilation process
more effi cient when there are a lot of fi les in a program, but it won’t be necessary for most of our
examples. In these instances, you start with an empty project to which you can add your own source
fi les. You can see how this works by creating a new project in a new solution for a Win32 console
program with the name Ex1_02. After you have entered the project name and clicked OK, click
Application Settings on the left side of the dialog box that follows. You can then select Empty project
from the additional options.
When you click Finish, the project is created as before, but this time without any source fi les.
By default, the project options will be set to use Unicode libraries. This makes use of a non-standard
name for the main function in the program. In order to use standard native C++ in your console
programs, you need to switch off the use of Unicode libraries. To do this, select the Project ➪
Properties menu item, or press Alt+F7, to display the Property Pages dialog for the project. Select the
All Confi gurations option from the Confi guration: drop-down list at the top. Select the General option
under Confi guration Properties in the left pane and select the Character Set property in the right pane.
You will then be able to set the value of this property to Not Set from the drop-down list to the right of
the property name, as shown in Figure 1-10. Click OK to close the dialog. You should do this for all the
C++ console program examples in the book. If you forget to do so, they won’t build.
You will be using
Unicode libraries in the Windows examples, though.
I really don't know what he is talking about there. Perhaps he means they won't build if you set them to use Unicode then try to use normal char variables instead of the CSTR, etc. macros?
The thing is, is that that statement I pasted was on page 16 of the book! He hasn't even got into programming yet really. On Feb. 14 I posted a question on the forum that's associated with the book, but still haven't got a response although the question has been viewed over 60 times. The only thing I can think of is that maybe that guidance is a leftover from an early edition of Visual Studio, but then again, the VS 2012 book's got a figure right on the facing page giving the reader a screenshot of the Configuration properties with the Character Set flag highlighted with Not Set selected. Ivor Horton knows his C++, so I really am baffled by his statement in the book.

I bought Beginning Visual C++ mainly because I want to eventually get into windows programming and give MFC a shot, and Horton covers MFC. Currently I'm bouncing back and forth between C++ Primer and C++ Primer Plus, which each provide a more fuller and rounded out coverage of the C++ standard, in my opinion.
Last edited on
Topic archived. No new replies allowed.