Different Data Types in ‘C’

Embed Size (px)

Citation preview

  • 8/6/2019 Different Data Types in C

    1/20

    Submitted by :Manish Mann

    ECE/09/128

  • 8/6/2019 Different Data Types in C

    2/20

    ` Data Type: set of values together with a set of

    operations is called a data type

    ` Data types are used to store various types of data

    that is processed by program.

    ` Data type attaches with variable to determine the

    number of bytes to be allocate to variable and

    valid operations which can be performed on thatvariable.

  • 8/6/2019 Different Data Types in C

    3/20

    ` C data can be classified into four categories:

    Fundamental data types

    Data type modifiers

    Derived data types

    UserDefined data types

  • 8/6/2019 Different Data Types in C

    4/20

    ` Fundamental data types are those that are notcomposed of any other data types.

    Five types of fundamental data types are:-

    1. int data type (for integers):

    Examples: 0

    -6728

    91

    No commas are used within an integer.Size: 2 bytes

    Range: -32768 to 32767

  • 8/6/2019 Different Data Types in C

    5/20

    2. char data type (forcharacters):

    The smallest integral data type.

    Used for characters: letters, digits, and special symbols.

    Each character is enclosed in single quotes.

    Examples: 'A,'a', '0', '*', '+', '$', '&.A blank space is a character and is written ' ', with a space.

    Size : 1 byte

    Range : -128 to 127

    3. float data type (for floating point numbers):It represents any real number.Size : 4 bytesRange: 3.4 * 10-38 to 3.4 * 1038 -1

  • 8/6/2019 Different Data Types in C

    6/20

    4. double data type (for double precision floatingpoint

    numbers):

    it is used to define BIG floating point numbers.

    It reserves twice the storage for the number.

    Size : 8 bytesRange : 3.4 * 10-4932 to 3.4 * 104932 -1

    5. void data type :

    It specifies the empty set of values.

    It is used for the functions that doesn't return value.

  • 8/6/2019 Different Data Types in C

    7/20

    ` The data types explained above have the followingmodifiers.

    1. short

    2. long

    3. signed

    4. unsigned

    ` The modifiers define the amount of storage allocated tothe variable. The amount of storage allocated is not cast instone. ANSI has the following rules:

    o short int

  • 8/6/2019 Different Data Types in C

    8/20

    Types Size (in bytes) Range

    short int 2 -32,768 -> +32,767

    unsigned sh

    ort int 2 0 -> +65,535unsigned int 4 0 -> +4,294,967,295

    long int 4 -2,147,483,648 -> +2,147,483,647

    signed int 2 same as int

    signedchar 1 -128 -> +127

    unsignedchar 1 0 -> +255float 4

    double 8

    long double 12

  • 8/6/2019 Different Data Types in C

    9/20

    ` The Data types that are derived from the

    fundamental data types.

    ` The types of derived data types are:

    1. Arrays

    2. Functions

    3. Pointers

    4. References

    5. Constants

  • 8/6/2019 Different Data Types in C

    10/20

    ` Arrays are used in C to represent structures of

    consecutive elements of the same type. The definition

    of a (fixed-size) array has the following syntax:

    int array[100];

    which defines an array named array to hold 100

    values of theprimitive type int.

    ` If declared within a function, the array dimension may

    also be a non-constant expression, in which case

    memory for the specified number of elements will be

    allocated.

  • 8/6/2019 Different Data Types in C

    11/20

    ` A function is a named part of a program that can be invoked fromother parts of the programs as often needed.

    ` Syntax of function:

    type fuction-name(parameter list)

    {.

    body of function

    }

    ` A function must have a return statement.

    ` A function can be invoked by two manners:

    1.Call by value: It copies the values of actual parameters and

    creates its own copy of arguments and uses them.2.Call by reference : It doesnt create a copy and directly refers

    to the original parameters only different name i.e references.

  • 8/6/2019 Different Data Types in C

    12/20

    ` In declarations , the asterisk modifier (*) specifies apointer type.

    ` For example, where the specifier int would refer to theinteger type, the specifier int * refers to the type

    "pointer to integer".` The following line of code declares a pointer-to-integer

    variable called ptr:

    int *ptr;

    ` Pointer values associate two pieces of information:1. Memory address

    2. Data type

  • 8/6/2019 Different Data Types in C

    13/20

    ` A reference is an alternative name for a object.

    ` A reference variable provides an alias for a previously

    defined variable.

    ` Its declaration consist of a

    base type, an

    &

    (ampersand) and a reference variable name.

    ` In the following example, ptr is set so that it points to

    the data associated with the variable a:

    int *ptr;

    int a;

    ptr = &a;

  • 8/6/2019 Different Data Types in C

    14/20

    ` The keyword const can be added to the

    declaration of an object to make that object a

    constant rather than a variable.

    ` The value of the named constant cannot bealtered during program run.

    ` Syntax:

    const type name = value;

    ` So it must be initialized at the time of declaration.

  • 8/6/2019 Different Data Types in C

    15/20

    ` The UserDefined data types are that derived data

    types which are defined by the user

    ` The types of user defined derived data types are :

    1. structure

    2. union

    3. enumeration

  • 8/6/2019 Different Data Types in C

    16/20

    ` A structure is a variables referenced under one name,providing a convinient means of keeping related informantiontogether.

    ` The variables that make up structure are called structureelements.

    ` Keyword struct is used to construct a structure.

    ` Syntax :

    struct name{

    structure elements;

    };` A structure is diff. from an array as, array is the collection of

    similar data types where as structure is collection of diff. datatypes.

  • 8/6/2019 Different Data Types in C

    17/20

    ` Unions in C are related to structures and are defined

    as objects that may hold (at different times) objects of

    different types and sizes.

    ` They are analogous to variant records in other.

    ` Unlike structures, the components of a union all refer

    to the same location in memory programming

    languages.

    ` The size of a union is equal to the size of its largest

    component type.

    ` Keyword union is used to declare and create a union.

  • 8/6/2019 Different Data Types in C

    18/20

    ` Used to create a group of values which are named constants in C,

    C++, C#, Java, etc.

    ` Examples in C/C++/C#:

    enum days {Mon, Tue, Wed, Thu, Fri, Sat, Sun}; //Mon = 0, Tues = 1, .

    . ., etc.

    days today, yesterday = Wed;

    today = yesterday + 1;

    ` The first constant in an enumeration is assigned the value zero, and

    each subsequent value is incremented by one over the previous

    constant.

    ` Specific values may also be assigned to constants in the

    declaration, and any subsequent constants without specific values

    will be given incremented values from that point onward.

  • 8/6/2019 Different Data Types in C

    19/20

    ` The reason for providing so many data types is to

    allow programmer to take advantage of hardware

    characteristics.

    ` Helps in avoiding wastage in memory.

    ` Helps in avoiding wastege of time.

  • 8/6/2019 Different Data Types in C

    20/20