• A string is stored in array, the name of the string is a pointer to the beginning of the string. 15
Strings If we use one-character string it requires two locations. The difference is shown below, The difference between array and string is shown below, 16
STRINGS Why do we need null? 17
String constant (or) Literal String constant is a sequence of characters enclosed in double quotes. When string constants are used in C program, it automatically initializes a null at end of string. Ex:- “Hello” “Welcome” “Welcome to C Lab” 18
String Input / Output C provides two basic methods to read and write strings. Using formatted input/output functions and using a special set of functions. Formatted input function :- scanf can be used with %s format specification to read a string. Ex:- char name; scanf(“%s”, name); Here don’t use “&” because name of string is a pointer to array. The problem with scanf is that it terminates its input on the first white space it finds. Ex:- NEW YORK Reads only NEW (from above example). 19
String Input / Output Unformatted input function :- • getchar() :- It is used to read a single character from keyboard. Using this function repeatedly we may read entire line of text Ex:- char ch=‟z‟; ch=getchar(); • gets() :- It is more convenient method of reading a string of text including blank spaces. Ex:- char line; gets(line); 20
String Input / Output 2. Formatted output function :- (Writing strings on the output) printf with %s format specifier we can print strings in different formats on to screen. Ex:- char name; printf(“%s”,name); printf(“%0.4”,name);
You've reached the end of your free preview.
Want to read all 26 pages?
- Fall '19