| Data type | Keyword | Size (in Bytes) | Range |
|---|---|---|---|
| Character | char | 1 | |
| Integer | int | 2 | -32768 to +32767 |
| Single-precision floating point | float | 4 | 10-38 to 10+38 |
| Double-precision floating point | double | 8 |
Qualifiers can also be applied to these data types.
signed and unsigned applies to int and char data types.
int will be automatically assumed for unspecified.
Examples -
signed s;
unsigned u;
long l;
long long ll;
short int n;
long int m;
//the word int can be omitted -
short n;
long m;
A compiler is free to choose any size for these.
RULE - short and int must be atleast 16 bits, long must be atleast 32 bits, and short must not be larger than int whch must not be larger than long.
unsigned variables store zero and positive values only. signed stores both. Ex - if char is 8 bits, unsigned will store from 0 to 255 and signed from -128 to 127.
int will be taken as longdouble unless suffixed with f/F (float) or l/L (long double)Octal and Hex constants may be followed by U or L suffix to make them unsigned or long respectively.
0xFUL // this is a unsigned long hex with value 15 in decimal
'\0' has an ASCII value of 0.
'0' has an ASCII value of 48 which is unrelated to zero.
'\013' - Octal Number
'\xAF' - Hex Number
| Escape Sequence | Function |
|---|---|
| \n | Newline character |
| \t | Horizontal tab character |
| \v | Vertical tab character |
| \b | Backspace character |
| \f | Formfeed |
| \a | Alert (bell) character |
| \r | Carrige return |
| ' | Single quote |
| " | Double quotes |
| \\ | Backslash itself |
| ? | Question mark |
| \0oo | Octal number |
| \xhh | Hex number |
A character constant 'a' is not the same as "a", the former has an integer value internally and the latter is delimited by a null character ('\0') character.
#define MAX
int p = MAX + 1;
//These are evaluated at the compile-time.
#include<stdio.h>
int main()
{
enum my {hs, ho}; //By default, first name in an enum has value = 0, second one has value = 1 and so on...
printf("%d\t", he);
printf("%d", ho);
return 0;
}
OUTPUT: 0 1
If not all values are specified, unspecified values continue the progression from the last specified value.
#include<stdio.h>
int main()
{
enum my {JAN = 23, FEB, MAR, APR, MAY};
printf("%d\t", JAN);
printf("%d\t", FEB);
printf("%d\t", MAR);
printf("%d\t", APR);
printf("%d\t", MAY);
return 0;
}
OUTPUT: 23 23 25 26 27
Names in an enumeration must be distinct, values may not be so.
const - It is used to indicate that the variable’s value cannot be changed now.const int p = 5;
//OR
int func(const ch[]); //ch's values cannot be changed by func()
| Binary Arithmetic Operators | Unary Arithmetic Operators |
|---|---|
| + | + |
| - | - |
| * | |
| / | |
| % |
float or double.| Relational Operators |
|---|
| > |
| < |
| >= |
| <= |
| == |
| != |
i < lim-1 is taken as i < (lim-1).!= is higher than assignment =.| Logical Operators |
|---|
| ! |
| && |
| |\ |
if(!valid) //if not valid
//OR
if(valid == 0) //if valid is false
When taking place implicitly, “narrower” type to a “wider” type is done. Ex - float to int.
signed or unsigned if non-character data is to be stored in char variables.unsigned oprands involved, and either oprand is long, float, double, or long double, other is converted to the same type. And short and char are converted to int.unsigned operands are involved comparison between signed and unsigned operands is machine-dependent.Cast Operators -
(type-name) expression
Ex -
(float) 4/2;
(unsigned int) 4.3 - 2;
i++ is equivalent to i = i + 1, and i-- is quivalent to i = i - 1.++i and Postfix - i++.++i increments the value of i and then uses it, and i++ increments the value before using it.(i + j)++ is illegal.| Bitwise Operators |
|---|
| & |
| \ |
| ^ |
| « |
| » |
| ~ |
x << 2 //shifts left by two positions (equivalent to multiplying by 4)
unsigned bit fills vacated bits with 0-bits.signed bit fills vacated bits with sign bit “arithmetic shift”, or with 0-bits “logical shift”.#include<stdio.h>
int main()
{
int p = 3;
printf("%d\n", ~p);
return 0;
}
OUTPUT: -4
i = i + 2;
//can be written as
i += 2; //+= is an assignment operator
#include<stdio.h>
int main()
{
char p = 'a';
int x;
p += x;
printf("%d\n", sizeof(p));
return 0;
}
OUTPUT: 1
a > b? max=a : max=b;
//can also be written as -
max = a > b? a : b;
IMPORTANT NOTE - The conditional expression is indeed and expression. Type conversion rules apply here.
// assume f is float and n is int
(n > 0)? f : n;
//result will be float no matter if the condition is true or not.
== and != operators.//Be careful in -
if((x & MASK) == 0) ...
x = f() + g(); //not sure which ammong f() or g() is evaluated first
//AND
printf("%sd %d\n", ++n, power(2, n)); //Different compiler, different results, based on what is evaluated first
a[i] = i++; //Subscript is the old value of i or new?
//Ans - Compilers can interpret it in different ways and generate different answers based on the interpretation.