First C Program
{tocify} $title= {Table of Contents}
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 Today C powers most of the world’s servers and systems. C is also used extensively in firms where performance is the main concern. Hello, World! Is a very basic entry-level program that every programmer learns in the beginning of a programming language. It is a program in which we display Hello, World! Message on the output screen. C 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 starting the abcd of C language, you need to learn how to write, compile and run the first c program.
To
write the first c program, open the C console and write the following code:
Parts of First 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.
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 can be placed anywhere in code but are generally placed at the
beginning of a program to provide more readability to code. #include
<stdio.h> includes the standard input output library
functions. The printf() function is defined in stdio.h.
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. 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:
#
include <stdio.h>
Here
file_name is the name of our header file where we stored functions. 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. It is impossible to include the same header file twice. For example,
to use the printf() function in a C program, which is used to display anything
on the console screen, the line #include <stdio.h> is required, because
the header file stdio.h contains the printf() function definition. All header
files will have .h extension.
3. The 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.
int
main() function
int main ()
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.
The main() function is the entry point of every program in c language.
In the Hello World code example above, there
was int written before the main() function, remember? Well, that is the return
type of the main() function. The curly braces { } just after the main()
function enclose the body of the main() function.
4. The printf() Function
The printf() is a function that is used to
print(show) anything on the console as output. This function is defined in the
stdio.h header file, which we have included in our C program.
printf (“Hello World! \n”);
The printf() are the functions that are used
to print the data on the console.
5. Return Statement
A return statement is used to return a
response to the caller function. It is generally the last statement of any C
language function. Do not worry about this too, we will cover this statement
when we learn about functions in the C language.
return 0;
In this example, the return 0 statement,
returns execution status to the OS. The 0 value is used for successful
execution and 1 for unsuccessful execution.
6. Semicolon
It is important to note that every statement
in C should end with a semicolon(;). If you miss adding any semicolon, the
compiler will give an error.
How to compile and run the c program
There are 2 ways to compile and run the c
program, by menu and by shortcut.
By menu
Now click on the compile menu then compile
sub menu to compile the c program.
Then click on the run menu then run sub
menu to run the c program.
By shortcut
Or, press ctrl+f9 keys compile
and run the program directly.
You will see the following output on user screen.
You can view the user screen any time by
pressing the alt+f5 keys.
Now press Esc to return to the turbo
c++ console.
Summary
- We have started our journey in programming and just wrote our first C program.
- We know how the compilation
and execution works in a C Program.
- We know different ways to
write Hello World Program in C Language on our system.