logo

Datu tipi C

Datu tips norāda datu veidu, ko mainīgais var saglabāt, piemēram, vesels skaitlis, peldošs, rakstzīme utt.

C Datu tipi

C valodā ir šādi datu veidi.

VeidiDatu veidi
Pamatdatu tipsint, char, float, double
Atvasinātais datu tipsmasīvs, rādītājs, struktūra, savienība
Uzskaites datu tipsenum
Nederīgs datu tipsnederīgs

Pamatdatu veidi

Pamatdatu veidi ir balstīti uz veseliem skaitļiem un uz peldošā komata. C valoda atbalsta gan rakstītus, gan neparakstītus literāļus.

Pamatdatu tipu atmiņas lielums var mainīties atkarībā no 32 vai 64 bitu operētājsistēmas.

java virkne char

Apskatīsim pamata datu tipus. Tā izmērs ir norādīts saskaņā ar 32 bitu arhitektūru .

Datu veidiAtmiņas lielumsDiapazons
char 1 baits-128 līdz 127
parakstīts char1 baits-128 līdz 127
neparakstīts raksts1 baits0 līdz 255
īss 2 baiti-32 768 līdz 32 767
parakstīts īsi2 baiti-32 768 līdz 32 767
neparakstīts īss2 baiti0 līdz 65 535
starpt 2 baiti-32 768 līdz 32 767
parakstīts int2 baiti-32 768 līdz 32 767
neparakstīts int2 baiti0 līdz 65 535
īss int 2 baiti-32 768 līdz 32 767
parakstīts īss int2 baiti-32 768 līdz 32 767
neparakstīts īss int2 baiti0 līdz 65 535
garš int 4 baiti-2 147 483 648 līdz 2 147 483 647
parakstīts garš int4 baiti-2 147 483 648 līdz 2 147 483 647
neparakstīts garš int4 baiti0 līdz 4 294 967 295
peldēt 4 baiti
dubultā 8 baiti
garš dubultā 10 baiti

Int:

Veseli skaitļi ir veseli skaitļi bez daļskaitļa vai decimāldaļas, un int datu tips tiek izmantots, lai tos pārstāvētu.

To bieži piemēro mainīgajiem lielumiem, kas ietver vērtības , piemēram, skaitļi, indeksi , vai citus ciparus. The int datu tips var pārstāvēt abus pozitīvs un negatīvi skaitļi jo tas ir parakstīts pēc noklusējuma.

An starpt aizņem 4 baiti atmiņa lielākajā daļā ierīču, ļaujot tajā saglabāt vērtības no aptuveni -2 miljardiem līdz +2 miljardiem.

Raksturs:

Atsevišķas rakstzīmes apzīmē ar char datu tips . Parasti izmanto, lai turētu ASCII vai UTF-8 kodēšanas shēmas rakstzīmes , piemēram, burti, cipari, simboli , vai komatiem . Tur ir 256 rakstzīmes ko var attēlot ar vienu rakstzīmi, kas aizņem vienu baitu atmiņas. Tādi tēli kā 'A', 'b', '5', vai '$' ir ievietotas atsevišķās pēdiņās.

Pludiņš:

Lai attēlotu veselus skaitļus, izmantojiet peldošs datu tips . Peldošos skaitļus var izmantot, lai attēlotu daļskaitļus vai skaitļus ar decimālzīmēm.

The pludiņa veids parasti izmanto mainīgajiem lielumiem, kuriem nepieciešama ļoti laba precizitāte, taču tie var nebūt ļoti precīzi. Tas var saglabāt vērtības ar precizitāti aptuveni 6 zīmes aiz komata un diapazons aptuveni 3,4 x 1038 iekšā 4 baiti atmiņas.

Dubultā:

Lai attēlotu, izmantojiet divus datu tipus divi peldoši veseli skaitļi . Ja ir nepieciešama papildu precizitāte, piemēram, zinātniskos aprēķinos vai finanšu lietojumos, tas nodrošina lielāku precizitāti salīdzinājumā ar pludiņu.

java apmācība iesācējiem

Dubultais tips , kas izmanto 8 baiti atmiņas un tā precizitāte ir aptuveni 15 zīmes aiz komata, iegūst lielākas vērtības . C pēc noklusējuma apstrādā peldošā komata skaitļus kā dubultskaitļus, ja nav norādīts precīzs veids.

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

Iepriekš minētajā piemērā mēs deklarējam četrus mainīgos: an int mainīgais personas vecumam, a char mainīgais skolēna atzīmei, a peldošais mainīgais temperatūras rādījumam un divi mainīgie skaitlis pi.

Atvasinātais datu tips

Papildus pamata datu tipiem atbalsta arī C atvasinātie datu tipi, ieskaitot masīvi, norādes, struktūras, un arodbiedrības . Šie datu tipi programmētājiem sniedz iespēju apstrādāt neviendabīgus datus, tieši modificēt atmiņu un veidot sarežģītas datu struktūras.

Masīvs:

An masīvs, atvasināts datu tips , ļauj saglabāt secību fiksēta izmēra elementi tāda paša veida. Tas nodrošina mehānismu vairāku viena un tā paša datu mērķu savienošanai ar vienu un to pašu nosaukumu.

Indekss tiek izmantots, lai piekļūtu masīva elementiem, izmantojot a 0 indekss pirmajam ierakstam. Masīva lielums ir fiksēts deklarēšanas laikā, un to nevar mainīt programmas izpildes laikā. Masīva komponenti tiek ievietoti blakus esošajos atmiņas reģionos.

Šeit ir masīva deklarēšanas un izmantošanas piemērs:

 #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;>

Rādītājs:

A rādītājs ir atvasināts datu tips, kas seko cita datu tipa atmiņas adresei. Kad rādītājs ir deklarēts, datu tips tas attiecas uz ir norādīja pirmais , un pēc tam mainīgais nosaukums priekšā ir zvaigznīte (*) .

Varat iegūt nepareizu piekļuvi un mainīt mainīgā vērtību, izmantojot norādes, norādot mainīgā atmiņas adresi. Rādītāji tiek plaši izmantoti uzdevumus piemēram, funkciju norādes, datu struktūras , un dinamiska atmiņas piešķiršana .

Šeit ir rādītāja deklarēšanas un izmantošanas piemērs:

 #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; } 

Izvade:

 Value of num: 42 

Struktūra:

Struktūra ir atvasināts datu tips, kas ļauj izveidot saliktus datu tipus, ļaujot grupēt daudzus datu tipus ar vienu nosaukumu. Tas dod jums iespēju izveidot savas unikālas datu struktūras, apvienojot dažāda veida mainīgos.

linux faili
  1. Struktūras locekļi vai lauki tiek izmantoti, lai atsauktos uz katru mainīgo tajā.
  2. Jebkurš datu tips, tostarp dažādas struktūras, var būt struktūras dalībnieks.
  3. Struktūras elementiem var piekļūt, izmantojot punktu (.) operatoru.

Šeit ir parādīta deklarācija un struktūras izmantošana:

 #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; } 

Izvade:

 Name: John Doe Age: 30 Height: 1.80 

Savienība:

Atvasināts datu tips, ko sauc par a savienība ļauj vienā atmiņas adresē saglabāt dažādu veidu datus. Atšķirībā no struktūrām, kur katram dalībniekam ir atsevišķa atmiņas telpa, savienības locekļiem ir viena atmiņas telpa. Vērtību jebkurā brīdī var turēt tikai viens arodbiedrības loceklis. Ja jums ir jāpārstāv daudzi datu tipi savstarpēji aizstājami, arodbiedrības noder. Tāpat kā struktūrām, jūs varat piekļūt arodbiedrības dalībniekiem, izmantojot punkts (.) operators.

Šeit ir piemērs, kā tiek deklarēta un izmantota savienība:

 #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; } 

Izvade:

 Integer Value: 42 Float Value: 3.14 

Uzskaites datu tips

Nosauktu konstantu kopa vai skaitītāji kas attēlo savienoto vērtību kolekciju, var definēt C, izmantojot uzskaites datu tips (enum). Uzskaitījumi sniedz jums līdzekļus, lai piešķirtu jēgpilnus nosaukumus integrālo vērtību grupai, kas atvieglo jūsu koda lasīšanu un apkopi.

Šeit ir piemērs, kā definēt un izmantot uzskaitījumu 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; } 

Izvade:

 Today is 2 

Nederīgs datu tips

The nederīgs datu tips C valodā lieto, lai apzīmētu konkrēta tipa trūkumu. Funkciju atgriešanas veidi, funkciju parametri , un norādes ir trīs situācijas, kurās to bieži izmanto.

Funkcijas atgriešanas veids:

A nederīgs atgriešanas veids funkcija nerada vērtību. A tukšuma funkcija izpilda uzdevumu vai darbību un beidz, nevis atgriež vērtību.

Piemērs:

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

Funkciju parametri:

The parametrs nav spēkā var izmantot, lai norādītu, ka funkcija nepieņem argumentus.

par katru mašīnrakstu

Piemērs:

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

Norādes:

Tipa rādītājā var saglabāt jebkuru adresi spēkā neesošs* , padarot to par universālu rādītāju. Tā piedāvā metodi darbam ar norādēm uz neskaidriem vai netipiskiem veidiem.

Piemērs:

 void* dataPtr; 

The nederīgs datu tips ir noderīga, lai definētu funkcijas, kas nepieņem nekādus argumentus, strādājot ar vispārīgiem rādītājiem vai ja vēlaties norādīt, ka funkcija neatgriež vērtību. Ir svarīgi atzīmēt, ka, kamēr nederīgs* var izmantot, lai izveidotu vispārīgus rādītājus, bet pašu tukšumu nevar deklarēt kā mainīgā tipu.

Šeit ir koda paraugs, kas parāda, kā izmantot tukšumu dažādās situācijās:

 #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; } 

Izvade:

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

Secinājums:

Rezultātā, datu tipi ir būtiski C programmēšanas valodā, jo tie nosaka informācijas veidu, ko var saturēt mainīgie. Tie nodrošina datu lielumu un formātu, ļaujot kompilatoram piešķirt atmiņu un veikt nepieciešamās darbības. C atbalstītie datu tipi ietver spēkā neesošs, uzskaitījums, atvasināts , un pamata veidi . Papildus peldošā komata veidiem, piemēram, peldēt un dubultā , C pamatdatu tipi ietver arī uz veseliem skaitļiem balstītus veidus, piemēram int, char , un īss . Šīs formas var būt parakstīts vai neparakstīts , un tie svārstās pēc izmēra un diapazona. Lai izveidotu uzticamu un efektīvu kodu, ir ļoti svarīgi saprast šo veidu atmiņas lielumu un apjomu.

Daži piemēri atvasinātie datu veidi ir arodbiedrības, norādes, struktūras , un masīvi . Masīvu dēļ blakus atmiņā var saglabāt vairākus viena veida elementus. Rādītāji sekojiet līdzi atmiņas adresēm, nodrošinot ātras datu struktūras darbības un dinamisku atmiņas piešķiršanu. Kamēr arodbiedrības ļauj daudziem mainīgajiem koplietot vienu un to pašu atmiņas vietu, struktūras grupē atbilstošos mainīgos.

korekts java

Kods kļūst salasāmāks un uzturējamāks, ja nosauktās konstantes tiek definētas, izmantojot uzskaites datu tipus. Uzskaitījumi dodiet nosauktām konstantēm veselu skaitļu vērtības, lai nodrošinātu saistīto datu jēgpilnu attēlojumu. Nederīgo datu tips norāda uz noteikta veida trūkumu. To izmanto kā atgriešanas veidu abiem funkcijas un funkciju parametri kas neņem nekādus argumentus un neatgriež vērtību. The tukšs* rādītājs darbojas arī kā vispārīgs rādītājs, kas var veikalu adreses dažādu veidu.

C programmēšanai ir nepieciešama laba izpratne par datu tipi . Programmētāji var nodrošināt atbilstošu atmiņas piešķiršanu, izvairīties datu pārpilde vai saīsināšana , un uzlabojiet to koda lasāmību un apkopi, izvēloties pareizo datu tips . C programmētāji var izveidot efektīvs, uzticams , un labi strukturēts kods, kas atbilst to lietojumprogrammu prasībām, jo ​​ir stingra izpratne par datu veidiem.