Monday, 20 July 2015

What is variable - variables for hackers !

A variable is a data storage unit used in your program.Variables are used in programs to store pieces of information that may change and may be used to dynamically influence the program.
Understanding Variables : Imagine you have a wallet(purse ) on which you have written the word hacker.
                                                         
         

Suppose you have some money and you want to keep it into the wallet. If we understand this case as a programmer, then wallet whose named hacker is a variable and money is values which need to store in wallet. Again, Variables are used in programs to store pieces of information same as wallet is used to store money.
All variables must be declared before use. A declaration means specify a type(e.g. int, char), and contains a list of  one or more variables of that type :
int a, b, c;
char t, arr[10];
We can now use a,b,c,t,arr to hold information( money :-P).

But The basic format for declaring variables is
data_type var, var, …;
where data_type is one of the four basic types, an integer, character, float, or double type. When the program is  compiled, most variables are preallocate memory of a fixed size according to system-specific definitions of size.

INTEGER :
1. These are whole numbers, both positive and negative.
2. SIZE:    4 bytes for 32-bit machines         
3. Stores signed integer values such as 100 or –100
We can define INTEGER by keyword:
int
For example of an integer value is 15. An example of declaring an integer variable called length is
int length;

CHAR:
1.These are single characters
2. SIZE : 1 byte
3. Stores a single character such as “d”
We can define CHAR  by keyword:
char
An example of declaring a character variable called letter is
char letter;

FLOAT:
1. These are numbers which contain fractional parts, both
positive and negative
2. SIZE : 4 bytes
3. Stores signed floating-point numbers such as –1.23
We can define FLOAT  by keyword:
float
An example of declaring a float variable called x is:
float x;

DOUBLE:
1. These are floating point numbers, both positive and negative.
2. SIZE : 8 bytes
3. Stores large floating-point numbers.
We can define DOUBLE  by keyword:
double
An example of declaring a double variable called voltage is
double voltage;

 Now let's take a real code example:
Linux Kernel < 2.6.36.2 - Econet Privilege Escalation Exploit
void do_child_lower(void)
{
    int pid;
     printf("[+] lower child spawning a helper...\n");

Here we declare a variable name pid whose type is integer.

After declared variable, we need to initialization it so that we can use variable to store something.we can initialize variable using assignment operator :
int i;
i = 1;
we create an integer name i, then assign it the value 1. The = operator is the assignment operator in C.
A variable may also be initialized in it's declaration.
int i = 0;
float no = 1.8;

The type qualifier const can be applied to the declaration of any variable to specify that its value will not be
changed.For an array, the const qualifier says that elements will not be altered.
const double x = 3.17
const char mgs[] = "hello" ;

Variable scope: The scope of a variable refers to the extent to which different parts of a program have access to the variable.
Variables can be declared as:
Inside a function which is called local variables or internal variables.
Outside of all functions which is called global variables or external variables.

Internal variables: Variables that are declared inside a function or block are called local variables. They can be used only by statements that are inside that function or block of code.
#include <stdio.h>
int main ()
{
 
  int a, b =10; /* local variable declaration */
  printf ("value of a = %d, b = %d , a, b,);
}
Global variables :  If Variables are defined at the  beginning of the code i.e outside of any functions are called Global variables.
This variable can be read from and written to by any function, and the changes to it will  persist between functions

Static variables : According to wikipedia() , a static variable is a variable that has been allocated statically—whose lifetime or "extent" extends across the entire run of the program.
Static variable declaration :
Let take a code snippet of Linux Kernel 2.6.27 < 2.6.36 - x86_64 compat Local Root Exploit
static char buffer[1024];
static int s;
static int flags=0;
static char krelease[64];

Difference between const and static variables are often confusing for the beginners.  A variable declared as const can only be initialized once. The program should not alternate the value of a variable declared as const. A variable declared as const should never appear to the left of an assignment operator throughout the program. In case of static, Static variables are permanent variables within their on function. Unlike global variables they are not known outside their function or file, but they maintain their values between function calls.

Important points to remember :
* We can use comparison operators and various arithmetic operations on variables.
* you can change value what the variable originally had.
* variable name may not contain any space. if a variable must comprise more than one word, it should be separated with _ (underscore)
int my name = rohit ; /*Wrong*/
int my_name  = rohit ; /*correct*/
* The first character of the variable name must either be alphabet or underscore. It should not start with the digit.No commas and blanks are allowed in the variable name.
* variable name can contain only the capital latter(A-Z), small latter (a-z), digits(0-9), and underscore(_)
* Local variable are also known as automatic variable. Automatic variables are internal to a function; they come into existence when the function is entered, and disappear when it is left. On the other hand,External variables are permanent.
* Static variables can be applied to internal variables(local variables) and external variables(global ) as well.

 If you like this post or have any question, please feel free to comment.
Facebook Twitter Google+

 
Back To Top