Char Data Type in C

C-Char Data Type

On this page we will discuss about Char Data Type which is used in C. The Char data type is most basic data type which is used to store letters and characters.
char data type in C

Char Data Type used in C

In C programming, the char data type is a data type that represents a single character. It is an integer data type that is typically stored in a single byte of memory and can represent any character in the ASCII character set, which includes the letters of the alphabet (both uppercase and lowercase), digits, punctuation marks, and special characters such as ‘\n’ (newline) and
‘\0’ (null character).

The Char data type is divided into two types:

  • Signed Data Type
    • It stores both positive and negative values so it ranges from -128 to +127.
  • Unsigned Data Type
    • It stores only positive values so it ranges from 0 to 255.

You can also use the char data type to store strings of characters. In C, a string is simply an array of characters that is terminated by a special character called the null character, which is represented by the ‘\0’ character.

Declaration of Character type variable

char character;

Initialization of Character type variable

A character type variable can be initialized in two ways as follows:

1. By assigning value to a variable using assignment operator.

character = 'A';

2. By assigning value to a variable during declaration only using an assignment operator.

char character = 'A';

Program to print Character Data Type

Example 1:

Run
#include <stdio.h>

int main()
{
    // Initializing different characters and displaying them
    char a = 'B';
    printf("character a = %c\n",a);
    printf("character a = %d, is value of an integer\n",a);
    
    char b = 'a'; 
    printf("character b = %c\n",b);
    printf("character b = %d, is value of an integer\n",b);
    
    char c = '0';
    printf("character c = %c\n",c);
    printf("character c = %d, is value of an integer\n",c);

    return 0;
}

Output:

character a = B
character a = 66, is value of an integer
character b = a
character b = 97, is value of an integer
character c = 0
character c = 48, is value of an integer

Example 2:

Run
#include <stdio.h>

int main(void) {
  
  // Intializing string of chharacters  
  char s[] = "PrepInsta Prime";
  
  // Displaying the string of characters
  printf("The string stored in s is: %s\n", s);
  
  return 0;
}

Output:

The string stored in s is: PrepInsta Prime

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription