Domanda

Let's see this line of C code:

if(sscanf(s, "%04d%02d%02d%02d%02d%02d", &year, &month, &day, &hour, &min, &sec) != EOF) { 
    // ... other stuff
}

I'm using Microchip C30 compiler. 's' and the other variables are defined before. The size of my whole code is: 237396 bytes.

Let's try to change the format string from literal to pointer:

const char *format = "%04d%02d%02d%02d%02d%02d";
if(sscanf(s, format, &year, &month, &day, &hour, &min, &sec) != EOF) { 
    // ... other stuff
}

The size is now: 243798 bytes! More than 6 kB!

Is it an expected behavior?

È stato utile?

Soluzione

In the first case the compiler knows what select set of sscanf() conversions are to be used: string to int.

sscanf(s, "%04d%02d%02d%02d%02d%02d", ...

In the second case, lacking this detail, the compiler thinks you might pass any format specifier, so the entire scanf() set of conversions needs to be in code.

const char *format
sscanf(s, format, ...
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top