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:

  1. int
  2. real
  3. char
  4. 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”
            Example:
            int a =Declaration
            int a=10; =Initialization

 Rules for constructing integer constants:

  1. Integer constant must contain one digit
  2. It must not have a decimal digit or value
  3. It can be positive or negative; if no sign mentioned default considered as positive.
  4. No commas and blanks are allowed in between the digits of number
  5. The integer range can be stored is -2147483648 to +2147483647 or -32768 to +32767(depends on compilers)
  6. Valid integer constants:
    1. 400
    2. -555
    3. 3089
    4. -4096
  7. Invalid integer constants:
    1. 50.36
    2. 0,88
    3. 0.74
Rules for constructing Real constants:

  1. They are also called as floating point constants.
  2. The real constant must have at least one digit
  3. It must have decimal point, if decimal point is not there it will be considered as whole.
  4. Default sign is positive
  5. No commas and blanks are allowed
  6. Valid examples:
    1. +655.6
    2. 888.6
    3. -63.36
    4. -7089
  7. Invalid examples:
    1. -42,6
    2. -12  .0

Rules for constructing character constants:

  1. A character constant is an alphabet, digit or a special character enclosed in a single inverted comma (‘ ‘)
  2. Character constants are also known as “One-byte integers”
  3. 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.
  4. Char is divided into signed and unsigned
  5. 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:
    1. 'a'
    2. 'J'
    3. '8'
    4. '!'
Invalid examples:
    1. "Z"
    2. 'ABCd'
    3. "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.

Comments

Popular posts from this blog

Basic Structure of C Program