User Tools

Site Tools


haas:fall2019:common:fgetc_input

Input with fgetc()

scanf() isn't the only game in town when it comes to input. Another common input function I highly recommend knowing is the venerable fgetc(), which specializes in reading just a single character (ie byte of data) from a specified file pointer.

1
#include <stdio.h>
 
int main()
{
    char a, b, c, d;
 
    fprintf(stdout, "Enter a char:");
    a = fgetc(stdin);
 
    fprintf(stdout, "Enter a char:");
    b = fgetc(stdin);
 
    fprintf(stdout, "Enter a char:");
    c = fgetc(stdin);
 
    fprintf(stdout, "Enter a char:");
    d = fgetc(stdin);
 
    fprintf(stdout, "You entered ASCII chars %hhu, %hhu, %hhu, and %hhu\n", a, b, c, d);
 
    return(0);

Questions

  • Does the input of all 4 chars work according to plan?
    • If not, what is “off”?
    • What chars are in the “off” entries?
    • What char does that represent?
  • If you wanted to convert your input, if it is a number (which is in ASCII char format) to a number, how might we accomplish that?
haas/fall2019/common/fgetc_input.txt · Last modified: 2013/02/15 15:10 by 127.0.0.1