Vous êtes sur la page 1sur 3

Strings Strings in C are represented by arrays of characters. String Constants String constant is a sequence of character enclosed by double quotes.

e.g., "Hello world !" String Variable A string variable ia an array of characters that has a defined length. It can be used to stored string values. In C language, a special character, called the "Null Character" ('\0'), is inser ted at the end of each string variable. In C, we don't have a separate data type to store strings. We use char acter arrays to store strings. We generally take an array of static length that is large enough to hold data. Declration and initialization of string String variable is an array of character. The general form of declartion of a st ring variable is char string_name [size]; The complier automatically supplies a null character ('\0') at the end of the st ring. Therefore, the size should be equal to the maximum number of character in the string plus one. e.g., char name[25]; char address[90]; First statement declare string varible name of length 25 and second statement de clare string varible address pf size 90.

Input/Output of string data Using scanf AND printf functions scanf function can be used with %s format specification to read a string. char name[25]; scanf ("%s", name); Note that, in the case of string, the ampersand (&) sign is not required before the variable name. The major problem with the scanf function is that it terminates its input on the first whi te space (blank, tab, carriage return, newline and formatted) it finds. Similarly, printf function can be used with % s format specification to write or print the strings to the screen.

Using Character I/O function For character input, the function available are getchar(), getche(), and getch((

), where as for character output, the function available is putchr(). getchr() function This function returns a character that has been recently typed. The typed charac ter is echoed to the computer screen. After typing the appropriate character, the progrogram is required to pr ess Enter key. General form of getchr function is variable_name = getchr(); Type of variable_name must be char getche() function This function also returns a character that has been recently typed. The typed character us also echoed to the computer screen. But the programmer is not required to type Enter key. General form of getche function is variable_name = getche(); Type of variable_name must be char.

getch() function This function also returns a character that has been recently typed. But, neithe r the programmer is required to type Enter key after typing a character nor the typed character is echoed to the computer screen. This function has advantages over the other function in the application where the use r want to hide the input, like password. General form of this function is: variable_name = getch(); Type of variable_name must be char.

putchr() function This function outputs a character constant or a chaarcter variable to the standa ra output device. General form of this function is putchar (variable_name); where variable_name is a type char variable containing a character

String I/O function Find string input, the function gets() is used, and for string output, the funct ion puts() is used.

gets() function This function accepts a string from the standard input device. The string may co nsist of multiple words. The size of the string is limited by the declration of the string variable. This inp ut is completed by pressing Enter key. puts() function This function output a string constant or a string variable to the standard oupu t device.

NULL and EOF constants The C language provides asimple method of finding the end of the string or file. The header file stdio.h defines two constants, NULL and EOF follows: #define EOF #define NULL (-1) 0

All function except gets return -1 as they read beyond the last character of the string. The gets function return 0 (NULL) as it read accross the last character of the string. The turbo C requires that inputing should end with control-z (decimal 26). When the input function encounters control-z, it takes it as the end of input data of a text file.

Vous aimerez peut-être aussi