logo

סוגי נתונים ב-C

סוג נתונים מציין את סוג הנתונים שמשתנה יכול לאחסן כמו מספר שלם, צף, תו וכו'.

C סוגי נתונים

ישנם סוגי הנתונים הבאים בשפת C.

סוגיםסוגי מידע
סוג נתונים בסיסיint, char, float, double
סוג נתונים נגזרמערך, מצביע, מבנה, איחוד
סוג נתוני ספירהenum
סוג נתונים בטלבָּטֵל

סוגי נתונים בסיסיים

סוגי הנתונים הבסיסיים הם מבוססי מספרים שלמים ומבוססים על נקודה צפה. שפת C תומכת הן בסימן חתום והן בלא חתום.

גודל הזיכרון של סוגי הנתונים הבסיסיים עשוי להשתנות בהתאם למערכת ההפעלה של 32 או 64 סיביות.

מחרוזת java ל-char

בוא נראה את סוגי הנתונים הבסיסיים. גודלו נתון לפי ארכיטקטורת 32 סיביות .

סוגי מידעגודל זיכרוןטווח
לְהַשְׁחִיר 1 בייט−128 עד 127
חתום char1 בייט−128 עד 127
char לא חתום1 בייט0 עד 255
קצר 2 בתים−32,768 עד 32,767
חתום קצר2 בתים−32,768 עד 32,767
קצר לא חתום2 בתים0 עד 65,535
int 2 בתים−32,768 עד 32,767
חתום int2 בתים−32,768 עד 32,767
int לא חתום2 בתים0 עד 65,535
קצר int 2 בתים−32,768 עד 32,767
חתום קצר אינט2 בתים−32,768 עד 32,767
קצר אינט2 בתים0 עד 65,535
long int 4 בתים-2,147,483,648 עד 2,147,483,647
חתום long int4 בתים-2,147,483,648 עד 2,147,483,647
לא חתום long int4 בתים0 עד 4,294,967,295
לָצוּף 4 בתים
לְהַכפִּיל 8 בתים
כפול ארוך 10 בתים

Int:

מספרים שלמים הם מספרים שלמים ללא חלקים שברים או עשרוניים, וה- סוג נתונים int משמש לייצג אותם.

זה מיושם לעתים קרובות על משתנים הכוללים ערכים , כמו ספירות, מדדים , או מספרים מספריים אחרים. ה סוג נתונים int עשוי לייצג את שניהם חִיוּבִי ו מספרים שליליים כי הוא חתום כברירת מחדל.

א int לוקח 4 בתים של זיכרון ברוב המכשירים, מה שמאפשר לו לאחסן ערכים בין כ-2 מיליארד ל-2 מיליארד+.

לְהַשְׁחִיר:

תווים בודדים מיוצגים על ידי סוג נתוני char . משמש בדרך כלל להחזיק ASCII אוֹ תווי ערכת קידוד UTF-8 , כמו אותיות, מספרים, סמלים , או פסיקים . יש 256 תווים שיכול להיות מיוצג על ידי char בודד, שתופס בייט אחד של זיכרון. דמויות כגון 'A', 'b', '5', אוֹ '$' מצורפים במרכאות בודדות.

לָצוּף:

כדי לייצג מספרים שלמים, השתמש ב- סוג נתונים צף . ניתן להשתמש במספרים צפים כדי לייצג יחידות חלקיות או מספרים עם מקומות עשרוניים.

ערימות java

ה סוג ציפה משמש בדרך כלל למשתנים הדורשים דיוק טוב מאוד, אך עשויים להיות לא מאוד מדויקים. זה יכול לאחסן ערכים עם דיוק של בערך 6 מקומות עשרוניים וטווח של בערך 3.4 x 1038 ב 4 בתים של זיכרון.

לְהַכפִּיל:

השתמש בשני סוגי נתונים לייצוג שני מספרים שלמים צפים . כאשר נדרש דיוק נוסף, כגון בחישובים מדעיים או יישומים פיננסיים, הוא מספק דיוק רב יותר בהשוואה לצוף.

סוג כפול , אשר משתמש 8 בתים של זיכרון ובעל דיוק של בערך 15 מקומות עשרוניים, מניב ערכים גדולים יותר . C מתייחסת למספרי נקודה צפה ככפלים כברירת מחדל אם לא מסופק סוג מפורש.

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

בדוגמה למעלה, אנו מכריזים על ארבעה משתנים: an משתנה int לגילו של האדם, א משתנה char לציון התלמיד, א משתנה לצוף עבור קריאת הטמפרטורה, ושני משתנים עבור מספר פאי.

סוג נתונים נגזר

מעבר לסוגי הנתונים הבסיסיים, C תומך גם סוגי נתונים נגזרים, לְרַבּוֹת מערכים, מצביעים, מבנים, ו איגודי עובדים . סוגי נתונים אלה נותנים למתכנתים את היכולת לטפל בנתונים הטרוגניים, לשנות ישירות את הזיכרון ולבנות מבני נתונים מסובכים.

מַעֲרָך:

א מערך, סוג נתונים נגזר , מאפשר לך לאחסן רצף של אלמנטים בגודל קבוע מאותו סוג. הוא מספק מנגנון לצירוף יעדים מרובים של אותם נתונים תחת אותו שם.

האינדקס משמש לגישה לאלמנטים של המערך, עם a 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 

מִבְנֶה:

מבנה הוא סוג נתונים נגזר המאפשר יצירת סוגי נתונים מרוכבים על ידי מתן אפשרות לקיבוץ של סוגי נתונים רבים תחת שם יחיד. זה נותן לך את היכולת ליצור מבני נתונים ייחודיים משלך על ידי מיזוג משתנים מסוגים שונים.

הורד את נגן המדיה של YouTube vlc
  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 

הִתאַחֲדוּת:

סוג נתונים נגזר הנקרא a הִתאַחֲדוּת מאפשר לך לאחסן סוגי נתונים שונים באותה כתובת זיכרון. בניגוד למבנים, שבהם לכל חבר יש מרחב זיכרון נפרד, חברי איגוד כולם חולקים מרחב זיכרון אחד. ערך יכול להיות מוחזק רק על ידי חבר אחד באיגוד בכל רגע נתון. כאשר אתה צריך לייצג סוגי נתונים רבים לסירוגין, איגודים מועילים. כמו מבנים, אתה יכול לגשת לחברי איגוד באמצעות ה נקודה (.) מַפעִיל.

הנה דוגמה לאיגוד שהוכרז ונעשה בו שימוש:

 #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 משמשת לציון היעדר סוג מסוים. סוגי החזרת פונקציות, פרמטרים של פונקציות , ו מצביעים הם שלושה מצבים שבהם הוא מנוצל לעתים קרובות.

סוג החזרת פונקציה:

א סוג החזר בטל הפונקציה לא מייצרת ערך. א פונקציה בטל מבצע משימה או פעולה ומסתיים במקום להחזיר ערך.

דוגמא:

מסגרת tkinter
 void printHello() { printf(&apos;Hello, world!
&apos;); } 

פרמטרים של פונקציה:

ה בטל פרמטר ניתן להשתמש כדי לציין שפונקציה אינה מקבלת ארגומנטים.

דוגמא:

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

מצביעים:

ניתן לאחסן כל כתובת במצביע מסוג בָּטֵל* , מה שהופך אותו למצביע אוניברסלי. הוא מציע שיטה לעבודה עם מצביעים על טיפוסים מעורפלים או לא טיפוסיים.

דוגמא:

 void* dataPtr; 

ה סוג נתונים ריק מועיל להגדרת פונקציות שאינן מקבלות ארגומנטים כלשהם בעת עבודה עם מצביעים גנריים או כאשר ברצונך לאותת שפונקציה אינה מחזירה ערך. חשוב לציין כי תוך כדי בָּטֵל* יכול לשמש לבניית מצביעים גנריים, לא ניתן להכריז על 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 כוללים גם סוגים מבוססי מספרים שלמים כמו int, char , ו קצר . טפסים אלה יכולים להיות חתם אוֹ לא חתום , והם משתנים בגודל ובטווח. כדי ליצור קוד אמין ויעיל, חיוני להבין את גודל הזיכרון והיקפו של סוגים אלה.

כמה דוגמאות של סוגי נתונים נגזרים הם איגודים, מצביעים, מבנים , ו מערכים . אלמנטים מרובים מאותו סוג יכולים להיות מאוחסנים יחד בזיכרון רציף עקב מערכים. מצביעים עקוב אחר כתובות הזיכרון, מה שמאפשר פעולות מהירות של מבנה נתונים והקצאת זיכרון דינמית. בזמן איגודי עובדים מאפשרים למשתנים רבים לחלוק את אותו שטח זיכרון, מבנים מקבצים משתנים רלוונטיים יחד.

קוד הופך להיות יותר קריא וניתן לתחזוקה כאשר קבועים בעלי שם מוגדרים באמצעות סוגי נתוני ספירה. ספירות תן קבועים בעלי שם ערכי מספר שלמים כדי לאפשר ייצוג משמעותי של נתונים קשורים. סוג הנתונים הריק מצביע על היעדר סוג מסוים. הוא משמש כסוג החזרה עבור שניהם פונקציות ו פרמטרים של פונקציות שלא לוקחים טיעונים ולא מחזירים ערך. ה מצביע ריק* מתפקד גם כמצביע כללי שיכול כתובות חנות מסוגים שונים.

תכנות C דורש הבנה מוצקה של סוגי מידע . מתכנתים יכולים להבטיח הקצאת זיכרון נאותה, להימנע הצפת נתונים אוֹ גְמִימָה , ולשפר את הקריאה והתחזוקה של הקוד שלהם על ידי בחירה נכונה סוג מידע . מתכנתי C עשויים ליצור יעיל, אמין , וקוד מובנה היטב העונה על הדרישות של היישומים שלהם על ידי הבנה מוצקה של סוגי נתונים.