Example 1: HelloWorld.c
This program print out the word: Hello World on the screen.

/*
HellowWorld.c
The HelloWorld program
*/

#include <stdio.h>

int main()
{
printf("Hello World.\n");
return 0;
}

Output:

  • /*, */, //: Comment signs. Everything between /* and */ are comments, all words in a line begin with // are also comments
  • #include <stdio.h>:
  • int main(){...}: Main function in C, every C program must have a main function, which can return a value or it can return null etc: void main(){...}
  • printf: a preset function in stdio.h library, which used to print out a string on the the screen.
    ex: printf("Anything string will be printed on to the screen");
  • \n: an escaped character, which equals to a new line character or an enter character. Below are sme escaped characters.
    Escape Sequence Represents

    \a

    Bell (alert)

    \b

    Backspace

    \f

    Formfeed

    \n

    New line

    \r

    Carriage return

    \t

    Horizontal tab

    \v

    Vertical tab

    \'

    Single quotation mark

    \"

    Double quotation mark

    \\

    Backslash

    \?

    Literal question mark

  • return 0; return a zero value for main function.

Example 2: InputDemo.c
This program demonstrate how different types of variables are input and output on standard mode.

/*
InputDemo.c
Demonstrate scanf: %d (int), %f (float), %lf (double)
*/
#include <stdio.h>

int main(void)
{
float temperature=0.0;
int a, b;
double d;

printf("\nEnter your temperature : ");
scanf("%f",&temperature);
printf("The temperature is : %f", temperature);

printf("\nEnter two integer values");
scanf("%d %d", &a, &b);
printf("A=%d, B=%d", a, b);

printf ("\nEnter a real number (double data type):");
scanf ("%lf", &d);
printf ("The number is: %lf", d);

return 0;
}


Example 3: ReadChar.c
Demonstrate getchar & putchar which input and output a single character

/*
ReadChar.c
Demonstrate getchar & putchar
*/
#include <stdio.h>

int main(void)
{
char c;

printf ("\nEnter one character:") ; // message to user
c = getchar () ; // get a character from key board and Stores it in variable C.
printf ("\nThe character you typed is = %c", c);
putchar(c);

return 0;
}