Strings
In ‘C’, a string is an array of characters terminated by a NULL character. Any character(s) enclosed in double quotation marks are a string constant.
e.g.:- “Hello everybody”, “a” etc.
A string is stored as an array of characters with a NULL character (ASCII value 0) represented by the character ‘\0’ at the end.
e.g.:-
Therefore the array size should be equal to the maximum number of characters in the string plus 1.
syntax : char array_name[size]
e.g.:- char name[25]; names[12][25]; etc.
Character arrays can initialise when they are declared.
e.g.:- char name[8] = “Trichur”;
char name[8] = {‘T’, ’r’, ’I’, ’c’, ’h’, ’u’, ’r’, ‘\0’};
String can initialise without giving the array size. Then ‘C’ compiler automatically determined the array size based on the initialising string.
e.g.:- char name[] = “Trichur”;
After declaring the string variable you can’t assign a new value to the sting variable using the assignment operator (=). To do this you should use the library function strcpy.
Syntax : strcpy(destination, Source);
The source can be a string variable or a string constant and the destination must be a string variable.
INPUT AND OUTPUT IN 'C'
'C' does not have any built in input/output statements as part of its syntax. All I/O operations are carries out through function calls from the standard I/O library. 'C' compiler treats all devices as files like standard input (stdin), the keyboard, standard output (stdout), the monitor and standard error (stderr) also the monitor. There are two different types of I/O operations. They are buffered I/O and unbuffered I/O. Buffer is a temporary storage area.
UNBUFFERED I/O: In unbuffered I/O, characters typed at the keyboard is made available to the program immediately without being stored temporarily in buffer area and it is not necessary to press the ENTER key to complete. This type of I/O is used in interactive programs where the processing must be executed without waiting the ENTER key press.
getch()
syntax : int getch(void);
Accepts a single character from the standard input device (Usually the keyboard). This function does not waits until the user types the ENTER key and does not echoes the user pressed character. It returns the integer value of the character.
e.g.:- int a;
a = getch();
printf("%d - - %c", a, a);
putch()
syntax : int putch(int argument);
Outputs a single character on the screen, at the cursor position. The argument may be a single character constant, character variable, integer or a function returning a char type value.
e.g.:- putch(a);
putch(65);
putch(getch());
getche()
syntax : int getche(void);
Accepts a single character from the keyboard. This also does not wait for the ENTER key but it echoes the entered character on the cursor position. getche() returns the integer value of the user pressed character.
e.g.:- getche();
a = getche();
putch(getche());
BUFFERED I/O: In buffered I/O, characters typed at the keyboard are collected until the user presses the ENTER key. Then the typed characters are made available to the program as a block. In the event that mistakes are made and the ENTER key is not pressed, the mistakes can be corrected where this is not possible in unbuffered I/O.
getchar()
syntax : int getchar(void);
Accepts a single character from the keyboard. This function waits until the ENTER key is pressed. It also return an integer value between 0 - 255.
e.g.:- x = getchar();
getchar();
putchar()
syntax : int putchar(int argument);
Displays a character on the screen at the cursor position. Argument can be a single character constant enclosed within single quotes, a character variable, an integer or a function returning char type value.
e.g.:- putchar(65);
putchar(getch());
ESCAPE SEQUENCES
These represent non-printable characters and characters that have meaning in the ‘C’ program. An escape sequence consists of the escape character (the backslash, \) and a second character.
‘\a’ Alert (bell)
‘\b’ Backspace (moves the cursor one space to the left)
‘\f’ Formfeed (advances the printer paper one page)
‘\n’ Newline (starts a new line)
‘\r’ Carriage return (moves the cursor to the beginning of the line)
‘\t’ Tab
‘\v’ Vertical tab
‘\’’ Single quote
‘\”’ Double quote
‘\?’ Question mark
‘\\’ Backslash
‘\0’ NULL
Escape sequences are used with output functions.
e.g.:- putchar(‘\a’); putchar(‘\n’); etc.
syntax: gets(string_varaiable);
This function accepts characters from the keyboard until user presses the ENTER key and stores them in the string variable given as the argument.
e.g.:- char name[20];
gets(name);
syntax : puts(string_expr);
Used to display a string of characters at the cursor position on the screen.
e.g.:- puts(name); puts(gets()); puts(“\n”); etc.
Formatted Input and Output – printf () and scanf ()
These functions are called formatted functions as they can read and print data in various pre-specified formats of any data type, which are under the user’s control. Format specifiers specify the form by which the values of the variables are to be input and printed.
syntax: printf(“control string”, argument_list);
The argument list consists of constants, variables, expressions or functions separated by commas. There must be one format command in the control string for each argument in the list and they must match in number, type and order.
The control string must be enclosed within double quotes. It consists of any printable characters (text), non-printing characters represented by escape sequences and the format commands.
Format commands defines how the data items are to be printed or accepted. A format command begins with a ‘%’ sign and is followed by a format code/specifier appropriate for the data item. Format codes are also called conversion characters. e.g.:- %d for integer, %s for string etc. You can give more than one-format commands in the control string.
Share with your friends: |