when i use for loop to get a string i have following problem why?

include<stdio.h>
void main()
{
int i;
char name[5];
for(i=0;i<5;i++)
scanf("%c",&name[i]); 
for(i=0;i<5;i++)
printf("%c",name[i]);
}

output
world

here for world i get only worl.
that is for 5 char I get only four why ?

Recommended Answers

All 3 Replies

At the end of the program, try getting the length of the 'name' using strlen() i.e.

...
printf("%c",name[i]);
printf("\nlength of name is: %d", strlen(name));
}
#include <stdio.h>

int main()
{
	int i;
	char name[5];
	for(i=0;i<5;i++)
		scanf("%c",&name[i]);

	for(i=0;i<5;i++)
		printf("%c",name[i]);
	return 0;
}

Compiled and ran your code...it works fine. Note main should return an int

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.