First C Program

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Before delving into the ABCD of the C language, it's imperative to grasp the basics of writing, compiling, and running the first C program. The initial C program typically begins with including the standard input-output library functions using #include <stdio.h>. The int main() function acts as the program's entry point, and within it, the printf() function prints data to the console. The return 0 statement signifies successful execution (0 for success, 1 for failure). Starting with a simple "Hello World!" program, C beginners can use the printf function to display the iconic message. To execute locally, install a C compiler, or use an online compiler for practice.

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.