Thread: Use of variable

  1. #1
    Registered User alice's Avatar
    Join Date
    Mar 2004
    Posts
    36

    Use of variable

    can someone help me to verify the following variable is come from which line (in the below program):

    ie. paramB used on line 39 is defined on line 35.

    -The variable variableA on line 10 =34
    -The variable variableB on line 10 =34
    -The variable variableB on line 17 =34
    -The variable variableB on line 19 =34
    -The variable variableB on line 28 =27
    -The variable variableB on line 41 =19
    -The variable sum on line 28 =24
    -The variable variableA on line 38 =38

    Code:
    01   #include <stdio.h>
    02   
    03   int variableA;
    04   float variableB;
    05   
    06   float functionC(int variableB);
    07   
    08   void functionA(int variableA, float paramA) {
    09     variableA = variableA + 1;
    10     variableB = variableA;
    11     paramA = functionC(variableA);
    12   }
    13   
    14   int functionB(int paramA, double paramB) {
    15     int variableB;
    16   
    17     variableB = paramB + 1;
    18     functionA(variableB, 1.2);
    19     return paramA + paramB + variableB;
    20   }
    21   
    22   float functionC(int variableB) {
    23     int index;
    24     int sum = 0;
    25   
    26     for (index = 1; index < variableA; index++) {
    27       int variableB = 4;
    28       sum += variableB;
    29     }
    30     return sum * 1.2;
    31   }
    32   
    33   int main() {
    34     float paramA = 3.5;
    35     int paramB = 2;
    36     int result;
    37   
    38     variableA = 0;
    39     result = functionB(paramB, paramA);
    40   
    41     variableB = functionC(result);
    42   }
    Last edited by alice; 06-05-2004 at 12:47 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    can someone help me to verify the following variable is come from
    Would you care to try that again, in English this time? And why the hell are you passing variables to you function when you have global variables called the exact same thing? Oh, while we're at it, main returns an int. Always.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > can someone help me to verify the following variable is come from:
    You tell us what you think, then we'll correct any mistakes you make
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User alice's Avatar
    Join Date
    Mar 2004
    Posts
    36
    sorry that I used void main ().
    I will correct it in the future.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you really want to know what each variable is, print it out.
    Code:
    printf("On line %d, variableA is %d\n", __LINE__, variableA );
    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User alice's Avatar
    Join Date
    Mar 2004
    Posts
    36
    what is __LINE__ mean in the printf statement?
    the number 01,02,03.... start at left side indicted the line number.
    it is remove while compiling in C.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    __LINE__ is a preprocessor macro which turns into whatever line number it's on. Like so:
    Code:
    #include <stdio.h>
    int main( void )
    {
        printf("line: %d\n", __LINE__ );
        printf("\n");
        printf("line: %d\n", __LINE__ );
        printf("line: %d\n", __LINE__ );
        printf("line: %d\n", __LINE__ );
        printf("\n");
        printf("\n");
        printf("line: %d\n", __LINE__ );
        return 0;
    }
    So, when used as I've illustrated, you can use it to display the line number in your program. Combine with whatever variable you're trying to display, you can end up seeing what the value of that variable is when you're on a given line number in your program. Not the greatest debugger, but it'll work in a pinch.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User alice's Avatar
    Join Date
    Mar 2004
    Posts
    36
    I only want to know, ie,

    for the statement sum+=variableB;,

    the variable sum is defined at the

    statement int sum=0;,

    is it correct?

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Yes you are correct.

    It's just an exercise to allow you to figure out scope, by creating globals, parameters and local variables with the same name.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about printing a variable???
    By Hoser83 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2006, 01:57 PM
  2. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  3. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  4. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM
  5. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 04:11 AM