0

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 ?

4
Contributors
3
Replies
4
Views
9 Years
Discussion Span
Last Post by gerard4143
0

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));
}
0
#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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.