Question

This is a discussion on Question within the C Programming forums, part of the General Programming Boards category; I sit around as an innocent newcomer to the world of programming, scrounging around on your magnificent forum, looking around ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    2

    Question

    I sit around as an innocent newcomer to the world of programming, scrounging around on your magnificent forum, looking around for code snippets that I actually might learn something from.

    and then I stumble over this

    Code:
    int x = '\n';
    Can someone please explain the occurrence of a newline in an int, or at least point me in the direction where I can read more about this phenomena?

    thanks in advance.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, JBerg!

    Char's are just small range int's. Newlines are 10 in the internal value a PC would normally use. (In a text file format for Windows, the newline expands to become two char's: a 10 (line feed), and a 13 (carriage return).

    Which is funny, because I've looked all over, and can't find a carriage that needs to be returned, even on old computers - but some of the ASCII values go back to the days of the telegraph and teletype.

    Google up and download an extended ASCII table - as a programmer, you will want to keep it with an icon to it, right on your desktop.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    2
    Thank you for the warm welcome, much appreciated

    I think I'll be able to get on with my small experiment now that I have a good clue on how to unravel the mysteries of the code

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    For a bit of fun, try printing out a newline char - but as an integer:

    Code:
    printf("\n Newlines have the value %d on my system \n", '\n');

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    And try out this simple example as well.

    Note: Ascii value in '\n'
    Hex - A
    Dec - 10
    Oct - 012

    Code:
    #include <stdio.h>
    
    int main()
    {
        printf("Frist Line \xANext line");
        getchar();
        return 0;
    }
    
    /* my output
    Frist Line
    Next line
    */
    As you could see the ASCII Hex value getting converted to the NL line feed. '\x' is used specific the printf the anything following '\x' escape sequence is the ASCII hex value and then interprets as a char.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question bout my work
    By SirTalksAlots in forum C Programming
    Replies: 4
    Last Post: 07-18-2010, 04:23 PM
  2. A question about a question
    By hausburn in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2010, 06:24 AM
  3. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 03:51 PM
  4. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 06:51 PM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM

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