Reading String in C

the reading string used input function scanf can be used with %s format specification to read in a string of characters.


char name[10];

scanf("%s",name);							
							

the problem with scanf function is that it terminates its input on the first white soace.for example you can enter name Hello World in the scanf function.

Hello World

then only the string "Bharat" will be read into the array name,since the blank space after the word 'Bharat' will terminate the reading of string.

The scanf function automatically terminates the string that is read with a null character the following memory allocation.

H

e

l

l

o

\0

?

?

?

?

?

?

?

?

?

The read the above line used two array to read the full line.


char name1[5],name2[5];
scanf("%s%s",name1,name2);
							
							

You can enter full string with used two array for example name1 is stroe 'Hello' and name2 'World' and finel display this string in the terminal.

Example


#include<stdio.h>
#include<conio.h>

void main()
{
	char str1[10],str2[10],str3[10];
	clrscr();
	
	printf("Enter Word 1:");
	scanf("%s",str1)
	
	printf("Enter Word 2:");
	scanf("%s",str2)
	
	printf("Enter Word 3:");
	scanf("%s",str3)
	
	printf("%s%s%s",str1,str2,str3);
	
	getch();
}							
							
Share Share on Facebook Share on Twitter Share on Google+ Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit