top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is a cyclic property of data type in c? Explain with any example.

+1 vote
3,302 views
What is a cyclic property of data type in c? Explain with any example.
posted Jun 3, 2015 by Mohammed Hussain

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+2 votes

In C for some data types, when we assign a value beyond range of that data type then it won't give any compiler error but assign a number according to some cyclic order. This is known as cyclic nature of data type.
only
char
int
long int
supports cyclic nature.

For example lets see how it happens in char both signed and unsigned.
Range of unsigned char is 0 to 255
0 1 2 3 4 ...... .... .... 255
----------------------------> (clock wise, i.e after 255 it will be 0, 1 and so on)
<--------------------------- (anti clock wire i.e after 0 it will be 255 , 254 and so on)
So if we assign the a value greater than 255 then value of variable will be changed to a value if we will move clockwise direction
If number is less than 0 then move in anti clockwise direction.

You can use this formula to validate:

If number is X and its greater than 255 then
New value = X % 256
If number is Y and its less than 0 then
New value = 256 – (Y% 256)

For sign char its range is -128 to 127

 If number is X and  its greater than 127 then
     p = X % 256
        if p <=127
        New value = p
 else
     New value = p – 256
 If number is Y and its less than -128 then
    p = Y % 256
       If p <= 127
       New value = -p
else
       New value = 256 -p 
answer Jun 4, 2015 by Arshad Khan
0 votes
#include<stdio.h>
int main(){
    signed char c1=130;
    signed char c2=-130;
    printf("%d  %d",c1,c2);
    return 0;
}

Output: -126 126 (why?)

This situation is known as overflow of signed char.
Range of unsigned char is -128 to 127. If we will assign a value greater than 127 then value of variable will be changed to a value if we will move clockwise direction as shown in the figure according to number. If we will assign a number which is less than -128 then we have to move in anti-clockwise direction.

answer Jun 8, 2015 by Manikandan J
Similar Questions
+4 votes

What is the point of declaring Pointer of different types (eg. integer,float,char) as we all know pointer takes 4 bytes of space regardless which type of pointer it is and only contains address?

+2 votes
void main(){
   int i=320;
   char *ptr=(char *)&i;
   printf("%d",*ptr); 
}

Please provide your explanation also?

...