본문 바로가기
학습/IT

[IT] C언어 입문(3) Data types, 데이터 타입(자료형)

by 개성공장 2021. 8. 20.
반응형

 data types

 * Suffixes(접미사)  for Data Type : int

 - int long, unsigned long

 - U or u : unsigned,   ex) 25U, 0x32u

 - L or l : long,   ex) 37L, 0x27L

 - UL or ul : unsigned long, 56uL, 234UL

 

 * Floating types

 - Variables of this type hold real values

 - float : 4bytes, 6 decimal places

 - double : 8bytes, 15 decimal places

 - long double : 12bytes, the compiler may provide more storage than double

 

 * Suffixes for Floating types

 - Any unsuffixed floating constant is of type 'double'

 - ex) 3.1415, 314.159e-2F, 0e0 (0^0 = 0), 1. (그냥 1은 int로 인식되나 1.은 double로 인식), 314.519e-22 (314.519 * 10의 -22승)

 - F of f : float,   ex) 3.7F

 - L or l : long double,   ex) 2.7L

 

 * Type Qualifier : const

 - Type qualifiers restrict or qualify the way an identifier of a given type can be used

 - const로 선언되는 변수는 상수처럼 취급, pi라는 변수를 변경할 수 없음

 - 일반적으로 storage class와 type 사이에 들어감

 - static 'const' int x = 5;

 - x is a constant int with static storage class

 - x can be initialized, but thereafter x cannot be modified

 

 - examples

const float pi = 3.14;
float area;
int r = 10;
area = pi * r * r;
pi = 10.5; /* compile error! */

 

 * typedef

 - Allows the programmer explicitly associate a type with an identifier

 - user defined type을 사용할 때, 나중에 구조체 등 학습시에 자주 사용

 - typedef char           sval8b;

 - typedef int              sval4B, sval32b;

 - typedef unsigned     long size_t;

 - sval18b   v;

 - sval4B   x, y;

 

 * sizeof Operator

 - sizeof { object }

 - Unary operator

 - To find the number of bytes needed to store object

 

 - example : sizeof(int), sizeof(double)

int i;
printf("%d\n", sizeof(i));  /*  결과 : 4  */

 

 * Type Conversion

 - An arithmetic expression (such as x+y) has both a value and a type

 - Integral promotion

 - (signed or unsigned) char or (signed or unsigned) short can be used in any place where an int or unsigned int may be used

 - unsigned가 하나만 있으면 해당 type은 unsigned로 취급, 더 넓은 범위의 type으로 취급

 - 일반 숫자는 int로 취급

 - example

 - int i; float f;   => i+f에서 i가 float type으로 바뀜

 - double d; int i;   => d+i 에서 i가 double type으로 바뀜

 

 * Type Casts

 - There are explicit type conversion called casts

 - 지정하고자 하는 type을 () 안에 표현

 - example

 - (double) 1

 - (long) ('A' + 1.0)

 - (double) (x=22)

 - (float) i+ 3 , ((float) i) + 3

 - d = (double) i / 5

 

 * Character Constants

 - Enclosed in a pair of single-quote marks

 - 'a'

 - '\n'

 - escape character를 표현하기 위해서는 '' 필요

 

 * String Constants

 - A sequence of characters enclosed in a pair of double-quote marks

 - "abc"

 - "abc\"def"   =>   abc"def

 - "abc\\def"   =>  abc\def

 

 * printf

 - The programmer need to declare the function that is used in his/her program

 - Function prototypes : A way of declaring functions

 - Printf's prototype is included in stdio.h

 - Printf is passed a list arguments, control_string, and other_arguments

 

 - example

printf("abcde");
printf("%s", "abcde");
printf("%c%c%c%c%c", 'a', 'b', 'c', 'd', 'e');   => abcde
printf("%c%4c%4c", 'a', 'b', 'c');   => a   b   c
% man 3 printf  (=> in UNIX계열 PC, to get a manual page)

 - control_string : contain conversion specification or formats

c : character

d : decimal integer

e : Floating-point number in scientific notation

f : Floating-point number

g : in the e-format or f-format, whichever is shorter

s : string

 

 * Scanf

 - Analogous to printf, but used for input

 

 

실습

---

#include <stdio.h>

int main(void)
{
	int i = 1;
	int j = 2;
	float f = 10.5;

	printf("%d\n", i);   // 1
	printf("%d\n", j);   // 2
	printf("%f\n", f);   // 10.50000
}

---

#include <stdio.h>

int main(void)
{
	float i = 100000.1
	double d = 100000.1

	printf("%f\n", f);   // 100000.101562
	printf("%lf\n", d);  // 100000.100000
}

=> floating type은 숫자가 커지는 경우 오차가 생길 수 있음

---

※ C언어 입문 시리즈
1. Introduction - C언어의 역사와 기본 개념
2. Variables - 변수, 대입연산자, 구문규칙, 데이터타입 등
3. Data types, 데이터 타입(자료형)
4. Operators, 연산자 - scanf, 산술연산자, 관계연산자, 증감연산자, 대입연산자, 동등연산자 등
5. Operators, 연산자 - 논리연산자, 단축평가, 대입연산자, if문 및 while문 활용
6. Control flow, 제어흐름 - While문, For문, If문, do-while문 등 루프문
7. Function, 함수 - Goto문, getchar와 putchar, 함수 정의와 프로토타입 선언
8. Scope rules/recursion, 변수의 영역규칙과 재귀호출, 난수생성 예시
9. Array와 Pointers, 배열과 포인터
10. Pointer, 역참조, swap 함수 활용, 배열과 포인터 비교
11. File operation 파일연산, String, 다차원 배열 예시
12. structure, union, enumerated types - 구조체, 공용체, 열거체
13. 자료구조(data structure) 예시 - 연결리스트(linked list)
14. C 전처리기(C preprocessor), 함수 포인터(function pointer)
반응형

댓글