confused about variables

This is a discussion on confused about variables within the C++ Programming forums, part of the General Programming Boards category; I'm new to C++ Programming and I am learning from Sans Teach Yourself C++ in Ten Minutes, but I'm consude ...

  1. #1
    yes, I'm registered!!! algi's Avatar
    Join Date
    Nov 2004
    Location
    Ipswich
    Posts
    161

    Unhappy confused about variables

    I'm new to C++ Programming and I am learning from Sans Teach Yourself C++ in Ten Minutes, but I'm consude about Variables.

    Is Int a Variable or a Function. Also a short variable is two bytes long and a long variable is 4 characters long. Does that mean that a short variable can only hold 2 letters or numbers. Please help I'm confused.

    Thanks in Advance, Algi

  2. #2
    Registered User
    Join Date
    Oct 2004
    Posts
    6
    variables are placeholdes that can (but dont have to be) be changed which are reserved in memory. The difference between the short and long is memory size that is reserved, which is actually bits it can store instead of numbers or letters

  3. #3
    yes, I'm registered!!! algi's Avatar
    Join Date
    Nov 2004
    Location
    Ipswich
    Posts
    161
    I'm still confused. How much can 2 bytes hold

  4. #4
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    1 Byte = 8bits
    2 Bytes = 16bits

    a character such as 'a' is 8 bits (1 byte)
    2 characters = 2 bytes (16 bits)

    an integer (a number) = 4 bits

    so say you have a variable

    char a;

    a can store 1 character byte

    where as if you have

    char a[2];

    a has an array of 2 bytes a[3] has an array of 3 bytes and so on
    Last edited by Rare177; 11-16-2004 at 11:25 AM.
    Good Help Source all round
    Good help for win programmers
    you will probably find something in here
    this thing also helps

    if you have never tried any of the above then maybe you should, they help alot

  5. #5
    yes, I'm registered!!! algi's Avatar
    Join Date
    Nov 2004
    Location
    Ipswich
    Posts
    161
    so a short variable can only say hold,: ab :and not: abc :

    I think I'm getting it thanks.

  6. #6
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    int is a variable that can hold whole numbers only. No decimals. I can't remember it's restrictions, but it's far more than 2 digits.

    int a = 1300086

    double w = 12.56

    valid variable declarations
    Last edited by stillwell; 11-16-2004 at 11:27 AM.

  7. #7
    yes, I'm registered!!! algi's Avatar
    Join Date
    Nov 2004
    Location
    Ipswich
    Posts
    161
    I'm starting to get it now. Thanks everyone for the help!!!

  8. #8
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    i edited my last post, read up
    Good Help Source all round
    Good help for win programmers
    you will probably find something in here
    this thing also helps

    if you have never tried any of the above then maybe you should, they help alot

  9. #9
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    The C++ standard specifies the minimum capacity for each type. A type-char must hold at least one byte, but on a 32-bit system it might be 4-bytes.

    A byte is 8 bits (binary digits). This is computer jargon. There is not type "byte" in C++ ... "byte" is not part of the C++ language! With 8 bits, the maximum count is 11111111 binary, which equals 255 decimal. With two bytes, you can count up to 65535.

    In order to keep your code standard-compliant (and portable) you should not store a number greater than 255 in a type-char... even if your system can handle it!


    A single variable can hold a single number. That number may represent a single ASCII character.

    To store a word or sentence, you need an array of characters. This is called a string variable.

    Character arrays are sometimes called C-style strings. (C++ also has a string class which is used to create string objects.)

    Here are the minimim capacities for each type:
    char -128 to +127 (1 byte)
    unsigned char 0 to 255 (1 byte)

    int -32,768 to +32,767 (2 bytes)
    unsigned int 0 to 65535 (2 bytes)

    long -2,147,483,648 to +2,147,438,647 (4 bytes)
    unsigned long 0 to 7,294,967,295 (4 bytes)

    float +/- 2.1E-38 to +/- 3.4 E38 (4 bytes)
    double +/- 2.2E-308 to +/- 1.8 E308 (8 bytes)
    Last edited by DougDbug; 11-16-2004 at 02:43 PM.

  10. #10
    yes, I'm registered!!! algi's Avatar
    Join Date
    Nov 2004
    Location
    Ipswich
    Posts
    161
    Thanks DougDBug

  11. #11
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Additionally, there are certain relations that must hold between the sizes of built in types.
    Along the lines of: sizeof(short) <= sizeof(int) <= sizeof(long), etc (you get the idea).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  12. #12
    yes, I'm registered!!! algi's Avatar
    Join Date
    Nov 2004
    Location
    Ipswich
    Posts
    161
    Thanks Zack L

  13. #13
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Now that you know what a variable is I'll explain what a function is.
    Imagine a simple math equation
    Code:
    f(x) = x^2+1
    g(x,y) = x/y
    For several values of x and y you have
    Code:
    f(2) = 2^2+1 = 5
    f(10) = 10^2+1 = 101
    g(4,5) = 4/5 = 0.8
    g(0,7) = 0/7 = 0
    For diferent values of x and y you have diferent results. Note that all functions return only ONE value.
    Now using C notation, it the exact same thinking, with a diference: you have to write the type of your input variables like this
    Code:
    f(int x){
    // do something and return x^2+1
    }
    g(int x,int y){
    // do something and return  = x/y
    }
    But you may note there's something missing here, which is the return type of the result. Any function can return a integer, a char, a floating point number, a pointer... whatever. So to point out this, one MUST specify always a return type for a function.
    So
    Code:
    int f(int x){
    // do something and return x^2+1
    }
    float g(int x,int y){
    // do something and return  = x/y
    }
    This way f and g are functions that calculate something using integers and return a single result.
    And to finish what does one need to write to point that the function should return something?
    A return statement:
    Code:
    return expression;
    So
    Code:
    int f(int x){
    // do something and return x^2+1
       return x*x + 2;
    }
    float g(int x,int y){
    // do something and return  = x/y
        return x/y;
    }
    Whenever a return statement is reached your function execution ends and the result of the expression in the return statement is returned.
    But what if y is 0 ?? Division by zero isn't allowed. So using the return statement:
    Code:
    float g(int x,int y){
    // do something and return  = x/y
        if(y == 0)
            return 0;
        else
            return x/(float)y;
    }
    And to finish why "x/(float)y" and not "x/y" ?
    x and y are integers so division between them 2 will result in a integer division or by other words, only the quocient is returned and the remainder is dispised. To get the remainder use % instead.
    e.g.
    Code:
    2/3 = 0		2%3 = 2
    5/4= 1		5%4 = 1
    13/11 = 1	13%11 = 2
    But we want it to return a complete floating point value, including the divided remainder.
    So after casting y ( "(float)y" ) , having therefore a float variable, the division will result in a floating point value.
    Code:
    2/(float)3 = 0.66666...
    5/(float)4= 1.25
    13/(float)11 = 1,1818...
    
    //Or if you declare implicit floats
    2/3.0 = 0.66666...
    5/4.0 = 1.25
    13/11.0 = 1,1818...
    So your g function receives 2 ints and calculates the division between them and returns it in floating point format.

    Also type void is allowed for functions return value. Or in other word use void for function that don't return anything.
    Code:
    void give_grandma_a_cake(){//no parameters
        go_to_grandmas_house();
        give_the_cake();
        run_from_scary_wolf();
    }
    
    int main(){
    /*****
        This expression is invalid because the function
        doesn't return anything
    
        int result =  give_grandma_a_cake();
    *****/
    
        //now this is valid because there's no return value
        //and therefore nothing to assign no any variable
        give_grandma_a_cake();
    
        return 0;//return 0 to the operation system ;)
    }
    The contrary is also valid. One doesn't need to send input variables to get a different result every time. A example of that is the rand() function which returns a different random value each time it is called.
    Code:
    int r1 = rand();
    int r2 = rand();
    int r3 = rand();
    r1, r2, and r3 have different values... or may have, but that's another story.
    Last edited by xErath; 11-16-2004 at 04:54 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best way to avoid using global variables
    By Canadian0469 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2008, 12:02 PM
  2. Father and Son Variables
    By khdani in forum Linux Programming
    Replies: 3
    Last Post: 11-28-2008, 06:42 PM
  3. Replies: 15
    Last Post: 09-30-2008, 03:12 AM
  4. confused about system stack
    By George2 in forum C Programming
    Replies: 4
    Last Post: 06-05-2006, 09:14 AM
  5. Replies: 6
    Last Post: 01-02-2004, 01:01 PM

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