Page 1 of 1

Scanf behaviour

Posted: Wed Oct 14, 2020 11:25 am
by cborn
i use zcc +zx -vn dice_081j.c -o dice081j -lndos -create-app -zorg=32768
but i already reinstalled z88dk by removing the directory and the do a BUILD_SDCC=0. I asume then all libs linked are the 'classic' ones.
I thoughed the above tread is about random but its a zx81 plot routine.

while learning c there is more then 1 obstacle, eg:
decimals: scanf("%d", &hand_of_dice) ;
characters: scanf("%cd", &hand_of_dice) ;
seem to behave different from each other
i have to check that by makeing al differenrt ways

Re: Scanf behaviour

Posted: Sat Oct 17, 2020 8:13 pm
by dom
I split the topic since we're moving onto how scanf works.

I often find it useful to write a short program and then verify behaviour with my system compiler, so:

Code: Select all

#include <stdio.h>

int main()
        char *str = "12";
        int   ret, hand_of_dice;

        ret = sscanf(str,"%d", &hand_of_dice) ;
        printf("Return code %d value %d\n",ret, hand_of_dice);

        ret = sscanf(str,"%cd", &hand_of_dice) ;
        printf("Return code %d value %d\n",ret, hand_of_dice);
I've used sscanf since I'm too lazy to type in things!

The output (using both z88dk and the system compiler) of this is:

Code: Select all

Return code 1 value 12
Return code 1 value 49
Return code indicates how many values have been parsed, and value is the decimal value of what was parsed. It's the result I expect: %d parses a whole number, and %c just a single character.