400+ Editable Festival Posters + Free Bonuses

PART 1 : Lesson 2 : Data Types In C





The first thing you need to know is that we can create variables to store values in. A variable is just a named area of storage that can hold a single value (numeric or character). We declare the name of each variable that we are going to use, and also its type.

      C language is rich in its data types.     
C supports three classes of data types:

1.      Primary data types  (Fundamental data-type)
2.      Derived data types
3.      User defined data types

                        All C’s variables must begin with a letter or a “_” (underscore) character.


integer (long & short):-

Integer constant range is -32768 to 32767. Integer constant always occupies two byte in memory. In two byte we cannot store a number bigger than 32767 and smaller than -32768. No fractional component is allowed.
Remember that out of two byte used to store an integer, the highest bit (16-bit) is used to store the sign of the integer. This bit is 1 if the number is negative and 0 if the number is positive. C offers a variation of the integer data-type that will provide long integer value. It is occupy four byte of memory.

Syntax:-          long int i;
                                                long int abc;



1. long integer cause the program to run a bit slower. The value of a long integer can vary from 2147483647 to -2147483648.
2. If there are such things as longs, symmetry required shorts as well integers which used less space in memory and thus help speed up program execution. It declared as: -


3.    We want to give it as much storages as a long. We add the suffix ‘L’ or ‘l’ at the end of the number. This 23 is an integer and would occupy two bytes, where as 23L is long integer and would occupy four bytes.

integer (signed and unsigned):- 
 
                        Unsigned integer still occupies two bytes.
  Unsigned int i;
  Unsigned i;

1. Long unsigned int which has a range of 0 to 4294967265 and occupies four bytes of memory.
2.  By default a short int is a signed short int and a long int is a signed long int.

char (signed and unsigned):- 

The way there is signed and unsigned int (either short or long), similarly there is signed and unsigned chars, both occupying one byte each, but having different range. A signed char is same as our ordinary char and has a range from -128 to 127, where as an unsigned char has a range from 0 to 255.

Example:-
                                                void main( )
                                                {
                                                            unsigned char ch;
                                                            for (ch=0; ch<=254; ch++)
                                                            {
                                                                        printf ("\n %d %c", ch, ch);
                                                            }
                                                            printf ("\n %d %c", ch, ch);
                                                }
float and double:-      

A float occupies four bytes in memory and range from -3.4e38 to 3.4e38. If this is insufficient then C offers a double data type which occupies 8 bytes in memory and has a range from -1.7e308 to 1.7e308. A variable of type double can be declared as: -


  •       Double a, population;

long double which can range from -1.7e4932 to 1.7e4932. A long double occupies 10 bytes in memory.

Example:-
                                                #include<stdio.h>
                                                #include<conio.h>

                                                void main( )
                                                {
                                                            char c;
                                                            unsigned char d;
                                                            int i;
                                                            unsigned int j;
                                                            long int k;
                                                            unsigned long int m;
                                                            float x;
                                                            double y;
                                                            long double z;
                                                           
                                                            clrscr( );
                                                           
                                                            scanf ("%c %c", &c, &d);
                                                            printf ("%c %c", c, d);
                       
                                                            scanf ("%d %u", &i, &j);
                                                            printf ("%d %u", i, j);

                                                            scanf ("%ld %lu", &k, &m);
                                                            printf ("%ld %lu", k,m);

                                                            scanf ("%f %lf %Lf", &x, &y, &z);
                                                            printf ("%f %lf %Lf", x, y, z);

                                                            getch( );
                                                }


Rules of Integer Constant:- (integer and long)

1.      It must have at least one digit.
2.      It must not have a decimal point.
3.      It must not have a fractional value.
4.      It could be either positive or negative.
5.      If no sign precedes an integer constant. It is assumed to be positive.
6.      No commas or blanks are allowed without an integer constant.
7.      The allowable range for integer constants is -32768 to 32767.

Rules of Real Constant:- (float and double)

1.      It must have at least one digit.
2.      It must have a decimal point.
3.      It could be either positive or negative.
4.      Default sign is positive.
5.      No commas or blanks are allowed within a real constant.

Rules of Character Constant:- (char only)

1.  A character constant is a single alphabet, a single digit or single special symbols enclosed within single inverted commas both the inverted commas should point to the left.

         Example:-   ’A’ is a valid character constant whereas ‘A’ is not.

The maximum length of a character constant can be one character.


Implicit Type Conversion:-

C permits mixing of constants and variables of different types in an expression. C automatically converts any intermediate values to the proper type so that the expression can evaluated without losing any significance. This automatic conversion is known as implicit type conversion.
Conversion Hierarchy Diagram
If the operands are of different types, the ‘lower’ type is automatically converted to the ‘higher’ type before the operation proceeds. The result is of the higher type.

Some instructions to be followed in type conversions:

1.      float to int causes type conversion then decimal part are lost.
2.      double to float causes rounding of digits.
3.      long int to int causes dropping of the excess higher order bits.

Type casting is the process of interpreting a variable or expression of one data type as if it would be of a different data type.
a.      Automatic type casting
b.      Explicit type casting

a)                  Automatic type casting:-

Whenever the compiler accepts data of a particular type, but the data is given as a different type, it will try to type cast automatically.

Example:-       int a=6.6;
                                                float b=9;

In the first case, an expression of type float is given and automatically interpreted as an integer. In the second case, an integer is given and automatically interpreted as a float.
Automatic type casting is of two types:-

1.      Promotion
2.      Demotion

1.      Promotion: -
                        They occur whenever a variable or expression of a smaller data type gets cast to a larger data type.
           
Example:-      float a=7;       (Here 7 is an int constant, gets promoted to float)
                                                long b=9;         (Here 9 is an int constant, gets promoted to long)       
                                                double d=a;    (Here a is a float, gets promoted to double)

                        There is generally no problem with automatic promotion.

2.      Demotion: -
                        They occur whenever a variable or expression of a larger data type gets cast to a smaller data type.

Example:-       int a=8.6; (Here 8.6 is an float, gets downcast to int)

Automatic demotion can result in the loss of information. In this example, the value of a will contain the 8, since int variable cannot handle floating point values.


b)                  Explicit type casting:-

There are cases where no automatic type casting can occur, where the compiler is unsure about what type to cast to, or other forms of type casting are needed.
           
Example:-       int a=int (8.5); (In this example they can be used to suppress the warning)
           
The value of an expression can be converted to a different data type if desired. The expression must be preceded by the name of the desired data type, enclosed in parenthesis.

Example:-       (int) 8.67                      // has a value of 8
                       
                       Casting has a very high-precedence, begin performed before the multiplication and division                           operations.






















1 comment:

  1. They learn up on different teams and gamers and attempt to make knowledgeable bets which might be} more likely to|prone to} repay. This extra analysis will get the individual placing the bet extra concerned in the sports activities betting hobby and extra involved in the sport. They develop a better appreciation and understanding of the sport, which permits them to gain even more enjoyment and leisure from the expertise. The cellular numbers in quantity of} states have been notably much less strong than in some of their friends. 복수자카지노 In Nevada’s case, this probably be} because of the of} magnetism of The Las Vegas Strip, however all three states require gamblers to register in-person at casinos to set up a cellular gaming account.

    ReplyDelete

Awesome Gadgets for Students and IT People