저 세상밖으로...

변수 본문

프로그래밍/C언어

변수

열혈매미 2016. 5. 6. 02:03

:: C의 변수 형태 ::


- 32비트 기준 - 

문자형char
1 Bytes-128~127
정수형short2 Bytes-32,768~32,767
int4 Bytes-2,147,483,648
~ 2,147,438,647
long4 Bytes-2,147,483,648 
~2.147.483.647
부호없는 문자형unsigned char1 Bytes0~255
부호없는 정수형unsigned short2 Bytes0~65,535
unsigned int4 Bytes0~4,294,967,295
unsigned long4 Bytes0~4,294,967,295
부동 소수형float4 Bytes1.2E-38~3.4E38
double8 Bytes2.2E-308~1.8E308
void형void0 Bytes값 없음




여러가지 형태의 변수 크기를 출력하는 소스

________________________________________


/* 바이트 단위로 C 형 변수의 크기를 구하는 소스 */


#include <stdio.h>


main()

{

printf("\n A char   is %d bytes", sizeof( char ));

printf("\n An int is %d bytes", sizeof( int ));

printf("\n A short is %d bytes", sizeof( short ));

printf("\n A long is %d bytes", sizeof( long ));

printf("\n An unsigned char is %d bytes", sizeof( unsigned char ));

printf("\n An unsigned int is %d bytes", sizeof( unsigned int ));

printf("\n An unsigned short is %d bytes", sizeof( unsigned short ));

printf("\n An unsigned long is %d bytes", sizeof( unsigned long ));

printf("\n A float is %d bytes", sizeof( float ));

printf("\n A double is %d bytes", sizeof( double ));

return 0;

}

__________________________________________


결과는...



A char   is 1 bytes

An int is 4 bytes

A short is 2 bytes

A long is 4 bytes

An unsigned char is 1 bytes

An unsigned int is 4 bytes

An unsigned short is 2 bytes

An unsigned long is 4 bytes

A float is 4 bytes

A double is 8 bytes



int가 4바이트가 잡힌다. 16비트 컴퓨터와는 다른듯.(무려 16비트 시절의 내용!!)



:: 상수를 선언하는 두 가지 방법 ::


#define PI 3.14159


const int count = 100;


#define 지시어로 생성되는 기호 상수와 const 키워드를 통해서 정의되는 기호 상수는 '포인터와 변수 범위'와 관련하여서 차이가 생긴다.


[참고 문헌]


Teach yourself C

http://newmkka.tistory.com/69


* 자료형 크기 관련

http://foxlime.tistory.com/115

'프로그래밍 > C언어' 카테고리의 다른 글

함수(function)  (0) 2016.05.07
문장(Statement), 수식(expression), 연산자(operator)  (0) 2016.05.06
Comments