cyley
cyley

Reputation: 23

Integer value of 'A'

I have written a program in c++ for switch cases to understand:

 int main()
  {
    int x = 65;

    switch(x)
      {

       case 'A':printf( "One" );
              break;

       case 'B': printf( "Zero" );
              break;

       case 'C': printf( "Hello World" );
              break;

       default: printf("Invalid");

      }
   }

My Confusion is I have Declare my int x = 65

and the output of this code is

One

How is it possible? Why it is relating to ASCII value of 65?

Upvotes: 0

Views: 4598

Answers (7)

BusyProgrammer
BusyProgrammer

Reputation: 2781

When you write any letter in between single apostrophes, you refer them as a char. This means they can be referred to as a numeric value. In your case, 'A' refers to the ascii value of 65, while 'B' refers to 66 and 'C' refers to 67. The compiler then implicitly converts the int value into a char value where required, like in your case. See the example below:

    int i = 99; // This ca be any number ranging from and including 65-90 and 97-122 (only for uppercase/lowercase letters)
    char s = i; // Variable s is assigned a character at the ascii value stored in variable i
    cout << s << endl; // Outputting s gives us the letter/character, outputting i gives the decimal value as an int.

NOTE: If you write "A", then this is a string, and not a char value. Your compiler cannot make a conversion from int to string. For example, this piece of code will produce a compilation error:

    int i = 99; // This ca be any number ranging from and including 65-90 and 97-122 (only for uppercase/lowercase letters)
    string s = i; // Here we are trying to store an int value in a string. This will fail
    cout << s << endl; // Won't work

What you need to see here is that we can perform mathematical operations on an int value, which we cannot perform on a string. For example, we cannot do the following:

    string total = "String a" * "String B";

But we CAN do the following:

    int total = 5 * 6;

On the other hand, you can perform SOME mathematical operations on a char value, just like an int value, and we can also use an int value to refer to a certain char according to the ascii table; therefore, these 2 data types are compatible. For example this works:

    char s = 'A' + 2; // This results in the character C;

Hope this answers your question.

Upvotes: 0

JedaiCoder
JedaiCoder

Reputation: 666

If you go to http://www.asciitable.com/ then you will see that the capital character 'A', has an ASCII value of 65. When you use switch(x) and question the value 'A', which is a character, then the character is converted(typecasted) into an integer by its ASCII decimal value;

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 311068

Objects of type char including character literals belong to integer types and internally they are stored like integral values (codes) that represent characters.

According to the C++ Standard (3.9.1 Fundamental types)

1 Objects declared as characters(char) shall be large enough to store any member of the implementation’s basic character set. If a character from this set is stored in a character object, the integral value of that character object is equal to the value of the single character literal form of that character.

and (4.5 Integral promotions)

1 A prvalue of an integer type other than bool, char16_t, char32_t, or wchar_t whose integer conversion rank (4.13) is less than the rank of int can be converted to a prvalue of type int if int can represent all the values of the source type; otherwise, the source prvalue can be converted to a prvalue of type unsigned int.

So when the expression in the switch statement

switch(x)

is evaluated its value is compared with the values converted to type int of each character literal used as label of the switch statement.

Thus if characters in your system are internally represented by ASCII codes then for example the value 65 in decimal or 0x41 in hex represents character literal 'A'.

Thus the comparison

x == 'A'

is equivalent to the comparison

x == 65

Upvotes: 1

user3853544
user3853544

Reputation: 583

What you are experiencing is a result of Implicit Conversion.

More specifically;

...

Integral promotion

Prvalues of small integral types (such as char) may be converted to prvalues of larger integral types (such as int). In particular, arithmetic operators do not accept types smaller than int as arguments, and integral promotions are automatically applied after lvalue-to-rvalue conversion, if applicable. This conversion always preserves the value.

The following implicit conversions are classified as integral promotions:

  • signed char or signed short can be converted to int;

...

Which means when a signed char (eg a 'char') is compared to an integer its type gets promoted to an integer.

On your system 'A' is defined as the value 0x41 or in decimal 65.

Your switch statement takes in an integer, also 65, and compares it to 'A'.

'A' is promoted to an integer and given the value 65.

It is effectively doing the following:

int a = 65;
char b = 'A';
if(a == static_cast<int>(b)){
   ...
}

Note The value given to a char is implementation specific so this may not be true on another machine!

Upvotes: 0

Striezel
Striezel

Reputation: 3758

Yes, it has to do with the fact that the ASCII value of A is 65.

In the switch statement you use x, which is an integer, but all cases only provide characters (type char). So somewhere before the decision whether x equals 'A', the compiler has to make an implicit conversion from char to int, and casting 'A' to int gives 65 - assuming that your compiler implementation uses ASCII and not some other character set.

Upvotes: 1

NathanOliver
NathanOliver

Reputation: 180945

'A' is a character literal. It looks like an A but really it is mapped to some integer value depending on the character set. In ASCII that value is 65. So what happens is the 'A' is promoted to an int with the value determined by the character set (65 in this case) and then it is compared to the value of x. Since they have the same value One is printed.

This is implementation defined behavior though. C++ does not mandate what the character set should be so it is possible for this code to print out any of you other outputs. It just depends on what value the character set maps 'A' to. This is why doing things like

char ch;
std::cin >> ch;
if (ch == 65)
...

Is bad because it relies on magic numbers where as

char ch;
std::cin >> ch;
if (ch == 'A')
...

will always work since it removes relying on a specific character set.

Upvotes: 8

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385274

'A' is 65.

Because your system is using the ASCII encoding for character literals, and character literals are numbers. You wrote 'A' in your code, but actually (assuming ASCII) that means (char)65.

And, obviously, 65 is 65.

A value comparison is being performed, without requiring the types to be the same.

Upvotes: 5

Related Questions