0

code as follows:

#include <stdio.h>
int main(){
        
    char a = 1;
    char b = 2;
        
    printf("%d %d\n", a, b);
}

output: 1 2

user3386109
  • 34,287
  • 7
  • 49
  • 68
  • @user3386109 Did you just remove the actual question? – Erik Mar 09 '21 at 08:28
  • 1
    @Erik You can look at the edit history. Just click the link above my name. In other words, "No." – user3386109 Mar 09 '21 at 08:30
  • Does this answer your question? [How assignment from int to char works in C?](https://stackoverflow.com/questions/18294350/how-assignment-from-int-to-char-works-in-c) – Abra Mar 09 '21 at 08:31
  • 1
    `printf()` is a function taking a variable number of arguments. For such functions, all `...` arguments undergo [default argument conversions](http://port70.net/~nsz/c/c11/n1570.html#6.5.2.2). Specifically values of type `char` are converted to values of type `int` even before any code in the library runs. – pmg Mar 09 '21 at 08:37

3 Answers3

1

total number of Character in ASCII table is 256 (0 to 255), in C language each character represented using the integer value, interesting thing is that if you use %d the it will print the integer code for the character

  printf("%d", 'a'); /* output : 97 /*

but if you use %c the it will print the character

  printf("%c", 'a'); /* output : a */


  printf("%c", 97); /* output : a */

  char a=1 ;/* this a is not storing 1 but it is holding the ascii character 
  correspond to 1, this is due to implicit type casting done by compiler */

refer to this :https://www.geeksforgeeks.org/type-conversion-c/

  • ASCII only defines codes from 0 to 127. The `char` type may hold other values, but those outside that use encodings beyond ASCII, and a C implementation might not use ASCII at all (the C standard permits other encodings, like EBCDIC). Additionally, `char` may be signed and limited to 127 on its high end. – Eric Postpischil Mar 09 '21 at 08:47
1

The char type is an integer type, similar to short or int. When you store characters in a char object, it actually stores a numeric code in the char object. For example, x = 'b'; stores a code for “b” in x. (In implementations that use ASCII, the code for “b” is 98.) If you read a character from input and store it with x = getchar();, and the input is “b”, then the code for “b” is stored.

In many expressions in C, a char value is automatically promoted to an int. This includes when it is passed to printf. If you print the value using %c, printf prints the character represented by the value. If you print using %d, printf prints a decimal numeral for the value.

char may be a signed type, in which case it can represent values at least from −127 to +127. Or it may be unsigned, in which case it can represent values from 0 to at least 255.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
0

Chars will store integers in the range of -128 to 127 or from 0 to 255 depending if the char is signed or unsigned. The printf function arguments a and b are promoted to ints, and printed as ints as expected by the %d delimiter. With the delimiter %c they are still promoted to ints, but printed as characters.

Aki Suihkonen
  • 19,144
  • 1
  • 36
  • 57
  • The C standard only requires a signed `char` to hold values from −127 (not −128) to 127, and it permits more than those intervals for signed `char` and unsigned `char`. – Eric Postpischil Mar 09 '21 at 08:50