λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
Programming/C language

4 Constant

by Dowon Kang 2024. 1. 23.

C μ–Έμ–΄μ—μ„œ μƒμˆ˜(Constant)λŠ” ν”„λ‘œκ·Έλž¨μ—μ„œ λ³€ν•˜μ§€ μ•ŠλŠ” κ³ μ •λœ 값을 λ‚˜νƒ€λƒ…λ‹ˆλ‹€. μƒμˆ˜λŠ” 주둜 ν”„λ‘œκ·Έλž¨μ—μ„œ μ‚¬μš©λ˜λŠ” κ°’ 쀑에 λ³€ν•˜μ§€ μ•Šμ•„μ•Ό ν•˜λŠ” κ²½μš°μ— μ‚¬μš©λ©λ‹ˆλ‹€. C μ–Έμ–΄μ—μ„œλŠ” μƒμˆ˜λ₯Ό λ‹€μŒ 두 가지 ν˜•νƒœλ‘œ λ‚˜νƒ€λ‚Ό 수 μžˆμŠ΅λ‹ˆλ‹€.

 

1) Const 

const ν‚€μ›Œλ“œλŠ” C μ–Έμ–΄μ—μ„œ μ‚¬μš©λ˜λŠ” ν•œ κ°€μ§€ λ°©λ²•μœΌλ‘œ, λ³€μˆ˜λ₯Ό μƒμˆ˜λ‘œ μ„ μ–Έν•˜λŠ” λ°μ— μ‚¬μš©λ©λ‹ˆλ‹€. constλ₯Ό λ³€μˆ˜ μ„ μ–Έ μ•žμ— λΆ™μ΄λ©΄, ν•΄λ‹Ή λ³€μˆ˜λŠ” κ°’을 λ³€κ²½ν•  μˆ˜ μ—†λŠ” μƒμˆ˜κ°€ λ©λ‹ˆλ‹€.

 

 

 

2) 기호 μƒμˆ˜(Symbolic Constant λ˜λŠ” λ§€ν¬λ‘œ μƒμˆ˜)

μ½”λ“œ λ‚΄μ—μ„œ 읽기 쉽고 μœ μ§€λ³΄μˆ˜κ°€ μš©μ΄ν•˜λ„λ‘ 이름을 λΆ€μ—¬ν•œ μƒμˆ˜μž…λ‹ˆλ‹€. #define μ „μ²˜λ¦¬κΈ°λ₯Ό μ‚¬μš©ν•˜μ—¬ μ •μ˜ν•˜λ©°, 일반적으둜 λŒ€λ¬Έμžλ‘œ μž‘μ„±λ©λ‹ˆλ‹€. 

#define μƒμˆ˜λŠ” λͺ¨λ“  값듀을 λ°”κΎΌ λ‹€μŒμ— 컴파일 되고 μ‚¬λΌμ§‘λ‹ˆλ‹€

일반적으둜 constλ₯Ό μ‚¬μš©ν•˜λŠ” κ²ƒμ΄ νƒ€μž… μ•ˆμ „μ„±μ΄λ‚˜ μ½”λ“œ κ°€λ…μ„± λ“±μ˜ μΈ‘λ©΄μ—μ„œ λ” κΆŒμž₯되며, #define은 μ£Όλ‘œ λ§€ν¬λ‘œλ‚˜ μ‘°κ±΄λΆ€ μ»΄νŒŒμΌ λ“± νŠΉλ³„ν•œ μƒν™©μ—μ„œ ν™œμš©λ©λ‹ˆλ‹€.

 

 

3) λ¦¬ν„°λŸ΄ μƒμˆ˜(Literal Constant)

μ½”λ“œμ—μ„œ 직접 λ‚˜νƒ€λ‚΄λŠ” μƒμˆ˜μž…λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄, λ‹€μŒμ€ μ •μˆ˜μ™€ 뢀동 μ†Œμˆ˜μ  μƒμˆ˜μ˜ μ˜ˆμž…λ‹ˆλ‹€.

  • μ •μˆ˜ λ¦¬ν„°λŸ΄: 10μ§„μˆ˜, 8μ§„μˆ˜, 16μ§„μˆ˜ λ“±μ˜ ν˜•νƒœλ‘œ ν‘œν˜„λ©λ‹ˆλ‹€. μ˜ˆλ₯Ό λ“€λ©΄ 10, 075, 0xA λ“±μ΄ μžˆμŠ΅λ‹ˆλ‹€.
  • 뢀동 μ†Œμˆ˜μ  λ¦¬ν„°λŸ΄: μ‹€μˆ˜λ₯Ό λ‚˜νƒ€λƒ…λ‹ˆλ‹€. μ˜ˆλ₯Ό λ“€λ©΄ 3.14, 2.0e5 λ“±μ΄ μžˆμŠ΅λ‹ˆλ‹€.
  • 문자 μƒμˆ˜: 큰&μž‘μ€ λ”°μ˜΄ν‘œλ‘œ 감싸진 문자λ₯Ό λ‚˜νƒ€λƒ…λ‹ˆλ‹€. 예λ₯Ό λ“€λ©΄ "A"(λ¬Έμžμ—΄), 'b'(문자) 등이 μžˆμŠ΅λ‹ˆλ‹€.

 

 


A constant is a value that cannot be altered or modified during the execution of a program. Constants in C can be classified into various types, including literal constants and symbolic constants.

 

Literal constants are values directly written in the code, such as integers, floating-point numbers, and characters.

 

Symbolic constants are variables declared with the const keyword, indicating that their values cannot be changed once assigned.

 

#define is also used to define macros, which are small code snippets that are expanded inline wherever they are used in the code. Macros can be simple or complex, and they are often used to create reusable pieces of code. 

 

While both const and #define can be used to create constants, they have some differences. const is a variable with a memory location, and its use is type-safe, whereas #define performs direct text substitution, which can lead to issues if not used carefully. In modern C programming, const is often preferred over #define for creating constants due to its type safety and better integration with the language's scoping rules. Macros defined using #define are still widely used for other purposes, such as code abstraction and conditional compilation.

 

 

β€» GDB(GNU Debugger)λŠ” GNU ν”„λ‘œμ νŠΈμ˜ μΌλΆ€λ‘œ 개발된 디버깅 λ„κ΅¬λ‘œ, ν”„λ‘œκ·Έλž¨μ˜ μ‹€ν–‰ 쀑에 였λ₯˜λ₯Ό μ°Ύκ³  μˆ˜μ •ν•˜λŠ” 데 μ‚¬μš©λ©λ‹ˆλ‹€. C, C++, Ada, Objective-C λ“±μ˜ ν”„λ‘œκ·Έλž˜λ° μ–Έμ–΄λ‘œ μž‘μ„±λœ ν”„λ‘œκ·Έλž¨μ„ λ””λ²„κΉ…ν•˜λŠ” 데에 주둜 μ‚¬μš©λ©λ‹ˆλ‹€.
GDBλ₯Ό ꡳ이 μ‚¬μš©ν•˜λŠ” μ΄μœ λŠ” μž„λ² λ””λ“œ λ¦¬λˆ…μŠ€ ν™˜κ²½μ— 크둜슀 gdb만 μ§€μ›λ˜λŠ” κ²½μš°κ°€ 있기 λ•Œλ¬Έμž…λ‹ˆλ‹€. 

 

 

'Programming > C language' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

6 scanf ν•¨μˆ˜ (feat. format specifiers)  (0) 2024.01.23
5 Operator  (1) 2024.01.23
3 Variable  (0) 2024.01.22
2 Hello world  (0) 2024.01.22
1 C language - Why do we learn it  (0) 2024.01.22

λŒ“κΈ€