C Programming - Rules

Before writing our first program, let us understand the rules for writing the C program. Every language specification defines its own syntax and rules.

Rules for writing a C Program:

  1. Each instruction is written as a separate statement.
  2. Every C statement must end with a ';' (semicolon), this is known as the statement terminator.
  3. All keywords, syntax should be entered in small case letters.
  4. The statements in a program must appear in the same order in which we wish them to be executed, unless jumping statements are not used.

Hello World | First C Program

To write 'Hello World' which is our first C Program, we need a IDE (Integrated Development Environment), in this example we are using Turbo C++ IDE, which is originally from Borland.

Example 1.1: Write a program to print a Message "Hello World" 1

#include<stdio.h>
#include<conio.h>
void main()
{
    clrscr();
    printf("Hello World"); /* to print the statement */
    getch();
}
Hello World

Code Explanation:

  • #include<stdio.h>: stands for Standard input output, .h – header file. It includes the standard input output library functions like printf(), scanf().
  • #include<conio.h>: Console input output header file. It includes the console (output screen) input output library functions like clrscr(), getch().
  • void main(): main() is a function from where the program starts its execution, void specifies that the function does not return any value. If we specify int then return 0 statement will appear.
  • {...}: Braces or curly brackets define the scope of the program.
  • clrscr(): to clear the screen. It will clear the output screen so that previous output will be cleared when running a new program.
  • printf(): is a function which is used to print data.
  • /*...*/: is used to comment.
  • getch(): used to input a single character. It holds the screen until the user presses any key.

How to compile and run the program

There are 2 ways to compile and run the c program, by menu and by shortcut key.

  1. By Menu: Click on compile menu and select compile option to compile the program. Then, click on run menu and select run option to run the program.
  2. By Shortcut key: Press ALT+F9 keys to compile the program and CTRL+F9 to run the program.

Hello World | VS Code

How to write 'Hello World' in Microsoft Visual Studio Code Editor.

Example 1.2: Write a program to print a Message "Hello World" 1

#include<stdio.h>
void main()
{
    printf("Hello World"); /* to print the statement */
}
Hello World

Code Explanation:

  • In VS Code, no need to write clrscr() and getch() as well as there header file conio.h, as VSCode terminal do this for you.

How to compile and run the program

There are 2 ways to compile and run the c program, by menu and by shortcut key.

  1. By Shortcut Menu: Right click on the program window and select run code.
  2. By Shortcut key: Press CTRL+ALT+N it will compile as well as run the program.

How to accept input from user

Example 2: WAP to Input 2 numbers and find the sum. 1

#include<stdio.h>
void main()
{
    int a, b, c;
    printf("Enter 1st Number: ");
    scanf("%d",&a);
    printf("Enter 2nd Number: ");
    scanf("%d",&b);
    c=a+b;
    printf("Sum = %d",c);
}
Enter 1st Number: 10
Enter 2nd Number: 20
Sum = 30
Code Explanation:
  • scanf(): function is used to receive the values from the user.
  • &: means the address of a variable.