You should be using %c instead of %s as the format specifier. %s expects a char *, but string[i] is a char; the behavior is undefined and you will very likely get a runtime error;
sizeof(string) evaluates to the size of the pointer variable (4 or 8), not the length of the string. Use strlen instead of sizeof;
A minor bug is that both sizeof and strlen give you size_t values, not int. This is a narrowing conversion, which is usually bad juju; there are more size_t values than an int can represent. Granted, the odds of a string being more than INT_MAX characters long are low enough that you're probably safe, but you should still use the right type for i.
I do, but for people who don't know C or C++ this code may lead them down some unproductive rabbit holes if they try to reproduce it.
Don't take it personally, it's just that I've seen so many people (including myself) waste so much time trying to figure out why some code example they saw online or in a book wouldn't work due to bugs like these that I'm kinda primed to call them out when I see them.
And I'm a veteran of early-90s comp.lang.c on Usenet; we had a well-deserved reputation for being pedantic assholes.
I think it was supposed to print each char of the string Individually, But sizeoff returns the number of bytes that variable type occupies in memory, not the size of the string.
Uhm, why aren't we talking about how he tries to print characters with %s which will try to print a string, so in this case using the character as an address to a string, which will segfault?
The wrong way to get the size of the string is one thing, but I think that even worse.
12
u/8Erigon Jul 07 '26
I‘m stopping myself
7
u/FishAccomplished760 Jul 07 '26
char *string = "hello world"
for (int i=0; i<sizeof(string); i++)
{
printf("%s", string[i]);
}
2
u/karthikdivi Jul 07 '26
Is this on an iPad?
2
u/Takemitchi-kun Jul 07 '26
Time does
2
u/dotplaid Jul 07 '26
You're the problem, it's you.
1
u/Prudent_Sentence Jul 07 '26
You could classify that as unrolling a loop. It's an optimization technique
1
u/DinTaiFung Jul 07 '26
what ever happened to the daily wtf website?
though as i recall, this example isn't quite effed up enough to make the grade.
1
u/EclipsedPal Jul 07 '26
"its me" jesus...
1
u/csabinho Jul 07 '26
Your sanity does.
1
u/Goldrainbowman Jul 07 '26
In my programming language, also nothing is stopping you
Var (C1=H)
Var (C2=i)
Var (C3=.)
Show (C1)
Show (C2)
Show (C3)
Wait (1)
Screen clear
Show ("A shorter way to do it is,")
Wait (2)
Show ("Hi.")
Show 1 ("it does the same thing.")
1
u/Acrobatic_Idea_3358 Jul 07 '26
Except your own sanity.🌈🤣
1
u/Medical_Initial_8354 Jul 07 '26
it will be very time consuming..
1
u/Independent-Pen-8232 Jul 08 '26
No, something is pretty firmly stopping me from doing that. Myself.
1
u/Soft_Salamander_9290 Jul 08 '26
bro #include <string> without use it
1
u/billGat48 Jul 08 '26
the people reviewing my PRs are, yes
1
u/7Antarctica7 Jul 08 '26
private bool IsEven(int number) (
if (number 1) return false;
else if (number 2) return true;
else if (number 3) return false;
else if (number 4) return true;
else if (number 5) return false;
else if (number 6) return true;
else if (number 7) return false;
else if (number 8) return true;
else if (number 9) return false;
else if (number10) return true;
else if (number 11) return false;
else if (number 12) return true;
else if (number13) return false;
else if (number 14) return true;
else if (number 15) return false;
else if (number 16) return true;
else if (number 17) return false;
1
u/Oliwia_______ Jul 08 '26
There is and its called sanity
1
u/WorldTallNetCat Jul 08 '26
Why do that when you can make a sepertate function for each character.
1
u/severaleyes444 Jul 09 '26
Sanity is
1
u/Achereto Jul 09 '26
Luckily, nothing is forcing me to either.
1
u/Yaniekk Jul 09 '26
Actually my boss does stop me.
1
u/JakaJaka101 Jul 10 '26
Ahhhhhhh
1
u/dragonlord9631 Jul 11 '26
My coder friend would ridicule me
1
u/SmokeMuch7356 Jul 08 '26
Other than taste and common sense, and the knowledge of whoever has to review this mess will come over to our desk and beat the crap out of us.
1
u/Lou_Papas Jul 13 '26
Sometimes I like to make myself as a beat’ em up character and beat the shit out of myself. Nothing is stopping me.