Primary Data Type In C Language

 Abdessalam Go
0

In the C programming language, there are three main categories of data types: primary, derived, and user-defined data types. In this article, we will focus only on the primary data types, also known as fundamental data types.


Primary Data Type

Primary data type are pre-defined, meaning they already exist in C programming language. Thes data type can be classify into types used for variable values and types used for constant values.


Table of contents :




Data type
Memory(byte)
Precision
Specifier
char       1    none     %c
int       4    none     %d
folat       4         6
    %f
double       8       15     %lf


Data Types For Variable Values:

Syntax of variable primary data type :
type name = value ;


1 character and integer :

Primary data types character and integer can be written in two formats:

unsigned → to represent non-negative values, which can be only zero or postive numbers only.

signedto represent both positive and negative values, including zero.

important notes:
- By default, all data types are signed, which means you do not need to write signed explicitly.
- To adjust the size or precision of a data type, you can use the long and short modifiers.


1.1) charachter :(Alpha Numerical) :

The char data type is used to store single characters.
Internally, each charcter is stored as integer value corresponding to its ASCII code  (for example, A = 65, B = 66, etc.).


 signed char : can store values from -128 to 127.


syntax code ;

We can declare a signed char variable in two ways:

- Declaration with initialization:
signed char name = 'value' ;

- Declaration first, then assignment:
signed char name ;
name = 'value' ;

example :




    1.2)int :

    The int data type is used to store integer numbers (whole numbers without decimal points).

    signed int :  can store values from -2147483648  to 2147483647 (on a 4-byte system).


    Syntax of the code :

    We can declare an integer variable in two ways:

    - Declaration with initialization:
    signed int name = value ;

    - Declaration first, then assignment:
    int name ;
    name = value ;

    example :


      unsigned int : The unsigned int data type is used to store non-negative integer values only.

      Range: from 0 to  4294967295 (on a 4-byte system).


      Syntax of the code :

      -Declaration with initialization:
      usigned int name = value ;

      -Declaration first, then assignment:
      unsigned int name ;
      name = value ;

      example :



        1.3) long int : 

        The long int data type is similar to int, but it can store larger integer values.

        The main difference is in the size and syntax.


        Signed long int → can store large positive and negative values (typically -2,147,483,648 to 2,147,483,647 on 4-byte systems, may be larger on 8-byte systems depending on the platform).


        Unsigned long int → can store values from 0 to 4,294,967,295 (on a 4-byte system) .


        1.4) long long  int : 

        The long long int data type is used to store very large integer values. It is an extension of long int and typically takes 8 bytes in memory.

        signed long long int : can store values from -9223372036854775808 to 9223372036854775807 .


        Syntax of the code :

        - Declaration with initialization: 
         long long int name = value ;

        - Declaration first, then assignment:
        long long name ;
        name = value ;

        example :


          Unsigned long long int → can store values from 0 to 18,446,744,073,709,551,615.


          Syntax of the code :

          - Declaration with initialization:
          unsigend long long name = value ;

          - Declaration first, then assignment:
          unsigned  long long name ;
          naname = value ;

          example :



            2 float and double :

            Float and double are data types in the C programming language used to store floating-point numbers (numbers with decimal points). 
            They are used when we need to represent fractional values instead of whole numbers.

            2.1) float :  


            This is the second part of the variable data types. It is mainly used to store floating-point numbers. 

            The float data type can store numbers with a range from 1.175494E−38 to 3.402823E+38, and it occupies 4 bytes in RAM.


            Syntax of the code :

            float name = value;     // declaration with initialization

            or

            float name;             // declaration
            name = value;           // assignment


            example :


              2.2) double :

                if we need to more precision  we use this type because  it have precision  value up to 15 and his range is 2.225074E-308 to 1.797693E+308 and take 8 byte on RAM .


              Syntax of the code :
              or 
              double name ;
              name = value ;

              double name = value ;
              example :


                2.3) long double :

                precision ois 18 number and range of 3.4E-4932 to 1.1E+4932 but it take 12 byte on RAM .

                Syntax of the code :
                long double  name = value ;
                or
                long name ;
                name = value ;
                example  :


                Data Types For Constant Values:


                In C languages constants are the fixed values (not change) that never change during the execution of a program . It can have any data type , and there are two ways to define constant which is : #define preprocessor and const keyword .

                1) #define :

                This directive does not store into RAM but it used to replace every  insurance of label with text . and this text does not change at all  .


                Syntax : 
                #define  label text 
                example :


                  Other example :


                    2) const :

                    This keyword is used to create constant  values . It work with all different primary data type , but it take as much space on mermory as any other variable .


                    Syntax :
                    const type name = value ;
                    example :


                    Note: the method of usage  of types or directives  on the memory of PC are not the same as when  we use it on Embedded  systems  . cause RAM of PC is very large than RAM of MCU , be carefull .
                    Tags

                    Post a Comment

                    0Comments
                    Post a Comment (0)

                    Cookies Consent

                    This website uses cookies to offer you a better Browsing Experience. By using our website, You agree to the use of Cookies

                    Learn More