-1
#include<stdio.h>
void main()
{
  int a=65;
  char d='A';
   if(a==d)
    printf("both are same");
 }

The output is both are same.here a is a integer so 65 is stored in 32 bits and d is a char which is stored in 8 bits how could they be same as is computer everything is converted to binary for any operation.

3 Answers3

3

The computer is able to compare a char to an int on a binary level because of Implicit type promotion rules.

If an int can represent all values of the original type (as restricted by the width, for a bit-field), the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions.

This means your char is promoted to an int before your processor compares the two.

Lanting
  • 3,060
  • 12
  • 28
2

C is a very flawed language, so there are many dirty, irrational things going on between the lines here:

  • char has implementation-defined signedness, so how it stores data depends on compiler. Is char signed or unsigned by default?
  • 'A' is a character literal, and as it happens, character literals are actually of type int in C. This doesn't make any sense, but that's just how it is.
  • In the line char d='A';, the literal 'A' (type int) gets converted to char. Which may or may not be signed. Signedness shouldn't in practice affect the basic character set A to Z though.
  • Most likely 'A' will be stored as the value 65, although this is not guaranteed by the standard. For that reason it is better to always write 'A' and never 65 (the former is also most readable).
  • In the expression a==d, the character operand is a small integer type. Small integer types undergo an implicit promotion to int when used in most expressions. This integer promotion is part of a set of rules for how expressions are balanced, to ensure that both operands of an operator are always of the same type. These rules are called the usual arithmetic conversions. For details see: Implicit type promotion rules
Lundin
  • 195,001
  • 40
  • 254
  • 396
1

The internal storage is the compiler's decision, and often depends on the target architecture.

However, this has nothing to do with the result your code shows; in the comparison, the char gets promoted to an int before comparing (because you can't compare apples with oranges; read the language rules). Therefore, it compares an int with an int, and they are equal.

Aganju
  • 6,295
  • 1
  • 12
  • 23
  • Your answer suggests that you cannot compare apples with oranges, but you can convert an apple into an orange - no offense :-) – andreee Jun 13 '18 at 10:59
  • @andreee A more accurate analogy might be: – Lundin Jun 13 '18 at 11:52
  • Imagine the C compiler being a fruit warehouse worker getting tasked with comparing 3 apples to 5 oranges. The worker is unwilling to admit that the requested task makes no sense, let alone that he doesn't understand the comparison criteria. So instead the worker grabs 3 bananas and 5 bananas and sneaks off into another room, comparing the bananas with each other instead. Aha, they are all yellow. Pleased with himself, he then delivers bananas to the customer, who then tries to sell them as apples. After which the fruit store goes bankrupt. And so the term "going bananas" was invented. – Lundin Jun 13 '18 at 11:52