First C Program

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Learn via video course

C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
By Prateek Narang
Free
star5
Enrolled: 1000
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
Prateek Narang
Free
5
icon_usercirclecheck-01Enrolled: 1000
Start Learning

Overview

A C program can be broken down into three main components that are preprocessor directives, header file, and main() function. Preprocessor directives include header files that contain a definition of pre-defined functions used in the program, for example, printf(), sqrt(), and define macros. The main function is the function in every C program which is responsible for the execution and termination of the program. When a C program executes, control reaches the main function first. Variables are used in programs to store values and their name is case sensitive and comments are non-executable lines in code added to provide documentation.

Scope of article

  • This article discusses how C programs are structured and the different components of a C program.
  • We will understand how to write our first C program.
  • We will also understand what statements and expressions are used in a C program.
  • This article does not discuss how C programs are compiled or how the compiler works.

Introduction

C is a general-purpose, procedural programming language and is considered the mother of all modern programming languages. Programs written in C language can run on other machines making it a highly portable language. C program has various functions as part of standard libraries, but it also allows a programmer to add their features and functions and access them anytime we want in our program. Despite several new programming languages, C is still popular because of the speed and efficiency required when working on applications like compilers and operating systems. Today C powers most of the world’s servers and systems.

C is also used extensively in firms where performance is the main concern. For example, C/C++ is a program of choice for High-Frequency Trading firms (HFT) where speed and low latency are the top priorities also, C is used in the gaming industry mainly to build game engines. Database software like MySQL is built using C. It is closer to computer hardware when compared to other languages such as JAVA because C code is directly converted to machine instruction specific to computer architecture.

Before writing our first C program, let us understand the bare minimum structure and components of a C program.

Essential part of C program

Different parts of C programs are

  • Preprocessor directives
  • Header File
  • Main() function
  • Variables
  • Statements and expression
  • Comments

All these different parts are essential in the C programming language. Let us discuss each of the components in detail.

essential-part-of-c-program

1. Preprocessor Directives

Preprocessor is a program that processes our C source code to generate an expanded source code before passing it to the compiler. Before the program is compiled, the preprocessor program processes the source code file to generate an expanded source file, which is then compiled to generate an object file with an extension .obj. Preprocessor directives are used to recognize lines in code that need to be preprocessed. Every preprocessor directive starts with a ** hash (#) symbol. Whichever lines begin with the # symbol are preprocessed by the compiler.

preprocessor directives

As shown in the above figure, if C source code has a preprocessor directive then the preprocessor performs necessary actions before the compilation of code.

Some of the commonly used preprocessor directives are

DirectiveDescriptionExample
#includeIncludes a header and user-defined file in program#include<stdio.h>
#defineAllow defintions of macro in program. Macros which can work similar to functions can also be defined using this directive.#define PI 3.141

#define area(r) (PI*r*r)
#if
#elif
#else
Used to check if a condition is true at compilation time#if !define(PI)
#define PI 3.141
#endif
#errorUsed to print error in stderr#error message

Apart from these C also allows some predefined macros like,

MacroDescription
__TIME __String containing current time in the format HH:MM:SS
__DATE __String containing current date
__LINE __Integer denoting current line number
__FILE __String containing the name of the file

Preprocessor directives can be placed anywhere in code but are generally placed at the beginning of a program to provide more readability to code.

2. Header File

A header file in C contains predefined standard library functions that can be used directly. We can use these functions by including an appropriate header file using the #include preprocessor directive. All header files in C have the extension .h. C program has many default header files which come along with C installation. For example, math.h header file has functions to calculate exponent power pow() and the square root of a number sqrt(). Similarly, to use printf() command we are required to include stdio.h (standard input-output) header file. To use these standard functions, an appropriate header must be included at the beginning of the program.

C allows users to create their header files by grouping several functions in a header file and including them in any program using the preprocessor.

Syntax to include a file

Here file_name is the name of our header file where we stored functions.

Why do we need to include header files in our C program?

It is not efficient to write some of the commonly used codes in the program repeatedly. For example, C has a pre-defined command printf() to display something on the screen. We do not write how printf works, but for our compiler to know the definition of the printf command and what this command does, we include a file that has the definition of these functions. This header file is stdio.h (standard input-output), and we use a preprocessor directive with filename #include<stdio.h> to tell the compiler to include this file before compiling our code. So, #include<stdio.h> tells the compiler to include the contents of file stdio.h in the program before the code is executed.

Note: It is impossible to include the same header file twice.

3. main() Function

Every C program must have a main() function. It is responsible for execution and termination of the program. When a C program executes control reaches the main function first. Main is a user-defined function in C and we can pass parameters to main() according to program requirements. Main function invokes the program code at the run-time and can have any return value.

Example

void main() function

Here, the void keyword is used before function main, which indicates that the function does not return any value. In other words, we use the void data type when we do not want to return any value from the function.

int main() function

In this example, the int keyword indicates the function main returns an integer data type. When we use any return type with the main function, it is compulsory for a function to return a value of specified type (in this case, integer). Because of this, we are using return 0; at the end of the function body to return value 0. Returning 0 indicates successful completion of the program and if any other value is returned from the program, it will represent unsuccessful termination of the program.

Function main() is followed by parentheses {} inside which the body of the function is written.

4. Variables

Variables in C are simply storage areas that are used to store data. Variables are identified using their unique name given by the user. Variables in C can be composed of letters, digits, and underscore. Its name must begin with either a letter or underscore. Variables have a specific data type that determines the size and range of value that variable can hold. Variable names in C are case sensitive.

Syntax of variables in C

or,

Some of the common used variable data types in C are mentioned in the table.

SizeData TypeValue RangeIllustration
char1 byte-128 to 127 or 0 to 255char letter = 'a';
int2 or 4 bytes-32,768 to 32,767 or -2,147,483,648 to 2,147,483,647int number = -100;
unsigned int2 or 4 bytes0 to 65,535 or 0 to 4,294,967,295unsigned_int positiveNumber = 100;
long4 byte-2,147,483,648 to 2,147,483,647long number = 1e9 + 7;
float4 byte1.2E-38 to 3.4E+38float decimal = 2.14;
double8 byte2.3E-308 to 1.7E+308long number = 1e18;

5. Statements and expression

Expression
  • An expression is a command which returns some value, because of which they appear on the right side of an assignment or as a parameter to a function.
  • Expressions are combinations of constants, variables, operators, and literal, giving a certain output.
  • Example

Statements

  • Statements are commands which perform something by receiving input from expressions.
  • Statements consist of specific keywords like printf, scanf.
  • Statement performs a specific action in the program, representing an action or command like assignment statements or print statements.

6. Comments

comments

Comments provide clarity in code by allowing others to read and understand the code. Compilers do not process C comments, and their purpose is to provide documentation in the program.

Syntax of comments

or,

Example program in C

Output

The above example is a simple C program that takes the user name as input and displays their name on the console. Here user_name is used as a variable that can hold 26 characters in its value. Note that we have not defined what functions like printf() and scanf() do but, their definitions are included in header file stdio.h.

Conclusion

  • A C program can be broken down into three main components preprocessor directives, header file, and main() function.
  • Main function is the first function that runs when a program executes. Instructions inside main() are executed in sequential order.
  • Header files are included to program files before the compilation of code, after which the compiler converts code to machine code.
  • Main function contains commands written with the help of statements and expressions.
  • Comments are not compiled and executed by the compiler and are used to comment on single or multiple code lines. Their purpose is to provide documentation in code.