Data Types in C
Introduction:
There are
two types of data: constants and variables.
Constants
is the data that cannot be changed once assigned; Variables data type is the
data which can e changed whenever required.
Data is of
four types:
- int
 - real
 - char
 - string
 
Syntax for declaring
variable:
Note:
- When we declare variable, memory will be allocated to the variable but it contains “garbage value” (in case of auto variable)
 - Default variables are signed.
 - If we declare it as well as initialize it, it is known as “Initialization”
 
            int a =Declaration
            int a=10; =Initialization
Rules for constructing integer constants:
- Integer constant must contain one digit
 - It must not have a decimal digit or value
 - It can be positive or negative; if no sign mentioned default considered as positive.
 - No commas and blanks are allowed in between the digits of number
 - The integer range can be stored is -2147483648 to +2147483647 or -32768 to +32767(depends on compilers)
 - Valid integer constants:
 - 400
 - -555
 - 3089
 - -4096
 
- Invalid integer constants:
 - 50.36
 - 0,88
 - 0.74
 
Rules for constructing Real constants:
- 400
 - -555
 - 3089
 - -4096
 
- 50.36
 - 0,88
 - 0.74
 
- They are also called as floating point constants.
 - The real constant must have at least one digit
 - It must have decimal point, if decimal point is not there it will be considered as whole.
 - Default sign is positive
 - No commas and blanks are allowed
 - Valid examples:
 - +655.6
 - 888.6
 - -63.36
 - -7089
 
- Invalid examples:
 - -42,6
 - -12  .0
 
- +655.6
 - 888.6
 - -63.36
 - -7089
 
- -42,6
 - -12 .0
 
Rules for constructing character constants:
- A character constant is an alphabet, digit or a special character enclosed in a single inverted comma (‘ ‘)
 - Character constants are also known as “One-byte integers”
 - Characters
are stored in ASCII standards in memory; format specifiers in printf() will inform
in which format it need to fetch from memory; we must use proper format
specifier to avoid unwanted results.
 - Char
is divided into signed and unsigned
 - When
you explicitly mention unsigned, then all the bits are data bits; otherwise
seventh bit is sign bit and remaining bits are data bits.
 
Valid examples:
- 'a'
 - 'J'
 - '8'
 - '!'
 
Invalid examples:
- "Z"
 - 'ABCd'
 - "C Language"
 
- 'a'
 - 'J'
 - '8'
 - '!'
 
- "Z"
 - 'ABCd'
 - "C Language"
 
Note:
There are three type of variables as shown
above i.e. integer, real(float), character
each type of variable can hold only the same
type of value in it; integer variable can hold only integer value, real or
float variable can hold only real values/floating values, character variables
can hold only character constants in it.
each type of variable can hold only the same type of value in it; integer variable can hold only integer value, real or float variable can hold only real values/floating values, character variables can hold only character constants in it.


Comments
Post a Comment