1. Write a logic in C language to find whether the given system is Little endian or Big endian
#include <stdio.h>
int main()
{
int a = 0x1;
char *ptr = (char *)&a;
if (*ptr == 1)
printf("the system is little endian\n");
else
printf("The system is big endian\n");
}
Explaination
As discussed in the Endianness chapter, if the system is little endian, the value 1 is stored at the lower memory address of a variable. In a big endian system, the value 1 is stored at the higher memory address. When a variable’s address is assigned to a pointer, the pointer holds the lower memory address of the variable. By dereferencing the pointer as a character pointer, we can access the value stored at the lower memory address of the variable.
2. Convert the data in a given variable to big endian.
#include <stdio.h>
int to_be32(int num)
{
int tmp = 0;
tmp = (num << 24) & 0xff000000;
tmp |= (num << 8) & 0x00ff0000;
tmp |= (num >> 8) & 0x0000ff00;
tmp |= (num >> 24) & 0x000000ff;
return tmp;
}
int main()
{
int a = 0x1, num;
char *ptr = (char *)&a;
printf("Enter value to convert into big endian:");
scanf("%i", &num);
if (*ptr == 1) {
num = to_be32(num);
printf("The value in big endian is: 0x%x\n", num);
} else
printf("The system is big endian no need for conversion\n");
}
Explaination
Before converting into big endian, we have to find what is the endianess of the system. For this we used program 1 to find out. If the system is little endian then conversion is needed. The function to_be32() converts given 32 bit value into big endian format and returns the converted value.
5. Convert binary input to its respective decimal value:
Sample input: 101
output: 5
#include <stdio.h>
#include <string.h>
/*
* powof - function to calculate x power y
*/
int powof(int x, int y)
{
int pow = 1;
if (y == 0)
return 1;
while (--y)
pow *= x;
return pow;
}
int main()
{
char str[32];
int i;
int val = 0;
printf("Enter a binary value:");
scanf("%s", str);
for (i = strlen(str) - 1; i >= 0; i--) {
if ((str[i] != '1') && (str[i] != '0')) {
printf("invalid binary input\n");
return 0;
}
if (str[i] == '1')
val += powof(2, strlen(str) - i);
}
printf("val = %d\n", val);
}
input:
1011001
output:
89
6. Convert a given Decimal input to its respective Binary representation:
Sample input: 8
output: 1000
#include <stdio.h>
int main()
{
int i;
int val = 0;
printf("Enter a decimal value:");
scanf("%d", &val);
for (i = 31; i >= 0; i--)
(val >> i & 0x1) ? printf("1") : printf("0");
}
input:
42
output:
00000000000000000000000000101010
7. Swap value in two variables without using temporary variable
#include <stdio.h>
int main()
{
int x = 10, y = 40;
printf("x=%d, y=%d\n", x, y);
x = x + y;
y = x - y;
x = x - y;
printf("x=%d, y=%d\n", x, y);
}
output:
x=10, y=40
x=40, y=10
8. Check the given number is palindrome or not.
A number is said to be a palindrome if, when the number is reversed, it remains the same as the original number.
sample input1 : 34543
output: The given number is palindrome
sample input2 : 6777
output: The given number is not a palindrome
#include <stdio.h>
int main()
{
int num, tmp;
int val = 0;
printf("Enter a number:");
scanf("%d", &num);
tmp = num;
while(tmp) {
val = val * 10 + tmp % 10;
tmp /= 10;
}
printf("val=%d\n", val);
if (val == num)
printf("the given number is palindrome\n");
else
printf("the given number is not a palindrome\n");
}
input:
121
output:
The given number is palindrome.
C programs – Part 2 (bitwise operations)
C programs – Part 3 (arrays)
C programs – Part 4 (strings)