C tutorial Lesson 11 - Typecasting

Hi all,

In the C tutorial on this site is a lesson on typecasting with the following code:

Code:

#include <stdio.h>
int main()
{
for ( int x = 0; x < 256; x++ ) {
/* Note the use of the int version of x to output a number and the use
* of (char) to typecast the x into a character which outputs the
* ASCII character that corresponds to the current number
*/
printf( "%d = %c\n", x, (char)x );
}
getchar();
}

This didn't compile in my Visual C++ 2008 express edition.

I tried it again but with declaring x before being used in the for loop and it worked:

Code:

#include <stdio.h>
int x;
int main()
{
for ( x = 0; x < 256; x++ ) {
/* Note the use of the int version of x to output a number and the use
* of (char) to typecast the x into a character which outputs the
* ASCII character that corresponds to the current number
*/
printf( "%d = %c\n", x, (char)x );
}
getchar();
}

Is this due to a change in the way things are formatted since the tutorial was written? Or is the tutorial wrong?

Is this due to a change in the way things are formatted since the tutorial was written? Or is the tutorial wrong?

Actually, it's due to the fact that declaring a variable inside a for (parameter) is only legal under "C99", the ISO C standard of 1999 which was adopted by ANSI in 2000 according to wikipedia. I don't use VC++, but the default is probably C89, aka C90, the previous standard. I would guess the reason that C89 is considered the default is that lots of pre C99 code is in circulation and I'm pretty sure there is some slight backward incompatibility I've run into before, but cannot remember the details. So generally unless you really feel you have to use some C99 specific feature, you shouldn't write code that requires it (which that does).

Ok, so making it global is declaring it outside of a function? Whereas if it was declared inside a function, only that function could change it?

Yes, exactly, and main() is a function. You actually can change a variable instantiated in one function from another with the use of pointers, which you will get to fairly soon if you keep doing tutorials.

But that means x will be in scope for the whole function, meaning if you have 2 loops, the second loop cannot declare it again.

LOL wut? this doesn't make sense. is 'x' your only variable? what happened to 'y'? did someone steal all your variable names? i think you can figure out how to make a second loop in a function.

Originally Posted by cyberfish

You can limit the scope like this -

Code:

{
int x;
for (x = 0; x < 256; x++) {
...
}
}

But as far as I know, no one does that.

yeah, no kidding, no one does that. your example code is irrelevant; declaring a locally-scoped variable is a well undertood standard practice.

Originally Posted by cyberfish

It's ugly either way. That's why both C99 and C++ support inline declaration.

again... what the hell are you saying, "it's ugly"? how is it ugly? i could say sticking declarations willy nilly throughout the length of a function is ugly. at any rate, the only thing that declaring it inline really accomplishes is to allow flexibility in coding styles.

if you're going to code in C, then use C89 by default if you have any hope for your code to be portable. if you don't care about portability, then do what you want.

LOL wut? this doesn't make sense. is 'x' your only variable? what happened to 'y'? did someone steal all your variable names? i think you can figure out how to make a second loop in a function.

yeah, no kidding, no one does that. your example code is irrelevant; declaring a locally-scoped variable is a well undertood standard practice.

again... what the hell are you saying, "it's ugly"? how is it ugly? i could say sticking declarations willy nilly throughout the length of a function is ugly. at any rate, the only thing that declaring it inline really accomplishes is to allow flexibility in coding styles.

if you're going to code in C, then use C89 by default if you have any hope for your code to be portable. if you don't care about portability, then do what you want.

.

It's ugly because the code is more readable when you are able to declare variables when you are actually using them. In C89 you had to declare everything at the top of your context (be it a function or whatever) and it was a pain to go back and forth between declaration and use to figure out if the types are correct or to make changes, particularly when you are working with SOMEONE ELSE'S code.

1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

That said, if you are faced with a 10 page long function, your best bet is typically to try and break it up into smaller functions that do one thing and do it well before you add a loop at the end.

Originally Posted by Bjarne Stroustrup (2000-10-14)

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.