Difference between scanf("%s",name); and scanf ("%[^\n]%*c", name); -


I have written RSS using GMP packages. While taking a plain text I have taken it as a string using the scanf ("% s", name); and then converted to gmp type after running its program for 50 times, which was successfully executed, the scanf function did not work. Therefore, I have the fgets (name, 100, stdin); Has read the input using and scanf ("% [^ \ n]% * c", name); And it works well.

What is the difference between using these 2 types of scan functions

 < Code> scanf (name, "% s");  

The user will scan the input until it encounters any place, that is, when you type hi bye , only hi < / Code> will be stored in name . On the other hand,

  scanf ("% [^ \ n]% * c", name);  

will scan unless it copes with \ n or an enter key. To scan a letter, % * c tells scanf and discard it. In your case, the % * c is marked by the \ n character which is present in stdin as you entered the string Was pressed.


Comments

Popular posts from this blog

winforms - C# Form - Property Change -

javascript - amcharts makechart not working -

java - Algorithm negotiation fail SSH in Jenkins -