Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As indicated by the other answers, the problem was caused by the <strong>carriage return</strong>.</p> <p>There are many ways solve this, some more complex than others. Therefore I will indicate the simplest method I've found:</p> <p>As <a href="http://www.gnu.org/software/libc/manual/html_node/Character-Input.html" rel="nofollow">the GNU website indicate</a> the most appropriate function for this is <code>getc(stream)</code> and I quote:</p> <blockquote> <p>This is just like <strong>fgetc</strong>, except that it is permissible (and typical) for it to be implemented as a macro that <strong>evaluates the stream</strong> <strong>argument more than once</strong>. <strong>getc</strong> is often highly optimized, so it is usually <strong>the best function to use to read a single character</strong>.</p> </blockquote> <p>I put it into practice <strong>but</strong> it still have the <strong>CR</strong> problem, so I printed the result of this function and notice that it <strong>return 10 when CR</strong>. After this I tried to get functionality and clarity for the code. Doing everything possible for this, now my code is the following:</p> <pre class="lang-c prettyprint-override"><code>main() { char a,b; printf("\nEnter the first char.. "); if ((a=getc(stdin))==10) a=getc(stdin); printf("\nEnter the second char.. "); if ((b=getc(stdin))==10) b=getc(stdin); printf("\nFirst char --&gt; %c",a); printf("\nSecond char --&gt; %c",b); } </code></pre> <p>And the nice output:</p> <blockquote> <p>First char --> z</p> <p>Second char --> z ('z' or the char there I wan to)</p> </blockquote> <hr> <p>I've learned that all the programs need they own solution, different than the others, and that is the reason <em>(I think)</em> for that are so many functions. What I can say is "Always have to adapt the program. Maybe this sentence works for me but is not guaranteed that gonna work for other codes."</p> <p>Thanks to all the guys that helped me!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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