logo

C의 데이터 유형

데이터 유형은 정수, 부동, 문자 등과 같이 변수가 저장할 수 있는 데이터 유형을 지정합니다.

C 데이터 유형

C 언어에는 다음과 같은 데이터 유형이 있습니다.

유형데이터 유형
기본 데이터 유형int, char, float, double
파생 데이터 유형배열, 포인터, 구조, 공용체
열거형 데이터 유형열거형
무효 데이터 유형무효의

기본 데이터 유형

기본 데이터 유형은 정수 기반과 부동 소수점 기반입니다. C 언어는 부호 있는 리터럴과 부호 없는 리터럴을 모두 지원합니다.

기본 데이터 타입의 메모리 크기는 32비트 또는 64비트 운영체제에 따라 달라질 수 있습니다.

100.00개 중 15개

기본 데이터 유형을 살펴보겠습니다. 그 크기가 주어져 있다 32비트 아키텍처에 따르면 .

데이터 유형메모리 크기범위
1바이트−128 ~ 127
서명된 문자1바이트−128 ~ 127
서명되지 않은 문자1바이트0~255
짧은 2바이트−32,768 ~ 32,767
짧게 서명됨2바이트−32,768 ~ 32,767
부호 없는 짧은2바이트0~65,535
정수 2바이트−32,768 ~ 32,767
서명된 정수2바이트−32,768 ~ 32,767
부호 없는 정수2바이트0~65,535
짧은 정수 2바이트−32,768 ~ 32,767
서명된 짧은 정수2바이트−32,768 ~ 32,767
부호 없는 짧은 정수2바이트0~65,535
긴 정수 4바이트-2,147,483,648 ~ 2,147,483,647
긴 정수로 서명됨4바이트-2,147,483,648 ~ 2,147,483,647
부호 없는 긴 정수4바이트0~4,294,967,295
뜨다 4바이트
더블 8바이트
긴 더블 10바이트

정수:

정수 분수나 소수 부분이 없는 정수입니다. int 데이터 유형 그들을 나타내는 데 사용됩니다.

다음을 포함하는 변수에 자주 적용됩니다. 가치 , 와 같은 개수, 인덱스 , 또는 기타 숫자. 그만큼 int 데이터 유형 둘 다를 나타낼 수 있음 긍정적인 그리고 음수 기본적으로 서명되어 있기 때문입니다.

정수 차지한다 4 바이트 대부분의 장치에 메모리 용량을 할당하여 약 -20억에서 +20억 사이의 값을 저장할 수 있습니다.

숯:

개별 문자는 다음과 같이 표시됩니다. 문자 데이터 유형 . 일반적으로 보유하는 데 사용됩니다. 아스키 또는 UTF-8 인코딩 체계 문자 , 와 같은 문자, 숫자, 기호 , 또는 쉼표 . 있다 256자 이는 1바이트의 메모리를 차지하는 단일 문자로 표현될 수 있습니다. 다음과 같은 문자 'A', 'B', '5', 또는 '$' 작은따옴표로 묶여 있습니다.

뜨다:

정수를 나타내려면 다음을 사용하십시오. 부동 데이터 유형 . 부동 숫자는 분수 단위 또는 소수 자릿수가 있는 숫자를 나타내는 데 사용할 수 있습니다.

그만큼 플로트 유형 일반적으로 매우 높은 정밀도가 필요하지만 매우 정확하지 않을 수 있는 변수에 사용됩니다. 약 의 정확도로 값을 저장할 수 있습니다. 소수점 이하 6자리 그리고 대략적인 범위 3.4x1038 ~에 4 바이트 기억의.

더블:

두 가지 데이터 유형을 사용하여 표현 두 개의 부동 정수 . 과학적 계산이나 금융 응용과 같이 추가적인 정밀도가 필요한 경우 부동 소수점에 비해 더 높은 정확도를 제공합니다.

더블형 , 이는 8바이트 메모리의 정확도는 약 소수점 이하 15자리, 더 큰 값 생성 . C는 명시적인 유형이 제공되지 않으면 기본적으로 부동 소수점 숫자를 double로 처리합니다.

 int age = 25; char grade = 'A'; float temperature = 98.6; double pi = 3.14159265359; 

위의 예에서는 4개의 변수를 선언합니다. 정수 변수 그 사람의 나이에 비해 문자 변수 학생의 성적에 대해서는 부동 변수 온도 판독값에 대한 두 가지 변수 숫자 파이.

파생 데이터 유형

기본적인 데이터 유형 외에도 C는 다음을 지원합니다. 파생된 데이터 유형, 포함 배열, 포인터, 구조체, 그리고 노동조합 . 이러한 데이터 유형은 프로그래머에게 이기종 데이터를 처리하고, 메모리를 직접 수정하고, 복잡한 데이터 구조를 구축할 수 있는 능력을 제공합니다.

정렬:

배열, 파생된 데이터 유형 , 다음의 시퀀스를 저장할 수 있습니다. 고정 크기 요소 같은 유형의. 이는 동일한 이름으로 동일한 데이터의 여러 대상을 결합하는 메커니즘을 제공합니다.

java 배열을 반환하는 방법

인덱스는 배열의 요소에 액세스하는 데 사용됩니다. 0 인덱스 첫 번째 항목에 대해. 배열의 크기는 선언 시 고정되며 프로그램 실행 중에는 변경할 수 없습니다. 어레이 구성 요소는 인접한 메모리 영역에 배치됩니다.

다음은 배열을 선언하고 활용하는 예입니다.

 #include int main() { int numbers[5]; // Declares an integer array with a size of 5 elements // Assign values to the array elements numbers[0] = 10; numbers[1] = 20; numbers[2] = 30; numbers[3] = 40; numbers[4] = 50; // Display the values stored in the array printf(&apos;Values in the array: &apos;); for (int i = 0; i <5; i++) { printf('%d ', numbers[i]); } printf('
'); return 0; < pre> <p> <strong>Output:</strong> </p> <pre> Values in the array: 10 20 30 40 50 </pre> <h3>Pointer:</h3> <p>A <strong> <em>pointer</em> </strong> is a derived data type that keeps track of another data type&apos;s memory address. When a <strong> <em>pointer</em> </strong> is declared, the <strong> <em>data type</em> </strong> it refers to is <strong> <em>stated first</em> </strong> , and then the <strong> <em>variable name</em> </strong> is preceded by <strong> <em>an asterisk (*)</em> </strong> .</p> <p>You can have incorrect access and change the value of variable using pointers by specifying the memory address of the variable. <strong> <em>Pointers</em> </strong> are commonly used in <strong> <em>tasks</em> </strong> such as <strong> <em>function pointers, data structures</em> </strong> , and <strong> <em>dynamic memory allocation</em> </strong> .</p> <p>Here is an example of declaring and employing a pointer:</p> <pre> #include int main() { int num = 42; // An integer variable int *ptr; // Declares a pointer to an integer ptr = # // Assigns the address of &apos;num&apos; to the pointer // Accessing the value of &apos;num&apos; using the pointer printf(&apos;Value of num: %d
&apos;, *ptr); return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> Value of num: 42 </pre> <h3>Structure:</h3> <p>A structure is a derived data type that enables the creation of composite data types by allowing the grouping of many data types under a single name. It gives you the ability to create your own unique data structures by fusing together variables of various sorts.</p> <ol class="points"> <li>A structure&apos;s members or fields are used to refer to each variable within it.</li> <li>Any data type, including different structures, can be a member of a structure.</li> <li>A structure&apos;s members can be accessed by using the dot (.) operator.</li> </ol> <p>A declaration and use of a structure is demonstrated here:</p> <pre> #include #include // Define a structure representing a person struct Person { char name[50]; int age; float height; }; int main() { // Declare a variable of type struct Person struct Person person1; // Assign values to the structure members strcpy(person1.name, &apos;John Doe&apos;); person1.age = 30; person1.height = 1.8; // Accessing the structure members printf(&apos;Name: %s
&apos;, person1.name); printf(&apos;Age: %d
&apos;, person1.age); printf(&apos;Height: %.2f
&apos;, person1.height); return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> Name: John Doe Age: 30 Height: 1.80 </pre> <h3>Union:</h3> <p>A derived data type called a <strong> <em>union</em> </strong> enables you to store various data types in the same memory address. In contrast to structures, where each member has a separate memory space, members of a union all share a single memory space. A value can only be held by one member of a union at any given moment. When you need to represent many data types interchangeably, unions come in handy. Like structures, you can access the members of a union by using the <strong> <em>dot (.)</em> </strong> operator.</p> <p>Here is an example of a union being declared and used:</p> <pre> #include // Define a union representing a numeric value union NumericValue { int intValue; float floatValue; char stringValue[20]; }; int main() { // Declare a variable of type union NumericValue union NumericValue value; // Assign a value to the union value.intValue = 42; // Accessing the union members printf(&apos;Integer Value: %d
&apos;, value.intValue); // Assigning a different value to the union value.floatValue = 3.14; // Accessing the union members printf(&apos;Float Value: %.2f
&apos;, value.floatValue); return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> Integer Value: 42 Float Value: 3.14 </pre> <h2>Enumeration Data Type</h2> <p>A set of named constants or <strong> <em>enumerators</em> </strong> that represent a collection of connected values can be defined in C using the <strong> <em>enumeration data type (enum). Enumerations</em> </strong> give you the means to give names that make sense to a group of integral values, which makes your code easier to read and maintain. </p> <p>Here is an example of how to define and use an enumeration in C:</p> <pre> #include // Define an enumeration for days of the week enum DaysOfWeek { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }; int main() { // Declare a variable of type enum DaysOfWeek enum DaysOfWeek today; // Assign a value from the enumeration today = Wednesday; // Accessing the enumeration value printf(&apos;Today is %d
&apos;, today); return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> Today is 2 </pre> <h2>Void Data Type</h2> <p>The <strong> <em>void data type</em> </strong> in the C language is used to denote the lack of a particular type. <strong> <em>Function return types, function parameters</em> </strong> , and <strong> <em>pointers</em> </strong> are three situations where it is frequently utilized.</p> <h3>Function Return Type:</h3> <p>A <strong> <em>void return type</em> </strong> function does not produce a value. A <strong> <em>void function</em> </strong> executes a task or action and ends rather than returning a value.</p> <p> <strong>Example:</strong> </p> <pre> void printHello() { printf(&apos;Hello, world!
&apos;); } </pre> <h3>Function Parameters: </h3> <p>The <strong> <em>parameter void</em> </strong> can be used to indicate that a function accepts no arguments.</p> <p> <strong>Example:</strong> </p> <pre> void processInput(void) { /* Function logic */ } </pre> <h3>Pointers:</h3> <p>Any address can be stored in a pointer of type <strong> <em>void*</em> </strong> , making it a universal pointer. It offers a method for working with pointers to ambiguous or atypical types.</p> <p> <strong>Example:</strong> </p> <pre> void* dataPtr; </pre> <p>The <strong> <em>void data type</em> </strong> is helpful for defining functions that don&apos;t accept any arguments when working with generic pointers or when you wish to signal that a function doesn&apos;t return a value. It is significant to note that while <strong> <em>void*</em> </strong> can be used to build generic pointers, void itself cannot be declared as a variable type.</p> <p>Here is a sample of code that shows how to utilize void in various situations:</p> <pre> #include // Function with void return type void printHello() { printf(&apos;Hello, world!
&apos;); } // Function with void parameter void processInput(void) { printf(&apos;Processing input...
&apos;); } int main() { // Calling a void function printHello(); // Calling a function with void parameter processInput(); // Using a void pointer int number = 10; void* dataPtr = &amp;number; printf(&apos;Value of number: %d
&apos;, *(int*)dataPtr); return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> Hello, world! Processing input... Value of number: 10 </pre> <h2>Conclusion:</h2> <p>As a result, <strong> <em>data types</em> </strong> are essential in the C programming language because they define the kinds of information that variables can hold. They provide the data&apos;s size and format, enabling the compiler to allot memory and carry out the necessary actions. Data types supported by C include <strong> <em>void, enumeration, derived</em> </strong> , and <strong> <em>basic types</em> </strong> . In addition to floating-point types like <strong> <em>float</em> </strong> and <strong> <em>double</em> </strong> , basic data types in C also include integer-based kinds like <strong> <em>int, char</em> </strong> , and <strong> <em>short</em> </strong> . These forms can be <strong> <em>signed</em> </strong> or <strong> <em>unsigned</em> </strong> , and they fluctuate in size and range. To create dependable and efficient code, it is crucial to comprehend the memory size and scope of these types.</p> <p>A few examples of <strong> <em>derived data types</em> </strong> are <strong> <em>unions, pointers, structures</em> </strong> , and <strong> <em>arrays</em> </strong> . Multiple elements of the same kind can be stored together in contiguous memory due to arrays. <strong> <em>Pointers</em> </strong> keep track of memory addresses, allowing for fast data structure operations and dynamic memory allocation. While <strong> <em>unions</em> </strong> allow numerous variables to share the same memory space, structures group relevant variables together.</p> <p> <strong> <em>Code</em> </strong> becomes more legible and maintainable when named constants are defined using enumeration data types. <strong> <em>Enumerations</em> </strong> give named constants integer values to enable the meaningful representation of related data. The void data type indicates the lack of a particular type. It is used as a return type for both <strong> <em>functions</em> </strong> and <strong> <em>function parameters</em> </strong> that don&apos;t take any arguments and don&apos;t return a value. The <strong> <em>void* pointer</em> </strong> also functions as a general pointer that can <strong> <em>store addresses</em> </strong> of various types.</p> <p>C programming requires a solid understanding of <strong> <em>data types</em> </strong> . Programmers can ensure adequate memory allocation, avoid <strong> <em>data overflow</em> </strong> or <strong> <em>truncation</em> </strong> , and enhance the readability and maintainability of their code by selecting the right <strong> <em>data type</em> </strong> . C programmers may create <strong> <em>effective, dependable</em> </strong> , and well-structured code that satisfies the requirements of their applications by having a firm understanding of data types.</p> <hr></5;>

바늘:

바늘 다른 데이터 유형의 메모리 주소를 추적하는 파생 데이터 유형입니다. 언제 바늘 선언되며, 데이터 형식 그것은 다음을 가리킨다. 먼저 언급 , 그리고 나서 변수 이름 앞에는 별표(*) .

변수의 메모리 주소를 지정하면 포인터를 사용하여 잘못된 액세스가 가능하고 변수의 값을 변경할 수 있습니다. 포인터 일반적으로 사용됩니다 작업 ~와 같은 함수 포인터, 데이터 구조 , 그리고 동적 메모리 할당 .

다음은 포인터를 선언하고 사용하는 예입니다.

 #include int main() { int num = 42; // An integer variable int *ptr; // Declares a pointer to an integer ptr = # // Assigns the address of &apos;num&apos; to the pointer // Accessing the value of &apos;num&apos; using the pointer printf(&apos;Value of num: %d
&apos;, *ptr); return 0; } 

산출:

 Value of num: 42 

구조:

구조는 단일 이름으로 여러 데이터 유형을 그룹화하여 복합 데이터 유형을 생성할 수 있는 파생 데이터 유형입니다. 다양한 종류의 변수를 융합하여 고유한 데이터 구조를 만들 수 있는 기능을 제공합니다.

관계이다
  1. 구조의 멤버나 필드는 구조 내의 각 변수를 참조하는 데 사용됩니다.
  2. 다양한 구조를 포함한 모든 데이터 유형이 구조의 멤버가 될 수 있습니다.
  3. 점(.) 연산자를 사용하여 구조체의 멤버에 액세스할 수 있습니다.

구조의 선언 및 사용은 다음과 같습니다.

 #include #include // Define a structure representing a person struct Person { char name[50]; int age; float height; }; int main() { // Declare a variable of type struct Person struct Person person1; // Assign values to the structure members strcpy(person1.name, &apos;John Doe&apos;); person1.age = 30; person1.height = 1.8; // Accessing the structure members printf(&apos;Name: %s
&apos;, person1.name); printf(&apos;Age: %d
&apos;, person1.age); printf(&apos;Height: %.2f
&apos;, person1.height); return 0; } 

산출:

 Name: John Doe Age: 30 Height: 1.80 

노동 조합:

파생된 데이터 유형은 노동 조합 동일한 메모리 주소에 다양한 데이터 유형을 저장할 수 있습니다. 각 멤버가 별도의 메모리 공간을 갖는 구조와 달리 공용체의 멤버는 모두 단일 메모리 공간을 공유합니다. 특정 순간에 조합의 한 구성원만이 가치를 보유할 수 있습니다. 많은 데이터 유형을 서로 바꿔서 표현해야 할 때 공용체는 유용합니다. 구조체와 마찬가지로 다음을 사용하여 공용체의 구성원에 액세스할 수 있습니다. 점(.) 운영자.

다음은 공용체를 선언하고 사용하는 예입니다.

 #include // Define a union representing a numeric value union NumericValue { int intValue; float floatValue; char stringValue[20]; }; int main() { // Declare a variable of type union NumericValue union NumericValue value; // Assign a value to the union value.intValue = 42; // Accessing the union members printf(&apos;Integer Value: %d
&apos;, value.intValue); // Assigning a different value to the union value.floatValue = 3.14; // Accessing the union members printf(&apos;Float Value: %.2f
&apos;, value.floatValue); return 0; } 

산출:

 Integer Value: 42 Float Value: 3.14 

열거형 데이터 유형

명명된 상수 집합 또는 열거자 연결된 값의 모음을 나타내는 이는 C에서 다음을 사용하여 정의할 수 있습니다. 열거형 데이터 유형(enum). 열거 정수 값 그룹에 의미가 있는 이름을 제공하는 수단을 제공하므로 코드를 더 쉽게 읽고 유지 관리할 수 있습니다.

다음은 C에서 열거형을 정의하고 사용하는 방법에 대한 예입니다.

 #include // Define an enumeration for days of the week enum DaysOfWeek { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }; int main() { // Declare a variable of type enum DaysOfWeek enum DaysOfWeek today; // Assign a value from the enumeration today = Wednesday; // Accessing the enumeration value printf(&apos;Today is %d
&apos;, today); return 0; } 

산출:

 Today is 2 

무효 데이터 유형

그만큼 무효 데이터 유형 C 언어에서는 특정 유형이 없음을 나타내는 데 사용됩니다. 함수 반환 유형, 함수 매개변수 , 그리고 포인터 자주 활용되는 세 가지 상황입니다.

함수 반환 유형:

무효 반환 유형 함수는 값을 생성하지 않습니다. ㅏ 무효 기능 작업이나 작업을 실행하고 값을 반환하는 대신 종료됩니다.

예:

 void printHello() { printf(&apos;Hello, world!
&apos;); } 

기능 매개변수:

그만큼 매개변수 무효 함수가 인수를 허용하지 않음을 나타내는 데 사용할 수 있습니다.

예:

문자열 Java에서 모두 교체
 void processInput(void) { /* Function logic */ } 

포인터:

모든 주소는 다음 유형의 포인터에 저장될 수 있습니다. 무효의* , 이를 범용 포인터로 만듭니다. 모호하거나 비정형적인 유형에 대한 포인터로 작업하는 방법을 제공합니다.

예:

피보나치 코드 자바
 void* dataPtr; 

그만큼 무효 데이터 유형 일반 포인터로 작업할 때 또는 함수가 값을 반환하지 않는다는 신호를 보내려는 경우 인수를 허용하지 않는 함수를 정의하는 데 유용합니다. 중요한 점은 다음과 같습니다. 무효의* 일반 포인터를 작성하는 데 사용할 수 있지만 void 자체는 변수 유형으로 선언할 수 없습니다.

다음은 다양한 상황에서 void를 활용하는 방법을 보여주는 코드 샘플입니다.

 #include // Function with void return type void printHello() { printf(&apos;Hello, world!
&apos;); } // Function with void parameter void processInput(void) { printf(&apos;Processing input...
&apos;); } int main() { // Calling a void function printHello(); // Calling a function with void parameter processInput(); // Using a void pointer int number = 10; void* dataPtr = &amp;number; printf(&apos;Value of number: %d
&apos;, *(int*)dataPtr); return 0; } 

산출:

 Hello, world! Processing input... Value of number: 10 

결론:

결과적으로, 데이터 유형 변수가 보유할 수 있는 정보의 종류를 정의하기 때문에 C 프로그래밍 언어에 필수적입니다. 이는 데이터의 크기와 형식을 제공하여 컴파일러가 메모리를 할당하고 필요한 작업을 수행할 수 있도록 합니다. C에서 지원하는 데이터 유형은 다음과 같습니다. 무효, 열거, 파생 , 그리고 기본 유형 . 다음과 같은 부동 소수점 유형 외에도 뜨다 그리고 더블 , C의 기본 데이터 유형에는 다음과 같은 정수 기반 유형도 포함됩니다. 정수, 문자 , 그리고 짧은 . 이러한 양식은 다음과 같습니다. 서명됨 또는 서명되지 않은 , 크기와 범위가 다양합니다. 신뢰할 수 있고 효율적인 코드를 작성하려면 이러한 유형의 메모리 크기와 범위를 이해하는 것이 중요합니다.

몇 가지 예 파생 데이터 유형 ~이다 공용체, 포인터, 구조 , 그리고 배열 . 배열로 인해 동일한 종류의 여러 요소가 연속 메모리에 함께 저장될 수 있습니다. 포인터 메모리 주소를 추적하여 빠른 데이터 구조 작업과 동적 메모리 할당이 가능합니다. 하는 동안 노동조합 수많은 변수가 동일한 메모리 공간을 공유할 수 있도록 하고, 구조는 관련 변수를 함께 그룹화합니다.

암호 열거형 데이터 유형을 사용하여 명명된 상수를 정의하면 읽기 쉽고 유지 관리가 쉬워집니다. 열거 관련 데이터를 의미 있게 표현할 수 있도록 명명된 상수에 정수 값을 제공합니다. void 데이터 유형은 특정 유형이 없음을 나타냅니다. 두 가지 모두에 대한 반환 유형으로 사용됩니다. 기능 그리고 함수 매개변수 인수를 사용하지 않고 값을 반환하지 않습니다. 그만큼 무효* 포인터 또한 다음을 수행할 수 있는 일반적인 포인터 역할도 합니다. 매장 주소 다양한 유형의.

C 프로그래밍에는 다음 사항에 대한 확실한 이해가 필요합니다. 데이터 유형 . 프로그래머는 적절한 메모리 할당을 보장할 수 있습니다. 데이터 오버플로 또는 잘림 , 올바른 코드를 선택하여 코드의 가독성과 유지 관리성을 향상시킵니다. 데이터 형식 . C 프로그래머는 다음을 만들 수 있습니다. 효과적인, 신뢰할 수 있는 , 데이터 유형에 대한 확고한 이해를 통해 애플리케이션의 요구 사항을 충족하는 잘 구조화된 코드입니다.