Our First Program in C

1.2  Basic Program
Let’s start here with a basic program which is so famous that is used in every beginner tutorial. We are going to discuss Hello World program. This prints (writes) “Hello World” onto the screen.
#include<stdio.h>
int main() {
  printf(“Hello World”);
  return 0;
}
The program above can be written in any text editor but has to be saved with the extension ‘.c’. Then the program must be compiled and run for the outcome. You’ll learn more about these processes in the previous tutorial. For now, when this program is compiled and run it’ll print “Hello World” onto the screen. Codeblocks is an IDE (Integrated Development Environment) which does all the above processes of writing, compiling and running code in one go.
Now let’s take a look at the code. The first line of this C code is called preprocessor directive. These lines start with “#” and are, as the name suggests; directions to the preprocessor. This line says to include a file called “stdio.h” in our program. These files are called header files which have an extension of “.h”. In this context the header file is of “Standard Input Output” which provides Input and Output functions i.e. ways to take input from the devices and output on the devices.
 Following preprocessor directive, we have main function (commonly called). As the name indicates this is the start of our program. Whatever code which is ought to be run is written inside this function.
Let’s examine it closely.
 1.)  int main ( ) {
 2.)      //Code which is intended to be executed/run.
 3.)      return 0;
 4.)  }

The first word indicates an integer (int) which the main function returns to the operating system. This is essential to know as how the program got executed in some cases. The last line is the returning statement. Any function has parameters which are denoted by “( )”. Parameters are followed by body of the function and are denoted by “{ }”. All the code must be written inside the braces of the main function.
-----------------------------------------------------------------------------------------------
Note:
The Program is even valid in most cases whenever you use void in place of int in the main function.
The double forward slash (//) indicates comments. Anything written after them will not be valid code as they are skipped by the compiler. They are only placed in between to guide the programmer. You ’ll learn about them later.
You’ll learn more about functions, their parameters etc, in further tutorials.
-----------------------------------------------------------------------------------------------
In our Hello World program, the code which we ought to run will print “Hello World” on the screen. This is achieved by calling a function called “printf( )” in the body of the main function. It is used to print data in formatted form. As it is a function, it has parameters “( )” which take a string as a value. Strings are any characters placed in between quotes “ ”. Hence as the printf function only processes string we’ve placed our two words in quotes i.e. “Hello World” and passed it as a value.  If you’re wondering where did we get this function from, remember the include statement at the beginning. At last, the statement to print our string must be terminated. All the C statements are ended by semicolon (“;”).  Notice that even the return statement at the end is ended in the same fashion.

This was an overview of a simple beginner code to give you an idea of what C is about. If you don’t get anything, it’s alright. We’ll dig deeper as we go along. Goodbye until our next post…  

Comments

Popular posts from this blog

Basic input, output and variables

Data Types in C

Variable