C Programming Language - An Overview
In this tutorial you will learn about C Programming Lanuage, Overview of C, Sample program - Printing a message, Executing a C Program and Basic structure of C programs
Overview of C
C is a programming language. It is most popular computer language today because it is a structured high level, machine independent language. Programmers need not worry about the hardware platform where they will be implemented.
Dennis Ritchie invented C language. Ken Thompson created a language which was based upon a language known as BCPL and it was called as B. B language was created in 1970, basically for unix operating system Dennis Ritchie used ALGOL, BCPL and B as the basic reference language from which he created C.
C has many qualities which any programmer may desire. It contains the capability of assembly language with the features of high level language which can be used for creating software packages, system software etc. It supports the programmer with a rich set of built-in functions and operators. C is highly portable. C programs written on one computer can run on other computer without making any changes in the program. Structured programming concept is well supported in C, this helps in dividing the programs into function modules or code blocks.
Sample program-1
Printing a message
Consider the following message
.
#include
main()
{
...../* Printing begins here */
.....printf (“C is a very good programming language.”);
...../* Printing ends here */
}
.
The first line is a preprocessor command which adds the stdio header file into our program. Actually stdio stands for standard input out, this header file supports the input-output functions in a program.
In a program, we need to provide input data and display processed data on standard output – Screen. The stdio.h header file supports these two activities. There are many header files which will be discussed in future.
The second line main() tell the compiler that it is the starting point of the program, every program should essentially have the main function only once in the program. The opening and closing braces indicates the beginning and ending of the program. All the statements between these two braces form the function body. These statements are actually the C code which tells the computer to do something. Each statement is a instruction for the computer to perform specific task.
The /* .... */ is a comment and will not be executed, the compiler simply ignores this statement. These are essential since it enhances the readability and understandability of the program. It is a very good practice to include comments in all the programs to make the users understand what is being done in the program.
The next statement printf() statement is the only executable line in the above sample program. The printf() function is a standard inbuild function for printing a given line which appears inside the double quotes. Therefore in the standard output device we can see the following line
C is a very good programming language.
The next line is again a comment statement as explained earlier. The closing brace indicates the end of the program.
======================================
Executing a C Program
The following basic steps is carried out in executing a C Program.
1. Type the C lanuage program.
2. Store the program by giving a suitable name and following it with an extension .c
3. Compile the program
4. Debug the errors if any, that is displayed during compile.
5. Run the program.
Basic structure of C programs
.....Documentation Section
.....
.....Link Section
.....
.....Definition Section
.....
.....Global declaration Section
.....
.....main() function section
.....{
..........Declaration Section
.....
..........Executable Section
.....}
.....Sub-program Section
.....function1
.....{
..........Statements
.....}
.....function2
.....{
..........Statements
.....}
.....function3
.....{
..........Statements
.....}
.
The documentation section consists of a set of comment lines giving the name of the program, the author and other details such as a short description of the purpose of the program.
The link section provides instructions to the compiler to link functions from the system library.
The definition section defines all the symbolic constants. The variables can be declared inside the main function or before the main function.
Declaring the variables before the main function makes the variables accessible to all the functions in a C language program, such variables are called Global Variables.
Declaring the variables within main function makes the usage of the variables confined to the main function only and it is not accessible outside the main function.
Every C program must have one main function. Enclosed in the main function is the declaration and executable parts.
In the declaration part we have all the variables.
There is atleast one statement in the executable part.
The two parts must appear between the opening and closing braces
The sub-program section contains all the user-defined functions that are called in the main function.
User-defined functions are generally placed immediately after the main function although they may appear in any order.
C Programming - Arrays
In this tutorial you will learn about C Programming - Arrays - Declaration of arrays, Initialization of arrays, Multi dimensional Arrays, Elements of multi dimension arrays and Initialization of multidimensional arrays.
The C language provides a capability that enables the user to define a set of ordered data items known as an array.
Suppose we had a set of grades that we wished to read into the computer and suppose we wished to perform some operations on these grades, we will quickly realize that we cannot perform such an operation until each and every grade has been entered since it would be quite a tedious task to declare each and every student grade as a variable especially since there may be a very large number.
In C we can define variable called grades, which represents not a single value of grade but a entire set of grades. Each element of the set can then be referenced by means of a number called as index number or subscript.
Declaration of arrays:
Like any other variable arrays must be declared before they are used. The general form of declaration is:
type variable-name[50];
The type specifies the type of the elements that will be contained in the array, such as int float or char and the size indicates the maximum number of elements that can be stored inside the array for ex:
float height[50];
Declares the height to be an array containing 50 real elements. Any subscripts 0 to 49 are valid. In C the array elements index or subscript begins with number zero. So height [0] refers to the first element of the array. (For this reason, it is easier to think of it as referring to element number zero, rather than as referring to the first element).
As individual array element can be used anywhere that a normal variable with a statement such as
G = grade [50];
The statement assigns the value stored in the 50th index of the array to the variable g.
More generally if I is declared to be an integer variable, then the statement g=grades [I];
Will take the value contained in the element number I of the grades array to assign it to g. so if I were equal to 7 when the above statement is executed, then the value of grades [7] would get assigned to g.
A value stored into an element in the array simply by specifying the array element on the left hand side of the equals sign. In the statement
grades [100]=95;
The value 95 is stored into the element number 100 of the grades array.
The ability to represent a collection of related data items by a single array enables us to develop concise and efficient programs. For example we can very easily sequence through the elements in the array by varying the value of the variable that is used as a subscript into the array. So the for loop
for(i=0;i < 100;++i);
sum = sum + grades [i];
Will sequence through the first 100 elements of the array grades (elements 0 to 99) and will add the values of each grade into sum. When the for loop is finished, the variable sum will then contain the total of first 100 values of the grades array (Assuming sum were set to zero before the loop was entered)
In addition to integer constants, integer valued expressions can also be inside the brackets to reference a particular element of the array. So if low and high were defined as integer variables, then the statement
next_value=sorted_data[(low+high)/2]; would assign to the variable next_value indexed by evaluating the expression (low+high)/2. If low is equal to 1 and high were equal to 9, then the value of sorted_data[5] would be assigned to the next_value and if low were equal to 1 and high were equal to 10 then the value of sorted_data[5] would also be referenced.
Just as variables arrays must also be declared before they are used. The declaration of an array involves the type of the element that will be contained in the array such as int, float, char as well as maximum number of elements that will be stored inside the array. The C system needs this latter information in order to determine how much memory space to reserve for the particular array.
The declaration int values[10]; would reserve enough space for an array called values that could hold up to 10 integers. Refer to the below given picture to conceptualize the reserved storage space.
| values[0] |
|
| values[1] |
|
| values[2] |
|
| values[3] |
|
| values[4] |
|
| values[5] |
|
| values[6] |
|
| values[7] |
|
| values[8] |
|
| values[9] | |
The array values stored in the memory.
Initialization of arrays:
We can initialize the elements in the array in the same way as the ordinary variables when they are declared. The general form of initialization off arrays is:
type array_name[size]={list of values};
The values in the list care separated by commas, for example the statement
int number[3]={0,0,0};
Will declare the array size as a array of size 3 and will assign zero to each element if the number of values in the list is less than the number of elements, then only that many elements are initialized. The remaining elements will be set to zero automatically.
In the declaration of an array the size may be omitted, in such cases the compiler allocates enough space for all initialized elements. For example the statement
int counter[]={1,1,1,1};
Will declare the array to contain four elements with initial values 1. this approach works fine as long as we initialize every element in the array.
The initialization of arrays in c suffers two draw backs
1. There is no convenient way to initialize only selected elements.
2. There is no shortcut method to initialize large number of elements.
/* Program to count the no of positive and negative numbers*/ #include<> void main( ) { int a[50],n,count_neg=0,count_pos=0,I; printf(“Enter the size of the array\n”); scanf(“%d”,&n); printf(“Enter the elements of the array\n”); for I=0;I < n;I++) scanf(“%d”,&a[I]); for(I=0;I < n;I++) { if(a[I] < 0) count_neg++; else count_pos++; } printf(“There are %d negative numbers in the array\n”,count_neg); printf(“There are %d positive numbers in the array\n”,count_pos); } |
Multi dimensional Arrays:
Often there is a need to store and manipulate two dimensional data structure such as matrices & tables. Here the array has two subscripts. One subscript denotes the row & the other the column.
The declaration of two dimension arrays is as follows:
data_type array_name[row_size][column_size];
int m[10][20]
Here m is declared as a matrix having 10 rows( numbered from 0 to 9) and 20 columns(numbered 0 through 19). The first element of the matrix is m[0][0] and the last row last column is m[9][19]
Elements of multi dimension arrays:
A 2 dimensional array marks [4][3] is shown below figure. The first element is given by marks [0][0] contains 35.5 & second element is marks [0][1] and contains 40.5 and so on.
| marks [0][0] 35.5 | Marks [0][1] 40.5 | Marks [0][2] 45.5 |
| marks [1][0] 50.5 | Marks [1][1] 55.5 | Marks [1][2] 60.5 |
| marks [2][0] | Marks [2][1] | Marks [2][2] |
| marks [3][0] | Marks [3][1] | Marks [3][2] |
Initialization of multidimensional arrays:
Like the one dimension arrays, 2 dimension arrays may be initialized by following their declaration with a list of initial values enclosed in braces
Example:
int table[2][3]={0,0,01,1,1};
Initializes the elements of first row to zero and second row to 1. The initialization is done row by row. The above statement can be equivalently written as
int table[2][3]={{0,0,0},{1,1,1}}
By surrounding the elements of each row by braces.
C allows arrays of three or more dimensions. The compiler determines the maximum number of dimension. The general form of a multidimensional array declaration is:
date_type array_name[s1][s2][s3]…..[sn];
Where s is the size of the ith dimension. Some examples are:
int survey[3][5][12];
float table[5][4][5][3];
Survey is a 3 dimensional array declared to contain 180 integer elements. Similarly table is a four dimensional array containing 300 elements of floating point type.
/* example program to add two matrices & store the results in the 3rd matrix */ #include<> #include<> void main() { int a[10][10],b[10][10],c[10][10],i,j,m,n,p,q; clrscr(); printf(“enter the order of the matrix\n”); scanf(“%d%d”,&p,&q); if(m==p && n==q) { printf(“matrix can be added\n”); printf(“enter the elements of the matrix a”); for(i=0;i < m;i++) for(j=0;j < n;j++) scanf(“%d”,&a[i][j]); printf(“enter the elements of the matrix b”); for(i=0;i < p;i++) for(j=0;j < q;j++) scanf(“%d”,&b[i][j]); printf(“the sum of the matrix a and b is”); for(i=0;i < m;i++) for(j=0;j < n;j++) c[i][j]=a[i][j]+b[i][j]; for(i=0;i < m;i++) { for(j=0;j < n;j++) printf(“%d\t”,&a[i][j]); printf(“\n”); } } |