strings and numbers

This is a discussion on strings and numbers within the C++ Programming forums, part of the General Programming Boards category; how come when you output numbers you don't need to add quotation marks around them and strings you do? for ...

  1. #1
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256

    strings and numbers

    how come when you output numbers you don't need to add quotation marks around them and strings you do? for example:

    cout << 2003; would be alright but

    cout << coolness; would signal an error

    what's the difference between a number and a word?

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    when you want to cout<< a word, the compiler assumes it is somekind of a variable, but because it does not see it defined/initialized anywhere in the code it returns an error.

    The reason that you could cout an integer is because no variables can start with a digit. You cannot have int 123 = 0;

    I'm pretty sure that there are some other reasons for this accurence, but this is the best explanation that i could think of.

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    That's just how compilers work man. Think about it. What if there wasn't that rule:

    char * s = hello world! This is 10 % of my program - woohoo!;

    The compiler would have quite a chore figuring out whether it was looking at a syntax error or a sentence.

    But moreover, a string literal is a human friendly representation of an array of numbers:

    Code:
    int array[] = { 72, 73, 0 };
    char buff[] = {72, 73, 0};
    char str[] = "HI";
    All three arrays contain the exact same values. The last one is just easier for humans to type.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  4. #4
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    If only the US government thought things through as well as the ISO people.
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    To expand on Sebastiani's point...

    A single variable can hold only a single number. To store a string, you need an array (or "string") of numbers. And, to make things even more interesting, the ASCII code for the character 1, is not 1, it's 49.


    '1' = 49 ASCII code for the character 1
    "1" = [49, 0] An array... ASCII "1" with null-termination.

    So, although cout << 123; and cout << "123"; will produce the same results, 123 and "123" are not the same... the first is a single number, the second is an array of four numbers (including the null termination.)

    The use of quotation marks to differentiate between variables and literals is common to many (most?) computer languages. I have only seen the use of 'single quotes' in C and C++. But, I haven't studied that many languages.

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    (Using the above posts for background information)

    The operator << for std::cout is overloaded to take many variable types. When you go std::cout << (int), it will interpret the variable as an int and so it will convert it to a string of characters and then output THAT to the screen.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 12-21-2007, 12:38 PM
  2. strings and numbers
    By Granger9 in forum C Programming
    Replies: 3
    Last Post: 09-10-2002, 04:11 PM
  3. convert numbers to strings
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 07-08-2002, 03:43 PM
  4. Replies: 13
    Last Post: 01-22-2002, 03:38 AM
  5. File Access with Strings and Numbers
    By Mace in forum C Programming
    Replies: 3
    Last Post: 12-01-2001, 03:07 AM

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