Scanf behaviour

ZX80, ZX 81, ZX Spectrum, TS2068 and other clones
Post Reply
cborn
Well known member
Posts: 267
Joined: Tue Oct 06, 2020 7:45 pm

Scanf behaviour

Post by cborn »

@dom
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
User avatar
dom
Well known member
Posts: 2076
Joined: Sun Jul 15, 2007 10:01 pm

Re: Scanf behaviour

Post 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.
Post Reply