데이터 타입

✒️ 2025-05-15 15:49 내용 수정

수제비 2024 정보처리기사 필기 5판 1권의 내용을 정리
TCPSchool의 내용을 정리


데이터를 메모리에 저장하는 방식과 처리되는 방식을 명시적으로 알려주는 역할


정수형

타입 메모리 크기 표현 범위
short 2 byte -32,768 ~ 32,768
unsigned short 2 byte 0 ~ 65,535
int 4 byte -2,147,483,648 ~ 2,147,483,648
unsigned int 4 byte 0 ~ 4,294,967,296
long 4 byte -2,147,483,648 ~ 2,147,483,648
unsigned long 4 byte 0 ~ 4,294,967,296
long long 8 byte -9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807
unsigned long long 8 byte 0 ~ 18,446,744,073,709,551,615
#include <stdio.h>

int main(void) 
{
    short s1 = 10;
    int i1 = 500000;
    long l1 = 10203949;
    long long ll1 = 9223372036854775805;

    printf("short %d\n", s1);
    printf("int %d\n", i1);
    printf("long %i\n", l1);
    printf("long long %ld\n", ll1);

    return 0;
}
short 10
int 500000
long 10203949
long long 9223372036854775805 

실수형

타입 메모리 크기 표현 범위
float 4 byte 3.4E +/- 38(7자리 숫자)
double 8 byte 1.7E +/- 308(15자리 숫자)
long double 8 byte double과 동일
#include <stdio.h>

int main(void) 
{
    float f1 = 3.14159265;
    double b1 = 1.68E308;

    printf("float %f\n", f1);
    printf("double %f\n", f1);

    return 0;
}
float 3.141593
double 168000000000000006634499432629324058099321591546296460407724972322402843086364687631742933169384835234322016627457252440649344102750463933925406947597510143106548712248522894439985397777914402834899017369628371218125244198943165482409138301211757462979529714163903795393980405362403727040258196554748398665728.000000

문자형

타입 메모리 크기 표현 범위
char 1 byte 2^(-7) ~ 2^7
unsigned char 2 byte 0 ~ 2^(-8)
#include <stdio.h>

int main(void) 
{
    char c1 = 'a';
    
    printf("char %c\n", c1);
    printf("char %d\n", c1);

    return 0;
}
char a
char 97

타입 변환

하나의 타입을 다른 타입으로 바꾸는 행위

1. 자동 타입 변환(묵시적 타입 변환)

#include <stdio.h>

int main(void)
{
    char c = 907;
    int num = 3.14;
    double test = 5;

    printf("char c 저장 값 : %d\n", c);
    printf("int num 저장 값 : %d\n", num);
    printf("double test 저장 값 : %f\n", test);
    
    return 0;
}
char c 저장 값 : -117
int num 저장 값 : 3       
double test 저장 값 : 5.000000

2. 강제 타입 변환(명시적 타입 변환)

#include <stdio.h>

int main(void)
{
    int a = 3;
    int b = 5;
    double da = 3 / 5; // 산술 결과는 int
    double db = (double) 3 / 5; // 산술 결과를 double로 변환

    printf("%d / %d : %f\n", a, b, da);
    printf("(double) %d / %d : %f\n", a, b, db);

    return 0;
}
3 / 5 : 0.000000
(double) 3 / 5 : 0.600000