EmbLogic's Blog

issue in c program

//just go thru the simple prog

#include
#include
int main()
{
char str[5];
int i;
for(i=0;i<5;i++)
{
printf("\n enter the character :");
scanf("%c",str+i);

}
printf("\n %s\n ", str);

return 0;
}

predict what is the output

if u think output is
"
enter the character :a
enter the character :b
enter the character :c
enter the character :d
enter the character :e
abcde
"

then u r wrong.. :P

output is
enter the character :a
enter the character :
enter the character :b
enter the character :
enter the character :c

a
b
c
"
this is the original output..

can someone please help and elaborate..

2 Responses to issue in c program

  1. hemant.kumar says:

    here when you type a then hit ENTER it takes ‘\n’ as a character thats wht you have got blank over there..

  2. Yep Enter takes a character length ..
    Plus u can enter the characters in one go i.e. abcde

    u dont need to use a for loop to accept value for each index of the char array

Leave a Reply to Hitesh Dhanwani Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>