in Academics, Downloads

C programming Assignment – S1 and S2 2010

C programming Assignment

The Questions was

Write a Short Note on

  1. UNION
  2. Preprocessor Directives
  3. Command Line Argument
  4. Typedef Statement

A union is like a structure in which all of the members are stored at the same address. Only one member can be in a union at one time. The union data type was invented to prevent the computer from breaking its memory up into many inefficiently sized chunks, a condition that is called memory fragmentation.

The union data type prevents fragmentation by creating a standard size for certain data. When the computer allocates memory for a program, it usually does so in one large block of bytes. Every variable allocated when the program runs occupies a segment of that block. When a variable is freed, it leaves a “hole” in the block allocated for the program. If this hole is of an unusual size, the computer may have difficulty allocating another variable to “fill” the hole, thus leading to inefficient memory usage. Since unions have a standard data size, however, any “hole” left in memory by freeing a union can be filled by another instance of the same type of union. A union works because the space allocated for it is the space taken by its largest member; thus, the small-scale memory inefficiency of allocating space for the worst case leads to memory efficiency on a larger scale.

Declaration of unions

A union is declared in the same way as a structure. It has a list of members, as in the example below:

union int_or_float
int int_member;
float float_member;
Declaring union variables is similar to declaring structure variables:

union int_or_float my_union1, my_union2;

Preprocessor Directives

Making programming versatile.

GCC, the GNU Compiler Collection, contains a C preprocessor. A preprocessor is a program that examines C code before it is compiled and manipulates it in various ways. There are two main uses of a preprocessor. One is to include external files, such as header files. The other is to define macros, which are names (possibly with arguments) that are expanded by the preprocessor into pieces of text or C code. Macros that are expanded into text are usually displayed to the user, but macros that are expanded into C code are executed with the rest of the C code that surrounds them.
1. It improves the readability of program.
2. It makes easy to modify
3. portability

Eg:All preprocessor directives, or commands, are preceded by a hash mark (#). One example you have already seen in previous chapters is the #include directive:

This directive tells the preprocessor to include the file stdio.h; in other words, to treat it as though it were part of the program text.

A file to be included may itself contain #include directives, thus encompassing other files. When this happens, the included files are said to be nested.

Here are a few other directives:

#if … #endif
The #if directive is followed by an expression on the same line. The lines of code between #if and #endif will be compiled only if the expression is true. This is called conditional compilation.
This is part of an #if preprocessor statement and works in the same way with #if that the regular C else does with the regular if.
#line constant filename
This causes the compiler to act as though the next line is line number constant and is part of the file filename. Mainly used for debugging.
This forces the compiler to abort. Also intended for debugging.
Below is an example of conditional compilation. The following code displays 23 to the screen.


#define CHOICE 500

int my_int = 0;

#if (CHOICE == 500)
void set_my_int()
my_int = 23;
void set_my_int()
my_int = 17;

int main ()
printf(“%dn”, my_int);

return 0;



It is possible to pass arguments to C programs when they are executed. The brackets which follow main are used for this purpose. argc refers to the number of arguments passed, and argv[] is a pointer array which points to each argument which is passed to main. A simple example follows, which checks to see if a single argument is supplied on the command line when the program is invoked.

main( int argc, char *argv[] ) 
if( argc == 2 )

printf("The argument supplied is %sn", argv[1]);

else if( argc > 2 )

printf("Too many arguments supplied.n");


printf("One argument expected.n");



Note that *argv[0] is the name of the program invoked, which means that *argv[1] is a pointer to the first argument supplied, and *argv[n] is the last argument. If no arguments are supplied, argc will be one. Thus for n arguments, argc will be equal to n + 1. The program is called by the command line,

   myprog  argument1

Typedef Statement

typedef is a keyword in the C and C++ programming languages. The purpose of typedef is to assign alternative names to existing types, most often those whose standard declaration is cumbersome, potentially confusing, or likely to vary from one implementation to another.

So, how do you actually declare a typedef? All you must do is provide the old type name followed by the type that should represent it throughout the code. Here’s how you would declare size_t to be an unsigned integer:
typedef unsigned int size_t;
From here on out, you would be able to use size_t instead of unsigned int. Note that in C, typedefs can also be used to remove some of the burden associated with declaring structs. In C, struct variables must be declared by a combination of the keyword struct and the name of the struct:

struct my_struct_type my_struct_variable;

This can be annoying, so some programmers use typedef to create shorter names:
typedef struct my_struct_type my_short_type_t;

Leave your vote

Write a Comment