C Tutorial/Data Type/Data Type

Материал из C\C++ эксперт
Перейти к: навигация, поиск

C Numeric Data Types

<source lang="cpp">Keyword Variable Type Range char Character (or string) -128 to 127 int Integer -32,768 to 32,767 short Short integer -32,768 to 32,767 short int Short integer -32,768 to 32,767 long Long integer -2,147,483,648 to 2,147,483,647 unsigned char Unsigned character 0 to 255 unsigned int Unsigned integer 0 to 65,535 unsigned short Unsigned short integer 0 to 65,535 unsigned long Unsigned long integer 0 to 4,294,967,295 float Single-precision +/-3.4E10^38 to +/-3.4E10^38

                 floating-point
                 (accurate to 7 digits)     

double Double-precision +/-1.7E10^308 to +/-1.7E10^308

                 floating-point
                 (accurate to 15 digits)</source>

C has really only four types of variables:

  1. char
  2. int
  3. float
  4. double

Get maximum and minimum values of data type

<source lang="cpp">#include <stdio.h> main(){

   int i,j ;
   i = 1;
   while (i > 0) {
       j = i;
       i++;
   }
   printf ("the maximum value of integer is %d\n",j);
   printf ("the value of integer after overflow is %d\n",i);

}</source>

the maximum value of integer is 2147483647
     the value of integer after overflow is -2147483648

Introduction to Data Types

  1. Data type determines how much storage space is allocated to variables.
  2. Data type determines the permissible operations on variables.
  3. The variables should be declared by specifying the data type.
  1. There is a family of integer data types and floating-point data types.
  2. Characters are stored internally as integers
  3. Characters are interpreted according to the character set.
  1. The most commonly used character set is ASCII.
  2. In the ASCII character set, A is represented by the number 65.

2.1.Data Type 2.1.1. <A href="/Tutorial/C/0040__Data-Type/Whatisadatatype.htm">What is a data type</a> 2.1.2. Introduction to Data Types 2.1.3. <A href="/Tutorial/C/0040__Data-Type/CNumericDataTypes.htm">C Numeric Data Types</a> 2.1.4. <A href="/Tutorial/C/0040__Data-Type/Useunsignedvariables.htm">Use unsigned variables</a> 2.1.5. <A href="/Tutorial/C/0040__Data-Type/SignedandUnsignedVariables.htm">Signed and Unsigned Variables</a> 2.1.6. <A href="/Tutorial/C/0040__Data-Type/Getmaximumandminimumvaluesofdatatype.htm">Get maximum and minimum values of data type</a> 2.1.7. <A href="/Tutorial/C/0040__Data-Type/Multipledeclarations.htm">Multiple declarations</a>

Multiple declarations

<source lang="cpp">#include <stdio.h>

int main() {

  int m,y,d;
 
  m = 1;
  y = 2;
  d = 3;
 
  printf(" %d  %d  %d \n",m, y, d);
  return 0; 

}</source>

1  2  3

Signed and Unsigned Variables

<source lang="cpp">Signed Range Unsigned Range char -128 to 127 unsigned char 0 to 255 int -32768 to 32,767 unsigned int 0 to 65,535 long -2,147,483,648 unsigned long 0 to 4,294,967,295 to 2,147,483,647</source>

Use unsigned variables

When declaring a numeric variable in C, you can use "signed" or "unsigned".

Variable is signed unless you specifically type unsigned before the variable type:


<source lang="cpp">unsigned int sh = 26;</source>

  1. The standard int variable can hold values from -32,768 up to 32,767.
  2. That"s half negative numbers, from -32,786 to -1, and then half positive numbers, from 0 up to 32,767.
  3. An unsigned number means that the variable holds only positive values.
  4. Your typical unsigned int has a range from 0 to 65,535.

What is a data type

For each variable you have to attach some data type.

The data type defines:

  1. the amount of storage allocated to variables.
  2. the values that they can accept.
  3. the operations that can be performed on variables.

2.1.Data Type 2.1.1. What is a data type 2.1.2. <A href="/Tutorial/C/0040__Data-Type/IntroductiontoDataTypes.htm">Introduction to Data Types</a> 2.1.3. <A href="/Tutorial/C/0040__Data-Type/CNumericDataTypes.htm">C Numeric Data Types</a> 2.1.4. <A href="/Tutorial/C/0040__Data-Type/Useunsignedvariables.htm">Use unsigned variables</a> 2.1.5. <A href="/Tutorial/C/0040__Data-Type/SignedandUnsignedVariables.htm">Signed and Unsigned Variables</a> 2.1.6. <A href="/Tutorial/C/0040__Data-Type/Getmaximumandminimumvaluesofdatatype.htm">Get maximum and minimum values of data type</a> 2.1.7. <A href="/Tutorial/C/0040__Data-Type/Multipledeclarations.htm">Multiple declarations</a>