pre-requisites
You will require a code editor and essential packages to execute C programs. The necessary packages are installed by default on most of the Linux distribution. You can run the following command to check the necessary package is installed or not. The command will display the installed version of gcc.
Example-1: Write and run your first C program
Write the following code using any text editor and save the file with the extension ‘.c’. The header file, stdio.h contains all necessary functions for standard input and output. Any source code of C program starts compilation from the main() method. printf() function is used here to print output in the terminal.
Run the following command to compile and execute the code. The source file name is first.c and executable filename is first_program here.
$ ./first_program
Example-2: Read user input
scanf() function is used in C to read input from the user which is under stdio.h. C language is a strongly typed language and it supports different data types. Integer and char data type are used in this example. A character array of 100 characters is declared by name variable and an integer is declared by age variable. After taking two inputs from the user the formatted values will be printed by prinf() function.
Example-3: Read command-line arguments
argc and argv variables are used as parameters in main() method to read command-line argument values. argc is used to read the total number of arguments and argv is used to read the argument values as an array. How to print total number of command-line arguments and first three argument values are shown in this example.
Example-4: Compare string using conditional statements
strcmp() function is used in C language to compare two strings. If two strings are equal then it returns 0. If the first string is larger than the second string then it returns 1. If the first string is less than the second string then it returns -1. In this example, two numeric values and a string value will be taken as input from the user. If the string value is add then it will print the summation of two numbers. If the string value is sub then it will print the subtraction of two numbers. If both if conditions return false then it will print 0.
#include <string.h>
int main(){
int n1, n2, result;
char operator[10];
printf("Enter first number :");
scanf("%d",&n1);
printf("Enter second number :");
scanf("%d",&n2);
printf("Enter operation name :");
scanf("%s",operator);
if(strcmp(operator,"add") == 0)
result = n1 + n2;
else if(strcmp(operator,"sub") == 0)
result = n1 - n2;
else
result=0;
printf("The result is : %d\n",result);
}
Example-5: Iterate a list of string using for loop
Array variable is declared by using [] in C program. A list of two dimensional character array is declared in this example that contains 5 string values. sizeof() function is used to count the total number of elements of any array in C. for loop is used in this example to iterate the flowers array and print each element value of the flowers array.
int main()
{
char flowers[10][20] = {"Rose", "Poppy", "Lily", "Tulip", "Marigold"};
int total=sizeof(flowers)/sizeof(flowers[0]);
for (int n = 0; n <total; n++)
{
printf("%s\n",flowers[n]);
}
}
Example-6: Find even numbers from a list using while loop
One dimensional array of 10 integer numbers is declared in this example. The of while loop in C language it shown here. The following code will find out all even numbers from numeric array. If the numbers which are divisible by 2 are even numbers. while loop is used here to read each element of the array and check the remainder value after dividing the element by 2. When the remainder value returns 0 for any element then it will be printed.
Example-7: Find out the area of a rectangle using the function
Each function in C contains return type, function name and the parameters. Parameter-less function can also be declared in C. If any function without main() function is declared in the source code then the prototype of that function must be declared before the function declaration. In this example, area() function is declared to calculate the area of any rectangle that contains two parameters to get the height and width values of the rectangle. main() function will read the height and width value from the user and call area() function to calculate and print the area. The prototype of the area() function is declared at the beginning of the code.
int area(int h, int w);
int area(int h, int w)
{
int area = h * w;
return area;
}
int main()
{
int height, width;
printf("Enter the height of the rectangle:");
scanf("%d", &height);
printf("Enter the width of the rectangle:");
scanf("%d", &width);
printf("The area of the rectangle = %d\n",area(height,width));
}
Try yourself:
- Write a C program to take a number as age value of a person and print the person is a teenager or young or old.
- Write a C program to find out a particular string in a list.
- Write a C Program using the function to calculate the area of trapezium.
Conclusion:
The most basic parts of programming are described here using simple examples to start programming with C language. The declarations of different variables, conditional statements, loop and function in C are shown in this article.