Data Type in 'C'

Father of C                                         - Dennis Ritchie
First Computer Programmer             - Ada
Cut                                                      - shift + del
Paste                                                   - shift + insert
Copy                                                    - ctrl + insert
Clear                                                   - ctrl + del
Compile                                              - alt + F9
Run                                                     - ctrl + F9
To see the output                                - alt + F5        

1.       Data Type in C

Data Type                              Meaning                                             Size (in bytes)
char(Range -128 to +127)       a character                                           1
int(Range -32768 to +32767) an integer                                              2
float                                         a single precision real number               4
double                                     a double precision real number             8
void                                         valueless                                              0
(The void data type is not  used as often as the above four.)

                                                C Data Types
                                                     |
                        |--------------------------------------------|
            Primary Data Types             Secondary Data Types
            1. Character                                         1. Array
              i. Unsigned Char
              ii. Signed Char
            2. Integer                                             2. Pointer
              i. Short
              ii. Long
            3. Float                                                3. Structure
            4. Double                                            4. Union
            5. Void                                                5. Enum etc.

Integers
- Integers data type can be categorised into two parts;Long and short.
- Long integers require twice the space in memory than ordinary integers do.
- Therefore long integers occupy four bytes of memory.
- The value of a long integer can vary form -2147483648 to +2147483647.
- Long integers declared using the keywords long. For example:
            long int i;
- Short integers require less space in memory and thus speed up the program execution.
- Short integer is nothing but our ordinary int.
- Short integers are declared using keywords int. For example:
            int i;
- If we add the suffix 'L' or 'l' at the end of the number, then it would be becoming as a long integer and hence occupy 4 bytes.

Constants and Variables
- A constant is a quantity that does not change. This quantity can be stored at a location in the memory of the computer.
- Real constants are often called Floating Point constants.
- The real constants could be written in two forms; Fractional (426.0, +325.34,-32.76) and Exponential (+3.2e-5, 4.1e8,-3.2e-5) Form.
- Range of real constant expressed in exponential form is -3.4e38 to 3.4e38.
- The part appearing before 'e' is called mantissa, whereas the part following 'e' is called exponential.
- In place of a small case 'e' a capital 'E' can also be used.

Rules for Constructing Variable Names
- A variable name is any combination of 1 to 8 alphabets, digits or underscores.
- Some compilers allow variables names whose length could be upto 40 characters. But it is recommended to stick on 8 characters.
- The first character in the variable name must be an alphabet.
- No commas, blanks or special character (except underscore) are allowed within a variable name.

Scope of Variables
- Variables can have two types of scopes: Local and Global.
- A variable with a global scope is accessible to all the statements in the program, whereas the one with local scope is available only to certain selected statements in the program.
- Global variables are declared outside all functions whereas the local variables are defined inside a function.

C Keywords
- Keywords also known as 'Reserved Word'.
- Keywords are the words whose meaning has already been explained to the C compiler.
- The keywords cannot be used as variable names.
- There are 32 keywords available in C, for example: break, case, else, char, if, for, void etc.

C Programming
- A comment can be enclosed within /*  */ (for multiple line comment) or // (for single line comment).

Printf(): It is a function which is used to print the value contained in a variable on the screen.
            Syntax: printf("<format string">,<list of variable>);
                        <format string> could be %c (for character), %d(for integer),%f(for float values).
            Example: printf("%d",n1);

/n: It is called a newline and takes the cursor to the next line.
            Example: printf("Average=%d\nSum=%d",avg,sum);
           

Scanf(): It is used for supplying the values in the variables.
            Syntax: scanf("<format string">,&<list of variable>);
            Example: scanf("%d",&n1);

Integers, Signed & Unsigned

When we know in advance that the stored value in a given integer variable will always be positive, then we can declare the variable to be unsigned, as in;

            unsigned int car_park;

With such a declaration, the range of permissible integer values will shift from the range -32768 to +32767 to the range 0 to 65535. Thus declaring the integers as unsigned almost doubles the size of the largest possible value. Unsigned integer occupies two bytes. The following unsigned declarations are same;
            short unsigned int i;
unsigned int i;
unsigned i;

The way there exists a short unsigned int, there also exists a long unsigned int which has a range of 0 to 4294967295 and occupies four bytes of memory.

By default a short int is a signed short int and a long int is a signed long int.

Char, Signed & Unsigned

Signed and unsigned chars, both occupying one byte each, but having different range.

A signed character is same as our ordinary char and has a range from -128 to +127; whereas an unsigned char has a range from 0 to 255.

#include<stdio.h>
void main
{
            char ch=291;
            printf(“%d%c\n”,ch,ch);
}

Output is
            35        #

Since variable ch has defined as char, therefore it cannot take a value bigger than +128. But when value exceeds +127, an appropriate value from the other side of the range is picked up and stored in ch. In this case, value is 35 and its corresponding character is # get printed.

Program1:

#include<stdio.h>
void main()
{
            char c;
            unsigned char d;
            int i;
            unsigned int j;
            long int k;
            unsigned long int m;
            float x;
            double y;

            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”,&x,&y);
            printf(“%f%lf”,x,y);
getch();
}

Post a Comment

Previous Post Next Post