User Tools

Site Tools


Sidebar

projects

  • cci0 (due 20170607)
  • sof0 (due 20170614)
  • dow0 (due 20170621)
  • mbe0 (due 20170628)
  • pnc0 (due 20170705)
  • pnc1 (due 20170712)
  • mbe1 (due 20170719) iOR
  • pnc2 (due 20170719) (or do both, 1 as bonus)
  • fcc0 (due 20170726)
  • sfa0 (due 20170802)
  • oop0 (due 20170809)
  • EoCE – bottom of your journal (due 20170819)
haas:summer2017:cprog:scanf_input

Table of Contents

scanf() input

1
#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    int a, *b;
    short int c;
    char d;
    float e;
 
    b = (int *) malloc (sizeof(int) * 1);
 
    fprintf(stdout, "Enter an integer: ");
    fscanf(stdin, "%u", &a);
 
    fprintf(stdout, "Enter another integer: ");
    fscanf(stdin, "%u", b); // why don't we need & in this case?
 
    fprintf(stdout, "Enter a short int: ");
    fscanf(stdin, "%hu", &c);
 
    fprintf(stdout, "Enter a char: ");
    fscanf(stdin, "%c", &d);
 
    fprintf(stdout, "Enter a float value: ");
    fscanf(stdin, "%f", &e);
 
    fprintf("Your ints are: %u and %u\n", a, *b);
    fprintf("Your short int is: %hu\n", c);
    fprintf("Your char is: '%c'\n", d);
    fprintf("Your float (trimmed at 2 decimal places) is: %.2f\n", e);
 
    return(0);
}

Questions

  • Why doesn't the input of b in fscanf() need a preceding &?
  • Does the program work properly?
    • If not, what seems to be going wrong?
    • How would you figure out where the problem seems to be manifesting?
    • How could you make it right?

Hints

  • Try moving the last fprintf() earlier in the list of final fprintf()'s. Does this make a difference? If so, WHY?
haas/summer2017/cprog/scanf_input.txt · Last modified: 2013/02/15 14:55 by 127.0.0.1