C Programming Examples for the Basic Concepts

c programming examplesBesides tutorials, going through the logic of C programming examples is another great way to learn the C syntax. You can then attempt to write the same piece of code on your own, and run it to confirm that you get the expected output.

Instead of several examples, let us take a single piece of code to explain all the basic C programming concepts. The requirement is to ask the user to enter the array length and its individual values. The program should then find the highest value, and display it along with its position on the user’s screen.

#include

int main()

{

int array[100], maximum, size, c, location = 1;

printf(“Input the number of elements in the arrayn”);

scanf(“%d”, &size);

printf(“Input %d integersn”, size);

for (c = 0; c < size; c++)

scanf(“%d”, &array[c]);

maximum = array[0];

for (c = 0; c < size; c++)

{

if (array[c] > maximum)

{

maximum = array[c];

location = c+1;

}

}

printf(“The highest number in the array is %d, and it is at location %d.n”, maximum, location);

return 0;

}

Body of the C Program

C programs usually include the following statement at the start, so that the compiler includes the system file that defines the standard input/output library functions:

#include

Anything followed by round brackets is a function. Main() is a special system function written before any other function in any C program; else the compiler will return an error. You can also define your own functions, but this example does not have any. The Return function indicates the end of the program.

Defining and Initializing Variables

There can be several types of variables in C (integer, character, string etc.). In the current code, all variables are defined to hold integer data, and “location” is initialized to 1. Variable values may change through the course of the program.

Data Input/Output from/to User

Printf and scanf are system functions defined in the stdio library. Printf displays text to the user, either asking for data input or showing results. Scanf enables the program to accept the entered values into the corresponding variables. Both functions have two parameters:

  • %d means that only a decimal integer value is acceptable. Similarly, you must use %c and %f for characters and floating point integers respectively.
  • &size tells the C program which variable will hold the input data.

Array

An array is a group of elements having the same data type, and is defined with square brackets following the name. In the example taken here, the array can have upto 100 elements, but its actual length is the user-entered value.

For Loop

For loops perform some processing repeatedly, while changing the value of the variable each time.

for (c = 0; c < size; c++)

This syntax initializes the variable “c” to zero. Its value is incremented by one (c++) in each successive iteration, as long as it is less than the value of “size.” Hence, the loop will be executed for each element in the array.

Check out more C programming examples. Once you are familiar with the basics, you will be able to write advanced applications in C.