Format specifiers for uint8_t, uint16_t, …?

They are declared in <inttypes.h> as macros: SCNd8, SCNd16, SCNd32 and SCNd64. Example (for int32_t): sscanf (line, “Value of integer: %” SCNd32 “\n”, &my_integer); Their format is PRI (for printf)/SCN (for scan) then o, u, x, X d, i for the corresponding specifier then nothing, LEAST, FAST, MAX then the size (obviously there is no … Read more

Program doesn’t execute gets() after scanf(), even using fflush(stdin)

If flushing std doesn’t work, then try reading in the extra characters and discarding, as suggested here. This will work: #include <string.h> #include <stdio.h> int main(){ char nombre[10]; char mensaje[80]; int c; printf(“Type your name:\n”); scanf(“%9s”, nombre); while((c= getchar()) != ‘\n’ && c != EOF) /* discard */ ; printf(“Now, type a message:\n”); gets(mensaje); printf(“%s:%s”,nombre,mensaje); … Read more

Get scanf to quit when it reads a newline?

Use fgets to read console input: int res = 2; while (res == 2) { char buf[100]; fgets(buf, sizeof(buf), stdin); res = sscanf(buf, “%f %f”, &real, &img); if (res == 2) c[i++] = real + img * I; } c[i++] = 1 + 0*I; // most significant coefficient is assumed to be 1 return i;

Why scanf(“%d”, […]) does not consume ‘\n’? while scanf(“%c”) does?

It is consistent behavior, you’re just thinking about it wrong. 😉 scanf(“%c”, some_char); // reads a character from the key board. scanf(“%d”, some_int); // reads an integer from the key board. So if I do this: printf(“Insert a character: “); scanf(“%c”, &ch); // if I enter ‘f’. Really I entered ‘f’ + ‘\n’ // scanf … Read more

tech