User Tools

Site Tools


Sidebar

projects

wcp1 (due 20240828)
wcp2 (due 20240904)
pct0 (bonus; due 20240905)
pct1 (bonus; due 20240905)
pct2 (due 20240905)
abc0 (due 20240906)
gtf0 (due 20240911)
pct3 (bonus; due 20240911)
wcp3 (due 20240911)
dtr0 (due 20240918)
pct4 (due 20240918)
wcp4 (due 20240918)
mmf0 (due 20240925)
pct5 (bonus; due 20240925)
wcp5 (due 20240925)
cnv0 (due 20241002)
gfo0 (due 20241002)
pct6 (due 20241002)
wcp6 (due 20241002)
fwg0 (due 20241009)
pct7 (bonus; due 20241009)
wcp7 (due 20241009)
haas:fall2024: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/fall2024/cprog/scanf_input.txt · Last modified: 2013/02/15 14:55 by 127.0.0.1