Sunday 29 September 2019

Writing your First C Program and Running it - C Programming Tutorial for Beginners


Writing your First C Program and Running it - C Programming Tutorial for Beginners

 C is a programming language developed at AT & T’s Bell Laboratories of USA in 1972. It was designed and written by a man named Dennis Ritchie. It was initially designed for programming UNIX operating system. Now the software tool as well as the C compiler is written in C.Major parts of popular operating systems like Windows, UNIX, Linux is still written in C.
  

C Hello World! Example: Your First Program
Here, is a Hello World program in C
#include<stdio.h>   //Pre-processor directive
void main()         //main function declaration
{
printf("Hello World"); //to output the string on   a display    
getch ();           //terminating function
}

  
Here is the code explanation:
Pre-processor directive
#include is a pre-processor directive in 'C.'
#include <stdio.h>, stdio is the library where the function printf is definedprintf is used for generating output. Before using this function, we have to first include the required file, also known as a header file (.h).

 The main function
The main function is a part of every 'C' program. We can represent the main function in various forms, such as:
  • main()
  • int main()
  • void main()
  • main(void)
  • void main(void)
  • int main(void)
The empty parentheses indicate that this function does not take any argument, value or a parameter. You can also represent this explicitly by placing the keyword void inside the parentheses. The keyword void means the function does not return any value, in this case, the last statement is always getch ().

  
#include<stdio.h>   //Pre-processor directive
int main()          //main function declaration
{
printf("Hello World");  //to output the string  on a display
return 0;           //terminating function
}
In the above example, the keyword int means the function will return an integer value. In this case, the last statement should always return 0.


The source code
After the main function has been declared, we have to specify the opening and closing parentheses. Curly brackets { }, indicate the starting and end of a program. These brackets must be always put after the main function. All the program code is written inside these brackets, such as declarative and executable part.
The printf function generates the output by passing the text "Hello World!"
The semicolon ; determines the end of the statement. In C, each statement must end with a semicolon.

 Summary
  • The main function is a mandatory part of every 'C' program.
  • To use the functionality of a header file, we have to include the file at the beginning of our program.
  • Every 'C' program follows a basic structure.
·         We will write a simple program that will say hello to us. Let's start.