Disclaimer: This question asks how "str literal" + "str literal"
works
For how 'a' + 'b'
or '9' - '0' = 9
('character' + 'character') works :
Question:
To everyone who's more familiar with C, thanks for reading
(compiled with clang, standard=C11)
Example:
(trying to print
__FILE__
without its".c"
extension)
printf("%s\n", __FILE__);
returnsfilename.c
printf("%.*s\n", (int)(".c" - __FILE__), __FILE__);
returnsfilename
1. How does C typecast string/string literals to int? Are whitespaces ignored?
- What does the value of an
(int)"string"
represent?Another example:
(int)("word" - "rd") = 6273 (int)("rd" - "word") = -6273 (int)("word" - " rd") = -5 (int)(" rd" - "word") = -5
Why does
(int)(".c" - __FILE__)
even work?
3. Is the printf
function above actually working?
4. Is there a string equivalent to 'a' + 1 = 'b' ?
Thanks in advance guys!
irrelevant guessing:
1
Why does (int)(".c" - __FILE__)
even work?
guessing its
some value of (first?) pointer to ".c"
- some value of (first?) pointer to __FILE__ string literal
2. What does the value of (int)"string"
actually represent?
- Why does
(int)(".c" - __FILE__)
even work?
idk but here's another example:
printf("%i", (int)".c");
printf("%i", (int)__FILE__);
printf("%i", (int)(".c" - __FILE__));
printf("%i", (int)(__FILE__ - ".c"));
printf("%./*i", (int)(".c" - __FILE__), (int)__FILE__);
printf("%./*i", (int)(".c" - __FILE__), (int)("c" - __FILE__));
output
---------------------
(int) ".c"= 4357629
(int) __FILE__= 4357620
(int) (".c" - __FILE__) : 9
(int) (__FILE__ - ".c"): -9
(int with precision specified) __FILE__ : 004357620
(int with precision specified) (".c" - __FILE__): 000000009
$
3. Is printf
actually working?
Assuming it does, probably:
printf("%.*s",(int)(".c" - __FILE__), __FILE__)
width = (int)(".c" - __FILE__)
specifier/str = __FILE__
printf
prints out __FILE__
as a string of width (".c" - __FILE__)
(two characters less)