A program in C consists of one or more functions, and one of them must be called main, which is the controlling function. Program execution always begins by executing the main function. Additional function definitions may precede or follow main.
A function definition consists of the following components:
The arguments are symbols that represent information being passed between the function and other parts of the program. (Arguments are also referred to as parameters). Each compound statement is enclosed within a pair of braces, i.e., { }. The braces may contain combinations of elementary statements (called expression statements) and other compound statements. Thus, compound statements may be nested, one within another. Each expression statement must end with a semicolon (;). Comments (remarks) may appear anywhere within a program, as long as they are placed within the delimiters /* and */ (e.g.,/* this is a comment */
). Such comments are useful for identifying the principal features of the program and also for explaining the underlying logic.
Let us look at an example C program. Consider the flow-chart below depicting how the roots of a quadratic polynomial can be found:
Based on this flowchart, a sample C program for finding the real roots of a quadratic polynomial is given below:
#include <stdio.h> /* a preprocessor directive to include the file stdio.h */ #include <math.h> /* program to find the real roots of a quadratic polynomial */ main() { /* type declaration of the variable */ float a, b, c, discriminant, d, w, root1, root2; /* function declaration */ float compute_discriminant(float x, float y, float z); printf("Give the values of the coefficients a,b,c of the polynomial:"); scanf("%f,%f,%f", &a, &b, &c); fflush(stdin); /* flushing the buffer associated with standard input */ discriminant = compute_discriminant(a,b,c); /* invoking a function */ d = 2 * a; if(discriminant == 0) printf("The roots are equal %f\n", - b / d); else if(discriminant < 0) printf("The roots are complex\n"); else { w = sqrt(discriminant); printf("The roots are real and different\n"); root1 = (-b - w) / d; root2 = (-b + w) / d; printf("Root1 = %f, Root2 = %f\n",root1,root2); } return; } /* Function definition */ float compute_discriminant (float x, float y, float z) { float d; /* local variable declaration */ d = y * y - 4 * x * z; return d; }
Here the main function utilizes a separate programmer defined function called compute_discriminant, to find the value of the discriminant. Within the programmer defined function, x, y, z are arguments (also called parameters). The values for these parameters are supplied from the main() function. The values of a, b, c are supplied to x, y, z respectively. Then the programmer-defined function performs the calculations and the result is returned to the main.
The programmer defined function is called in the main function, in the statement:
discriminant = compute_discriminant( a, b, c );
The above statement invokes the function compute_discriminant with arguments a, b, and c. The function main() transfers the control of the program execution to the function compute_discriminant(). The called function finally, after doing its work, returns the control back to the calling function, viz. main(), with the result of the calculation performed by it.
You would notice that the main function also includes a function declaration, which indicates that compute_discriminant accepts floating-point arguments and returns a floating-point value.
1. Identify the errors in the following C program: # include { STDIO.H }*/ c program *\ main [ ]{ printf("Welcome to C"} ; Correct the program to display "Welcome to C" on the screen. 2. Match the following: (i) main (a) defined in the header file stdio.h (ii) /*.. */ (b) terminates a expression (iii) ; (c) encloses compound statements (iv) { } (d) the controlling function of a C program (v) printf (e) encloses comment entries 3. Do you find anything odd in the following program? # include <stdio.h> main() { /* printf("Hi! How are you !!!!"); */ } 4. In the function, xyz (a, b, c); a, b and c are known as_____________. 5. LAB WORK: Enter, compile and run the following c program and watch their output. Note that some of the programs contain typical errors which are done by beginner programmers. So, study the errors carefully and correct the programs. a) # include<stdio.h> main() { printf("Welcome to c programming\n"); printf("Hope you enjoy programming\n"); printf("Wish you a good beginning\n\n"); } b) # include <stdio.h. main() { char name [30]; printf("This program demonstrates how C interacts with an user\n"); printf("Enter your name:"); gets(name); fflush(stdin); printf("Your name is : %s\n",name); } c) # include <stdio.h> main() { /* C does quick calculate for you */ /* Multiplication and division */ int num1,num2,result=0; printf("\nEnter the first number:"); scanf("%d",&num1); fflush(stdin); printf("\nEnter the second number:"); scanf("%d",&num2); fflush(stdin); /* multiplication */ result = num1* num2; printf("We get %d after dividing %d by %d\n",result, num1,num2); /* Division */ result = num1 / num2; printf("We get %d after dividing %d by %d\n",result, num1,num2); } d. # include <stdio.h> /* This is a demonstration of modular programming */ main() /* controlling module */ { printf("We are in main function now…\n"); message(); printf(We are back in function main again…\n"); } message() { printf("Hi! We are in message now and returning to main\n"); }
Elements of the C Language - Identifiers, Keywords, Data types and Data objects
The C Language - An Introduction
How to move your Email accounts from one hosting provider to another without losing any mails?
How to resolve the issue of receiving same email message multiple times when using Outlook?
Self Referential Data Structure in C - create a singly linked list
Mosquito Demystified - interesting facts about mosquitoes
Elements of the C Language - Identifiers, Keywords, Data types and Data objects
How to pass Structure as a parameter to a function in C?
Rajeev Kumar is the primary author of How2Lab. He is a B.Tech. from IIT Kanpur with several years of experience in IT education and Software development. He has taught a wide spectrum of people including fresh young talents, students of premier engineering colleges & management institutes, and IT professionals.
Rajeev has founded Computer Solutions & Web Services Worldwide. He has hands-on experience of building variety of websites and business applications, that include - SaaS based erp & e-commerce systems, and cloud deployed operations management software for health-care, manufacturing and other industries.