c - %X format specifier to represent an integer -



c - %X format specifier to represent an integer -

i've been taking zed a. shaw's "learn c hard way" tutorials. 1 of credit questions (in exercise 9) requested me represent char[4] int.

after scouting around came on question same thing , hence managed come answer:

int i; int intname = 0; for(i = 0; < 4; i++){ intname = intname + name[i]; } printf("intname: %d\n", intname); printf("intname: %x\n", intname);

output:

intname: 291 intname: 123

the name @ hand was, "zed", calculated ascii decimal values , did indeed add together 291, fine. after searching couldn't find explanation why %x homecoming 123.

in scenario, format specifier do? awesome if of detailed reply what's going on. learning c seems far more in-depth , sense should understand these basics such these before moving on else.

however after searching couldn't find explanation why %x homecoming 123.

it's printing hex representation of number. hex 123 = decimal 291. printf man page (the first happened find):

o, u, x, x unsigned int argument converted unsigned octal (o), unsigned decimal (u), or unsigned hexadecimal (x , x) notation. letters abcdef used x conversions; letters abcdef used x conversions. precision, if any, gives minimum number of digits must appear; if converted value requires fewer digits, padded on left zeros. default precision 1. when 0 printed explicit precision 0, output empty.

c

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -