Campus Buzz

all the stuffs learned while preparin for campus will be posted here!


Sunday, May 21, 2006

DAY 5- C is amusing!

What are the various built-in data tpyes that can be used for storing integers.
Myth:int short int long int
Reality: Even char data type(espcially unsigned char) can be used for storing integers also known as "tiny" integers. However, doing so is sometimes troublesome. Unlike integers, we dont know whether the char type by default is signed type or unsigned tpye. It varies from machine to machines. Also, the conversion of char value to larger data type value during calculation is to be considered.

consider this code,
char c;
while((c = getchar()) != EOF)
What's wrong with this code?

getchar() will definitely accept integer value. Also, EOF should not be similiar to any other character value. hence it is assigned value -1. two errors may arise:If type char is signed, and if EOF is defined (as is usual) as -1, the character with the decimal value 255 ('\377' or '\xff' in C) will be sign-extended and will compare equal to EOF, prematurely terminating the input. If type char is unsigned, an actual EOF value will be truncated (by having its higher-order bits discarded, probably resulting in 255 or 0xff) and will not be recognized as EOF, resulting in effectively infinite input.

Myth: the size of int is 2 bytes
Reality: The size of int can be equal to 2 byte or 4 byte dpending upon the machine to machine. The point is Type int is supposed to represent a machine's natural word size. if u r using 32-bit processor and if u get the size of int as 4 byte, dont assume that your system has gone haywireHowever, if u want to control the size of data type, u can use typedef int16 and int32 for 16-bit and 32-bit integers respectiverly.however, Typedefs will never do anything about byte order problems (e.g. if you're trying to interchange data or conform to externally-imposed storage layouts). You no longer have to define your own typedefs, because the Standard header contains a complete set.

what is the difference between int (*ip)[] and int *ip[]
there is a lot of difference b/w them. int (*ip)[] means that ip is the pointer to integer array whereas *ip[] means that ip is an array of pinters pointing towards an integer. Why it is so?
when ip is parethesis, it has a higher priority than that of [], that's why ip is considered as a pointer to array in the first declaration while in other it is considered as array of pointers.

Is it legal to write
typedef struct x1(.......)x1;

it is absolutely legal.You can use both
struct x1 X;
x1 x;
It is possible to use the same name for both the tag and the typedef, since they live in separate namespaces.

What are various kind of namespaces?

There are four different kinds of namespaces, for:

  • labels (i.e. goto targets);
  • tags (names of structures, unions, and enumerations; these three aren't separate even though they theoretically could be);
  • structure/union members (one namespace per structure or union); and
  • everything else (functions, variables, typedef names, enumeration constants), termed ``ordinary identifiers'' by the Standard.
Can we have identifier starting with underscore? Why it is allowed?
They are some rules like:

1. Don't give anything a name with a leading underscore.
2. Don't give anything a name which is already a standard macro (including the ``future directions'' patterns).
3. Don't give any functions or global variables names which are already taken by functions or variables in the standard library, or which match any of the ``future directions'' patterns. (Strictly speaking, ``matching'' means matching in the first six characters, without regard to case )
4. Don't redefine standard typedef or tag names.

However, there are some exceptios to these rules:
1. You may use identifiers consisting of an underscore followed by a digit or lower case letter for labels and structure/union members.
2. You may use identifiers consisting of an underscore followed by a digit or lower case letter at function, block, or prototype scope.
3. You may use names matching standard macro names if you don't #include any header files which #define them.
4. You may use names of standard library routines as static or local variables (strictly speaking, as identifiers with internal or no linkage, remember global vairables have external linkages whereas static and local have internal or on linkages ).
5. You may use standard typedef and tag names if you don't #include any header files which declare them.

The main motive for providing such provisions so that the programmer can create its own hidden or internal identifiers.

Source: C FAQs


Post a Comment

<< Home