본문 바로가기
학습/IT

[IT] C언어 입문(2) Variables - 변수, 대입연산자, 구문규칙, 데이터타입 등

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

□ Variables and data type

 

 * Computing

 - By human

 : Needs a pencil, a piece of paper

Following a sequence of computing steps(procedural)

Recoding the intermediate results on the paper(imperative)

 - C is an imperative and procedural language

 : Specifies explicit manipulation of the state of the computer system (imperative)

 Specifies an explicit sequence of steps to perform (procedural)

 

 * Variables and Assignments

 - Variable : A named memory location in which a program can store intermediate results and from which it can read them

 - Assignment : Stores the value of an expression in a variable

 

 * Characters in C programs

 - A C program is constructed by the programmer as a sequence of characters

 - Characters are collected by the compiler into syntactic units (tokens)

 - Lowercase letters, Uppercase letters, digits

 - Other characters : {} () \ ' " : ; / ? ...

 - White space characters : blank, new line, tab etc...

 

 * Syntax Rules

 - The syntax of C will be described using Backus-Naur Form (used in ALGOL 60)

 - :: : To be rewritten as

 - | : Separating choices

 - {} : grouping

 - {}+ : Repeat one or more times

 - {}* : Repeat zero or more times

 - ()opt : optional

 

 * Syntax rules 예제

 - letter_or_digit ::= letter | digit

 - letter ::= lowercase_letter | uppercase_letter

 - lowercase_letter ::= a | b | ... | z

 - uppercase_letter ::= A | B | ... | Z

 - digit ::= 0 | 1 | 2 | ... | 9

 - alphanumeric_string ::= letter_or_digit*

 - conditional_statement ::= if (expression) statement { else statement }opt

 

 * Keywords

 - Have a strict meaning as individual tokens

 - Reserved (cannot be redefined or used in other contexts)

 - 피해야할 단어 : auto, break, case, char, const, continue, default, do, goto, else, if, int .... 등

 

 * Identifiers, 식별자

 - Syntax : indentifier ::= { letter | _ } { letter | _ | digit}*

 - 식별자는 프로그램 내에서 고유한 이름으로 주어져야 함

 - (underscore)를 이용하여 이름을 구분하여 줌

 - 예시 : _id_1, programming_made_study

 - 잘못된 예시 : prog-language ('-' 사용불가), My name, 2010hello, auto ...

 - '=' : assignment operator

 

 * declarations, 선언

 - declaration ::= type identifier { , identifier }*;   => int i;

 - 모든 변수의 타입은 사용 전에 선언됨 : type information needed for storage allocation by the computer

 

 * Data Types

 - Primitive date types

 - Character tpye : char

 - Integral types : short, int, long

 - Floating types : float, double, long double

 

 - Derived types : Array, Pointer

 - User-defined types : Structure, Union, Enumeration types

 

 * Data Type : char

 - A variable of type 'char' can be used to hold small integer values (1byte, 256 distinct values)

 - Most machines use either ASCII or EBCDIC character code

 - Either char = signed char  /  char = unsigned char

 - signed char : -128 ~ 127

 - unsigned char : 0 ~ 255

 

 * Data Type : int

 - The integer values that are representable on a machine

 - int is stored in 4bytes

 - if a is a variable of type int, the range of values that a can take on is ( 2^-31 =<  int  =< 2^31 -1 )

 - integer overflow can occur, but the program continues to run

 

 * short, long, and unsigned (int)

 - short : 2bytes integer, ( -2^15 =<  int  =< 2^15 -1 )

 - long : 4bytes integer, The compiler may provide more storage than int

 - unsigned : 4bytes unsigned integer ( 0 =<  int  =< 2^32 -1 )

 - short, int, long은 컴파일러 등에 따라 몇 바이트가 될 지는 달라질 수 있음

 - 64bit 운영체제에 대응하여 8바이트 변수 타입 등이 추가되기도 함

 

 

※ 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)
반응형

댓글