Saturday, May 7, 2016

Basics of C Programming - IV ( Data types,Format specifiers and Operators )


As it is assumed that the readers do have basic idea about any of the Programming languages, they must be aware of “Data types”. Data types in C are no less than those in other programming languages . For the sake of completion, we will still define it.

Data types are those entities in C which define the identity of a variable. The variables are defined by their data types. For example-

int sum; ==> sum is a variable which can store only integer value because it has been defined to be of “int” data type
Similarly , other data types are “float”, ”double”, ”char”, etc.

Variables can be declared to be signed or unsigned. Signed variables may store negative values as well as positive values, whereas usigned variables will only store non-negative values.

unsigned int sum; ==>”sum” can store only zero and positive integer values.

When a variable is declared as :-

int sum;

By default, it is treated as a signed variable. So, to declare a variable as “signed”, we need not explicitly mention this as we do while declaring an “unsigned” variable.

Size of different data types in GCC.

In a GCC environment, the size of different data types are as follows:-

Data types                   Size(in Bytes)

char                                        1

short                                       2

int                                           4

long                                        4

float                                       4

double                                    8

One can themselves check the size using sizeof operator. For example,

printf(“size of int, char and float variables are %d ,%d and %d respectively”, sizeof(int),sizeof(char),sizeof(float));

<<Please note that “short” and “long” do not fall into the category of data types. They are rather termed as modifiers. They modify the range of “int” variables when used along with them. Similarly, “signed” and “unsigned” also fall into the category of modifiers.>>

Range of data types

Each and every variable defined/declared has a maximum and minum limit of holding values, which is known as range.

  • For signed integer data types, viz., char,int,short,long, the range is :-

       -2^(n-1) to 2^(n-1) – 1, where n represents the bit size of the data type.

  • For unsigned integer data types,viz., unsigned char, unsigned int, etc., the range is :-

       0 to 2^(n) – 1, where n represents the bit size of the data type.

  • For example, in case of signed int, the range will be :-

       -2^(32-1) to 2^(32-1)-1 (int is 4 bytes =32 bits)

  • In case of signed char , it is :-

       -2^(8-1) to 2^(8-1)-1= -128 to 127

Format Specifiers- Format specifiers are those entities in C, which let you choose the format in which you want the input to be given to the program and also the format in which you want the output to be printed on the screen or to be copied to a buffer.

Various format specifiers in C are %d,%c,%s,%u,%h,%o,%ld,%lu, %i, %p

Format specifiers are used with the following functions:-

printf, fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf, vsnprintf

scanf, fscanf, sscanf, vscanf, vsscanf, vfscanf

.
.
printf(“Enter the character to be inputted\n”);
scanf(“%c”,&inp);
.
.
Here, the function scanf() will accept input value considering inp as character type.

.
.
int a=10.5;
printf(“The number is %f”,a);
.
.
Here, the function printf() will print the value considering the variable a as float data type.

To know in detail about each of the format specifiers and what they represent , refer to :-
man printf
Conversions section
man scanf
Conversions section

<<By reading manual pages, you are actually making yourself stronger . Rather than looking out for answers on other places around the world, it's always better to look at the man pages since they are the ultimate source of information and are never wrong.
TRUST MAN PAGES MORE THAN ANY OTHER SOURCE OF INFORMATION.>>



Operators-Operators are those entities which define the kind of operations to be performed on the constant(s) or variable(s(, which are referred to as operand(s) in this context. An operator can be unary which requires only one operand or it can be binary which requires 2 operands for the operation to be performed.

  • Arithmetic Operators- + , - , * , / , % ( Results are arithmetic numbers )
  • Relational Operators- < , > , == , != , <= , >=
  • Assignment Operators- = , += , -= , *= , /= , %= , &= , |= , ^= , ~= , <<= , >>=
  • Increment/decrement Operators- ++ , --
  • Logical Operators- &&, || , ! ( Results are either 0 or 1 )
  • Bitwise Operators- & , | , ^ , ~ , << , >> (Operation on the 2 operands bit by bit. Results are arithmetic numbers)
  • Conditional Operator( Ternary Operator) - ?:
  • Special Operators- comma , sizeof, [], &(address of), *(dereference) , (), . , ->


There is no need to explicitly show the usage of these operators in a Program. The usage of these operators will be implicit in the codes in the coming sections.
However, some special properties of a few operators are worth mentioning.

Logical Operators' Short-Circuit property

void main()
{
int a=5,b=10,c=-1,d=20,w=0,x,=0,y=0,z=0;
w=++a && ++b; //operation1
x=++c && ++d; //operation2
a=5;
b=10;
c=-1;
d=20;
y=++a || ++b; //operation3
z=++c || ++d; //operation4
}

After operation 1
a=6
b=11
Because a=6 is true and the operation is && so the final output will definitely depend on the other value.
After operation 2
c=0
d=20
Because when c becomes 0 , the expression becomes false due to &&. 0 && anything is false. So, the second expression need not be evaluated at all and hence is not evaluated.
After operation 3
a=6
b=10
Because a=6 ORRED with any number is True, since 6 is a positive number. 1 || anything is true.So, the second expression is not evaluated at all.
After operation 4
c=0
d=21
Because 0 || anything = may be true or false, as it depends on the second number . Hence , the second expression is also evaluated.



Comma Operator
Bitwise Operation
Shift Operation


------To be updated later------


Endianness,Enumerators,Structures and Unions to be included in a separate tutorial.

No comments:

Post a Comment