C প্রোগ্রামিং এর Char Data Type সম্পর্কে আলোচনা

Submitted by tushar pramanick on Tue, 03/05/2013 - 14:41

The char Data Type

An object of the char data type represents a single character of the character set used by your computer. For example, A is a character, and so is a. But 7 is a number.

But a computer can only store numeric code. Therefore, characters such as A, a, B, b, and so on all have a unique numeric code that is used by computers to represent the characters. Usually, a character takes 8 bits (that is, 1 byte) to store its numeric code.

For many computers, the ASCII codes are the de facto standard codes to represent a character set. (ASCII, just for your information, stands for American Standard Code for Information Interchange.) The original ASCII character set has only 128 characters because it uses the lower 7 bits that can represent 27 (that is, 128) characters.

 

On IBM-compatible PCs, however, the character set is extended to contain a total of 256 (that is, 28) characters. Appendix C, "ASCII Character Set," gives a list of the 256 characters.

 

Character Variables

A variable that can represent different characters is called a character variable.

You can set the data type of a variable to char by using the following declaration format:

 

char  variablename;

where variablename is the place where you put the name of a variable.

If you have more than one variable to declare, you can either use the following format:

char  variablename1;
char  variablename2;
char  variablename3;


or this one:

char  variablename1, variablename2, variablename3;

 

 

Character Constants

A character enclosed in single quotes (`) is called a character constant. For instance, `A', `a', `B', and `b' are all character constants that have their unique numeric values in the ASCII character set.

You can remember to use the single quote (`), instead of the double quote ("), in character constants because a character constant represents a single character. You'll see later in the book that the double quote is used to indicate a string of characters.

From the ASCII character set listed in Appendix C, you will find that the unique numeric (decimal) values of `A', `a', `B', and `b' are 65, 97, 66, and 98, respectively. Therefore, given x as a character variable, for instance, the following two assignment statements are equivalent:

 

x = `A';
x = 65;

 

So are the following two statements:

 

x = `a';
x = 97;

 

Later in this hour, you'll see a program (in Listing 4.2) that converts numeric values back to the corresponding characters.

 

The Escape Character (\)

Actually, you first saw the escape character (\) in Hour 2, "Writing Your First C Program," when you learned to use the newline character (\n) to break a message into two pieces. In the C language, the backslash (\) is called the escape character; it tells the computer that a special character follows.

 

For instance, when the computer sees \ in the newline character \n, it knows that the next character, n, causes a sequence of a carriage return and a line feed.

 

Besides the newline character, several other special characters exist in the C language, such as the following:

 

Character Description
\b The backspace character; moves the cursor to the left one character.
\f The form-feed character; goes to the top of a new page.
\r The return character; returns to the beginning of the current line.
\t The tab character; advances to the next tab stop.

 

 Printing Out Characters

You already know that the printf() function, defined in the C header file stdio.h, can be used to print out messages on the screen. In this section, you're going to learn to use the character format specifier, %c, which indicates to the printf() function that the argument to be printed is a character. Let's first have a look at the program in Listing 4.1, which prints out characters on the screen.

 

TYPE
Listing 4.1. Printing out characters on the screen.


  /* 04L01.c: Printing out characters */
  #include <stdio.h>

  main()
  {
     char c1;
     char c2;

     c1 = `A';
    c2 = `a';
    printf("Convert the value of c1 to character: %c.\n", c1);
    printf("Convert the value of c2 to character: %c.\n", c2);
    return 0;
 }

 

OUTPUT
After the executable file of 04L01.c in Listing 4.1 is created, you can run it to see what will be printed out on the screen. On my machine, the executable file is named as 04L01.exe. The following is the output printed on the screen after I run the executable from a DOS prompt:

 

C:\app> 04L01
Convert the value of c1 to character: A.
Convert the value of c2 to character: a.
C:\app>

 

ANALYSIS
As you know, line 2 includes the header file, stdio.h, for the printf() function. Lines 5_14 make up the main() function body.

 

Lines 6 and 7 declare two character variables, c1 and c2, while lines 9 and 10 assign c1 and c2 with the character constants `A' and `a', respectively.

 

Note that the %c format specifier is used in the printf() function in lines 11 and 12, which tells the computer that the contents contained by c1 and c2 should be printed as characters. When the two statements in lines 11 and 12 are executed, two characters are formatted and output to the screen, based on the numeric values contained by c1 and c2, respectively.

 

Now look at the program shown in Listing 4.2. This time, %c is used to convert the numeric values back to the corresponding characters.

 

TYPE
Listing 4.2.
Converting numeric values back to characters.

 

1:  /* 04L02.c: Converting numeric values back to characters */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     char c1;
7:     char c2;
8:
9:     c1 = 65;
10:    c2 = 97;
11:    printf("The character that has the numeric value of 65 is: %c.\n", c1);
12:    printf("The character that has the numeric value of 97 is: %c.\n", c2);
13:    return 0;
14: }

 

OUTPUT
The following is the output printed on the screen after I run the executable file, 04L02.exe, from a DOS prompt:

 

C:\app> 04L02
The character that has the numeric value of 65 is: A.
The character that has the numeric value of 97 is: a.
C:\app>

 

ANALYSIS
The program in Listing 4.2 is similar to the one in Listing 4.1 except for the two statements in lines 9 and 10. Note that in lines 9 and 10 of Listing 4.2, the character variables c1 and c2 are assigned 65 and 97, respectively.

 

As you know, 65 is the numeric value (decimal) of the A character in the ASCII character set; 97 is the numeric value of a. In lines 11 and 12, the %c format specifier converts the numeric values, 65 and 97, into the A and a, respectively. The A and a characters are then printed out on the screen.

 

 

Comments

Related Items

মডুলার C প্রোগ্রামিং (Modular C Programming)

কেবল মাত্র একটি ফাংশন দিয়ে কোনো বড়ো জটিল সমস্যা সমাধানের চেষ্টা করা ভাল প্রোগ্রামিংয়ের পদ্ধতি নয়। সঠিক পদ্ধতি হ'ল সমস্যাটিকে কয়েকটি ছোট ছোট এবং সরল টুকরো করে ফেলা যাতে তা আরও বিশদে বোঝা যায় । তারপরে এই ছোট এবং সরল সমস্যাগুলি সমাধান করার জন্য ছোট ছোট ফাংশন ব্লক তৈরি করা এবং পরে সেগুলি নিয়মানুযায়ী সংযোজিত করা ।

Programming Style

Programming Style

In this section, I'd like to briefly highlight some points that will help you write clean programs that can easily be read, understood, and maintained.

Exercises : Answer the following Question

To help solidify your understanding of this hour's lesson, you are encouraged to answer the quiz questions and finish the exercises provided in the Workshop before you move to the next lesson.

Question and Answer

    Q Is the C preprocessor part of the C compiler?

    A No. The C preprocessor is not part of the C compiler. With its own line-oriented grammar and syntax, the C preprocessor runs before the compiler in order to handle named constants, macros, and inclusion of files.

Compiling Your Code Under Conditions

Compiling Your Code Under Conditions

You can select portions of your C program that you want to compile by using a set of preprocessor directives. This is useful, especially when you're testing a piece of new code or debugging a portion of code.