programminginc.net

C Language Programming

  • Home
  • C Examples
Home > C Programming > C Program to Print Hello World!

C Program to Print Hello World!

The Hello World! program displays a message on the screen using the printf function, then exits. printf is defined in the stdio.h library of input/output functions. That is why we use #include directive.

Note the structure of the program: include directives, followed by the definition of the function main. The body of the function — the working part of the definition — is the part enclosed in curly braces. Note also the comments: the text enclosed /* like this */. Comments are meant to enlighten the human reader; the compiler ignores them.

A Simple C Program to print “Hello World”.

#include<stdio.h> 
main() { 
      printf("Hello World\n") ; 
}

The first line tells the compiler which header file to include in the program. Header files contain information that the computer uses when your program is made into an executable program. The second line contains the mainfunction header. A main function header is needed in every C program, because the program starts execution at this header. The function header is followed by an open brace { to show where the function begins. This open brace is matched by the closed brace } on line 4. The closed brace signifies the end of the function.

The third line causes the string “Hello world!” to appear on the screen. This line is composed of a function call, printf, an argument surrounded in parentheses, (“Hello world!\n”), and is terminated by a semicolon, ;.

A slightly more complicated “Hello World” program.

/* 
   It is  a good idea to include comments
   that describe the purpose of a program.
*/

#include<stdio.h>

main() {
   printf("Hello World\n") ;
   printf("My name is Marvin the Paranoid Android.") ;  // no \n
   printf("I have brains the size of a planet.\n") ;
}

Another simple Hello world program showing usage of printf with variable declaration.

/* 
   More about Marvin and Earth.
*/

#include<stdio.h>

main() {

int dm ;     // dm is an integer variable
double pi ;  // pi is a floating point variable

   printf("Hello World\n") ;
   printf("My name is Marvin the Paranoid Android.") ;  // no \n
   printf("I have brains the size of a planet.\n") ;

   dm = 7926 ;
   pi = 3.14159265 ;

   printf("\n\nHow big is that?\n") ;
   printf("The planet Earth has a diameter of %d miles at the equator\n", dm) ;
   printf("Which means the circumference of earth is about %f miles at the equator.\n",
      dm * pi ) ; 

   printf("\n") ;
   printf("\n") ;

   printf("That's pretty big!\n") ;

   /* comments can go anywhere */
   // single line comments using double slashes.
   
   printf("\n\nTo might find me (Marvin) working at Milliways") ; 
   printf("(the Restaurant at the End of the Universe).\n") ;

   // Here 'End' refers to time, not location.

}

post ajax

No post

Copyright © 2021  programminginc.net