Elementary programming with c einternational

Page 280

Session 17 Strings Concepts

null terminator.

17.1.1 Pointers to strings Strings can also be stored and accessed using character pointers. A character pointer to a string is declared as follows: char *pstr = “WELCOME”; pstr is a pointer that is initialized to point to a string constant. The pointer may be modified to point elsewhere. However, the modification would cause the string to be inaccessible.

17.1.2 String I/O operations String input/output (I/O) operations in C are carried out using function calls. These functions are part of the standard I/O library called stdio.h. A program that uses the string I/O functions must have the following statement at the beginning: #include <stdio.h>; When the program containing this statement is compiled, the contents of the file stdio.h become a part of the program. 

Simple string I/O operations The gets() function is the simplest method of accepting a string through standard input. Input characters are accepted till the Enter key is pressed. The gets() function replaces the terminating ‘\n’ new line character with the ‘\0’ character. The syntax is as follows: gets(str); where, str is a character array that has been declared. Similarly, the puts() function is used to display a string on the standard output device. The newline character terminates the string output. The function syntax is: puts(str); where, str is a character array that has been declared and initialized. The following program accepts a name and displays a message.

page 270 of 356

Version 1.1 © Aptech Limited

Elementary Programming with C


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.