Why is it that this won't work instead of using atoi ?
Inline Code Example Here #include<stdio.h>

int main(void)
{
printf("%d",(int)"10");

return 0;
}

Recommended Answers

All 2 Replies

Casting in C says 'treat me like something else at the byte level'. "10" is not the same thing as 10 at the byte level, so the cast doesn't do what you want.

If you want to convert "10" to 10 then you need to call one of the conversion functions, such as atoi()

printf("%d\n", atoi("10"));
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.