Email or username:

Password:

Forgot your password?
✧✦✶✷Catherine✷✶✦✧

Can you answer this simple question about the C programming language?

$ cat test.c
#include <string.h>
#include <stdio.h>
int main() { printf("%d\n", strlen("\x01c")); }
$ cc test.c && ./a.out

What number does this program print?

Anonymous poll

Poll

0
15
3.5%
1
127
29.5%
2
211
49%
other / results
78
18.1%
431 people voted.
Voting ended 9 May at 2:37.
48 comments | Expand all CWs
prom™️

@whitequark I cheated using 'cplr' - a command-line C runner using tinycc that I hacked up. Still, great value to me - like a crash-fast repl. Then again, I should know the answer.

prom™️

@iximeow arbitrary oldksl imo. other platforms used length-prefixed strings.

iximeow

@promovicz length-prefixed strings have nothing to do with this

prom™️

@iximeow uh? i would have thought that this is about the null-termination trap - since strlen does not count the terminator. you'd have to add a 1 for allocation - for example. length-prefixed strings don't have that ambiguity.

iximeow

@promovicz if that was so, it would print at least 2 (and possibly more depending on if a null happens to be immediately after `c`)

prom™️

@iximeow i meant a different aspect: on a platform with length prefixes, we would be unlikely to ask this question. but whitequark will know what she's playing at.

Iranon

@whitequark idk off the top of my head what %zu formats an integer as.

✧✦✶✷Catherine✷✶✦✧

@iranon it's just a decimal (same as %d but without a warning)

Genders: ♾️, 🟪⬛🟩; Soni L.

@whitequark "wait does C even support hex escapes? uh. oh. oh, no."

Genders: ♾️, 🟪⬛🟩; Soni L.

@whitequark (today we learned that C supports hex escapes. and that it is, unfortunately, designed around CHAR_BIT != 8. which makes sense we suppose...

remember to terminate your escapes with "" we guess, e.g. "\x01""c", or "\x01""\x02""")

Xandra Granade 🏳️‍⚧️

@whitequark Putting `printf("\x01c")` into godbolt, it was very weird to see `putchar 28` until I tried `printf("\x001c")`. God damn it, C.

iximeow

@fay59 @whitequark its because the hex literal is truncated to a byte, and then that's your character ?????

Random Internet Cat

@iximeow This is prohibited by the C++ standard, and GCC rejects if you pass "-pedantic -pedantic-errors".

prom™️

@iximeow @whitequark ah. yes. that seems "logical" to me, because this is C. pardon my (slightly) gray hair.

Félix

@iximeow @whitequark I know I ran into this before because now I remember I tried to write “\x01” “c” to resolve the ambiguity and it STILL merges them

lawless polymorph

@fay59 @iximeow @whitequark i tried this out and it didn't merge them 😕

Félix

@typeswitch @iximeow @whitequark lol I guess I got confused because -Xclang -ast-dump shows char[3] “\001c” and the octal syntax eats exactly 3 digits

Félix

@typeswitch @iximeow @whitequark (and c isn’t an octal digit, but \010000 still parses to “\010””000” and I should probably get away from computers for the rest of the day)

Brian Swetland

@whitequark Wow.

a. I was wrong.
b. I've used \x.. an awful lot over the years.
c. I have no idea how the combination of (a) and (b) has not yet caused me grief.

lawless polymorph

@whitequark thanks to this post i was able to find a bug in my mirth to C99 compiler, so, thank you :) github.com/mirth-lang/mirth/pu

Chris Harrington ☕✊

@whitequark I knew I would be wrong, let myself be wrong, was wrong, and let my soul leave me for a moment as I figured out why.

Ben Bradley

@whitequark I voted but after reading other responses I might want to change my vote. Now to try the compilers I have, and to compile as C.
"I started learning C in 1986 and thot by now I'd be an egg-spurt"

Gorgeous na Shock!

@whitequark Hey I answered and then was worried because the majority answered a different thing but then I tried it after doubting my sanity and it turned out I was right! I'm pretty good at this. 😏

Kudos to those folks for answering honestly, tho. 😌

kandi3kan3

@whitequark I've been bitten by this more than once. 🙃

jn

@whitequark i knew it was a trick question, but i didn't see how

tnt

@whitequark If you have syntax highlight it makes it obvious

humm

@whitequark C does not define the exact width of a byte. A char (read: byte) must be at least 8 bits wide, but can be as wide as the implementation wants. So if you have 12-bit chars and want to specify their values using \x, two hexadecimal digits won’t be enough. And saying “all following digits are part of the number” is easier for both the specification and the programmer than saying “the number of digits is ceil(CHAR_BIT/4).” In theory, in a world where you actually have systems with CHAR_BIT ≠ 8.

Go Up