## Posts Tagged ‘**C**’

## Modulo operator in C

If you are programming in C, then you would know that the way to find the remainder when an integer **a** is divided by **n** is by using the modulo operator ( **%** ), like this, **r = a % n**. This modulo operator or fundamentally the task of finding the remainder when an integer is divided by another integer, can be used to find whether an integer is odd or even. If an integer leaves remainder 1 when divided by 2, it’s odd, else it’s even. Let’s implement a function in C to do the same.

/* * This function returns true if an integer is odd, else false */ bool is_odd(int a) { return a % 2 == 1; }

This is a pretty simple function and should help us in our quest. Let’s use this function to find whether the integers 1 and -1 are odd or not.

int main(int argc, char **argv) { if (is_odd(1)) printf("1 is odd"); if (is_odd(-1)) printf("-1 is odd"); return 0; }

Run this simple program on a Windows and a Linux machine and be startled. Instead of returning true for both values, the function ** is_odd** returns true for 1 and false for -1, though it’s common knowledge that -1 is odd. Why is it so ? This is due to the way the modulo operation is implemented in the C language. In fact, different languages implement the task of finding the remainder differently and a programmer should be careful when using the modulo operator or a function that does the same in any programming language. The implementation in C is known as the truncated division operation to find the remainder. This Wikipedia article describes the different ways the modulo operation can be implemented. It also shows a table specifying the behaviour of the modulo operation in different programming languages.

In C, the truncated division operation gives a remainder whose sign is the same as that of the dividend. Hence, in function is_odd, a % 2, for a = -1 returns a remainder of -1 and the subsequent comparison with 1 returns false. The safe way to find whether an integer is odd or even using the modulo operator in C is to do the below.

bool is_odd(int a) return a % 2 != 0; }

Thanks to this blog from my friend Shreevatsa which helped me look up the different ways of finding the remainder and their choice in a programming language like C or C++.

## What’s wrong with this piece of code ?

struct foo { int a, b; }; void kstrtol(long *x) { *x = -1; } int main(void) { struct foo foo; foo.b = 42; kstrtol((long *)&foo.a); return 0; }

## Application Of Logarithms To Exponential Values And Other Integers

We will be dealing with positive integers and logarithms to base 10.

How do you calculate the no of digits present in an exponential value or for that matter a very huge integer ? The answer is simple as shown below.

no of digits = floor(log (x ^ y)) + 1

**Explanation** : The logarithm of any number to base 10 is the number of times 10 is multiplied with itself to obtain that number. A little experimentation will show that when 10 is multiplied with itself x times or put another way, is raised to power x, we obtain an integer which contains x + 1 digits. Hence, obtaining the base 10 logarithm of any number and adding 1 to it will give you the number of digits in that number. Carefully note the floor function being used around the logarithm as logarithms generally give you floating point or decimal values.

Find the first n digits of an exponential value. Eg – Find first 5 digits of 12^13.

x = log 12^13 = 13 * log 12 = 14.0293561 y = floor(10^(x - floor(x) + n - 1)) = floor(10^(14.0293561 - 14 + 5 - 1)) = floor(10^(4.0293561)) = 10699 y is the first five digits of 12^13.

**Explanation**:

12^13 = 106993205379072.

log 12^13 = 14.0293561.

10^14.0293561 = (10^14)×(10^0.0293561) = (100000000000000)×(10^0.0293561) ≈ 106993205379072

The above argument shows that 10^0.0293561 when multiplied by 10^14 adds 06993205379072 to 100000000000000. In other words, it gives the first 15 digits of 12^13. Hence, if we want the first 5 digits of 12^13 multiply 10^0.0293561 with 10^4 which is also the same as raising 10 to power 4.0293561

**NOTE**: As n increases, the logarithm log x^y also needs to be calculated for more decimal places.

## Finding the offset of a field inside a structure in C

How do you find the offset of a field inside a structure from the beginning of the structure in C given only the structure name and the field name.

Below is the code.

#define offset_of(a, b) &(((a*)0)->b) struct mystruct { int a; long b; int c; }; void main() { printf("%llu\n", offset_of(struct mystruct, c)); } //output on a 64 bit machine 8

What we are doing here is typecasting 0 to be of type mystruct pointer which sets the beginning of the structure as 0 and then return the address of ‘c’ which should get us the offset. This is possible because of the ability to typecast variables to required types which is one of the strengths of C.

## Find Direction Of Growth Of Stack In C

#include <stdio.h> typedef unsigned long long UINT64; UINT64 f(int b){ return (UINT64)&b; } bool downwards(int a) { int b = 0; if ((&a - f(b)) < 0) return false; else return true; } int main(int argc, char* argv[]) { int a = 0; if (downwards(a)) printf("Downwards\n"); else printf("Upwards"); return 0; }

## Array indexing in C, the unknown kind

int a[5] = { 1, 2, 3, 4, 5 };

printf(“%d\n”, a[3], 3[a]);

output: 4, 4 -> Holy crap! I didn’t know that. I tried it out on the latest version of GCC and it works. Surely, C is like a sea.