C - Programming













In computing, C  is a general-purpose programming language initially developed by Dennis Ritchie between 1969 and 1973 at AT&T Bell Labs. Like most imperative languages in the ALGOL tradition, C has facilities for structured programming and allows lexical variable scope and recursion, while a static type system prevents many unintended operations. Its design provides constructs that map efficiently to typical machine instructions, and therefore it has found lasting use in applications that had formerly been coded in assembly language, most notably system software like the Unix computer operating system.





C is one of the most widely used programming languages of all time, and C compilers are available for the majority of available computer architectures and operating systems.





The C programming language is a popular and widely used programming language for creating computer programs. Programmers around the world embrace C because it gives maximum control and efficiency to the programmer.





If you are a programmer, or if you are interested in becoming a programmer, there are a couple of benefits you gain from learning C:





You will be able to read and write code for a large number of platforms - everything from micro controllers to the most advanced scientific systems can be written in C, and many modern operating systems are written in C.


The jump to the object oriented C++ language becomes much easier. C++ is an extension of C, and it is nearly impossible to learn C++ without learning C first.





What is C?


C is a computer programming language. That means that you can use C to create lists of instructions for a computer to follow. C is one of thousands of programming languages currently in use. C has been around for several decades and has won widespread acceptance because it gives programmers maximum control and efficiency. C is an easy language to learn. It is a bit more cryptic in its style than some other languages, but you get beyond that fairly quickly.





C is what is called a compiled language. This means that once you write your C program, you must run it through a C compiler to turn your program into an executable that the computer can run (execute). The C program is the human-readable form, while the executable that comes out of the compiler is the machine-readable and executable form. What this means is that to write and run a C program, you must have access to a C compiler. If you are using a UNIX machine (for example, if you are writing CGI scripts in C on your host's UNIX computer, or if you are a student working on a lab's UNIX machine), the C compiler is available for free. It is called either "cc" or "gcc" and is available on the command line. If you are a student, then the school will likely provide you with a compiler -- find out what the school is using and learn about it. If you are working at home on a Windows machine, you are going to need to download a free C compiler or purchase a commercial compiler. A widely used commercial compiler is Microsoft's Visual C++ environment (it compiles both C and C++ programs). Unfortunately, this program costs several hundred dollars. If you do not have hundreds of dollars to spend on a commercial compiler, then you can use one of the free compilers available on the Web. See here as a starting point in your search.





We will start at the beginning with an extremely simple C program and build up from there. I will assume that you are using the UNIX command line and gcc as your environment for these examples; if you are not, all of the code will still work fine -- you will simply need to understand and use whatever compiler you have available.





The Simplest C Program


Let's start with the simplest possible C program and use it both to understand the basics of C and the C compilation process. Type the following program into a standard text editor (vi or emacs on UNIX, Notepad on Windows or TeachText on a Macintosh). Then save the program to a file named samp.c. If you leave off .c, you will probably get some sort of error when you compile it, so make sure you remember the .c. Also, make sure that your editor does not automatically append some extra characters (such as .txt) to the name of the file. Here's the first program: 





#include <stdio.h>


int main()


{


    printf("Hello World!\n");


    return 0;


}








When executed, this program instructs the computer to print out the line "Hello World!" -- then the program quits. You can't get much simpler than that!





To compile this code, take the following steps:




  • On a UNIX machine, type gcc samp.c -o samp (if gcc does not work, try cc). This line invokes the C compiler called gcc, asks it to compile samp.c and asks it to place the executable file it creates under the name samp. To run the program, type samp (or, on some UNIX machines, ./samp).







  • On a DOS or Windows machine using DJGPP, at an MS-DOS prompt type gcc samp.c -o samp.exe. This line invokes the C compiler called gcc, asks it to compile samp.c and asks it to place the executable file it creates under the name samp.exe. To run the program, type samp.







  • If you are working with some other compiler or development system, read and follow the directions for the compiler you are using to compile and execute the program.








You should see the output "Hello World!" when you run the program. Here is what happened when you compiled the program:





If you mistype the program, it either will not compile or it will not run. If the program does not compile or does not run correctly, edit it again and see where you went wrong in your typing. Fix the error and try again.





The Simplest C Program: What's Happening?


Let's walk through this program and start to see what the different lines are doing (Click here to open the program in another window):




  • This C program starts with #include <stdio.h>. This line includes the "standard I/O library" into your program. The standard I/O library lets you read input from the keyboard (called "standard in"), write output to the screen (called "standard out"), process text files stored on the disk, and so on. It is an extremely useful library. C has a large number of standard libraries like stdio, including string, time and math libraries. A library is simply a package of code that someone else has written to make your life easier (we'll discuss libraries a bit later).

  • The line int main() declares the main function. Every C program must have a function named main somewhere in the code. We will learn more about functions shortly. At run time, program execution starts at the first line of the main function.

  • In C, the { and } symbols mark the beginning and end of a block of code. In this case, the block of code making up the main function contains two lines.

  • The printf statement in C allows you to send output to standard out (for us, the screen). The portion in quotes is called the format string and describes how the data is to be formatted when printed. The format string can contain string literals such as "Hello World!," symbols for carriage returns (\n), and operators as placeholders for variables (see below). If you are using UNIX, you can type man 3 printf to get complete documentation for the printf function. If not, see the documentation included with your compiler for details about the printf function.

  • The return 0; line causes the function to return an error code of 0 (no error) to the shell that started execution. More on this capability a bit later.




Printf


The printf statement allows you to send output to standard out. For us, standard out is generally the screen (although you can redirect standard out into a text file or another command).


Here is another program that will help you learn more about printf:


#include <stdio.h>





int main()


{


    int a, b, c;


    a = 5;


    b = 7;


    c = a + b;


    printf("%d + %d = %d\n", a, b, c);


    return 0;


}


Type this program into a file and save it as add.c. Compile it with the line gcc add.c -o add and then run it by typing add (or ./add). You will see the line "5 + 7 = 12" as output.


Here is an explanation of the different lines in this program:



  • The line int a, b, c; declares three integer variables named a, b and c. Integer variables hold whole numbers.

  • The next line initializes the variable named a to the value 5.

  • The next line sets b to 7.

  • The next line adds a and b and "assigns" the result to c. The computer adds the value in a (5) to the value in b (7) to form the result 12, and then places that new value (12) into the variable c. The variable c is assigned the value 12. For this reason, the = in this line is called "the assignment operator."

  • The printf statement then prints the line "5 + 7 = 12." The %d placeholders in the printf statement act as placeholders for values. There are three %d placeholders, and at the end of the printf line there are the three variable names: a, b and c. C matches up the first %d with a and substitutes 5 there. It matches the second %d with b and substitutes 7. It matches the third %d with c and substitutes 12. Then it prints the completed line to the screen: 5 + 7 = 12. The +, the = and the spacing are a part of the format line and get embedded automatically between the %d operators as specified by the programmer.














Printf: Reading User Values

The previous program is good, but it would be better if it read in the values 5 and 7 from the user instead of using constants. Try this program instead:

#include <stdio.h>


int main()

{

    int a, b, c;

    printf("Enter the first value:");

    scanf("%d", &a);

    printf("Enter the second value:");

    scanf("%d", &b);

    c = a + b;

    printf("%d + %d = %d\n", a, b, c);

    return 0;

}



Here's how this program works when you execute it:




  • Make the changes, then compile and run the program to make sure it works. Note that scanf uses the same sort of format string as printf (type man scanf for more info). Also note the & in front of a and b. This is the address operator in C: It returns the address of the variable (this will not make sense until we discuss pointers). You must use the & operator in scanf on any variable of type char, int, or float, as well as structure types (which we will get to shortly). If you leave out the & operator, you will get an error when you run the program. Try it so that you can see what that sort of run-time error looks like.


Let's look at some variations to understand printf completely. Here is the simplest printf statement:




  • printf("Hello");This call to printf has a format string that tells printf to send the word "Hello" to standard out. Contrast it with this:







  • printf("Hello\n");The difference between the two is that the second version sends the word "Hello" followed by a carriage return to standard out.

  • The following line shows how to output the value of a variable using printf.
    printf("%d", b);The %d is a placeholder that will be replaced by the value of the variable b when the printf statement is executed. Often, you will want to embed the value within some other words. One way to accomplish that is like this:
    printf("The temperature is ");printf("%d", b);printf(" degrees\n");An easier way is to say this:
    printf("The temperature is %d degrees\n", b);You can also use multiple %d placeholders in one printf statement:
    printf("%d + %d = %d\n", a, b, c);




In the printf statement, it is extremely important that the number of operators in the format string corresponds exactly with the number and type of the variables following it. For example, if the format string contains three %d operators, then it must be followed by exactly three parameters and they must have the same types in the same order as those specified by the operators.

You can print all of the normal C types with printf by using different placeholders:


int (integer values) uses %d


float (floating point values) uses %f


char (single character values) uses %c


character strings (arrays of characters, discussed later) use %s

You can learn more about the nuances of printf on a UNIX machine by typing man 3 printf. Any other C compiler you are using will probably come with a manual or a help file that contains a description of printf.





C ERRORS TO AVOID




  • Using the wrong character case - Case matters in C, so you cannot type Printf or PRINTF. It must be printf.

  • Forgetting to use the & in scanf

  • Too many or too few parameters following the format statement in printf or scanf

  • Forgetting to declare a variable name before using it












Scanf

The scanf function allows you to accept input from standard in, which for us is generally the keyboard. The scanf function can do a lot of different things, but it is generally unreliable unless used in the simplest ways. It is unreliable because it does not handle human errors very well. But for simple programs it is good enough and easy-to-use.

The simplest application of scanf looks like this:

scanf("%d", &b);

The program will read in an integer value that the user enters on the keyboard (%d is for integers, as is printf, so b must be declared as an int) and place that value into b.

The scanf function uses the same placeholders as printf:




  • int uses %d

  • float uses %f

  • char uses %c

  • character strings (discussed later) use %s




You MUST put & in front of the variable used in scanf. The reason why will become clear once you learn about pointers. It is easy to forget the & sign, and when you forget it your program will almost always crash when you run it.

In general, it is best to use scanf as shown here -- to read a single value from the keyboard. Use multiple calls to scanf to read multiple values. In any real program, you will use the gets or fgets functions instead to read text a line at a time. Then you will "parse" the line to read its values. The reason that you do that is so you can detect errors in the input and handle them as you see fit.

The printf and scanf functions will take a bit of practice to be completely understood, but once mastered they are extremely useful.

Try This!

Modify this program so that it accepts three values instead of two and adds all three together:

#include <stdio.h>


int main()

{

    int a, b, c;

    printf("Enter the first value:");

    scanf("%d", &a);

    printf("Enter the second value:");

    scanf("%d", &b);

    c = a + b;

    printf("%d + %d = %d\n", a, b, c);

    return 0;

}

You can also delete the b variable in the first line of the above program and see what the compiler does when you forget to declare a variable. Delete a semicolon and see what happens. Leave out one of the braces. Remove one of the parentheses next to the main function. Make each error by itself and then run the program through the compiler to see what happens. By simulating errors like these, you can learn about different compiler errors, and that will make your typos easier to find when you make them for real.




Variables


As a programmer, you will frequently want your program to "remember" a value. 


For example, if your program requests a value from the user, or if it calculates a value, you will want to remember it somewhere so you can use it later. 


The way your program remembers things is by using variables.


For example:


int b;


This line says, "I want to create a space called b that is able to hold one integer value." A variable has a name (in this case, b) and a type (in this case, int, an integer). 


You can store a value in b by saying something like:


b = 5;


You can use the value in b by saying something like:


printf("%d", b);


In C, there are several standard types for variables:


int - integer (whole number) values


float - floating point values


char - single character values (such as "m" or "Z")





Datatypes





C has a concept of 'data types' which are used to define a variable before its use. The definition of a variable will assign storage for the variable and define the type of data that will be held in the location.





The value of a variable can be changed any time.





C has the following basic built-in datatypes.







  • int - data type








int is used to define integer numbers.





    {


        int Count;


        Count = 5;


    }







  • float - data type








float is used to define floating point numbers.








    {


        float Miles;


        Miles = 5.6;


    }




  • double - data type








double is used to define BIG floating point numbers. It reserves twice the storage for the number. On PCs this is likely to be 8 bytes.








    {


        double Atoms;


        Atoms = 2500000;


    }




  • char - data type








char defines characters.





    {


        char Letter;


        Letter = 'x';


    }





Modifiers


The data types explained above have the following modifiers.




  • short

  • long

  • signed

  • unsigned





The modifiers define the amount of storage allocated to the variable. The amount of storage allocated is not cast in stone. ANSI has the following rules:





        short int <=    int <= long int


            float <= double <= long double





What this means is that a 'short int' should assign less than or the same amount of storage as an 'int' and the 'int' should be less or the same bytes than a 'long int'. What this means in the real world is:








                 Type Bytes             Range
---------------------------------------------------------------------

            short int  2          -32,768 -> +32,767          (32kb)
unsigned short int 2 0 -> +65,535 (64Kb)
unsigned int 4 0 -> +4,294,967,295 ( 4Gb)
int 4 -2,147,483,648 -> +2,147,483,647 ( 2Gb)
long int 4 -2,147,483,648 -> +2,147,483,647 ( 2Gb)
signed char 1 -128 -> +127
unsigned char 1 0 -> +255
float 4
double 8
long double 12





These figures only apply to today's generation of PCs. Mainframes and midrange machines could use different figures, but would still comply with the rule above.





You can find out how much storage is allocated to a data type by using the sizeof operator discussed in Operator Types Session.





Here is an example to check size of memory taken by various datatypes.





int


main()


{


  printf("sizeof(char) == %d\n", sizeof(char));


  printf("sizeof(short) == %d\n", sizeof(short));


  printf("sizeof(int) == %d\n", sizeof(int));


  printf("sizeof(long) == %d\n", sizeof(long));


  printf("sizeof(float) == %d\n", sizeof(float));


  printf("sizeof(double) == %d\n", sizeof(double));


  printf("sizeof(long double) == %d\n", sizeof(long double));


  printf("sizeof(long long) == %d\n", sizeof(long long));





  return 0;


}





Qualifiers


A type qualifier is used to refine the declaration of a variable, a function, and parameters, by specifying whether:




  • The value of a variable can be changed.

  • The value of a variable must always be read from memory rather than from a register





Standard C language recognizes the following two qualifiers:







  • const

  • volatile





The const qualifier is used to tell C that the variable value can not change after initialization.





const float pi=3.14159;





Now pi cannot be changed at a later time within the program.





Another way to define constants is with the #define preprocessor which has the advantage that it does not use any storage





The volatile qualifier declares a data type that can have its value changed in ways outside the control or detection of the compiler (such as a variable updated by the system clock or by another program). This prevents the compiler from optimizing code referring to the object by storing the object's value in a register and re-reading it from there, rather than from memory, where it may have changed. You will use this qualifier once you will become expert in "C". So for now just proceed.





What are Arrays:


We have seen all baisc data types. In C language it is possible to make arrays whose elements are basic types. Thus we can make an array of 10 integers with the declaration.





int x[10];


The square brackets mean subscripting; parentheses are used only for function references. Array indexes begin at zero, so the elements of x are:





Thus Array are special type of variables which can be used to store multiple values of same data type. Those values are stored and accessed using subscript or index.





Arrays occupy consecutive memory slots in the computer's memory.





x[0], x[1], x[2], ..., x[9]


If an array has n elements, the largest subscript is n-1.





Multiple-dimension arrays are provided. The declaration and use look like:





        int name[10] [20];


      n = name[i+j] [1] + name[k] [2];


Subscripts can be arbitrary integer expressions. Multi-dimension arrays are stored by row so the rightmost subscript varies fastest. In above example name has 10 rows and 20 columns.





Same way, arrays can be defined for any data type. Text is usually kept as an array of characters. By convention in C, the last character in a character array should be a `\0' because most programs that manipulate character arrays expect it. For example, printf uses the `\0' to detect the end of a character array when printing it out with a `%s'.





Here is a program which reads a line, stores it in a buffer, and prints its length (excluding the newline at the end).





       main( ) {


               int n, c;


               char line[100];


               n = 0;


               while( (c=getchar( )) != '\n' ) {


                       if( n < 100 )


                               line[n] = c;


                       n++;


               }


               printf("length = %d\n", n);


       }





Array Initialization





As with other declarations, array declarations can include an optional initialization


Scalar variables are initialized with a single value


Arrays are initialized with a list of values


The list is enclosed in curly braces


int array [8] = {2, 4, 6, 8, 10, 12, 14, 16};


The number of initializers cannot be more than the number of elements in the array but it can be less in which case, the remaining elements are initialized to 0.if you like, the array size can be inferred from the number of initializers by leaving the square brackets empty so these are identical declarations:





int array1 [8] = {2, 4, 6, 8, 10, 12, 14, 16};


int array2 [] = {2, 4, 6, 8, 10, 12, 14, 16};


An array of characters ie string can be initialized as follows:





char string[10] = "Hello";






Branching and Looping

In C, both if statements and while loops rely on the idea of Boolean expressions. Here is a simple C program demonstrating an if statement:

#include 

int main() 



int b; 

printf("Enter a value:"); 

scanf("%d", &b); 

if (b < 0) 

printf("The value is negativen"); 

return 0; 

}

This program accepts a number from the user. It then tests the number using an if statement to see if it is less than 0. If it is, the program prints a message. Otherwise, the program is silent. The (b < 0) portion of the program is the Boolean expression. C evaluates this expression to decide whether or not to print the message. If the Boolean expression evaluates to True, then C executes the single line immediately following the if statement (or a block of lines within braces immediately following the if statement). If the Boolean expression is False, then C skips the line or block of lines immediately following the if statement.

Here's slightly more complex example:

#include <stdio.h>


int main()

{

    int b;

    printf("Enter a value:");

    scanf("%d", &b);

    if (b < 0)

        printf("The value is negative\n");

    return 0;

}

In this example, the else if and else sections evaluate for zero and positive values as well.

Here is a more complicated Boolean expression:

if ((x==y) && (j>k))

    z=1;

else

    q=10;



This statement says, "If the value in variable x equals the value in variable y, and if the value in variable j is greater than the value in variable k, then set the variable z to 1, otherwise set the variable q to 10." You will use if statements like this throughout your C programs to make decisions. In general, most of the decisions you make will be simple ones like the first example; but on occasion, things get more complicated.

Notice that C uses == to test for equality, while it uses = to assign a value to a variable. The && in C represents a Boolean AND operation.

Here are all of the Boolean operators in C:

   equality          ==

  less than         <

  Greater than      >

  <=                <=

  >=                >=

  inequality        !=

  and               &&

  or                ||

  not               !



You'll find that while statements are just as easy to use as if statements. For example:

while (a < b)

{

    printf("%d\n", a);

    a = a + 1;

}

This causes the two lines within the braces to be executed repeatedly until a is greater than or equal to b. The while statement in general works as illustrated to the right.

C also provides a do-while structure:

#include <stdio.h>


int main()

{

    int a;


    printf("Enter a number:");

    scanf("%d", &a);

    if (a)

    {

        printf("The value is True\n");

    }

    return 0;

}

The for loop in C is simply a shorthand way of expressing a while statement. For example, suppose you have the following code in C:

x=1;

while (x<10)

{

    blah blah blah

    x++; /* x++ is the same as saying x=x+1 */

}

You can convert this into a for loop as follows:

for(x=1; x<10; x++)

{

    blah blah blah

}

Note that the while loop contains an initialization step (x=1), a test step (x<10), and an increment step (x++). The for loop lets you put all three parts onto one line, but you can put anything into those three parts. For example, suppose you have the following loop:

a=1;

b=6;

while (a < b)

{

    a++;

    printf("%d\n",a);

}

You can place this into a for statement as well:

for (a=1,b=6; a < b; a++,printf("%d\n",a));

It is slightly confusing, but it is possible. The comma operator lets you separate several different statements in the initialization and increment sections of the for loop (but not in the test section). Many C programmers like to pack a lot of information into a single line of C code; but a lot of people think it makes the code harder to understand, so they break it up.



Looping: A Real Example

Let's say that you would like to create a program that prints a Fahrenheit-to-Celsius conversion table. This is easily accomplished with a for loop or a while loop:

#include <stdio.h>


int main()

{

    int a;

    a = 0;

    while (a <= 100)

    {

        printf("%4d degrees F = %4d degrees C\n",

            a, (a - 32) * 5 / 9);

        a = a + 10;

    }

    return 0;

}

If you run this program, it will produce a table of values starting at 0 degrees F and ending at 100 degrees F. The output will look like this:

   0 degrees F =  -17 degrees C

  10 degrees F =  -12 degrees C

  20 degrees F =   -6 degrees C

  30 degrees F =   -1 degrees C

  40 degrees F =    4 degrees C

  50 degrees F =   10 degrees C

  60 degrees F =   15 degrees C

  70 degrees F =   21 degrees C

  80 degrees F =   26 degrees C

  90 degrees F =   32 degrees C

 100 degrees F =   37 degrees C

The table's values are in increments of 10 degrees. You can see that you can easily change the starting, ending or increment values of the table that the program produces.

If you wanted your values to be more accurate, you could use floating point values instead:

#include <stdio.h>


int main()

{

    float a;

    a = 0;

    while (a <= 100)

    {

        printf("%6.2f degrees F = %6.2f degrees C\n",

            a, (a - 32.0) * 5.0 / 9.0);

        a = a + 10;

    }

    return 0;

}

You can see that the declaration for a has been changed to a float, and the %f symbol replaces the %d symbol in the printf statement. In addition, the %f symbol has some formatting applied to it: The value will be printed with six digits preceding the decimal point and two digits following the decimal point.

Now let's say that we wanted to modify the program so that the temperature 98.6 is inserted in the table at the proper position. That is, we want the table to increment every 10 degrees, but we also want the table to include an extra line for 98.6 degrees F because that is the normal body temperature for a human being. The following program accomplishes the goal:

#include <stdio.h>


int main()

{

    float a;

    a = 0;

    while (a <= 100)

    {

if (a > 98.6)

        {

            printf("%6.2f degrees F = %6.2f degrees C\n",

                98.6, (98.6 - 32.0) * 5.0 / 9.0);

        }

        printf("%6.2f degrees F = %6.2f degrees C\n",

            a, (a - 32.0) * 5.0 / 9.0);

        a = a + 10;

    }

    return 0;

}

This program works if the ending value is 100, but if you change the ending value to 200 you will find that the program has a bug. It prints the line for 98.6 degrees too many times. We can fix that problem in several different ways. Here is one way:

#include <stdio.h>


int main()

{

    float a, b;

    a = 0;

    b = -1;

    while (a <= 100)

    {

if ((a > 98.6) && (b < 98.6))

        {

            printf("%6.2f degrees F = %6.2f degrees C\n",

                98.6, (98.6 - 32.0) * 5.0 / 9.0);

        }

        printf("%6.2f degrees F = %6.2f degrees C\n",

            a, (a - 32.0) * 5.0 / 9.0);

        b = a;

        a = a + 10;

    }

    return 0;

}


Errors to Avoid

Putting = when you mean == in an if or while statement

Forgetting to increment the counter inside the while loop - If you forget to increment the counter, you get an infinite loop (the loop never ends).

Accidentally putting a ; at the end of a for loop or if statement so that the statement has no effect - For example: for (x=1; x<10; x++); printf("%d\n",x); only prints out one value because the semicolon after the for statement acts as the one line the for loop executes.





Arrays

In this section, we will create a small C program that generates 10 random numbers and sorts them. To do that, we will use a new variable arrangement called an array.

An array lets you declare and work with a collection of values of the same type. For example, you might want to create a collection of five integers. One way to do it would be to declare five integers directly:

int a, b, c, d, e;

This is okay, but what if you needed a thousand integers? An easier way is to declare an array of five integers:

int a[5];

The five separate integers inside this array are accessed by an index. All arrays start at index zero and go to n-1 in C. Thus, int a[5]; contains five elements. For example:

int a[5];



a[0] = 12;

a[1] = 9;

a[2] = 14;

a[3] = 5;

a[4] = 1;

One of the nice things about array indexing is that you can use a loop to manipulate the index. For example, the following code initializes all of the values in the array to 0:

int a[5];

int i;



for (i=0; i<5; i++)

    a[i] = 0;

The following code initializes the values in the array sequentially and then prints them out:

#include <stdio.h>



int main()

{

    int a[5];

    int i;



    for (i=0; i<5; i++)

        a[i] = i;

    for (i=0; i<5; i++)

        printf("a[%d] = %d\n", i, a[i]);

}

Arrays are used all the time in C. To understand a common usage, start an editor and enter the following code:

#include <stdio.h>



#define MAX 10



int a[MAX];

int rand_seed=10;



/* from K&R

   - returns random number between 0 and 32767.*/

int rand()

{

    rand_seed = rand_seed * 1103515245 +12345;

    return (unsigned int)(rand_seed / 65536) % 32768;

}



int main()

{

    int i,t,x,y;



    /* fill array */

    for (i=0; i < MAX; i++)

    {

        a[i]=rand();

        printf("%d\n",a[i]);

    }



    /* more stuff will go here in a minute */



    return 0;

}

This code contains several new concepts. The #define line declares a constant named MAX and sets it to 10. Constant names are traditionally written in all caps to make them obvious in the code. The line int a[MAX]; shows you how to declare an array of integers in C. Note that because of the position of the array's declaration, it is global to the entire program.

The line int rand_seed=10 also declares a global variable, this time named rand_seed, that is initialized to 10 each time the program begins. This value is the starting seed for the random number code that follows. In a real random number generator, the seed should initialize as a random value, such as the system time. Here, the rand function will produce the same values each time you run the program.

The line int rand() is a function declaration. The rand function accepts no parameters and returns an integer value. We will learn more about functions later. The four lines that follow implement the rand function. We will ignore them for now.

The main function is normal. Four local integers are declared, and the array is filled with 10 random values using a for loop. Note that the array a contains 10 individual integers. You point to a specific integer in the array using square brackets. So a[0] refers to the first integer in the array, a[1] refers to the second, and so on. The line starting with /* and ending with */ is called a comment. The compiler completely ignores the line. You can place notes to yourself or other programmers in comments.

Now add the following code in place of the more stuff ... comment:

/* bubble sort the array */

for (x=0; x < MAX-1; x++)

    for (y=0; y < MAX-x-1; y++)

        if (a[y] > a[y+1])

        {

            t=a[y];

            a[y]=a[y+1];

            a[y+1]=t;

        }

/* print sorted array */

printf("--------------------\n");

for (i=0; i < MAX; i++)

printf("%d\n",a[i]);



This code sorts the random values and prints them in sorted order. Each time you run it, you will get the same values. If you would like to change the values that are sorted, change the value of rand_seed each time you run the program.

The only easy way to truly understand what this code is doing is to execute it "by hand." That is, assume MAX is 4 to make it a little more manageable, take out a sheet of paper and pretend you are the computer. Draw the array on your paper and put four random, unsorted values into the array. Execute each line of the sorting section of the code and draw out exactly what happens. You will find that, each time through the inner loop, the larger values in the array are pushed toward the bottom of the array and the smaller values bubble up toward the top.

TRY THIS!

In the first piece of code, try changing the for loop that fills the array to a single line of code. Make sure that the result is the same as the original code.

Take the bubble sort code out and put it into its own function. The function header will be void bubble_sort(). Then move the variables used by the bubble sort to the function as well, and make them local there. Because the array is global, you do not need to pass parameters.

Initialize the random number seed to different values.

C ERRORS TO AVOID

C has no range checking, so if you index past the end of the array, it will not tell you about it. It will eventually crash or give you garbage data.

A function call must include () even if no parameters are passed. For example, C will accept x=rand;, but the call will not work. The memory address of the rand function will be placed into x instead. You must say x=rand();.



More on Arrays

Variable Types

There are three standard variable types in C:

Integer: int

Floating point: float

Character: char

An int is a 4-byte integer value. A float is a 4-byte floating point value. A char is a 1-byte single character (like "a" or "3"). A string is declared as an array of characters.

There are a number of derivative types:

double (8-byte floating point value)

short (2-byte integer)

unsigned short or unsigned int (positive integers, no sign bit)

Operators and Operator Precedence

The operators in C are similar to the operators in most languages:

+ - addition

- - subtraction

/ - division

* - multiplication

% - mod

The / operator performs integer division if both operands are integers, and performs floating point division otherwise. For example:

void main()

{

    float a;

    a=10/3;

    printf("%f\n",a);

}

This code prints out a floating point value since a is declared as type float, but a will be 3.0 because the code performed an integer division.

Operator precedence in C is also similar to that in most other languages. Division and multiplication occur first, then addition and subtraction. The result of the calculation 5+3*4 is 17, not 32, because the * operator has higher precedence than + in C. You can use parentheses to change the normal precedence ordering: (5+3)*4 is 32. The 5+3 is evaluated first because it is in parentheses. We'll get into precedence later -- it becomes somewhat complicated in C once pointers are introduced.

Typecasting

C allows you to perform type conversions on the fly. You do this especially often when using pointers. Typecasting also occurs during the assignment operation for certain types. For example, in the code above, the integer value was automatically converted to a float.

You do typecasting in C by placing the type name in parentheses and putting it in front of the value you want to change. Thus, in the above code, replacing the line a=10/3; with a=(float)10/3; produces 3.33333 as the result because 10 is converted to a floating point value before the division.

Typedef

You declare named, user-defined types in C with the typedef statement. The following example shows a type that appears often in C code:

#define TRUE  1

#define FALSE 0

typedef int boolean;



void main()

{

    boolean b;



    b=FALSE;

    blah blah blah

}



This code allows you to declare Boolean types in C programs.

If you do not like the word "float'' for real numbers, you can say:

typedef float real;

and then later say:

real r1,r2,r3;

You can place typedef statements anywhere in a C program as long as they come prior to their first use in the code.

Structures

Structures in C allow you to group variable into a package. Here's an example:

struct rec

{

    int a,b,c;

    float d,e,f;

};



struct rec r;

As shown here, whenever you want to declare structures of the type rec, you have to say struct rec. This line is very easy to forget, and you get many compiler errors because you absent-mindedly leave out the struct. You can compress the code into the form:

struct rec

{

    int a,b,c;

    float d,e,f;

} r;

where the type declaration for rec and the variable r are declared in the same statement. Or you can create a typedef statement for the structure name. For example, if you do not like saying struct rec r every time you want to declare a record, you can say:

typedef struct rec rec_type;

and then declare records of type rec_type by saying:

rec_type r;

You access fields of structure using a period, for example, r.a=5;.

Arrays

You declare arrays by inserting an array size after a normal declaration, as shown below:

int a[10];        /* array of integers */

char s[100];      /* array of characters

                    (a C string) */

float f[20];      /* array of reals */

struct rec r[50]; /* array of records */

Incrementing

Long Way     Short Way

i=i+1;       i++;

i=i-1;       i--;

i=i+3;       i += 3;

i=i*j;       i *= j;





Functions

Most languages allow you to create functions of some sort. Functions let you chop up a long program into named sections so that the sections can be reused throughout the program. Functions accept parameters and return a result. C functions can accept an unlimited number of parameters. In general, C does not care in what order you put your functions in the program, so long as a the function name is known to the compiler before it is called.

We have already talked a little about functions. The rand function seen previously is about as simple as a function can get. It accepts no parameters and returns an integer result:

int rand()

/* from K&R

   - produces a random number between 0 and 32767.*/

{

    rand_seed = rand_seed * 1103515245 +12345;

    return (unsigned int)(rand_seed / 65536) % 32768;

}

The int rand() line declares the function rand to the rest of the program and specifies that rand will accept no parameters and return an integer result. This function has no local variables, but if it needed locals, they would go right below the opening { (C allows you to declare variables after any { -- they exist until the program reaches the matching } and then they disappear. A function's local variables therefore vanish as soon as the matching } is reached in the function. While they exist, local variables live on the system stack.) Note that there is no ; after the () in the first line. If you accidentally put one in, you will get a huge cascade of error messages from the compiler that make no sense. Also note that even though there are no parameters, you must use the (). They tell the compiler that you are declaring a function rather than simply declaring an int.

The return statement is important to any function that returns a result. It specifies the value that the function will return and causes the function to exit immediately. This means that you can place multiple return statements in the function to give it multiple exit points. If you do not place a return statement in a function, the function returns when it reaches } and returns a random value (many compilers will warn you if you fail to return a specific value). In C, a function can return values of any type: int, float, char, struct, etc.

There are several correct ways to call the rand function. For example: x=rand();. The variable x is assigned the value returned by rand in this statement. Note that you must use () in the function call, even though no parameter is passed. Otherwise, x is given the memory address of the rand function, which is generally not what you intended.

You might also call rand this way:

if (rand() > 100)

Or this way:

rand();

In the latter case, the function is called but the value returned by rand is discarded. You may never want to do this with rand, but many functions return some kind of error code through the function name, and if you are not concerned with the error code (for example, because you know that an error is impossible) you can discard it in this way.

Functions can use a void return type if you intend to return nothing. For example:

void print_header()

{

    printf("Program Number 1\n");

    printf("by Marshall Brain\n");

    printf("Version 1.0, released 12/26/91\n");

}

This function returns no value. You can call it with the following statement:

print_header();

You must include () in the call. If you do not, the function is not called, even though it will compile correctly on many systems.

C functions can accept parameters of any type. For example:

int fact(int i)

{

    int j,k;



    j=1;

    for (k=2; k<=i; k++)

        j=j*k;

    return j;

}

returns the factorial of i, which is passed in as an integer parameter. Separate multiple parameters with commas:

int add (int i, int j)

{

    return i+j;

}

C has evolved over the years. You will sometimes see functions such as add written in the "old style," as shown below:

int add(i,j)

    int i;

    int j;

{

    return i+j;

}

It is important to be able to read code written in the older style. There is no difference in the way it executes; it is just a different notation. You should use the "new style," (known as ANSI C) with the type declared as part of the parameter list, unless you know you will be shipping the code to someone who has access only to an "old style" (non-ANSI) compiler.














Comments

Popular posts from this blog

Write a Program to Add two 3x3 Matrix using C

C program for Unit Conversion

Write a Program to Add two 5x5 Matrix using C