#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    142
    Rep Power
    2

    Type casting in C


    I have two questions.

    1) Is the new data type only valid in the expression in which the type of the variable is cast, or is it still in effect during later lines in the code?

    To give a specific example,

    Code:
        int student, score;
        double average;
    
        printf("How many students took the test?> ");
        scanf("%d", &student);
    
        printf("What was the total score?> ");
        scanf("%d", &score);
    
        average = (double)score / (double)student;
    
        printf("%d students took the test, and the total score was %d points. the average is %f.", student, score, average);
    The type of 'score' and 'student' is changed into 'double' in
    Code:
    average = (double)score / (double)student;
    .
    But in the next line, I treated the two variables as an 'int' variable, which seemed to work.
    Does this mean that after the assignment statement, the types of 'student' and 'score' return to 'int'?

    2) What is the difference between the following two codes, given that
    Code:
    double x;
    int round_x;
    ?

    Code:
    round_x = (int)(x + 0.5)
    and
    Code:
    round_x = x + 0.5
    ?
    Since the variable 'round_x' is type 'int', it will only give the integer part of the expression 'x + 0.5', whether I type cast the expression or not.
    So type casting in this case seems useless for me, but this was the piece of code provided for rounding numbers in my textbook.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    101
    Rep Power
    0
    1. No. It's only valid in the typecast expression. For prove you can use the sizeof operator.

    Code:
    #include<stdio.h>
    
    int main()
    {
    	int score = 5,student = 3;
    	double average = (double)score /(double)student;
    	printf("%d is the average\n",average);
    	printf("An int is a size of 4, score = %d size and student = %d size",sizeof(score),sizeof(student));
    	return 0;
    }
    2. The difference is is that one's casting and the other isn't.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Originally Posted by 046
    2) What is the difference between the following two codes, given that
    Code:
    double x;
    int round_x;
    ?

    Code:
    round_x = (int)(x + 0.5)
    and
    Code:
    round_x = x + 0.5
    ?
    Since the variable 'round_x' is type 'int', it will only give the integer part of the expression 'x + 0.5', whether I type cast the expression or not.
    So type casting in this case seems useless for me, but this was the piece of code provided for rounding numbers in my textbook.
    In both examples, the result of x+0.5 is a double, but then you truncate it to an int before assigning it to an int variable. The difference is that in the second example, this is done automatically and the compiler should issue a warning about possible loss of data. In the second example, the warning is issued because the compiler thinks you might not know about the possible mistake that you are making, whereas in the first example you are using the typecast to tell the compiler that, yes, you do indeed intend to truncate that double value.

    That typecast is extremely important and must not be left out. It keeps you honest and clearly informs you and the compiler of your intentions. Even more importantly, it clearly communicates your intentions to the other programmers who will come after you to maintain or reuse your code. Otherwise, those other programmers will spot your "mistake" and try to correct it and in the process cause it to no longer work as you had intended.

    Do not leave out typecasts. And do not ignore warnings.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    142
    Rep Power
    2
    Thanks for the answer.

    2. The difference is is that one's casting and the other isn't.
    I do see that, but I wanted to ask whether there is a difference in the results.
    Would I get different answers?


    Also, another question: what is the associativity of type casting?
    If you had the expression
    Code:
    (double) a / b
    where a and b are both 'int' functions and there values were 3 and 4 respectively,
    would (1) only 'a' be of type 'double' yielding the value 0.75 (because the operator works on 'double' and 'int' variables, not two 'int' variables), or (2) 'a / b' as a whole considered an expression of type 'double', with its value 1.00 (because the evaluation is done before the type is cast)?
  8. #5
  9. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802
    The types of the cast variables are not changed at all. A cast is an expression like any other - it is the result of the expression that has the cast type.
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Originally Posted by 046
    ... , but I wanted to ask whether there is a difference in the results.
    The difference is that
    Code:
    round_x = x + 0.5;
    is a mistake, which the compiler will warn you about, whereas
    Code:
    round_x = (int)(x + 0.5);
    is intentional and the compiler will be quite alright about it.

    It is an important difference, as already discussed.

    Always turn warnings on and up. Never ignore warnings.

    Originally Posted by 046
    Also, another question: what is the associativity of type casting?
    RTFM! C++ Operator Precedence. It's the same order for C; C++ just has a few more operators.

    Originally Posted by 046
    If you had the expression
    Code:
    (double) a / b
    where a and b are both 'int' functions and there values were 3 and 4 respectively, ... or (2) 'a / b' as a whole considered an expression of type 'double', with its value 1.00 (because the evaluation is done before the type is cast)?
    No, in Case (2) you would get 0.0, since integer-divide 3/4 is zero. But then to actually get that you would need to write
    Code:
    (double) (a / b)
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    142
    Rep Power
    2
    It's the same order for C; C++ just has a few more operators.
    Sorry, I got a bit lazy there.

    Thanks for the answers, I think I've cleared all my questions on type casting for now.

IMN logo majestic logo threadwatch logo seochat tools logo