Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Use scanf in following way to read the student name:</p> <pre><code>scanf(" %[^\n]",studentlist[i].name); </code></pre> <p>The first space in the format specifier is important. It negates the newline from previous input. The format, by the way, instructs to read until a newline (\n) is encountered.</p> <p><strong>[Edit: Adding explanation on request]</strong></p> <p>The format specifier for accepting a string is <code>%s</code>. But it allows you to enter non-whitespace characters only. The alternative way is to specify the characters that are acceptable (or not acceptable, based on the scenario) within square brackets.</p> <p>Within square brackets, you can specify individual characters, or ranges, or combination of these. To specify characters to be excluded, precede with a caret (^) symbol.</p> <p>So, <code>%[a-z]</code> would mean any character between a and z (both included) will be accepted</p> <p>In your case, we need every character other than the newline to be accepted. So we come up with the specifier <code>%[^\n]</code></p> <p>You will get more info on these specifiers from the web. Here's one link for convenience: <a href="http://beej.us/guide/bgc/output/html/multipage/scanf.html" rel="nofollow noreferrer">http://beej.us/guide/bgc/output/html/multipage/scanf.html</a></p> <p>The space in the beginning actually 'consumes' any preceding white space left over from previous input. You can refer the answer here for a detailed explanation: <a href="https://stackoverflow.com/questions/6083045/scanf-n-skips-the-2nd-input-but-n-does-not-why">scanf: &quot;%[^\n]&quot; skips the 2nd input but &quot; %[^\n]&quot; does not. why?</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload