C tutorial Lesson 11 - Typecasting

This is a discussion on C tutorial Lesson 11 - Typecasting within the C Programming forums, part of the General Programming Boards category; Hi all, In the C tutorial on this site is a lesson on typecasting with the following code: Code: #include ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    13

    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?

    I have no idea as I'm just learning the very basics for now!

    Cheers.

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    457
    declaring x outside of main makes it a global variable - probably not a good idea for a loop index. You want to declare it in main(), but before the loop.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    13
    Ah ok, so should it work the way it's written in the tutorial or does it need to be changed?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by folem001 View Post
    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).
    Code:
    //C89
    int x;  // declaration
    for (x = 0; x < 256 ; x++)
    //C99
    for (int x = 0; x < 256; x++)
    You still don't have to make it global tho.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    13
    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?

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    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.

    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.

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

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by folem001 View Post
    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.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User jephthah's Avatar
    Join Date
    May 2010
    Location
    seattle
    Posts
    49
    Quote Originally Posted by cyberfish View Post
    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.


    Quote Originally Posted by cyberfish View Post
    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.



    Quote Originally Posted by cyberfish View Post
    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.



    .
    Last edited by jephthah; 05-30-2010 at 06:39 PM.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by jephthah View Post
    what the hell are you saying, "it's ugly"? how is it ugly?
    Why it's practically obscene! Just kidding. I think the reason this is usually considered insignificant is because it is.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by jephthah View Post
    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.

  11. #11
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    It is customary to use i for numerical iteration.

    If you get a 10 pages long function written by someone else (or yourself a few months ago), and you want to add a loop at the end, what variable will you use?

    x? y? i? r? j?

    You'll need to go through the whole function to figure out what you can use, and whether you'll need to declare it.

    In C99/C++, you can always just use i, and declare it inline.
    for (int i = 0; i < n; ++i)

    Even if i is already defined, it will mask (or whatever the right term is) the local i, which is usually what you want.

    Without it, if you have 10 loops in the function and they used up i j x y z r p q o n... do you really want to keep track of it?

    That's why it's ugly.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,306
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Quote Originally Posted by cyberfish View Post
    It is customary to use i for numerical iteration.

    If you get a 10 pages long function written by someone else (or yourself a few months ago), and you want to add a loop at the end, what variable will you use?

    x? y? i? r? j?

    You'll need to go through the whole function to figure out what you can use, and whether you'll need to declare it.

    In C99/C++, you can always just use i, and declare it inline.
    for (int i = 0; i < n; ++i)

    Even if i is already defined, it will mask (or whatever the right term is) the local i, which is usually what you want.

    Without it, if you have 10 loops in the function and they used up i j x y z r p q o n... do you really want to keep track of it?

    That's why it's ugly.
    What's wrong with reusing i ???

  14. #14
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    That's what most people do.

    You just need to scroll up to make sure i is declared before this point, and make sure it's used as an iterator (and nothing outside loops reference it).

  15. #15
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    So let's just add a loop to a very very very ...long function without scrolling up to start of function.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with file
    By nevrax in forum C Programming
    Replies: 12
    Last Post: 04-23-2007, 07:18 PM
  2. re:c++.prog.newbie on CPP board
    By edwardtisdale in forum C Programming
    Replies: 13
    Last Post: 04-30-2004, 01:35 AM
  3. resources with dev-c++
    By lambs4 in forum Windows Programming
    Replies: 0
    Last Post: 04-13-2003, 07:06 AM
  4. Error with Lesson 9 of the tutorial
    By Sugapablo in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 07:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21