#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
int main(void)
{
uint64_t *ptr = malloc(sizeof(uint64_t));
scanf("%llu",&ptr);
printf("%llu\n", *ptr);
free(ptr);
}
The compiler says that
mod_5_working.c:9:14: error: unknown conversion type character 'l' in format [-Werror=format=]
scanf("%llu",&ptr);
^
mod_5_working.c:9:11: error: too many arguments for format [-Werror=format-extra-args]
scanf("%llu",&ptr);
I've tried using %u
but it says that I should use %llu
.
CodePudding user response:
scanf("%llu",&ptr);
is senseless, like the compiler tells you, you are taking the address of a pointer.uint64_t
doesn't necessarily correspond tounsigned long long
. Some 64 bit systems useunsigned long
for 64 bit numbers.
The correct, portable specifiers to use when scanning/printing uint64_t
is SCNu64
and PRIu64
from inttypes.h:
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdlib.h>
int main(void)
{
uint64_t *ptr = malloc(sizeof(uint64_t));
scanf("%"SCNu64, ptr);
printf("%"PRIu64"\n", *ptr);
free(ptr);
}
CodePudding user response:
scanf
expects a pointer, not a pointer to a pointer. Simply pass the pointer to scanf
, no need to &
it. Also you should always check the return value of malloc
for errors.
uint64_t *ptr = malloc(sizeof(uint64_t));
if (!ptr)
return -1; /* Maybe handle it better */
scanf("%" SCNu64, ptr);
printf("%" PRIu64 "\n", *ptr);
free(ptr);
Edit: It's a better practice to use SCNu64
and PRIu64
(from <inttypes.h>
) to make it portable and accurate.
CodePudding user response:
Use the matching specifier.
#include <stdio.h>
#include <inttypes.h> /* Format conversions for exact-width types */
scanf("%" SCNu64, ptr); // No &.
printf("%" PRNu64 "\n", *ptr);