So what am i doing wrong here?

so what am i doing wrong here?
#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("%s", e);
i++;
}

return 0;
}

Attached: wealreadytried.png (265x315, 203K)

What the hell are you trying to do

print out each char in c variable. it works but it prints out way too much shit.

'i' is uninitialised. Also, you're weirdly mixing two different ways of iterating over a string.
You don't need i at all.

Read a book, are you trying to print the string one character forward untill the string runs out?
Hello
ello
llo
lo
o

e is a pointer.
try dereferencing it

no. i want to print each char in the string.

Print with "%c" not "%s" for a start in that case

instead of "%s", use "%c".

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.

that just prints garbage for me.

#include
int main()
{
const char* c = "this is a long string that has a bunch of stuff inside of it";
puts(c);
}

Try
char *s = "hello user";
while (*s)
printf("%c", *(s++);
~
~
~
~
~

i can't use that because im going to use fprintf eventually.

String literals are automatically null terminated, idiot.

It's already NULL-terminated, retard.

I forgot a ')' there but you get the point.

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.

>printf
>not putc

Gets compiled to the same assembly, again, read a book.

this works. thanks.

it's not C that's a niggerlicious brainlet piece of shit

No problem, but you really shouldnt write C if you couldnt figure this out

You can't italicize in C so I don't know what yore talking about....

...

Right, it is the people who use it.

That's compiler-dependent, not something defined by the C standard. putc/putchar is far more elegant

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;
}

i is still uninitialised.

In that you are correct, I must say that Im just far too used to using printf.

That is just technically UB, and most modern compilers will initialize it to 0.

>modern compilers will initialize it to 0
gcc sure as heck wont

I stand corrected, gcc DOES do that. Still a bad practice tough.

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.

>the dont think i be like i is, but i is

The art of coaxing computers into doing what a language operator wants.

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)

const char* string = "hello world";
for(int i = 0; string[i] != '\0'; i++){
printf("%c", string[i]);
}


If you don't need individual elements:
const char* string = "hello world";
printf("%s", string);


In C++ this would be
std::string string = "hello world";
for(const auto& e: string){
std::cout

Holy fuck kekd

Seriously?

Attached: Setzer - Shocked.gif (32x48, 511)

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

That's because you need to dereference the pointer e.
So it would be printf("%c", (char) *e);

Who mans the man pages?

man printf
That'll actually give the shell command. If you want the C library function, it's man 3 printf

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.

my solution would be to change e to a char , start with for(i = 0; i < len; c++){
printf("%c",*c);
i++;
}

So ya see, you double-click on the printer icon and then it 'prints' it out for you.