컴퓨터 과학 & 영상처리 관련/C / C++

int와 float의 연산(정수와 소수)

꺄뜨르 2012. 9. 4. 15:37

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int i = 10;
    float f = 5.5;
    double d = 6.6;
    char c = 'a';
    short s = 20;
    long l = 30;
   
    printf("int - float, %d - %g = %g\n",i,f,i-f);
    printf("int + double, %d + %g = %g\n",i,f,i+f);   
    printf("int * double, %d * %g = %g\n",i,f,i*f);   
    printf("int / double, %d / %g = %g\n\n",i,f,i/f);   
   
    printf("int - double, %d - %g = %g\n",i,d,i-d);
    printf("int + double, %d + %g = %g\n",i,d,i+d);
    printf("int * double, %d * %g = %g\n",i,d,i*d);
    printf("int / double, %d / %g = %g\n",i,d,i/d);

    return 0;
}

 

 

결과

int - float, 10 - 5.5 = 4.5
int + double, 10 + 5.5 = 15.5
int * double, 10 * 5.5 = 55
int / double, 10 / 5.5 = 1.81818

 

int - double, 10 - 6.6 = 3.4
int + double, 10 + 6.6 = 16.6
int * double, 10 * 6.6 = 66
int / double, 10 / 6.6 = 1.51515

 

정수와 소수의 연산의 결과는 소수...이걸 만약에 쓸때 그대로 쓸지

(int) 같이 형변환해서 쓸지..잘 해라