print out each char in c variable. it works but it prints out way too much shit.
Dylan Barnes
'i' is uninitialised. Also, you're weirdly mixing two different ways of iterating over a string. You don't need i at all.
Kayden Bennett
Read a book, are you trying to print the string one character forward untill the string runs out? Hello ello llo lo o
Julian Hill
e is a pointer. try dereferencing it
Jackson Wood
no. i want to print each char in the string.
Jace Cook
Print with "%c" not "%s" for a start in that case
James Rogers
instead of "%s", use "%c".
Luis Taylor
You need to null terminate your string. This piece of shit grandpa language requires it otherwise you get undefined behaviour. Also make a string wrapper struct that has string length built in. Fuck it is sad people still make these mistakes but I guess C is C.
Lincoln Flores
that just prints garbage for me.
Liam Taylor
#include int main() { const char* c = "this is a long string that has a bunch of stuff inside of it"; puts(c); }
i can't use that because im going to use fprintf eventually.
Aaron Martin
String literals are automatically null terminated, idiot.
Grayson Reed
It's already NULL-terminated, retard.
Chase Lewis
I forgot a ')' there but you get the point.
Jonathan Martinez
Oh shit I forgot, I don't use this brainlet niggerlicious low level language. Also OP watch out because as I recall if you wan't a mutable string you might not get it declaring it that way and const in C is a fucking waste of time because you can modify the underlying memory.
James Anderson
>printf >not putc
Evan Lewis
Gets compiled to the same assembly, again, read a book.
Michael Moore
this works. thanks.
Grayson Sanchez
it's not C that's a niggerlicious brainlet piece of shit
Kayden Peterson
No problem, but you really shouldnt write C if you couldnt figure this out
James King
You can't italicize in C so I don't know what yore talking about....
Alexander Ortiz
...
Andrew Cooper
Right, it is the people who use it.
Levi Clark
That's compiler-dependent, not something defined by the C standard. putc/putchar is far more elegant
Colton Hughes
If anyone was wondering how to get OP's godawful code to work here it is:
#include #include
int main() { const char* c = "this is a long string that has a bunch of stuff inside of it"; char* e; int len; int i;
len = strlen(c);
for (e = (char*)c; i < len; e++) { printf("%c", *e); i++; }
return 0; }
Christopher Barnes
i is still uninitialised.
Jayden Gutierrez
In that you are correct, I must say that Im just far too used to using printf.
Leo Richardson
That is just technically UB, and most modern compilers will initialize it to 0.
Camden Bailey
>modern compilers will initialize it to 0 gcc sure as heck wont
Blake Collins
I stand corrected, gcc DOES do that. Still a bad practice tough.
Brayden Cook
Even if you're just lucky with the fact that something happens to be the value you think it is, compilers will still optimise things based on the assumption that UB never happens and can completely break your program. NEVER allow undefined behaviour into your programs.
Caleb Richardson
>the dont think i be like i is, but i is
Gavin Ross
The art of coaxing computers into doing what a language operator wants.
Leo Jones
It's not necessary to get the length of the string. Check for null/0 to see where it ends(which is how strlen works anyway)
printf("%s", e); Prints out an entire string, that is, from the pointer e, up until it finds a newline, or null character ('\n' or '\0'). What you want is to print out a single character wtih '%c'. printf("%c", e);
Alternatively, you could reduce this entire code to 2 lines: int main() { const char* c = "this is a long string that has a bunch of stuff inside of it"; printf("%s", c);
return 0; }
Also, please RTFM: man printf
Jayden Bailey
That's because you need to dereference the pointer e. So it would be printf("%c", (char) *e);
William Cox
Who mans the man pages?
Kayden Walker
man printf That'll actually give the shell command. If you want the C library function, it's man 3 printf
Lincoln Adams
The problem is you are not printing a char but a string printf("%s",e) would print all the chars upto /0 not a single char, you could also see that after every increment of e the the preceding char is not printed.
Aiden Garcia
my solution would be to change e to a char , start with for(i = 0; i < len; c++){ printf("%c",*c); i++; }
Thomas Cox
So ya see, you double-click on the printer icon and then it 'prints' it out for you.