i need solve following exercise:
"write program acquires first characters of string integer octal , write on string empty, display contents of second string in decimal, using functions
sscanf
,sprintf
. example, if user enters 12 (octal) system must show 10 (decimal)."
after scanf
, array seconda
="12325"
. problem not know how make understand string octal number , how convert decimal sprintf
.
this current code:
#include <stdio.h> int main(void) { char prima[] = "12325dsdfa"; char secodna[500]; sscanf_s(prima, "%[0123456789]o", secodna, 500); }
you're mis-using %o
, mixing character set format %[]
, passing many arguments.
you should use %o
, convert unsigned integer octal many characters possible.
then convert decimal string using %u
sprintf()
.
something this:
#include <stdio.h> int main(void) { unsigned int x; if(sscanf("12foo", "%o", &x) == 1) printf("%u\n", x); return 0; }
note above outputs decimal string stdout rather keeping around (i.e. uses printf()
instead of sprintf()
), that's trivial change. prints "10
".
Comments
Post a Comment