logo

أنواع البيانات في لغة C

يحدد نوع البيانات نوع البيانات التي يمكن للمتغير تخزينها مثل عدد صحيح أو عائم أو حرف وما إلى ذلك.

ج- أنواع البيانات

هناك أنواع البيانات التالية في لغة C.

أنواعأنواع البيانات
نوع البيانات الأساسيةكثافة العمليات، شار، تعويم، مزدوج
نوع البيانات المشتقةالمصفوفة، المؤشر، الهيكل، الاتحاد
نوع بيانات التعدادالتعداد
نوع البيانات الفارغةفارغ

أنواع البيانات الأساسية

تعتمد أنواع البيانات الأساسية على الأعداد الصحيحة والفاصلة العائمة. تدعم لغة C كلا من الأحرف الموقعة وغير الموقعة.

قد يتغير حجم الذاكرة لأنواع البيانات الأساسية وفقًا لنظام التشغيل 32 أو 64 بت.

.جافا التالي

دعونا نرى أنواع البيانات الأساسية. يتم إعطاء حجمها وفقا للهندسة المعمارية 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 بايت

كثافة العمليات:

الأعداد الصحيحة هي أعداد كاملة بدون أي أجزاء كسرية أو عشرية، و نوع البيانات كثافة العمليات يستخدم لتمثيلهم

يتم تطبيقه بشكل متكرر على المتغيرات التي تتضمن قيم ، مثل التهم والمؤشرات أو أرقام رقمية أخرى. ال نوع البيانات كثافة العمليات قد يمثل كلاهما إيجابي و أرقام سلبية لأنه تم توقيعه بشكل افتراضي.

ان كثافة العمليات يستغرق فترة تصل 4 بايت من الذاكرة على معظم الأجهزة، مما يسمح لها بتخزين قيم تتراوح بين -2 مليار و+2 مليار تقريبًا.

شار:

يتم تمثيل الشخصيات الفردية بواسطة نوع بيانات شار . تستخدم عادة لعقد أسكي أو أحرف نظام الترميز UTF-8 ، مثل الحروف والأرقام والرموز ، أو فواصل . هناك 256 حرفا والتي يمكن تمثيلها بحرف واحد، والذي يشغل بايتًا واحدًا من الذاكرة. شخصيات مثل 'أ'، 'ب'، '5'، أو '$' محاطة بعلامات اقتباس مفردة.

يطفو:

لتمثيل الأعداد الصحيحة، استخدم نوع البيانات العائمة . يمكن استخدام الأرقام العائمة لتمثيل الوحدات الكسرية أو الأرقام ذات المنازل العشرية.

ال نوع تعويم يستخدم عادةً للمتغيرات التي تتطلب دقة جيدة جدًا ولكنها قد لا تكون دقيقة جدًا. يمكنه تخزين القيم بدقة تبلغ حوالي 6 منازل عشرية ومجموعة من حوالي 3.4 × 1038 في 4 بايت من الذاكرة.

مزدوج:

استخدم نوعين من البيانات للتمثيل اثنين من الأعداد الصحيحة العائمة . عندما تكون هناك حاجة إلى دقة إضافية، كما هو الحال في الحسابات العلمية أو التطبيقات المالية، فإنها توفر دقة أكبر مقارنة بالتعويم.

نوع مزدوج ، والذي يستخدم 8 بايت من الذاكرة ولها دقة حوالي 15 منزلة عشرية، تعطي قيمًا أكبر . يتعامل لغة C مع أرقام الفاصلة العائمة على أنها أرقام مزدوجة بشكل افتراضي إذا لم يتم توفير نوع صريح.

الممثلة زينات أمان
 int age = 25; char grade = 'A'; float temperature = 98.6; double pi = 3.14159265359; 

في المثال أعلاه، نعلن عن أربعة متغيرات: an متغير كثافة العمليات بالنسبة لعمر الشخص أ متغير شار بالنسبة لدرجة الطالب أ متغير تعويم لقراءة درجة الحرارة، ومتغيرين ل رقم بي

نوع البيانات المشتقة

بالإضافة إلى أنواع البيانات الأساسية، تدعم لغة C أيضًا أنواع البيانات المشتقة، مشتمل المصفوفات، المؤشرات، الهياكل، و النقابات . تمنح أنواع البيانات هذه المبرمجين القدرة على التعامل مع البيانات غير المتجانسة، وتعديل الذاكرة مباشرة، وبناء هياكل بيانات معقدة.

مجموعة مصفوفة:

ان المصفوفة، نوع بيانات مشتق ، يتيح لك تخزين تسلسل من عناصر ذات حجم ثابت من نفس النوع. يوفر آلية لضم أهداف متعددة لنفس البيانات تحت نفس الاسم.

يتم استخدام الفهرس للوصول إلى عناصر المصفوفة، باستخدام أ 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 باستخدام نوع بيانات التعداد (التعداد). التعدادات يمنحك الوسائل اللازمة لإعطاء أسماء منطقية لمجموعة من القيم المتكاملة، مما يجعل قراءة التعليمات البرمجية الخاصة بك وصيانتها أسهل.

فيما يلي مثال لكيفية تعريف التعداد واستخدامه في لغة 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;); } 

معلمات الوظيفة:

ال المعلمة فارغة يمكن استخدامها للإشارة إلى أن الوظيفة لا تقبل أي وسائط.

مثال:

 void processInput(void) { /* Function logic */ } 

المؤشرات:

يمكن تخزين أي عنوان في مؤشر من النوع فارغ* مما يجعله مؤشرًا عالميًا. وهو يوفر طريقة للعمل مع المؤشرات للأنواع الغامضة أو غير النمطية.

مثال:

 void* dataPtr; 

ال نوع البيانات الفارغة مفيد في تحديد الوظائف التي لا تقبل أي وسيطات عند العمل باستخدام مؤشرات عامة أو عندما ترغب في الإشارة إلى أن الوظيفة لا تُرجع قيمة. ومن المهم أن نلاحظ أنه في حين فارغ* يمكن استخدامه لبناء مؤشرات عامة، ولا يمكن تعريف الفراغ نفسه كنوع متغير.

فيما يلي عينة من التعليمات البرمجية التي توضح كيفية الاستفادة من الفراغ في مواقف مختلفة:

 #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 أيضًا أنواعًا تعتمد على الأعداد الصحيحة مثل كثافة العمليات، شار ، و قصير . يمكن أن تكون هذه الأشكال وقعت أو غير موقعة ، وهي تتقلب في الحجم والمدى. لإنشاء تعليمات برمجية موثوقة وفعالة، من الضروري فهم حجم الذاكرة ونطاق هذه الأنواع.

بعض الأمثلة على أنواع البيانات المشتقة نكون النقابات والمؤشرات والهياكل ، و صفائف . يمكن تخزين عناصر متعددة من نفس النوع معًا في ذاكرة متجاورة بسبب المصفوفات. المؤشرات تتبع عناوين الذاكرة، مما يسمح بعمليات بنية البيانات السريعة وتخصيص الذاكرة الديناميكية. بينما النقابات تسمح للعديد من المتغيرات بمشاركة نفس مساحة الذاكرة، وتقوم الهياكل بتجميع المتغيرات ذات الصلة معًا.

شفرة يصبح أكثر وضوحًا وقابلية للصيانة عندما يتم تعريف الثوابت المسماة باستخدام أنواع بيانات التعداد. التعدادات إعطاء قيم عددية للثوابت المسماة لتمكين التمثيل الهادف للبيانات ذات الصلة. يشير نوع البيانات الفارغ إلى عدم وجود نوع معين. يتم استخدامه كنوع الإرجاع لكليهما المهام و معلمات الوظيفة التي لا تأخذ أي وسيطات ولا تُرجع قيمة. ال مؤشر باطل* يعمل أيضًا كمؤشر عام يمكنه ذلك عناوين المتجر من مختلف الأنواع.

سطر جديد في بيثون

تتطلب البرمجة بلغة C فهمًا قويًا أنواع البيانات . يمكن للمبرمجين ضمان تخصيص الذاكرة بشكل مناسب، وتجنب تجاوز البيانات أو اقتطاع ، وتحسين سهولة قراءة التعليمات البرمجية الخاصة بهم وصيانتها عن طريق تحديد الخيار الصحيح نوع البيانات . يمكن لمبرمجي لغة C إنشاء فعالة ويمكن الاعتماد عليها ، والتعليمات البرمجية جيدة التنظيم التي تلبي متطلبات تطبيقاتهم من خلال الحصول على فهم قوي لأنواع البيانات.