Reading and Writing String using scanf() gets and printf() puts

In this class, we will try to understand Reading and Writing string using scanf() gets and printf() puts.

Reading and Writing String using scanf() gets and printf() puts

In our previous class, we have covered how to declare and initialize a string.

printf(), puts and scanf(), gets

Writing String

We will try to understand writing the string by using the programming example shown below.

Reading and Writing String using scanf(), gets and printf(), puts
Writing String Example

In c, we have two options: printf and other by puts enclosed with the name of the ‘string’.

The essential point that we have to understand in writing and reading a string is the use of %s conversion specifier.

The %s conversion specifier is used to write or read a string.

Reading String

The image below is the program to understand the scanf() and gets to scan a string.

Reading String Example
Reading String Example

The essential point to understand about scanf is that it will scan only up to the first space in the string.

In the above example, the scanf() function will scan only up to Learning, and the remaining string after the Learning is not read.

Note: It is NOT at all suggestible to use scanf() for strings.

The other alternative for scanf() is the gets functions.

The gets function is used by giving the name of the string enclosed in the parentheses.

Using the gets function, we can scan all the characters, including the spaces in the string.

The scan of all the characters will end in gets if we press enter after entering the string.