User Tools

Site Tools


Sidebar

projects

ntr0 (due 20210825)
pct1 (bonus; due 20210819)
wcp1 (due 20210818)
pct2 (due 20210825)
wcp2 (due 20210825)
lob0 (due 20210901)
pct3 (bonus; due 20210901)
wcp3 (due 20210901)
led0 (due 20210908)
pct4 (due 20210908)
wcp4 (due 20210908)
led1 (due 20210915)
gfo0 (due 20210915)
pct5 (bonus; due 20210915)
wcp5 (due 20210915)
led2 (due 20210922)
pct6 (due 20210922)
wcp6 (due 20210922)
iwb0 (due 20210929)
pct7 (bonus; due 20210929)
wcp7 (due 20210929)
iwb1 (due 20211006)
pct8 (due 20211006)
wcp8 (due 20211006)
iwb2 (due 20211020)
pct9 (bonus; due 20211020)
bwp1 (bonus; due 20211020)
wcp9 (due 20211020)
clr0 (due 20211027)
gfo1 (due 20211027)
pctA (due 20211027)
wcpA (due 20211027)
clr1 (due 20211103)
pctB (bonus; due 20211103)
wcpB (due 20211103)
clr2 (due 20211110)
pctC (due 20211110)
wcpC (due 20211110)
gfo2 (due 20211117)
pctD (bonus; due 20211117)
pctE (bonus; due 20211201)
bwp2 (bonus; due 20211201)
EoCE (due 20211209)
haas:fall2021:c4eng: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/fall2021/c4eng/scanf_input.txt · Last modified: 2013/02/15 14:55 by 127.0.0.1