In this article, we will see

  1. How to configure a VIM editor? (Windows users can skip this)
  2. How to write a C program that will print some messages onto the screen?

Configure VIM editor:

Open your Linux terminal and enter the below command and hit ENTER to navigate to the HOME directory.

$ cd ~

Enter the below command to create a new file i.e. “.vimrc”

Once vim editor is opened, press “i” to enter into INSERT mode.

Type all the below config info in the .vimrc file. After copying, press ESC:wq to save & exit from the editor.

syntax on
filetype plugin indent on
set tabstop=4
set shiftwidth=4
set expandtab ts=4 sw=4 ai
highlight Normal ctermfg=black ctermbg=white

Now create a new file named welcome.c using the below command.

vim welcome.c

Type below example program in the VIM editor.

#include <stdio.h>
int main()
{
    printf("Welcome to C tutorials");
    return 0;
}

Save and exit from the editor by pressing ESC:wq and hit ENTER

Now compile and execute the program using the below commands

$ gcc welcome.c
$ ./a.out

Header file inclusion:

#include <stdio.h>

stdio.h  –  Standard input and output

“stdio.h” is a header file.

  • It contains the declarations of functions, global variables and few other macros that will be shared among multiple C files.
  • The extension of a header file is “.h”.

We can add header files into our program in two different ways:

  • <filename.h>  Use ankle brackets, if the header file is in standard path.  Example: <header_file_name.h>
  • “filename.h”  Use double quotes, If a header file is in the user-defined path. Example: “header_file_name.h”
  • If you compile the above program, it will display the Hello world! message on to the screen. How is it possible? Did we write any code regarding the interaction with the display? No. Because it is a very complex code and it was already written by the people who developed C.  Our task is just to use that code and need to get our work done.
  • As I said earlier, In C we have functions that will be used to write a block of code. So, C creators have chosen a function name called printf which contain a block of code that will be used to write a message onto the screen.
  • The file stdio.h will contain the function declarations such as printf, scanf and few macros related to the input and output operations.
  • Examples of header files: stdio.h, math.h, file.h

We will see more about header files in our pre-processor tutorial.

#include:

“#include” is a pre-processor directive and is used to import header files into the C program.

  • Syntax: #include <header_file_name.h>   (For standard header files)
  • Syntax: #include “header_file_name.h”    (For user-defined header files)

Main function:

int main()

In C programming, execution starts from the main function. We can’t write a program without a main function. So it is a mandatory function in C

  • Here “int” is an “Integer Datatype”. We can use ‘int’ in different contexts. For example, In the above example, we are using “int” as a return type to a function. This means that the main function will return an integer value as a status to the calling function.
  • The open and closed round brackets after main will specify main as a function. We will see more about “data types” and “functions” in further articles.

printf:

printf("Hello world!");

As I explained earlier, printf is a standard function that is used to display a message on the screen.

  • The printf function definition is available in the “glibc” library and the function declaration or prototype is available in the “stdio.h” header file. So, in order to use printf, we need to include the “stdio.h” header file into our program.

Return:

return 0;

The “return” statement is used to return a value from the function.

  • In this example, we are returning ‘0’ i.e. an integer value from the main function because we mentioned the return type of main() as an integer.

Syntax: return <return_value>;

Categorized in:

Tagged in: