• Home
  • 1.4 Character Datatype Handling – Part 1

1.4 Character Datatype Handling – Part 1

View Categories

1.4 Character Datatype Handling – Part 1

< 1 min read

This program demonstrates how the char datatype stores values and how character literals are represented using ASCII values. A char variable can also store numeric values, which are interpreted as corresponding ASCII characters when printed.



⚙️
main.c

Copy to clipboard

#include <stdio.h>

int main()
{
    char ch1 = '0';
    char ch2 = 'A';
    char ch3 = 65;

    printf("The character is %c with decimal equivalent %d\n", ch1, ch1);
    printf("The character is %c with decimal equivalent %d\n", ch2, ch2);
    printf("The character is %c with decimal equivalent %d\n", ch3, ch3);

    return 0;
}

Output:
The character is 0 with decimal equivalent 48
The character is A with decimal equivalent 65
The character is A with decimal equivalent 65

char stores a single byte value. Character literals like '0' and 'A' are stored as their ASCII values (48 and 65 respectively).

A numeric value such as 65 can also be assigned to a char. When printed using %c, it displays the corresponding ASCII character.

printf() format specifiers:

  • %c → character representation
  • %d → numeric (ASCII) value

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *