Monday, July 9, 2012

Character Data Types in C Programing Language

Character Data Types or variables are used to store single characters from the ASCII set. They're accommodated in a single byte. Character variables are declared and defined as in the statement below:

char bee = 'b', see = 'C', ccc;

This declaratory statement assigns the character constant 'b' to the char variable bee, and the character constant 'c' to the char variable named see. The char variable ccc is undefined.

Character constants are single characters. They must be enclosed in right single quotes. Escape sequences may be also assigned to character variables in their usual backslash notation, with the "compound character" enclosed in right single quotes. Thus the statement:

char nextline = '\n';

assigns the escape sequence for the newline character to the char variable nextilne. [In ANSI C a character constant is a sequence of one or more characters enclosed in single quotes. Precisely how the value of such a constant is to be interpreted is left to the implementation.]

Since character variables are accommodated in a byte, C regards chars as being a sub- range of ints, (the subrange that fits inside a byte) and each ASCII character is for all purposes equivalent to the decimal integer value of the bit picture which defines it. Thus 'A', of which the ASCII representation is 01000001, has the arithmetical value of 65 decimal. This is the decimal value of the sequence of bits 01000001, as you may easily verify. In other words, the memory representation of the char constant 'A' is indistinguishable from that of the int constant, decimal 65.

The upshot of this is that small int values may be stored in char variables, and char values may he stored in int variables! Character variables are therefore signed quantities restricted to the range [-128, 127]. However, it is a requirement of the language that the decimal equivalent of each of the printing characters be non-negative.

We are assured then that in any C implementation in which a char is stored in an 8-bit byte, the corresponding int value will always be a non-negative quantity, whatever the value of the leftmost (sign) bit may be. Now, identical bit patterns within a byte may be treated as a negative quantity by one machine, as a positive by another. For ensuring portability of programs which store non-character data in char variables the unsigned char declaration is useful: it changes the range of chars to [0, 255].

[Note :However, the ANSI extension signed char explicitly declares a signed character type to override, if need be, a possible default representation of unsigned chars.]

One consequence of the fact that C does not distinguish between the internal representation of byte-sized ints and chars is that arithmetic operations which are allowed on ints are also allowed on chars! Thus in C, if you wish to, you may multiply one character value by another.

Here are some variables declared as chars, and defined as escape sequences:

char newline = '\n',
char single_quote = '\"';

Character constants can also be defined via their octal ASCII codes. The octal value of the character is preceded by a backslash, and is enclosed in single quotes:

char terminal_bell = '\07';
/* 7 = octal ASCII code for beep */

char backspace = '\010';
/ *10 = octal code for backspace */

[Note: For ANSI C Compilers character constants may be defined by hex digits instead of octals. Hex digits are preceded by x, unlike 0 in the case of octals. Thus in ANSI C:

char backspace =\x A';

is an acceptable alternative declaration to

char backspace = '\010';

Any number of digits may be written, but the value stored is undefined if the resulting character value exceeds the limit of char.]

On an ASCII machine both '\b' and '\010' are equivalent representations. Each will print the backspace character. But the latter form, the ASCII octal equivalent of '\b', will not work on an EBCDIC machine, typically an IBM mainframe, where the collating sequence of the characters (i.e., their gradation or numerical ordering) is different. In the interests of portability therefore it is preferable to write '\b' for the backspace character, rather than its octal code. Then your program will work as certifiably on an EBCDIC machine as it will on an ASCII.

Note that the character constant 'a' is not the same as the string "a". (We will learn later that a string is really an array of characters, a bunch of characters stored in consecutive memory locations, the last location containing the null character; so the string "a" really contains two chars, 'a' immediately followed by '\0'). It is important to realise that the null character is not the same as the decimal digit 0, the ASCII code of which is 00110000.

Just as %d in printf () or scanf () allows us to print and read ints, %c enables the input and output of single characters which are the values of char variables. Let's look at Programs 2. 10 and 2.11 below:

/* Program 1 */ 
#include <stdio.h>
main ( )
{ 
char a = 'H', b = 'e', c 'I', d 'o', newline '\n';
printf ("%c", a); 
printf ("%c", b); 
printf ("%c", c); 
printf ("%c", c); 
printf ("%c", d); 
printf ("%c", newline); 
}

The output of Program 1 is easily predictable (what is it?).

/* Program 2 */ 
#include <stdio.h>
main ( )
{ 
char val_1, val_2; 
int vai_3; 
printf ("Press any of the keys a - z, then press \n"); 
scanf ("%c", &val_1); 
printf ("Press another key in a - z, then press \n"); 
scanf ("%c", &val_2); 
printf ("Assuming you're working on an ASCII machine, \nthe chars that you typed have decimal equivalents %d and %d, respectively", val_1, val_2); 
val_3 = val_1 * val_2; 
printf ("Their product is %d\n", val_3); 
}

Execute the above program to verify that char variables behave like one byte ints in arithmetic operations.

The %c format conversion character in a printf () outputs escape sequences, as you saw in Program 2. Execute Program 3 if you want a little music:

/* Program 3 */ 

#include <stdio.h> 
main ( )
{ 
char bell = '\007'; /* octal code of the terminal bell */ 
char x = 'Y', y = 'E', z = 'S', exclam = '!'; 
printf ("Do you hear the bell ? %c%c%c%c%c%c%c", bell, x, bell, y, bell, z, exclam); 
}

Program 4 assigns a character value to an int variable, an integer value to a char variable, and performs a computation involving these variables. Predict the output of the program, and verify your result by executing it

/* Program 4 */ 
#include <stdio.h> 
main ( )
{ 
int alpha = 'A', beta; 
char gamma = 122; beta = gamma - alpha; 
printf ("beta seen as an int is: %d\n", beta); 
printf ("beta seen as a char is: %c\n", beta); 
}

End-0f-File (EOF) character

One character that is often required to be sensed in C programs is not strictly speaking a character at all: it's the EOF or End-0f-File character, and its occurrence indicates to a program that the end of terminal or file input has been reached. Because the EOF is not a character, being outside the range of chars, any program that's written to sense it must declare an int variable to store character values. As we see in Program 2.13, this is always possible to do. An int variable can accommodate all the characters assigned to it, and can also accommodate EOF. We'll see uses for EOF in later units.

The putcular ( )function (pronounced "put character"), which takes a character variable or constant as its sole argument, is often a more convenient alternative for screen output thin is the printf ( ) When this function is invoked (for which purpose you may need to #include stdio.h), the character equivalent of its argument is output on the terminal, at the current position of the cursor:

putchar (char_var);

Suppose char_var has been assigned the value 'A'. Then 'A' will be displayed where the cursor was.

Reciprocally, getchar ( ) pronounced "get character") gets a single character from the keyboard, and can assign it to a char variable. (stdio.h 'may have to be #included before getchar ( )can be used.) It has no arguments, and is typically invoked in the following way:

char_var = getchar ( );

When such a statement is encountered, the execution of the program is stayed until a key is pressed. Then char_var is assigned the character value of the key that was pressed.

Program 5 below illustrates the usage of these functions. Your compiler may require the inclusion of stdio.h to invoke getchar ( ) and putchar ( ).

/* Program 5 */ 
#include <stdio.h> 
main ( )
{ 
char key_pressed; 
printf ("Type in a lowercase letter (a - z), press :"); 
key_pressed = getchar ( ); /* get char from keyboard*/ 
printf ("You pressed"); 
putchar (key_pressed - 32); /* put upperease char on Terminal*/ 
putchar ('\n');

/* this program converts to uppercase because ASCII decimal equivalent is 32 less than for the corresponding lower case character. */
}

1 comments:

  1. Casino Slots - Mapyro
    Welcome to the world's largest selection 창원 출장안마 of casino slots. Play thousands of exciting 의정부 출장안마 casino 충청북도 출장마사지 games, and 공주 출장마사지 win big with 사천 출장샵 Mapyro's unique Slots Wheel!

    ReplyDelete