In previous two lesson we’ve know what is C? and then I’ve teach you how to install Dev C++ on windows. That’s enough theoretical knowledge for the time being. It’s time to open your IDE and write your first code.

Hello World! – C Program

Consider following C program


#include <stdio.h>

int main() {
   /* My first program */
   printf("Hello, World! \n");

   return 0;
}

Output:

Hello, World!

Above C program or you can say a simple C program consist following things

  1. Preprocessor Commands
  2. Header Files
  3. Statements & Expressions
  4. Functions
  5. Comments
  6. Whitespaces
  7. Escape Sequences

For the time being we’ll not go in deep detail of these things but you must have little bit knowledge what are the purpose of these things.

Preprocessor Commands


#include <stdio.h>

In above program your seeing

Everything in C/C++ program begins with # is a preprocessor directive. Preprocessor tells the compiler to do preprocessing before going to actual program. Like in above example we’re telling the compiler to add stdio.h in our program before moving to the actual program.

Header Files

Header files are those files which consist of function declaration and macro definition. At this stage you don’t have to worry about this. In easy words header files consist of built in functions or macros that helps the user to write the code. For example, in above example stdio.h is a header file which stands for “standard input output”. It consist declaration of standard i/o functions like printf(), scanf() etc.

Statements & Expressions

Statements are expressions, function calls, assignments, control flow statement which make up C program. In-short everything that ends with ; is called statement.

Functions

Functions are the building block of C program. Functions are combination of statement. Different functions can perform different operation as defined. For example printf() is a function in above example used to print anything on console.

Every C/C++ program must have main() function whenever a control enters in program it first looks for main() function. You can say main() is the heart of any C/C++ program. All other function can be called from main().

Comments

Comments are used to give additional useful information to the programmer about a particular line or a particular function. Comments are ignored by the compilers, they are only used to give information to programmers. Above program


/* My first program */

is comment and ignored by compiler.

Whitespaces

In C/C++ whitespaces (i.e. tabs, spaces and enter) are ignored by compiler. For a compiler ; is an end of statement other than that if a statement doesn’t contain ; it’ll give error because it’ll consider 2 lines as single line let me get through this more clearly. Take example of above program


#include <stdio.h>

int main(){ /* My first program */ printf("Hello, World! \n");return 0;}

When you run above program it’ll work as well and will give same output as the previous program. The difference between this program and the previous program is simple this program is difficult to understand by the programmer and will hard to debug if there’ll be any error. Whitespaces provide us the way to indent our program which can me more precisely understandable.

Escape Sequences

As your seeing there is “\n” written at the end of the printf() function. Everything in C/C++ in an output function that’s starts with backslash (“\”) is known as escape sequence. The purpose of these Escape Sequences is to give shape to the output of the program. In this program “\n” is used for new line in output.

Some Important Facts

  1. C/C++ is case sensitive language mean if you write Main() or main() both are meant different for compiler.
  2. Each statement in C/C++ must end with semicolon “;”.
  3. You can call each function or write each statement as much time as you want.
  4. Use comments in program to know what are you doing. It’s a good practice.
  5. Indent your program that’ll help you t understand or debug your program more easily.