Xenomai Timer

Xenomai Timer :Xenomai has two time sources: the sytem timer, which counts the number of nanoseconds since 1970, and a hardware dependent high resolution counter which counts the time since an unspecified point in time (usually the system boot time). This hardware dependent high resolution counter is called “tsc” on a PC, and gave its name to Xenomai native API calls.rt_timer_tsc returns the value of this hardware dependent high-resolution counter.
rt_timer_info returns the same thing in the tsc member of the RT_TIMER_INFO structure, and the value of the system timer at exactly the same time as when the high-resolution counter was read.

This allows to have a correspondence between the two time sources.

rt_alarm_inquire is not related to this and returns some information
about a given alarm. Now, if you allow me, a little advice for the implementation of a “timer library”: you could be tempted to create only one periodic alarm object with Xenomai, and to manage a timer list yourself. Don’t do this. Creating an alarm object for each timer library object make Xenomai aware of the existence of all your application timers, this has several

advantages:
– it gives you information about all your timers in /proc/xenomai
– it allows Xenomai to use its anticipation algorithm for all your timers
– if you are concerned about the scalability of Xenomai timers list
management, you can check the options in the “Scalability” menu of
Xenomai configuration menu (“Real-time subsystem” sub-menu of kernel
configuration menu).
more about timers

Xenomai POSIX skin supports two clocks:
CLOCK_REALTIME maps to the nucleus system clock, keeping time as the amount of time since the Epoch, with a resolution of one system clock tick.

CLOCK_MONOTONIC maps to an architecture-dependent high resolution counter, so is suitable for measuring short time intervals. However, when used for sleeping (with clock_nanosleep()), the CLOCK_MONOTONIC clock has a resolution of one system clock tick, like the CLOCK_REALTIME clock.[1]

Counting Digits from given Range

Imagine you sell those metallic digits used to number houses, locker doors, hotel rooms, etc. You need to find how many of each digit to ship when your customer needs to number doors/houses:
1 to 100
51 to 300
1 to 2,000 with zeros to the left
The obvious solution is to do a loop from the first to the last number, convert the counter to a string with or without zeros to the left, extract each digit and use it as an index to increment an array of 10 integers.
I wonder if there is a better way to solve this, without having to loop through the entire integers range.
GitHub Link
Thanx to mathematician’s Post that helped me to understand the complexity of problem, i just understood the equation and converted it to code. Enjoy !

Minimum cost flow House coloring with three colors

There are a row of houses, each house can be painted with three colors red, blue and green. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color. You have to paint the houses with minimum cost. How would you do it?

Note: Painting house-1 with red costs different from painting house-2 with red. The costs are different for each house and each color.

Approach:
Dynamic Programming solution:
we can paint the i’th house with blue, red or green.

Enter No. of Houses to paint : 6
1 4 5
1 3 9
6 7 2
9 6 4
3 5 2
6 7 4

Minimum Cost to paint all houses are 20
#include <stdio.h>
#include <stdarg.h>
#define COLOR 3
#define MAX(a,b) ((a) > (b) ? a : b)
#define MIN(a,b) ((a) < (b) ? a : b)
#define R 0
#define B 1
#define G 2
#define Color 3


int min( int num, ... )
{
    va_list arguments;
    int min=0;
    int tmp=0;
    /* Initializing arguments to store all values after num */
    va_start ( arguments, num );
    /* Sum all the inputs; we still rely on the function caller to tell us how
     * many there are */
    min = va_arg ( arguments, int );
    //printf("\n1st|%d|\n",min);
    for ( int x = 1; x < num; x++ )
    {
		tmp = va_arg(arguments,int);
		min = MIN(min,tmp);
    }
    va_end ( arguments );
    // Cleans up the list
	//printf("\nMin is %d", min);
    return min;
}
typedef struct {
    int  color;
    int cost;
    //struct nodee *root;
}node;
int main()
{
	node cost[3];
	int house = 3;
	printf("Enter No. of Houses to paint : ");
	scanf("%d",&house);
	int color[house][COLOR];
//	int ji=0;
	for(int i=0; i<house;++i){
		for(int j=0; j<COLOR;++j){
				scanf("%d",&color[i][j]);
//				color[i][j]= ++ji;
			}

		}
	//cost[0].cost=color[0][R];cost[1].cost=color[0][B];cost[2].cost=color[0][G];
	for(int i= 0; i< Color ;i++){
			cost[i].cost=color[0][i];
			cost[i].color = i;
		} // Initial Submission of values, and colour
//	printf("\nA.cost\t%d(%d)\tB.cost\t%d(%d)\tC.cost\t%d(%d)\n",cost[0].cost,cost[0].color,cost[1].cost,cost[1].color,cost[2].cost,cost[2].color);
	//A.color = R; B.color = B; C.color = G;
	int tmp_B=0,tmp_G=0,tmp_R=0;
	//printf("\nR_cost\t%d\tB_cost\t%d\tG_cost\t%d\n",R_cost,B_cost,G_cost);
	for(int i=1; i<house;++i)
		{
			for(int j= 0; j< Color ;j++){
				if(cost[j].color == R){
						if(color[i][B] < color[i][G]){
							cost[j].cost = cost[j].cost + color[i][B];
							cost[j].color = B;
							//add_linked_list();
							//cost[j].root = malloc(sizeof(struct node));
							//cost[j].root->next = 0;
							//cost[j].root->x = color[i][B];
						}else{
								cost[j].cost = cost[j].cost + color[i][G];
								cost[j].color = G;
						}
					}else if(cost[j].color == B){
							if(color[i][R] < color[i][G]){
								cost[j].cost = cost[j].cost + color[i][R];
								cost[j].color = R;
							}else{
									cost[j].cost = cost[j].cost + color[i][G];
									cost[j].color = G;
								}
						}else{
								//Case for G
								if(color[i][R] < color[i][B]){
									cost[j].cost = cost[j].cost + color[i][R];
									cost[j].color = R;
									}else{
											cost[j].cost = cost[j].cost + color[i][B];
											cost[j].color = B;
										}
							}
//			printf("\nA.cost\t%d(%d)\tB.cost\t%d(%d)\tC.cost\t%d(%d)\n",cost[0].cost,cost[0].color,cost[1].cost,cost[1].color,cost[2].cost,cost[2].color);
			}
			/*
			if(A.color == )
			//R_cost = min(2,color[i][B],color[i][G])+R_cost;
			if(color[i][B] < color[i][G]){

				tmp_B = R_cost + color[i][B];
				}else{
						tmp_G = R_cost + color[i][G];
					}

			//B_cost = min(2,color[i][R],color[i][G])+B_cost;
			if(color[i][R] < color[i][G]){

				tmp_R = B_cost + color[i][R];
				}else{
						tmp_G = B_cost + color[i][G];
					}
			//G_cost = min(2,color[i][B],color[i][R])+G_cost;
			if(color[i][R] < color[i][B]){

				tmp_R = G_cost + color[i][R];
				}else{
						tmp_B = G_cost + color[i][B];
					}
			R_cost = tmp_R;
			B_cost = tmp_B;
			G_cost = tmp_G;
			//printf("\n G %d:%d",color[i][B],color[i][R]);
			*/
//			printf("\nA.cost\t%d(%d)\tB.cost\t%d(%d)\tC.cost\t%d(%d)\n",cost[0].cost,cost[0].color,cost[1].cost,cost[1].color,cost[2].cost,cost[2].color);
		}
	printf("\nMinimum Cost to paint all houses are %d\n",min(3,cost[0].cost,cost[1].cost,cost[2].cost));
}

Increment decrement problem in c

Hello guys let's we have an syntax and related values let's see how this works. Evaluate the given syntax 
z = ++x + y-- - ++y - x-- - x-- - ++y - x--
where x = 7 y = -3
z = ((((((++x + y--) - ++y) - x--) - x--) - ++y) - x--)
so first(++x + y--) will be evaluated
 (++x + y--)
   8 + (-3)        x=8 y = -4
 z = (((((5 - ++y) - x--) - x--) - ++y) - x--)
 (5 - ++y)
  5 - ( -3)  x=8 y = -3

 z = ((((8 - x--) - x--) - ++y) - x--)
 (8 - x--)
  8 - 8      x =7 y = -3 
 z = (((0- x--) - ++y) - x--)
 (0- x--)
  0 - 7      x =6 y = -3
 z = (((-7 - ++y) - x--)
 (-7 - ++y)
  -7 - (-2)  x = 6 y = -2
 z = (-5 - x--)
 -5 -6       x =5 y -2
 z = -11

So answer is -11

What would be the output of this Code ?

Hello, Guys let’s have a fun, i am giving  you a code just tell me what will be the output of this code if you enter there parameters.

For Test case 1 -->Q
For Test case 2 -->QQ

Please tell why putchar() function is not working and if it is working why it is not taking any Char input?


#include <stdio.h>
main()
{
putc(getc(stdin),stdout);
putchar(getchar( ));
return 0;
}

Explain the output for these test cases.

Adding Leach protocol in ns2.34

let’s your ns2 is installed in /home/arun/ns/ns-allinone2.34
I suppose that ns-2.34 is installed with the compiler gcc-4.3.
download both files in  …//ns.2.34
1. LeachCode
2.Installing_file
and make sure that in leach-setup.sh please change the path in this file and run

arun@merom:~$ cd /home/arun/ns/ns-allinone-2.34/ns-2.34/
arun@merom:/home/arun/ns/ns-allinone-2.34/ns-2.34$ sudo bash leach-setup.sh
arun@merom:/home/arun/ns/ns-allinone-2.34/ns-2.34$./configure
arun@merom:/home/arun/ns/ns-allinone-2.34/ns-2.34$make clean
arun@merom:/home/arun/ns/ns-allinone-2.34/ns-2.34$make depend
arun@merom:/home/arun/ns/ns-allinone-2.34/ns-2.34$make
arun@merom:/home/arun/ns/ns-allinone-2.34/ns-2.34$make install

after running  “make ” you will get error

[trace/cmu-trace.cc: In member function ‘void CMUTrace::format(Packet*, const char*)’:
trace/cmu-trace.cc:1327: error: ‘format_rca’ was not declared in this scope

Once the above steps are done, run the following commands in the Terminal:
./configure
make clean
make depend
make
When you using the make command you will get the following error:
Code:
********************************************************************
trace/cmu-trace.cc: In member function ‘void CMUTrace::format(Packet*, const char*)’:
trace/cmu-trace.cc:1327: error: ‘format_rca’ was not declared in this scope
trace/cmu-trace.cc: At global scope:
trace/cmu-trace.cc:1523: error: no ‘void CMUTrace::format_rca(Packet*, int)’ member function declared in class ‘CMUTrace’
make: *** [trace/cmu-trace.o] Error 1
********************************************************************
  To fix it you need to add the following code in “cmu-trace.h” file starting from line 165:
Code:
********************************************************************
 #ifdef MIT_uAMPS
 void    format_rca(Packet *p, int offset);
#define ADV_CHAR             ‘A’
#define REQ_CHAR             ‘R’
#define DATA_CHAR            ‘D’
#endif
********************************************************************
 And following into ns-default.tcl (line 765) to suppress few warnings:
 ********************************************************************
# ——————————————————
Phy/WirelessPhy set alive_ 1
Phy/WirelessPhy set Efriss_amp_ 100e-12
Phy/WirelessPhy set Etwo_ray_amp_ 0.013e-12
Phy/WirelessPhy set EXcvr_ 50e-9
Phy/WirelessPhy set sleep_ 0
Phy/WirelessPhy set ss_ 1
Phy/WirelessPhy set dist_ 0
# ——————————————————
********************************************************************
Now run the following the commands to re-compile ns in terminal.
make clean
make depend
make
 If the above commands passed successfully then you are ready to test your “Leach” by running “./test_leach” command.
 ./test_leach
Now go to the following directory “yourpath/ns-allinone-2.34/ns-2.34/ns-234-leach/mit/leach_sims” and check the file “leach.err”,
You may get few errors in the leach.err file as follows:
********************************************************************
can’t read “env(RCA_LIBRARY)”: no such variable
    while executing
“source $env(RCA_LIBRARY)/ns-ranode.tcl”
    (file “mit/uAMPS/sims/uamps.tcl” line 9)
      ………………….
      …..Code Omitted…..
      ………………….
      (procedure “source” line 8)
    invoked from within
“source tcl/mobility/$opt(rp).tcl”
    (file “tcl/ex/wireless.tcl” line 187)
********************************************************************
The problem is in the path. What modify all the paths in “ns-allinone-2.34/ns-2.34/mit/uAMPS/sims/uamps.tcl”. Here is the modified code below the commented out lines (lines beginning with ‘#’):
Code:
********************************************************************
############################################################################
#
# This code was developed as part of the MIT uAMPS project. (June, 2000)
#
############################################################################
global opt bs
#source $env(RCA_LIBRARY)/ns-ranode.tcl
source /home/arun/ns/ns-allinone-2.35-RC4/ns-2.35/mit/rca/ns-ranode.tcl
#source $env(uAMPS_LIBRARY)/ns-bsapp.tcl
source /home/arun/ns/ns-allinone-2.35-RC4/ns-2.35/mit/uAMPS/ns-bsapp.tcl
#source $env(uAMPS_LIBRARY)/extras.tcl
source /home/merom/ns-allinone-2.35-RC4/ns-2.35/mit/uAMPS/extras.tcl
#source $env(uAMPS_LIBRARY)/stats.tcl
source/home/arun/ns/ns-allinone-2.35-RC4/ns-2.35/mit/uAMPS/stats.tcl
#Uncomment these lines to use gdb to debug the c code
#source mit/uAMPS/ns-bsapp.tcl
#source mit/uAMPS/extras.tcl
#source mit/uAMPS/stats.tcl
#source $env(RCA_LIBRARY)/resources/ns-resource-manager.tcl
source /home/arun/ns/ns-allinone-2.35-RC4/ns-2.35/mit/rca/resources/ns-resource-manager.tcl
#source $env(RCA_LIBRARY)/resources/ns-energy-resource.tcl
source /home/arun/ns/ns-allinone-2.35-RC4/ns-2.35/mit/rca/resources/ns-energy-resource.tcl
#source $env(RCA_LIBRARY)/resources/ns-neighbor-resource.tcl
source /home/arun/ns/ns-allinone-2.35-RC4/ns-2.35/mit/rca/resources/ns-neighbor-resource.tcl
********************************************************************
Now run the following the commands to re-compile ns in terminal.
make clean
make depend
make
and execute following again: ./test_leach
Now check the file “leach.err”, if no error reported then go to “leach.out”  for results!

 

 

run again all commands it will work :)

for more please refer

http://forum.wsnlab.ir/pdf/LEACH.inNS2.wsnlab.ir.pdf

 

Understanding “extern” keyword in C

Hello , one more important stuff to understand

I’m sure that this post will be as interesting and informative to C virgins (i.e. beginners) as it will be to those who are well versed in C. So let me start with saying that extern keyword applies to C variables (data objects) and C functions. Basically extern keyword extends the visibility of the C variables and C functions. Probably that’s is the reason why it was named as extern.

Though (almost) everyone knows the meaning of declaration and definition of a variable/function yet for the sake of completeness of this post, I would like to clarify them. Declaration of a variable/function simply declares that the variable/function exists somewhere in the program but the memory is not allocated for them. But the declaration of a variable/function serves an important role. And that is the type of the variable/function. Therefore, when a variable is declared, the program knows the data type of that variable. In case of function declaration, the program knows what are the arguments to that functions, their data types, the order of arguments and the return type of the function. So that’s all about declaration. Coming to the definition, when we define a variable/function, apart from the role of declaration, it also allocates memory for that variable/function. Therefore, we can think of definition as a super set of declaration. (or declaration as a subset of definition). From this explanation, it should be obvious that a variable/function can be declared any number of times but it can be defined only once. (Remember the basic principle that you can’t have two locations of the same variable/function). So that’s all about declaration and definition.
Now coming back to our main objective: Understanding “extern” keyword in C. I’ve explained the role of declaration/definition because it’s mandatory to understand them to understand the “extern” keyword. Let us first take the easy case. Use of extern with C functions. By default, the declaration and definition of a C function have “extern” prepended with them. It means even though we don’t use extern with the declaration/definition of C functions, it is present there. For example, when we write.

    int foo(int arg1, char arg2);

There’s an extern present in the beginning which is hidden and the compiler treats it as below.

    extern int foo(int arg1, char arg2);

Same is the case with the definition of a C function (Definition of a C function means writing the body of the function). Therefore whenever we define a C function, an extern is present there in the beginning of the function definition. Since the declaration can be done any number of times and definition can be done only once, we can notice that declaration of a function can be added in several C/H files or in a single C/H file several times. But we notice the actual definition of the function only once (i.e. in one file only). And as the extern extends the visibility to the whole program, the functions can be used (called) anywhere in any of the files of the whole program provided the declaration of the function is known. (By knowing the declaration of the function, C compiler knows that the definition of the function exists and it goes ahead to compile the program). So that’s all about extern with C functions.
Now let us the take the second and final case i.e. use of extern with C variables. I feel that it more interesting and information than the previous case where extern is present by default with C functions. So let me ask the question, how would you declare a C variable without defining it? Many of you would see it trivial but it’s important question to understand extern with C variables. The answer goes as follows.

    extern int var;

Here, an integer type variable called var has been declared (remember no definition i.e. no memory allocation for var so far). And we can do this declaration as many times as needed. (remember that declaration can be done any number of times) So far so good. :)
Now how would you define a variable. Now I agree that it is the most trivial question in programming and the answer is as follows.

    int var;

Here, an integer type variable called var has been declared as well as defined. (remember that definition is the super set of declaration). Here the memory for var is also allocated. Now here comes the surprise, when we declared/defined a C function, we saw that an extern was present by default. While defining a function, we can prepend it with extern without any issues. But it is not the case with C variables. If we put the presence of extern in variable as default then the memory for them will not be allocated ever, they will be declared only. Therefore, we put extern explicitly for C variables when we want to declare them without defining them. Also, as the extern extends the visibility to the whole program, by externing a variable we can use the variables anywhere in the program provided we know the declaration of them and the variable is defined somewhere.
Now let us try to understand extern with examples.
Example 1:

int var;
int main(void)
{
   var = 10;
   return 0;
}

Analysis: This program is compiled successfully. Here var is defined (and declared implicitly) globally.
Example 2:

extern int var;
int main(void)
{
  return 0;
}

Analysis: This program is compiled successfully. Here var is declared only. Notice var is never used so no problems.
Example 3:

extern int var;
int main(void)
{
 var = 10;
 return 0;
}

Analysis: This program throws error in compilation. Because var is declared but not defined anywhere. Essentially, the var isn’t allocated any memory. And the program is trying to change the value to 10 of a variable that doesn’t exist at all.
Example 4:

#include "somefile.h"
extern int var;
int main(void)
{
 var = 10;
 return 0;
}

Analysis: Supposing that somefile.h has the definition of var. This program will be compiled successfully.
Example 5:

extern int var = 0;
int main(void)
{
 var = 10;
 return 0;
}

Analysis: Guess this program will work? Well, here comes another surprise from C standards. They say that..if a variable is only declared and an initializer is also provided with that declaration, then the memory for that variable will be allocated i.e. that variable will be considered as defined. Therefore, as per the C standard, this program will compile successfully and work.
So that was a preliminary look at “extern” keyword in C.
I’m sure that you want to have some take away from the reading of this post. And I would not disappoint you. :)
In short, we can say
1. Declaration can be done any number of times but definition only once.
2. “extern” keyword is used to extend the visibility of variables/functions().
3. Since functions are visible through out the program by default. The use of extern is not needed in function declaration/definition. Its use is redundant.
4. When extern is used with a variable, it’s only declared not defined.
5. As an exception, when an extern variable is declared with initialization, it is taken as definition of the variable as well.

Copied from GeekforGeeks Thanx GeekforGeeks.

How Many Ip Address !!!

Given a string containing only digits, restore it by returning all possible valid IP address combinations.
For example:
Given “25525511135”,
return [“255.255.11.135″, “255.255.111.35”]{hint:recursion,backtrack}

Here is the Solution ……
it was taken me about 5 hours to solve see !!!

//cc Arun Kumar Gupta

import java.util.*;
public class AllIpAddress
{
public static void main (String [] args)
{
Scanner sc = new Scanner(System.in);
String input = sc.next();
int[] intArray = new int[input.length()];
int length = input.length();

for (int i = 0; i < input.length(); i++) {
intArray[i] = Character.digit(input.charAt(i), 10);
//System.out.println(intArray[i]);
}
int i = 0 , j = 1 , k = 2;
AllIpAddress ip = new AllIpAddress();
ip.Ipaddress(intArray , i , j , k , length-1);

}
void Ipaddress(int [] intArray , int i , int j , int k , int length)
{
try{
int where = 0 ,change = 0;
int p1 =createIP( intArray , -1 , i);
int p2 =createIP( intArray , i ,  j );
int p3 =createIP( intArray ,  j , k );
int p4 =createIP( intArray ,  k ,  length );
//System.out.println(“**********Garbage”+p1+”.”+p2+”.”+p3+”.”+p4);
//System.out.println(“i = “+i+”j = “+j+”k = “+k);
if((p1<=255)&&(p2<=255)&&(p3<=255)&&(p4<=255)&&(p4>0))
{
System.out.println(p1+”.”+p2+”.”+p3+”.”+p4);
}
if(p4 >255)
{
//if(k != j)
//++k;
where =4;
//System.out.println(“Problem at P4″);
}
else if(p3>255)
{
//if(j != i)
//++j;
//–k;
where = 3;
//System.out.println(“Problem at P3″);
}
else if(p2>255)
{
//++i;
where = 2;
//System.out.println(“Problem at P2″);
}
else if(p1>255)
{

//System.out.println(“Problem at P1″);
System.exit(0);
}

if((k == length) && (j+1 == k))
{
//System.out.println(“if((k == length) && (j+1 == k))”);
//System.out.println(i+”_”+j+”_”+k);
++i;
j = i+1;
k = j+1;
change = 1;
//System.out.println(i+”_”+j+”_”+k);

}
if((k == length) &&(change == 0) )
{
//System.out.println(“if((k == length) &&(change == 0) )”);
++j;
k = j+1;
change = 0;
}
else if(change == 0)
{
if( (k) < length )
++k;
}
/*if(where ==4)
{
++k;
}
if(where ==3)
{
++j;
–k;
}
if(where ==2)
{
++i;
–j;
}
/*if(k==length)
{
–k;
–j;
}*/
Ipaddress(intArray , i , j , k , length);
}
catch(ArrayIndexOutOfBoundsException e){
//System.out.println(“Exception thrown  :” + e);
}
}
static int createIP(int [] intArray , int start , int end)
{
//System.out.println(“I am at createIP()start = “+start+”\tend = “+end);
int val = 1 , fina = 0;
for(int e =end ; e>start ; –e)
{
fina = intArray[e]*val + fina;
val = val*10;
}
return fina;
}
}

Constant In C

A C constant is usually just the written version of a number. For example 1, 0, 5.73, 12.5e9. We can specify our constants in octal or hexadecimal, or force them to be treated as long integers.
  • Octal constants are written with a leading zero – 015.
  • Hexadecimal constants are written with a leading 0x – 0x1ae.
  • Long constants are written with a trailing L – 890L.

Character constants are usually just the character enclosed in single quotes; ‘a’, ‘b’, ‘c’. Some characters can’t be represented in this way, so we use a 2 character sequence as follows.
'\n' newline
'\t' tab
'\\' backslash
'\'' single quote
'\0' null ( Usedautomatically to terminate character string )

In addition, a required bit pattern can be specified using its octal equivalent.
‘\044′ produces bit pattern 00100100.
Character constants are rarely used, since string constants are more convenient. A string constant is surrounded by double quotes eg “Brian and Dennis”. The string is actually stored as an array of characters. The null character ‘\0′ is automatically placed at the end of such a string to act as a string terminator.
A character is a different type to a single character string. This is important poing to note.

Defining Constants

ANSI C allows you to declare constants. When you declare a constant it is a bit like a variable declaration except the value cannot be changed.
The const keyword is to declare a constant, as shown below:
int const a = 1;
const int a =2;
Note:
  • You can declare the const before or after the type. Choose one an stick to it.
  • It is usual to initialise a const with a value as it cannot get a value any other way.
The preprocessor #define is another more flexible (see Preprocessor Chapters) method to define constants in a program.
#define TRUE  1
#define FALSE 0
#define NAME_SIZE 20
Here TRUE, FALSE and NAME_SIZE are constant
You frequently see const declaration in function parameters. This says simply that the function is not going to change the value of the parameter.
The following function definition used concepts we have not met (see chapters on functions, strings, pointers, and standard libraries) but for completenes of this section it is is included here:
void strcpy(char *buffer, char const *string)

The enum Data type

enum is the abbreviation for ENUMERATE, and we can use this keyword to declare and initialize a sequence of integer constants. Here’s an example:
enum colors {RED, YELLOW, GREEN, BLUE};
I’ve made the constant names uppercase, but you can name them which ever way you want.
Here, colors is the name given to the set of constants – the name is optional. Now, if you don’t assign a value to a constant, the default value for the first one in the list – RED in our case, has the value of 0. The rest of the undefined constants have a value 1 more than the one before, so in our case, YELLOW is 1GREEN is 2 and BLUE is 3.
But you can assign values if you wanted to:
enum colors {RED=1, YELLOW, GREEN=6, BLUE };
Now RED=1, YELLOW=2, GREEN=6 and BLUE=7.
The main advantage of enum is that if you don’t initialize your constants, each one would have a unique value. The first would be zero and the rest would then count upwards.
You can name your constants in a weird order if you really wanted…
#include 

int main() {
enum {RED=5, YELLOW, GREEN=4, BLUE};

printf("RED = %d\n", RED);
printf("YELLOW = %d\n", YELLOW);
printf("GREEN = %d\n", GREEN);
printf("BLUE = %d\n", BLUE);
return 0;
}

This will produce following results

RED = 5
YELLOW = 6
GREEN = 4
BLUE = 5

Copied From Tutorial C

Difference between Near , Far and Huge Pointer

as we know by default the pointers are near for example int *p is a near pointer… size of near pointer is 2 bytes in case of 16 bit compiler…….. n we already know very well size varies compiler to compiler…… They only store the offset of the address the pointer is referencing. . An address consisting of only an offset has a range of 0 – 64K bytes…. i think there is no need to discuss near pointers anymore…. so come to the main point….. that is far and huge pointers……


far and huge pointers:
Far and huge pointers have a size of 4 bytes. They store both the segment and the offset of the address the pointer is referencing. thn what is the difference between them ………..
Limitation of far pointer:
We cannot change or modify the segment address of given far address by applying any arithmetic operation on it. That is by using arithmetic operator we cannot jump from one segment to other segment. If you will increment the far address beyond the maximum value of its offset address instead of incrementing segment address it will repeat its offset address in cyclic order. this is also called wrapping…..i.e. if offset is 0xffff and we add 1 then it is 0x0000 and similarly if we decrease 0x0000 by 1 then it is 0xffff and remember there is no change in the segment….

Now i am going to compare huge and far pointers……….

1.
When a far pointer is incremented or decremented ONLY the offset of the pointer is actually incremented or decremented but in case of huge pointer both segment and offset value will change…..
like if we have

int main()
{
char far* f=(char far*)0x0000ffff;
printf(“%Fp”,f+0x1);
return 0;
}

then the output is:
0000:0000

as we see there is no change in segment value…….

and in case of huge……..

int main()
{
char huge* h=(char huge*)0x0000000f;
printf(“%Fp”,h+0x1);
return 0;
}
then the o/p is:
0001:0000

it shows bcoz of increment operation not only offset value but segment value also change………. that means segment will not change in case of far pointers but in case of huge it can move from one segment to another ……
2.
When relational operators are used on far pointers only the offsets are compared.In other words relational operators will only work on far pointers if the segment values of the pointers being compared are the same. and in case of huge this will not happen, actually comparison of absolute addresses takes place…... lets understand with the help of an example…
in far………………………….

int main()
{
char far * p=(char far*)0x12340001;
char far* p1=(char far*)0x12300041;
if(p==p1)
printf(“same”);
else
printf(“different”);
return 0;
}

Output:
different
in huge…………………..
int main()
{
char huge * p=(char huge*)0x12340001;
char huge* p1=(char huge*)0x12300041;
if(p==p1)
printf(“same”);
else
printf(“different”);
return 0;
}


Output:
same
Explanation:
as we see the absolute address for both p and p1 is 12341 (1234*10+1 or 1230*10+41) but they are not considered equal in 1st case becoz in case of far pointers only offsets are compared i.e. it will check whether 0001==0041…. that we know is false…. and know see what will happen in case of huge…..the comparison operation is performed on absolute addresses that are equal as i already told……
3.
A far pointer is never noramlized but a huge pointer is normalized . A normalized pointer is one that has as much of the address as possible in the segment, meaning that the offset is never larger than 15.
suppose if we have 0x1234:1234 then the normalized form of it is 0x1357:0004(absolute address is 13574)…….
A huge pointer is normalized only when some arithmetic operation is performed on it … and not noramlized during assignment….
int main()
{
char huge* h=(char huge*)0x12341234;
char huge* h1=(char huge*)0x12341234;
printf(“h=%Fp\nh1=%Fp”,h,h1+0x1);
return 0;
}
Output:
h=1234:1234
h1=1357:0005
Explanation:
as i said above huge is not normalized in case of assignment……but if an arithmetic operation is performed on it ….. it will be normalized…. so h is 1234:1234 and h1 is 1357:0005……….that is normalized…….
4.
The offset of huge pointer is less than 16 because of normalization and not so in case of far pointers……………….
lets take an example to understand what i want to say…..
int main()
{
char far* f=(char far*)0x0000000f;
printf(“%Fp”,f+0x1);
return 0;
}
Output:
0000:0010
in case of huge
int main()
{
char huge* h=(char huge*)0x0000000f;
printf(“%Fp”,h+0x1);
return 0;
}

Output:
0001:0000
Explanation:
as we increment far pointer by 1 it will be 0000:0010…… and as we increment huge pointer by 1 thn it will be 0001:0000 bcoz its offset cant be greater than 15 in other words it will be normalized…………
Copied from C Stuff