top of page
Search

Write a Simple C Program to Print "Hello World!" with Fully Detailed Explanation of Each Syntax



A Simple "Hello World!" C Program:

Below C program is a very simple and basic program in the C programming language. This C program displays “Hello World!” in the output window.


All syntax and commands in C programming are case sensitive.


Each statement should be ended with semicolon (;) which is a statement terminator.

#include <stdio.h>

int main ()

{

/* Our first simple C basic program */ printf("Hello World!");

getch();

return 0;

}

Basic C Commands Explanation:

  • #include <stdio.h> This is a preprocessor command that includes a standard input output header file(stdio.h) from the C library before compiling a C program.

  • int main() This is the main function from where the execution of any C program begins.

  • { This indicates the beginning of the main function.

  • /*_some_comments_*/ Whatever is given inside the command “/* */” in any C program, won’t be considered for compilation and execution.

  • printf(“Hello_World! “); printf command prints the output onto the screen.

  • getch(); This command waits for any character input from keyboard.

  • return 0; This command terminates the C program (main function) and returns 0.

  • } This indicates the end of the main function.


printf() and scanf()

Two commonly used functions for Input and Output tasks in C is printf() and scanf().To use these functions, we must include the stdio library in the source code. To do this just type the following code at the beginning of your program.


#include <stdio.h>


printf() is used to display your messages to the user. These messages can be used to request input from a user or to display the result.


The syntax for printing a simple message:

printf(“Text to be displayed”); Whatever we type within the double quotes will be displayed as such on the screen.

E.g.: printf(“Enter your age”);


Syntax for printing the values stored inside a variable:

printf (“format specifier”,variable1,variable 2); Matching format specifier must be specified for printing values inside a variable. (Refer the list of format specifier giver below) printf() can print any number of variables in a single statement but every variable should be separated by a comma.

E.g.: int a=10;

float b=11.5;

printf(“%d%f”, a,b);


Here the value stored inside the variables a and b will be displayed.


LIST OF FORMAT SPECIFIER:

  • %d Integer

  • %ld Long Integer

  • %f Float

  • %lf Double

  • %c Single character

  • %s String


ESCAPE SEQUENCES IN C:

An escape sequence is a series of characters that represents certain special action. It begins with a backslash character (\). The following is a list of escape sequences.

  • \n prints a new line

  • \b backs up one character

  • \t moves the output position to the next tab stop

  • \\ prints a backslash

  • \" prints a double quote

  • \' prints a single quote


scanf() is used to read the input value from the keyword.

Syntax: scanf(“format specifier”, &variable1, &variable2);

E.g.: int a; float b;

scanf(“%d %f”, &a, &b);


NOTE: C language is case sensitive. For example, printf() and scanf() are different from Printf() and Scanf(). All characters in printf() and scanf() functions must be in lower case.



10 views
bottom of page