Pages

Monday, December 18, 2017

All about volatile variable in C

What is volatile keyword in C?
volatile is a data type qualifier in C.

Role of volatile variable in signal handler?
When a variable is declared as volatile, the C compiler does not optimize the code where this variable involves.

It also means that someone might externally change the value of this variable over time, hence do not cache the value of this variable.

In case of interrupt handlers or signal handlers where the counter is dependent is on the incoming data, this counter variables value changes due to external factors like arrival of new data on the port. Hence even though the program did not modified the data, the value of this counter variable changed. Such variable should be declared as volatile variable, so that the C compiler does not optimize the usage of this variable and hence does not pick up the value from the cache.

Logical & and addition

What is going to be the output of this code?

  1 #include
  2 int main(void)
  3 {
  4     int a=10, b=12;
  5     int c;
  6     c = a&b + 65;
  7     printf("c=%d\n", c);

  8}

This code looks simple enough. Guess??

Friday, December 15, 2017

const behavior of a variable in C Programming Language

When you intend to not change the value of a variable you declare it as a const in C language.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
int c1()
{
  int *a;
  const int b;

  a=&amp;b;

  printf("*a=%d, b=%d\n",*a, b);
  
  *a = *a + 1;
  //b = b + 1; 

  printf("*a=%d, b=%d\n", *a, b);

//return ;
}

main()
{       
        printf("C=%d\n", c1());
}




If you execute 
  b = b + 1:
then the compiler complains:
c1.c:12:5: error: cannot assign to variable 'b' with const-qualified type 'const int'
  b = b + 1; 
  ~ ^
c1.c:5:13: note: variable 'b' declared const here
  const int b;
  ~~~~~~~~~~^

That's what is expected.
But if we declare a pointer variable and that points to the const variable, and you increment the "value" of what the pointer variable is pointing to, then the compiler does not complain. Why?

Using only 
  *a = *a + 1;
actually increments the value of b because it's same as *a.



Let's change the program slightly:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
int c1()
{
  int *a;
  const int b;

  a=&b;

  printf("*a=%d, b=%d\n",*a, b);
  
  *a = *a + 1;
  //b = b + 1; 

  printf("*a=%d, b=%d\n", *a, b);

//return ;
}

main()
{       
        printf("C=%d\n", c1());

}

This time the compiler gives an error.

s1.c:11:6: error: read-only variable is not assignable
  *a = *a + 1;
  ~~ ^

So, the rational is if you use the const identifier, then you can't update it's value.



Static variable behavior

How the static variable behaves?

#include
//int s1();
int s1()
{
  static int s;
  return s++;
}

main()
{
  int i=1;
  while (i<=2)
  {      
    i++;
    printf("S=%d\n", s1());
  }

}



Simple program. But here's the trick.
s is a static variable. 

    return s++;
the post increment is done as if:
    
    return s;
    s = s + 1;

So, when the first time s1() prints, it prints the value of s = 0, s being a static variable it's initialized to 0. But the first time s1() returns before it's being incremented. The next statement of course executes, and s is incremented. Since it's a static variable, it's not in the call stack, hence it's got incremented after the s1() function returns, but before the function actually exits.

As per Wikipedia:
When the program (executable or library) is loaded into memorystatic variables are stored in the data segment of the program's address space (if initialized), or the BSS segment (if uninitialized), and are stored in corresponding sections of object files prior to loading.

As per this 1995 Linux Journal article: http://www.linuxjournal.com/article/1059
Executable code is always placed in a section known as .text; all data variables initialized by the user are placed in a section known as .data; and uninitialized data is placed in a section known as .bss.

Hence the static variable actually increments.