Monday, January 4, 2016

C Programming Tutorial

To learn any new programming language, for example C, C++, Java, etc., most commonly used approach is to write a basic HelloWorld program first and then learn the concepts from that. Let's follow the same approach here and first write a C program and learn about basic syntax of C programming.

HelloWorld in C:

  1. #include<stdio.h>
  2. int main()
  3. {
  4. printf("Hello world!");
  5. return 0;
  6. }

How to Run the HelloWorld C Program:

To compile and run the above C program,
  • Or you can make use of online services like IdeOne. For learning purposes, this is the better option.
The above program will print 'Hello world!' as output.

Let's analyze the program line by line.
  • The first line is called a pre-processor directive.
    In this program, pre-processor directive '#include' informs the compiler to include the header file 'stdio.h' to the HelloWorld program. By doing so, the standard C functions like 'printf' are included in the program.
  • Next line, 'int main()' defines the main function, which will be invoked by the runtime when you run the program.
  • The lines written inside '{...}' is called the definition of main function.
  • Next line 'printf("Hello world!");' invokes a funtion 'printf' defined inside the header file 'stdio.h'. This prints the string 'Hello world!' in the output.
  • Next line 'return 0;' stop execution of the main function and return value '0' to the runtime.
  • Note that each statement in your program has to be terminated with a semicolon(;). This marks the end of the statement.

No comments: