C programming basics

05 min read

Structure of a C Program

  • Every C program consists of one or more modules called functions. One of the functions must be the main function. Program execution always starts from the main function, which may, later on, call other functions.
  • C programming is Case-sensitive. So, follow the same case throughout.

 

Task: Write a program which would print the message 'Hello World'

Solution:

1) For displaying any content on to the screen, the function most commonly used is printf( ) Any information that is given within double quotes in the printf statement will be displayed as it is on the screen. At last, there is a semicolon because every simple C statement should be ended up with a semicolon

printf("Hello World");

2) Every C statement must be enclosed within a function (for a simple program it is usually the main function).

int main( )
{
printf("Hello World");
}

3) Basic functions(Built-in functions) like printf( ), scanf( ) etc are defined in the header file 'stdio.h' . Hence this header file must be included if these functions are to be used in the program. The first line is a reference to this file. The inclusion of this required information will be handled automatically by the compiler.

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

4) In the below program,  the word 'int' before 'main' denotes that the main( ) function will return an integer(decimal number) value after its execution. So, 'return 0' statement at the end helps to return the integer zero. This should be the last statement in the program.

include <stdio.h> 
int main( )
{
printf("}Iello World");
return 0;
}

5) The above-mentioned code will display the message but the output screen will vanish immediately. So, to retain the output screen as long as we want, system ('pause') statement can be used. 'system' is a function used to execute any operating system command like 'pause' which means to stop temporarily.

include <stdio.h> 
int main( )
{
printf("Hello World");
system(`pause);
return 0;
}
POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error