How to take single character,string and sentences in C

 









We know to take a single character in C, we use %c format specifier.

scanf("%c",&single_character_variable);


To take a string as input in C, we use %s format specifier.

scanf("%s", string);     // It accepts string only until it finds the first space.


To take a line as a input in C, we use -   %[^\n]%*c 

scanf("%[^\n]%*c",string);   // Here, [ ] is the scanset character. ^\n stands for taking input until a newline is not encountered. Then, with this %c, it reads the newline character and here, the used * indicated that this newline character is discarded. 







Comments