C supports two categories of data types.
1) Primary Data Types
2) User defined data types in C
1) Primary Data Types
a. Integer (int)
- This holds only whole number values.
- int type consumes 2 bytes in memory.
- Its format specifier is %d for signed integers and %u for unsigned integers.
- The valid range is
1. Signed -32768 to +32767
2. Unsigned 0 to 65535 - Example:
int rollno, marks;
int rate, qty;
int cust_ID;
int sum=0; - The modifier signed, unsigned, short and long can be applied to int.
- A type modifier alters the meaning of the base type to yield a new types.
b. Real (float)
- This holds number values with decimal places.
- float type consumes 4 bytes in memory.
- Its format specifier is %f
- The valid range for floating point number is 3.4E-38 to 3.4E+38
- Example:
float percentage;
float bill_amt;
c. Double (double)
- This holds number values with decimal places.
- double type consumes 8 bytes in memory.
- Its format specifier is %f
- The valid range for double is 1.7E-308 to 1.7E+308
- Example:
float percentage;
float bill_amt; - A type modifier long can be applied to double
- The valid range for long double number is 3.4E-4932 to 3.4E+4932
d. Character (char)
- This holds only character values.
- char type consumes single byte in memory.
- Its format specifier is %c
- The valid range is
1. Signed -128 to +127
2. Unsigned 0 to 255 - Example:
char status=’T’;
User defined data types in C / Secondary data types / User derived data types in C
- The user defined data types in C is also called as user derived data types or secondary data types.
- These are formed by combination of primary data type values
- As per google, data types constitute the semantic and characteristics of storage of data elements.
- The user defined data types in C or Secondary data types in C programming language are:
1. Array
2. Structure
3. Union
4. Enumeration
5. Pointer - The summary of format specifier is tabulated below:
Data Type | Format |
Signed character | %c |
Unsigned character | %c |
Character | %c |
Short signed decimal integer | %d , %i |
Octal | %o |
Hexadecimal | %x , %X |
Exponent format | %g , %G |
Short unsigned integer | %u |
Integer | %d |
Long integer | %ld |
Float | %f |
Double | %f |
Long double | %lf |
Always begin with + or – | + |
Left justify | – |
Operators with example
- Operators are used with operands in expression and assignment statement.
- According to number of operands , operators are
1) Unary operators (one operand)
-> ! ~ ++
— + – * &
2) Binary operators (two operands)
+ – * / %
3) Ternary operators (three operands)
? : - As per the type of application, operators are
1) Arithmetic operation
+ – * / %
2) Relational operation
< <= > >= == !=
3) Logical operators
&& || !
4) Bitwise operators
& |
5) Assignment operators
= += -= *= /=
%= <<= >>= |=
6) Shift operators
<< >> - Following examples explain the use of some operators.
Example | Meaning |
c=a+b; | value of addition (+) of a,b is assigned (=) to c |
p=&q; | address of (&) p is assigned to pointer variable p |
a<b && a<c | return non-zero if a is less than (<) b and (&&) a is less than c |
r=a%b; | returns reminder i.e. modules(%) after division of a, b |
a==b | compares whether a and b are equal (==). Returns true(1) if a and b are equal otherwise false (0) |
a=b*c | product(*) of b, c is assigned (=) to a |
*a=*b; | value at (*) b is assigned (=) to value at (*) a |
a+=b | this is same as a=a+b; value of variable a is incremented by b i.e. value of a+b is assigned to variable a |
a=20; b=a<<1; c=a<<2; d=a>>1; | here binary equivalent of a is 10100 left shift operator shifts all 1 to left by specified number of places. And similarly the right shift operator. b=a<<1=>101000=>b=40 c=a<<2=>1010000=>c=80 d=a>>1=>1010=>d=5 this indicates that left shift double the number and right shift half the number. |
a>>=2; | this is same as a=a>>2; right shift (>>) a by 2 and assign evaluated value to variable a |
Precedence and associativity of operators in C
Following table gives the rule for priority and associativity(order of evaluation) of operators in an expression.
Operators | Meaning |
( ) | Function call |
[ ] | Array |
-> | Pointers to structure operator |
! | Negation |
~ | Ones complement |
++ | Increment |
— | Decrement |
+ | Unary plus |
– | Unary minus |
* | Value at |
& | Address of |
(type) | Type cast |
sizeof | Size of |
* | Multiplication |
/ | Division |
% | Modules |
+ | Addition |
– | Subtraction |
<< | Left shift |
>> | Right shift |
< | Less than |
<= | Less than or equal to |
> | Greater than |
>= | Greater than or equal to |
== | Equal to |
!= | Not equal to |
& | Bitwise AND |
| | Bitwise OR |
&& | Logical AND |
|| | Logical OR |
? : | Ternary / Conditional |
= | Assign to |
+= | Add & assign to |
-= | Subtract & assign to |
*= | Multiply & assign to |
/= | Divide & assign to |
%= | Modulo & assign to |
&= | AND & assign to |
|= | OR & assign to |
, | Comma |