Others have already answered the first two questions correctly, so I will answer your third question:
When you type character and press ENTER, two characters are placed in the input buffer, character character and newline .
You need to consider both of these options. So, the first scanf consumes a new line, and the other reads a character.
Step by Step code analysis:
printf("enter string \n"); scanf("%s",&str);
Over two statements, you see Enter the string , and the program is waiting for your input. Suppose you enter the character C and press Enter once. When you perform this action, the input buffer takes two characters:
- The character
C you entered and - Newline
\n
The scanf statement reads only one character ( C ) from the input buffer. Thus, the newline character remains unread in the Inuput buffer.
printf("enter char \n"); scanf("%c",&ch); //does not scan my char
Enter enter char displayed over two statements, but scanf simply skips (do not wait for uer to enter), this is because this unread newline character in the input buffer is read by this scanf .
To get the next input character correctly, you need an additional scanf .
scanf("%c",&ch); //with this second line do scan my char