3.5 – Integer Types in C language Part-3

by subbu on August 31, 2013

long int type in C language:

  • It is an integer type used to store integer constants of higher ranges.
  • It can be written as long int or long as short hand
  • It takes 4 bytes or 32 bits of memory.
  • Higher most bit is used to identify the sign and remaining 31 bits are used to store the actual value.
  • The range of long int is -231 to +231-1 (-2,147,483,648 to 2,147,483,647).
  • %ld is used as a formatting character in printf() and scanf().

range of long int in C language

#include<stdio.h>
int main()
{
int x=40000;
long int y=40000;
printf("x=%d",x);
printf("\ny=%ld",y);
return 0;
}

Output:
x= -25536
y= 40000

Here x is an int type variable initialized with 40000, but the output of which is -25536 because assigning more than its upper limit result overflows. Where as, long int variable y could store 40000.

Unsigned types in C language:

Signed integer types can store both negative and positive values. All the integer types learned so far like int, long int are by default signed types. The keyword signed can be used with the declaration statement to identify that, a variable can store both positive and negative values but using the keyword signed is optional.

signed int x;
signed long y;
signed char ch;

We can write the same code without using signed keyword to get the same effect

int x;
long y;
char ch;

Some times we know in advance that we are not going to store negative values. Say for example phone number, quantity and size of something may not have negative values. In such case we can define integer variables as unsigned types using the keyword unsigned to store only positive numbers. Now a question rises what we gain by defining a variable as unsigned type? The answer is that it just doubles the positive range.

Why the range of unsigned types is double to the signed types?

Generally the first bit of any integer type variable is used to identify the sign and remaining bits are used to store the actual value. When we define a variable as unsigned, even the first bit is used to store the value because no bit is needed to store the sign, which doubles the range.

int x;

Here x is signed int type of variable. Only 15 bits are used to store the value because the first bit is used to represent sign. The maximum value that it can store is 111 1111 1111 1111 (215-1 or 32767).

unsigned int y;

Here y is unsigned type of variable, Total 16 bits are used to store the value because no bit is needed to store sign. The maximum value that it can store is 1111 1111 1111 1111 (216-1 or 65535). It is just double to the signed int type.

Summarizing unsigned types:

  • Both int, long int can be defined as unsigned types using the keyword unsigned.
  • These take the same memory as int, long int.
  • Incase of unsigned types, even the higher bit is used to store the value so that the total memory is used to store the value.
  • These types can only store positive numbers.
  • The positive range of unsigned types is double to the signed types.
  • Range of unsigned int is 0 to 65,535 (216-1); unsigned long is 0 to 4,294,967,295 (232-1).
  • %u, %lu are used as formatting characters to represent unsigned int, unsigned long int in printf() and scanf().

Range of unsigned types in C language

#include<stdio.h>
int main()
{
int x;/*signed type*/
unsigned int y;/*unsigned type*/
unsigned int z;
unsigned int p;
x=40000;
y=40000;
z=-2;/*underflow in unsigned type*/
p=65538;/*overflow in unsigned type*/
printf("x=%d",x);
printf("\ny=%u",y);
printf("\nz=%u",z);
printf("\np=%u",p);
return 0;
}

Output:
x=-25536
y=40000
z=65534
p=2

Here x is a signed int type of variable assigning 40000 results overflow.
Range of unsigned type is 0 to 65535, assigning -2 to the variable z results underflow that wraps towards upper limit, assigning 65538 to the variable p results overflow that wraps towards lower limit.


char type in C language:

In previous session under computer memory and data representation, we have discoursed character representation in length. We have learned that a character stores in the memory in a binary format. Individual characters are represented with a unique binary combination. Decimal equal to that binary number is called its ASCII value.

  • char is the data type selects 1 byte or 8bits of memory to store a single character constant or small integer constants.
  • Higher bit is used to store the sign (0 for + and 1 for -) and remaining 7bits are used to store the value
  • It can be defined both as signed and unsigned type.
  • The signed type can store characters, whose ASCII values are in between-128 to 127
  • The unsigned type can store characters, whose ASCII values are in between 0 to 255.
  • %c is the formatting character for both signed char and unsigned char

Range of char type in C language

How a character is stored in char type:

When a character constant is assigned to char type variable then the first or higher most bit is stored with the sign that is 0 for positive and 1 for negative. Binary equal to the ASCII value of a character is stored in remaining 7 bits. Equal character is printed when %c is used as formatting character and equal ASCII value is printed when %d is used as formatting character.

Example:
When a character constant ‘a’ is assigned to the char type of variable ch, then the first bit is stored with 0 and remaining bits are stored with 1100001 because the ASCII value of ‘a’ is +97.
Character ‘a’ is printed when %c is used as a formatting character, ASCII value 97 is printed when %d is used a formatting character.

Storing a single character

#include<stdio.h>
int main()
{
char ch='a';
printf("%c",ch);
printf("\n%d",ch);
return 0;
}

Output:
a
97

Though the purpose of char type is to store single character constant, it can also be used to store smaller integer constants.

#include<stdio.h>
int main()
{
char ch='A';/*assigning 01000001 ASCII of 'A' is 65*/
char dh=97;/*assigning 01100001 ASCII of 'a' is 97*/
char eh=-125;/*assigning negative integer*/
unsigned char fh=291;/*overflowed unsigned char is 35, which is ASCII of #*/
printf("ch=%c",ch);
printf("\ndh=%c",dh);
printf("\neh=%d",eh);
printf("\nASCII of %c is %d",fh);
return 0;
}

Output:
ch=A
dh=a
eh=-125
ASCII of # is 35

How to rectify the problem in accepting two consecutive characters in C language:

There is a problem sighted while accepting two characters in a sequence from the keyboard, program accepts only one character and takes enter as the next character, which results defective in execution.
Specification: C program to find number of characters in between any two given characters

#include<stdio.h>
int main()
{
char ch1,ch2;
int d;
printf("Enter the first character:");
scanf("%c",&ch1);
printf("Enter the second character:");
scanf("%c",&ch2);
d=ch2-ch1;
printf("Number of characters in between %d",d-1);
return 0;
}

Execution:
Enter the first character:a
Enter the second character:Number of characters in between -88

fflush(stdin);

In the above example program execution is completed even before accepting the second character. By adding the statement fflush(stdin); as the next statement to the scanf(), we can rectify the problem.

#include<stdio.h>
int main()
{
char ch1,ch2;
int d;
printf("Enter the first character:");
scanf("%c",&ch1);
fflush(stdin);
printf("Enter the second character:");
scanf("%c",&ch2);
d=ch2-ch1;
printf("Number of characters in between %d",d-1);
return 0;
}

Execution:
Enter the first character:a
Enter the second character:f
Number of characters in between 4

Specification: C program to accept any character and print its ASCII value

#include<stdio.h>
int main()
{
char ch;
printf("Enter any character:");
scanf("%c",&ch);
printf("ASCII value of %c is %d",ch,ch);
return 0;
}

Execution:
Enter any character:D
ASCII value of D is 68

Specification: C program to accept any ASCII value and print its character

#include<stdio.h>
int main()
{
char ch;
printf("Enter an ASCII value:");
scanf("%d",&ch); /*assigning integer to char type*/
printf("equal character to ASCII %d is %c",ch,ch);
return 0;
}

Execution:
Enter an ASCII value:35
equal character to ASCII 35 is #

Previous post:

Next post: