Monday, July 9, 2012

Float Data Types & Variables in C Programming Language

Integer and character data types are incapable of storing numbers with fractional parts. Depending on the precision required, C provides two variable types for computation with "floating point" numbers, i.e. numbers with a decimal (internally a binary) point. Such numbers are called floats because the binary point can only be represented in the binary- digits expansion of the number, in which it is made to "float" to the appropriate "position" for optimal precision. (You can immediately see the difficulty of imagining a binary "point" within any particular bit of a floating point word, which can contain only a 0 or a 1!) Typically, some of the leftmost bits of a floating point number store its characteristic (the positive or negative power of two to which it is raised), and the remaining its mantissa, the digits which comprise the number. In base 10, for example, if 2.3 is written as 0.0023 *103, the mantissa is 0.0023, and the characteristic (exponent) is 3.

Single precision floating point variables are declared by the float specification, for example:

float bank_balance = 1.234567E8;

/*En means 10 raised to the power n */

The scientific notation En, where the lowercase form en is also acceptable, is optional; one may alternatively write:

float bank_balance = 123456700.0;

Floats are stored in four bytes and are accurate to about seven significant digits; on PCs their range extends over the interval [E-38, E37].

It must never be lost sight of that the floating point numbers held in a computer's memory are at best approximations to real numbers. There are two reasons for this shortcoming. First, the finite extent of the word size of any computer forces a truncation or round-off of the value to be stored; whether a storage location is two bytes wide, or four, or even eight, the value stored therein can be precise only to so many binary digits.

Second, it is inherently impossible to represent with unlimited accuracy some fractional values as a finite series of digits preceded by a binary or decimal point. For example

1/7 = 0. 142857142857142857 ..... ad infinitum

As long as a finite number of digits is written after the decimal point, you will be unable to accurately represent the fraction 1/7. But rational fractions that expand into an infinite series of decimals aren't the only types of floating point numbers that are impossible to store accurately in a computer. Irrational numbers such as the square root of 2 have a periodic (non- repeating) expansions__there's no way that you can predict the next digit, as you could in the, expansion of 1/7 above, at any point in the series. Therefore it's inherently impossible to store such numbers infinitely accurately inside the machine. PI, the ratio of the circumference of any circle to its diameter, is another number that you cannot represent as a finite sequence of digits. It's not merely irrational, it's a transcendental number. (It cannot be the root of any algebraic equation with rational coefficients.) So an element of imprecision may be introduced by the very nature of the numbers to be stored.

Third, in any computation with floating point numbers, errors of round-off or truncation are necessarily introduced. For suppose you multiply two n-bit numbers; the result will in general be a 2n-bit number. If this number (2n) of bits is larger than the number of bits in the location which will hold the result, you will be forcing a large object into a small hole! Ergo, there'll be a problem! Some of it will just have to be chopped off. Therefore it is wisest to regard with a pinch of salt any number emitted by a computer as the result of a computation, that has a long string of digit-, after the decimal point. It may not be quite as accurate as it seems.

The %e, %f and %g format conversion characters are used in the scant () and printf () functions to read and print floating point numbers. %e (or %E) is used with floating point numbers in exponent format, while %g (or %G) may be used with floats in either format. %g (or %G) in the printf () outputs a float variable either as a string of decimal numbers including a decimal point, or in exponent notation, whichever is shorter. An uppercase E or G prints an uppercase E in the output. We shall be discussing more about format control in a later Unit.) Program 2.15 below finds the average of five numbers input from the keyboard, and print, it:

/* Program 1 */
#include <stdio.h>  
main()  
{   
 float val_1, val_2, val_3, val_4, val_5, total 0.0, avg;   
 pritf ("\nEnter first number...");   
 scanf ("%f", &val_1);   
 printf ("\nEnter third number...");  
 printf ("\nEnter second number...");   
 scanf ("%f', &,val_2);   
 scanf ("%f', &val_3);   
 printf ("\nEnter fourth number...");  
 scanf ("%f", &val_4);   
 avg = total / 5;  
 printf ("\nEnter fifth number...");   
 scanf ("%f", &val_5);  
 total = val_1 + val_2 + val_3 + val_4 + val_5;  
 printf ("\nThe average of the numbers you entered is: %f\n", avg);   
} 

Here's a sample conversation with the program:

Enter first number .. 32.4
Enter second number .. 56.7
Enter third number .. 78.3
Enter fourth number .. 67.8
Enter fifth number... -93.9
The average of the numbers you entered is: 28.260000

Character Data Types in C Programing Language

Character Data Types or variables are used to store single characters from the ASCII set. They're accommodated in a single byte. Character variables are declared and defined as in the statement below:

char bee = 'b', see = 'C', ccc;

This declaratory statement assigns the character constant 'b' to the char variable bee, and the character constant 'c' to the char variable named see. The char variable ccc is undefined.

Character constants are single characters. They must be enclosed in right single quotes. Escape sequences may be also assigned to character variables in their usual backslash notation, with the "compound character" enclosed in right single quotes. Thus the statement:

char nextline = '\n';

assigns the escape sequence for the newline character to the char variable nextilne. [In ANSI C a character constant is a sequence of one or more characters enclosed in single quotes. Precisely how the value of such a constant is to be interpreted is left to the implementation.]

Since character variables are accommodated in a byte, C regards chars as being a sub- range of ints, (the subrange that fits inside a byte) and each ASCII character is for all purposes equivalent to the decimal integer value of the bit picture which defines it. Thus 'A', of which the ASCII representation is 01000001, has the arithmetical value of 65 decimal. This is the decimal value of the sequence of bits 01000001, as you may easily verify. In other words, the memory representation of the char constant 'A' is indistinguishable from that of the int constant, decimal 65.

The upshot of this is that small int values may be stored in char variables, and char values may he stored in int variables! Character variables are therefore signed quantities restricted to the range [-128, 127]. However, it is a requirement of the language that the decimal equivalent of each of the printing characters be non-negative.

We are assured then that in any C implementation in which a char is stored in an 8-bit byte, the corresponding int value will always be a non-negative quantity, whatever the value of the leftmost (sign) bit may be. Now, identical bit patterns within a byte may be treated as a negative quantity by one machine, as a positive by another. For ensuring portability of programs which store non-character data in char variables the unsigned char declaration is useful: it changes the range of chars to [0, 255].

[Note :However, the ANSI extension signed char explicitly declares a signed character type to override, if need be, a possible default representation of unsigned chars.]

One consequence of the fact that C does not distinguish between the internal representation of byte-sized ints and chars is that arithmetic operations which are allowed on ints are also allowed on chars! Thus in C, if you wish to, you may multiply one character value by another.

Here are some variables declared as chars, and defined as escape sequences:

char newline = '\n',
char single_quote = '\"';

Character constants can also be defined via their octal ASCII codes. The octal value of the character is preceded by a backslash, and is enclosed in single quotes:

char terminal_bell = '\07';
/* 7 = octal ASCII code for beep */

char backspace = '\010';
/ *10 = octal code for backspace */

[Note: For ANSI C Compilers character constants may be defined by hex digits instead of octals. Hex digits are preceded by x, unlike 0 in the case of octals. Thus in ANSI C:

char backspace =\x A';

is an acceptable alternative declaration to

char backspace = '\010';

Any number of digits may be written, but the value stored is undefined if the resulting character value exceeds the limit of char.]

On an ASCII machine both '\b' and '\010' are equivalent representations. Each will print the backspace character. But the latter form, the ASCII octal equivalent of '\b', will not work on an EBCDIC machine, typically an IBM mainframe, where the collating sequence of the characters (i.e., their gradation or numerical ordering) is different. In the interests of portability therefore it is preferable to write '\b' for the backspace character, rather than its octal code. Then your program will work as certifiably on an EBCDIC machine as it will on an ASCII.

Note that the character constant 'a' is not the same as the string "a". (We will learn later that a string is really an array of characters, a bunch of characters stored in consecutive memory locations, the last location containing the null character; so the string "a" really contains two chars, 'a' immediately followed by '\0'). It is important to realise that the null character is not the same as the decimal digit 0, the ASCII code of which is 00110000.

Just as %d in printf () or scanf () allows us to print and read ints, %c enables the input and output of single characters which are the values of char variables. Let's look at Programs 2. 10 and 2.11 below:

/* Program 1 */ 
#include <stdio.h>
main ( )
{ 
char a = 'H', b = 'e', c 'I', d 'o', newline '\n';
printf ("%c", a); 
printf ("%c", b); 
printf ("%c", c); 
printf ("%c", c); 
printf ("%c", d); 
printf ("%c", newline); 
}

The output of Program 1 is easily predictable (what is it?).

/* Program 2 */ 
#include <stdio.h>
main ( )
{ 
char val_1, val_2; 
int vai_3; 
printf ("Press any of the keys a - z, then press \n"); 
scanf ("%c", &val_1); 
printf ("Press another key in a - z, then press \n"); 
scanf ("%c", &val_2); 
printf ("Assuming you're working on an ASCII machine, \nthe chars that you typed have decimal equivalents %d and %d, respectively", val_1, val_2); 
val_3 = val_1 * val_2; 
printf ("Their product is %d\n", val_3); 
}

Execute the above program to verify that char variables behave like one byte ints in arithmetic operations.

The %c format conversion character in a printf () outputs escape sequences, as you saw in Program 2. Execute Program 3 if you want a little music:

/* Program 3 */ 

#include <stdio.h> 
main ( )
{ 
char bell = '\007'; /* octal code of the terminal bell */ 
char x = 'Y', y = 'E', z = 'S', exclam = '!'; 
printf ("Do you hear the bell ? %c%c%c%c%c%c%c", bell, x, bell, y, bell, z, exclam); 
}

Program 4 assigns a character value to an int variable, an integer value to a char variable, and performs a computation involving these variables. Predict the output of the program, and verify your result by executing it

/* Program 4 */ 
#include <stdio.h> 
main ( )
{ 
int alpha = 'A', beta; 
char gamma = 122; beta = gamma - alpha; 
printf ("beta seen as an int is: %d\n", beta); 
printf ("beta seen as a char is: %c\n", beta); 
}

End-0f-File (EOF) character

One character that is often required to be sensed in C programs is not strictly speaking a character at all: it's the EOF or End-0f-File character, and its occurrence indicates to a program that the end of terminal or file input has been reached. Because the EOF is not a character, being outside the range of chars, any program that's written to sense it must declare an int variable to store character values. As we see in Program 2.13, this is always possible to do. An int variable can accommodate all the characters assigned to it, and can also accommodate EOF. We'll see uses for EOF in later units.

The putcular ( )function (pronounced "put character"), which takes a character variable or constant as its sole argument, is often a more convenient alternative for screen output thin is the printf ( ) When this function is invoked (for which purpose you may need to #include stdio.h), the character equivalent of its argument is output on the terminal, at the current position of the cursor:

putchar (char_var);

Suppose char_var has been assigned the value 'A'. Then 'A' will be displayed where the cursor was.

Reciprocally, getchar ( ) pronounced "get character") gets a single character from the keyboard, and can assign it to a char variable. (stdio.h 'may have to be #included before getchar ( )can be used.) It has no arguments, and is typically invoked in the following way:

char_var = getchar ( );

When such a statement is encountered, the execution of the program is stayed until a key is pressed. Then char_var is assigned the character value of the key that was pressed.

Program 5 below illustrates the usage of these functions. Your compiler may require the inclusion of stdio.h to invoke getchar ( ) and putchar ( ).

/* Program 5 */ 
#include <stdio.h> 
main ( )
{ 
char key_pressed; 
printf ("Type in a lowercase letter (a - z), press :"); 
key_pressed = getchar ( ); /* get char from keyboard*/ 
printf ("You pressed"); 
putchar (key_pressed - 32); /* put upperease char on Terminal*/ 
putchar ('\n');

/* this program converts to uppercase because ASCII decimal equivalent is 32 less than for the corresponding lower case character. */
}

Saturday, July 7, 2012

Range Modifiers for Integer Variables in C

On occasions you may need to work with strictly non-negative integers, or with integers in a shorter or longer interval than the default for ints. The following types of range modifying declarations are possible:

(I) unsigned

usage: unsigned int stadium_seats;

This declaration "liberates" the sign bit, and makes the entire word (including the freed sign bit) available for the storage of non-negative integers. [Note: The sign bit_the leftmost bit of a memory word _ determines the sign of the contents of the word when it's set to 1, the value stored in the remaining bits is negative. Most architectures use two's complement arith- metic, in which the sign bit is "weighted", i.e. it has an associated place value which is nega- tive. Thus on a 16-bit machine its value is -215, or -32,768. So a 16-bit signed number such as 10000000 00111111 would have the value 20 + 21 + 22+ 23 +24 + 25 -215 = -32,705. As an unsigned integer this string of bits would have the value 32831.] On PCs the unsigned decla- ration allows for int variables the range [0, 65535] and is useful when one deals with quan- tities which are known beforehand to be both large and non-negative, e.g. memory addresses, a stadium's seating capacity, etc.

Just as %d in-the printf ( ) prints decimal int values, %u is used to output unsigned ints, as the program below illustrates. Execute the program and determine its output also examine the effect changing the % u format conversion specifiers to %d in the printf Os. Can you explain your results?

#include <stdio.h> 
main()
{
    unsigned int stadium_seats, tickets_sold, standing_viewers;
    stadium_seats = 40000;
    tickets_sold = 50000;
    standing_viewers = tickets_sold - stadium_seats;
    printf ("Tickets sold: %u\n", tickets_sold);
    printf ("Scats available: %u\n", stidium_scats);
    printf ("There could be a stajnpede because\,n");
    printf ("there may be nearly %u standees at the match.\n", standing_viewers);
}

(II) short
usage: short int friends;

The short int declaration may be useful in instances where an integer variable is known beforehand to be small. The declaration above ensures that the range of friends will not ex- ceed that of ints, but on some computers the range may be shorter (e.g. -128 through 127) friends may be accommodated in a byte, thus saving memory. There.was a time in the olden days of computing,,when main memory was an expensive resource, that programmers tried by such declarations and other stratagems to optimise core usage to the extent possible. (The VAX computer uses two bytes to store short ints, half the amount it uses for ints but for present-day PCs, with memory cheap and plentiful, most compiler writers make no distnc- tion between ints and short ints.)

The %d specification in the printf ( ) is also used to output short ints.

(III) unsigned short
usage: unsigned short int books

The range of books will not exceed that of unsigned ints it may be shorter.

(IV) long
usage: long int stars_in_galaxy;

This declaration is required when you need to use integers larger than the default for ints. On most computers long ints are 4-byte integers ranging over the interval [-2147483648, 2147483647]. When a long integer constant is assigned a value the letter L (or I) must be written immediately after the rightmost digit: long int big_num = 123456789OL;

% ld in the printf 0 outputs long decimal ints, as you may verify by executing

#include <stdio.h> 
main ( )
{
    long int population-2000 = 123456789OL;
    printf ("The population of this country in 2000 AD\,n");
    printf ("will exceed %Id if we do\n", populaton_2000);
    printf ("not take remedial steps now.\n");
}


(V) unsigned long
usage: unsigned long int population_2000

The unsigned long declaration transforms the range of long ints to the set of 4-byte non- negative integers. Here population_2000 is allowed to range over [0, 4294967295] (Let's hope that larger-sized words will not be required to store this value!) unsigned longs are output by the %lu format conversion specifier in the printf 0.

In the above declarations shorter forms are allowed: thus you may write:
unsigned letters; /* instead of unsigned int letters*/
long rope; /* insted of long int rope*/
unsigned short note; /* instead of unsigned short int note*/ etc.

Integer Data Types in C Programming Language

C program variables and constants are of four types: char, int, float and double. Before an identifier can be used in a C program its type must be explicitly declared. Here's a declaratory statement of C:

int apples;

This statement declares the programmer's intent that apples will store a signed integer value, i.e. apples may be either a positive or a negative integer within the range set for variables of type int. Now this range can be very much machine dependent; it depends, among other things on the word size of your machine. For most compilers for the IBM PC ints are stored in two consecutive bytes, and are restricted to the range [-32768, 32767]. Compare this with the VAX or the Macintosh, where ints are,4-byte signed integers in the range [-2147483648, 2147483647]. In declaring apples to be an int you are telling the compiler how much space to allocate for its storage. You have also made an assumption of the range in which you expect its value to lie.

In contrast to Pascal, in C it is possible and in fact usual to both declare a variable's type and, where needed, define its value in the same statement:

int salary = 5000;

It is not correct to assume that a variable which has only been declared e.g.:

int volts;
/* volts is unpredictable */

but has not been defined, i.e. assigned a value, automatically gets the value 0. In fact its value may be anything but 0! Note that the thousands, millions or billions place values are never separated by commas, when constants are defined in C programs.

Let's 1(x)k at Program 2.1, and its output. The program adds two ints x and y, and prints their sum, z. Don't worry if you can't understand everything about the program just yet. Only remember that the printf () can be. used to print numbers just as easily as it prints strings. To print an int x the following printf () will do:

printf ("The value of x is: %d\n", x);

The %d signifies that x is to be printed as a decimal integer.

#include <stdio.h>
main()
{ 
int x = 5, y = 7, z; 
z = x + y; 
printf ("The value of x is: %d\n", x); 
printf ("The value of y is: %d\n", y); 
printf ("Their sum, z, is: %d\n", z); 
}

The output of Program is appended below.
The value of x is: 5
The value of y is: 7
Their sum, z, is: 12

To understand the very great importance of using variables in a computation only after having assigned them values, execute the following program and determine its output on your computer:


#include <stdio.h>
main()
{
int x, y, z; /* x, y and z are undefined. */ /* contd. */
z = x + y ; 
printf ("The value of x is: %d\n", x); 
printf ("The value of y is: %d\n", y); 
printf ("Their sum, z, is: %d\n", z);
}
On our machine, a VAX 11/780 from Digital Equipment Corporation, U.S.A. the output was:

The value of x is: 2146744409 The value of y is: 2146744417 Their sum, z, is: -1478470

Now, looking at the output of this program, could you possibly have predicted the values x, y and z would get? Moral: Never assume a variable has a meaningful value, unless you give it one.

Tuesday, April 3, 2012

Escape Sequences in C Programming Language

Let’s have a look at a simple C program:


#include
main ( )
{
printf ("This is easy !!\n");
}


You might be wondering about the \n (pronounced backslash n) in the string argument of the function printf ( ):

"This is easy!!\n"

The \n is an example of an escape sequence: it's used to print the newline character. If You have executed Programs 1.1 or 1.2 you will have noticed that the \n doesn't appear in the output Each \n in the string argument of a printf ()causes the cursor to be placed at the beginning of die next line of output. Think of an escape sequence as a "substitute character" for printing hard-to-get characters. In an earlier section we learnt that s are not allowed in strings., but including \n's within one makes it easy to output it in several lines: if you wish to print a string in two or more lines, place the \n wherever you want to insert a new line in the output, as in the example below:

main ( )
{
printf ("This\nstring\nwill\nbe\nprinted\,nin\n9\nlines.\n");
}

Each \n will force the cursor to be positioned at the beginning of the next line on the screen, from where further printing will begin. Here is part of the output from executing the above program:

This
string
will
etc.

If a string does not contain a \n at its end, the next string to be printed will begin in the same line as the last, at the current position of the cursor, i.e. alongside the first string.

We have seen that the "double quotes" character is used to mark the beginning and end of a C string. How then can the "double quotes" character itself be included within a string? This time the escape sequence \" comes to our aid: it prints as “ in the output. Similarly, the escape sequence, \\ helps print \, and \' the single quote character,'. All escape sequences in C begin with a backslash.

Be careful to remember that "" is not the escape sequence for the "double quote" character (as it is in some versions of BASIC). In C "" is the null string., the null string is not "empty ", as one might have thought; it contains a single character, the null character, ASCII 0 (in which all bits are set to zero). We will see later that the last character of every C string is the (invisible) null character. Its presence helps the computer determine the actual end of the string.

Other escape sequences available in C are:

\a

ring terminal bell (the a is for alert) [ANSI] extension]

\?

question mark [ANSI extension]

\b

Backspace

\r

carriage return

\f

Formfeed

\t

horizontal tab

\V

vertical tab

\O

ASCII null character

Placing any of these within a string causes either the indicated action, or the related character to be output.

A Simple C Program

The best way to learn C or any programming language is to begin writing programs in it; so here's our first C program:

# include <stdio.h>
main()
{
     printf ("This is easy !!\n");
}

There are a few important points to note about this program, points which you will find common to all C programs.

Programs in C consist of functions, one of which must be main (). When a C program is executed, main ( ) is where the action starts. Then other functions may be "invoked". Here you can ask a very basic question that what a function is? A function is a sub-program that contains instructions or statements to perform a specific computation on its variables. When its instructions have been executed, the function returns control to the calling program, to which it may optionally he made to return the results of its computations. Because main ( ) is a function, too, from which control returns back to the operating system at program termination, in ANSI C it is customary, although not required, to include a statement in main ( ) which explicitly returns control to the operating environment. In ANSI C the above program is correctly written as follows:

/*ANSI C Version */ 
#include <stdio.h>
void main (void)
{
    printf ("This is easy!!\n");
    return 0;
}

We'll learn more about functions as we go along our C programming tutorial, but for now you may recognize them by the presence of parentheses after their names.

Apart from main ( ), another function in the program above is the printf ( ). printf ( ) is an example of a "library function". It's provided by the compiler, ready for you to use. In addition to its variables, a function, including main ( ), may optionally have arguments, which are listed in the parentheses following the function name. The arguments of functions are somewhat different from the arguments that people have: they are values of the parameters in terms of which the function is defined, and are passed to it by the calling program. The instructions which comprise a function are listed in terms of its parameters and its variables.

In the example above, main ( ) has no arguments. printf ( ) has one: it's the bunch (more properly: string) of characters enclosed in double quotes:
"This is easy!!\n"

When the printf ( ) is executed, its built-in instructions process this argument. The result is that the string is displayed on the output device, which we will usually assume is a terminal. The output of the program will be:
This is easy!!

With the cursor stationed at the beginning of the next line on the screen. In C a string is not a piece of thread: it's any sequence of characters enclosed in double quotes. A string must not include a "hard" carriage-return (), i.e. all its characters must be keyed in on a single line on the terminal.

"This is most certainly not a C string. It contains a carriage return character."

As we have seen in the case of printf () when a function is invoked, it is passed (the values of ) its arguments. It then executes its instructions, using these values. On completion the function returns control to the module from which it was invoked. The nice thing about a function is that it may be called as often as required in a program, to perform the same computational steps on the different sets of values that are passed to it. Thus there can be several invocations to printf ( ) within a program, with different string arguments each time.

Thursday, March 29, 2012

An Overview of C Programming Language

C is a general purpose computer programming language. It was originally created by Dennis M. Ritchie (circa 1971) for the specific purpose of rewriting much of the Unix operating system. This now famous operating system was at that time in its infancy: it had recently been written for a discarded DEC PDP - 7 computer by Ken Thompson, an electrical engineer and a colleague of Ritchie's at Bell Labs. Thompson had written the first UNIX in a language he chose to call B. The intention of these programmers was to port UNIX on to other, architecturally dissimilar machines. Clearly, using any assembly language was out of the question: what was required was a language that would permit assembly-like (i.e. low or hardware level) operations, such as bit manipulation, and at the same time be run able on different computers. None of the languages then available could have served the purpose: a new one was called for, and C was born. The rest is history: UNIX became spectacularly successful, in large measure because of its portability, and C, in which most of it was written, has since come to occupy a pre-eminent position in die development of systems programs.

The success of UNIX, and the consequent popularity of C for systems programming, forced it on the attention of applications programmers, who came to appreciate the rich variety of its operators and its control structures. These enable, and in fact encourage, the practice of modular programming: the individual sub-tasks that make up a large program can be written in independent blocks or modules, each of manageable size. In other words, the language possesses features which make possible a "divide and conquer" approach to large programming tasks. When programs are organized as modules they are necessarily well-planned, because each module contains only the instructions for a well-defined activity. Such programs are therefore more easily comprehensible by other readers than unstructured programs.

Another desirable, quality of modular Programs is that they may be modified without much difficulty. If changes are required, only a few modules may be affected, leaving the remainder of the program intact.

And last but not least, such programs are easier to debug, because errors can be localized to modules, and removed. For a concrete example suppose that you have to write a program to determine all prime numbers up to ten million which are of the form k * k + 1, where k is an integer (2, 5, 17 and 37 are trivial examples of such primes). Then one module of the program could be written to generate the number k * k + 1 for the next admissible value of k; and another module to test whether the last number generated is a prime. Organized in this way, not only will the program be elegant, it would also be easy to debug, understand or modify.

Wednesday, February 1, 2012

Find the size of data types in C

This is a simple program to find the size of different basic data types in C Programming Language. I have added only four data types you can add more if you wish.
#include <stdio.h>
#include <malloc.h>

main()
{
int i;
printf("Size of an Integer \t=\t %d (Bytes)\n",sizeof(int));
printf("Size of a Character \t=\t %d (Bytes)\n",sizeof(char));
printf("Size of a Float \t=\t %d (Bytes)\n",sizeof(float));
printf("Size of a Double \t=\t %d (Bytes)\n",sizeof(double));
}



THE OUTPUT WILL BE LIKE THIS:

 


Tuesday, December 20, 2011

What is bridge in computer networking?

Segmenting a large network with a network device has numerous benefits. Among these are reduced collisions (in an Ethernet network), contained bandwidth utilization, and the ability to filter out unwanted packets. However, if the addition of the interconnect device required extensive reconfiguration of stations, the benefits of the device would be outweighed by the administrative overhead required to keep the network running. Bridges were created to allow network administrators to segment their networks transparently. This means that individual stations need not know whether there is a bridge separating them or not. It is up to the bridge to make sure that packets get properly forwarded to their destinations. This is the fundamental principle underlying all of the bridging behaviors.

Bridges work at the Data Link layer of the OSI model. Since bridges work in the Data Link layer they do not examine the network layer addresses. They just look at the MAC addresses for Ethernet and Token Ring, token bus and determine whether or not to forward or ignore a packet.

Functions of a Bridge
  1. Isolates networks by MAC addresses
  2. Manages network traffic by filtering packets
  3. Translates from one MAC protocol to another

Now let us examine the functionality of a bridge in detail.

1. Isolates networks by MAC addresses

A bridge divides a network into separate collision domains. This reduces congestion as only frames that need to be forwarded are sent across interfaces. All transmissions between nodes connected to same segment are not forwarded and therefore do not load the rest of the network.

Thus bridges effectively improve the bandwidth of the network by reducing the unnecessary traffic in the network.

For example, if you have one segment called Segment 100: it has 50 users (in several departments) using this network segment. The Engineering Department is CAD (Computer Aided Design) - oriented, while the Accounting Department is into heavy number crunching (year end reports, month end statements, etc.). On this network, any traffic between Clients of Accounting Department and the Accounting File Server (in the Accounting Department) will be heard across the Segment 100. Likewise, any traffic between the Engineering Dept clients (to the CAD File Server) will be heard throughout the Network Segment. The result is that “Other” Department accesses to the Generic File Server are incredibly slow: this is because of the unnecessary traffic that’s being generated from other departments (Engineering and Accounting).

The solution is to use one Bridge to isolate the Accounting Department, and another bridge to isolate the Engineering Department. The Bridges will only allow packets to pass through that are not on the local segment. The bridge will first check its “routing” table to see if the packet is on the local segment. If it is, it will ignore the packet, and not forward it to the remote segment. If Client of Accounting Department sends a packet to the Accounting File Server then Bridge #1 will check its routing table (to see if the Accounting File Server is on the local port). If it is on the local port, then Bridge #1 will not forward the packet to the other segments. If a Client of Accounting Department sends a packet to the Generic File Server, Bridge #1 will again check its routing table to see if the Generic File Server is on the local port. If it is not, then Bridge #1 will forward the packet to the remote port.

2. Manages network traffic by filtering packets

Bridges listen to the network traffic, and build an image of the network on each side of the bridge. This image of the network indicates the location of each node (and the bridge’s port that accesses it). With this information, a bridge can make a decision whether to forward the packet across the bridge - if the destination address is not on the same port - or, it can decide not to forward the packet (if the destination is on the same port).

This process of deciding whether or not to forward a packet is termed “filtering packets.” Network traffic is managed by deciding which packets can pass through the bridge; the bridge filters packets.

3. Translates from one protocol to another

The MAC layer also contains the bus arbitration method used by the network. This can be CSMA/CD, as used in Ethernet, or Token Passing, as used in Token Ring. Bridges are aware of the Bus Arbitration and special translation bridges can be used to translate between Ethernet and Token Ring LANs.

Bridges physically separate a network segment by managing the traffic (that’s based on the MAC address). Bridges are store and forward devices. They receive a packet on the local segment, store it, and wait for the remote segments to be clear before forwarding the packet. The two physical types of bridges are Local and Remote Bridges.


Monday, December 19, 2011

What are the functions of Repeaters

All types of network connections suffer from attenuation and pulse distortion. For a given cable specification and bit rate, each has a maximum length of cable. Repeaters can be used to increase the maximum interconnection length and will do the following:

Functions of Repeaters
  • Clean signal pulses.
  • Pass all signals between attached segments.
  • Boost signal power.
  • Possibly translate between two different media types (e.g., fiber – optic to twisted – pair cable)