Introduction to C Programming

June 02, 2018 | 1 Tags | 0 Comment

Introduction to C Programming
    A C Program basically consists of the following parts:
  1. Preprocessor
  2. Functions
  3. Variables
  4. Comments

A C Program always starts executing in a special function called main() function. Let’s take a look at a simple C code that would print the words “Hello World”

#include <stdio.h>

int main() {
   /* my first program in C */
   printf("Hello, World! \n");

   return 0;
}
    Let's examine the program:
  1. The first line of the program #include <stdio.h> is a preprocessor command, which tells the C compiler to include stdio.h file before going to compilation
  2. The next line int main() is the main function where the program execution begins
  3. The next line /* ... */ is a comment in C and will be ignored by the compiler
  4. The next line printf(...) is another function declared in stdio.h file which will display the message "Hello, Word!"
  5. The next line return 0; terminates the main() function and returns the value 0
Compile and Execute C Program in Linux

Open a text editor, copy and paste the above code. Save the file as hello.c. Open a command prompt and go to the directory where you’ve saved the file.

If you’re using gcc compiler, you type gcc hello.c and press enter to compile your code. If there are no errors in your code, it would generate a.out executable file. Now, type a.out to execute your program and you will see the output “Hello, World!”.

Compile C program using gcc compiler

If you’re using clang compiler, you type clang hello.c and press enter to compile your code. If there are no errors in your code, it would generate a.out executable file. Now, type a.out to execute your program and you will see the output “Hello, World!”.

Compile C program using clang compiler

C
Samuel Yang image
Samuel Yang

If you like this tutorial, you can support me

Donate Now