Built-in Data Types: In 'C' Language


Built-in Data Types:

In this tutorial, you will learn about data types and how to declare a variable in C programming.


In C programming, variables or memory locations should be declared before it can be used. Similarly, a function also needs to be declared before use.

Data types simply refers to the type and size of data associated with variables and functions.

Data types in C


  1. Fundamental Data Types
    • Integer types
    • Floating type
    • Character type
  2. Derived Data Types
    • Arrays
    • Pointers
    • Structures
    • Enumeration

This tutorial will focus on fundamental data types. To learn about derived data types, visit the corresponding tutorial.

int - Integer data types


Integers are whole numbers that can have both positive and negative values but no decimal values. Example: 0, -5, 10

In C programming, keyword int is used for declaring integer variable. For example:

int id;
Here, id is a variable of type integer.
You can declare multiple variable at once in C programming. For example:
int id, age;
The size of int is either 2 bytes(In older PC's) or 4 bytes. If you consider an integer having size of 4 byte( equal to 32 bits), it can take 232 distinct states as: -231,-231+1, ...,-2, -1, 0, 1, 2, ..., 231-2, 231-1. If you try to store larger number than 231-1, i.e,+2147483647 and smaller number than -231, i.e, -2147483648, program will not run correctly.
Similarly, int of 2 bytes, it can take 216 distinct states from -215 to 215-1.

float - Floating types

Floating type variables can hold real numbers such as: 2.34, -9.382, 5.0 etc. You can declare a floating point variable in C by using either float or double keyword. For example:
float accountBalance;
double bookPrice;

Here, both accountBalance and bookPrice are floating type variables.
In C, floating values can be represented in exponential form as well. For example:
float normalizationFactor = 22.442e2;

Difference between float and double

The size of float (single precision float data type) is 4 bytes. And the size of double (double precision float data type) is 8 bytes. Floating point variables has a precision of 6 digits whereas the precision of double is 14 digits.

char - Character types

Keyword char is used for declaring character type variables. For example:
char test = 'h';
Here, test is a character variable. The value of test is 'h'.
The size of character variable is 1 byte.


WAP to swap the values of two variables without using third variable.

Let's see a simple c example to swap two numbers without using third variable.
  1. #include<stdio.h>  
  2.  int main()    
  3. {    
  4. int a=10, b=20;      
  5. printf("Before swap a=%d b=%d",a,b);      
  6. a=a+b;//a=30 (10+20)    
  7. b=a-b;//b=10 (30-20)    
  8. a=a-b;//a=20 (30-10)    
  9. printf("\nAfter swap a=%d b=%d",a,b);    
  10. return 0;  
  11. }   

Output:
Before swap a=10 b=20
After swap a=20 b=10


Next

Comments

Popular posts from this blog

Variables,Operators and Expressions in 'C' Language

For Loop in C