First Program in C - Featured

Welcome back to Think & Free. This is another episode of Express Learning Programming with GNU C Programming Language. In our previous lesson, we talked about all basics concepts and other basic things about the GNU C Programming language & about GNU GCC Compiler. In this lesson, I will show you how to make our first C Program, and how to compile & Run It.

We need some source code editor/text editor program for create our program. In Microsoft Windows, we can use Notepad for it. If You are Linux or Mac User you can use another editor. No matter what is it. Let’s begin building our first C Program.

Building “Hello, World” Program

In our first Project in C, let’s try to print a simple “Hello, World” string on our console window. Here is the source of our first “Hello, World” Program.

#include <stdio.h>
int main()
{
    printf("Hello, World\n");
    return 0;
}

Write down this code using your favorite code editor and save it with a .c extension. .c is an extension for C Language source files. .h is an extension for the C Header files.

In this program, we can see the first line of code #include <stdio.h>. stdio.h will include standard input-output functions in C Language. This header is an important header to every C Project. In future lessons, I will talk about all things about headers. next, we can see our program entry point. It is the main function. int main(){} is our program entry point. In the previous lesson, I told you, the main function is the beginning of any of Program, written in C. Let’s see what is inside in our main function.

The Printf(); Function

printf("Hello, World\n");

printf("Hello, World\n"); printf(); It is the one of a function of C used to get a print out some string, character or, some other type of data in the console window. Hello, World is a String, constant. if something inside double quotes, GNU C Programming Language will it was a string type of data. In our future lessons, I will tell why I’m called it was a String, constant. Keep it on your mind I will explain later.

See every C Statements are ends with a semicolon (;). It was another main thing about GNU C Programming Language. Keep in mind your every C Statements must end with a semicolon.

At the end of “Hello, World\n“, see something special. it was a “\n”. What is that?. In C these types of things called an escape sequence. These type of things is used to print some special character in C. for the example, “\n” used to get a new line after printing something. In future tutorials, I will explain more about the escape sequence.

at the last, return 0; Return Value will indicate what is the returning point or end-state of some function or our program. 0 means returning without any errors.

Compile & Run Our First C Program

Finally, our program must be compiled before running. in our previous lesson, I told you C is a Compiled Programming Language, and I told you we need some compiler program to compile our Source Code. I’m using The GNU GCC Compiler for Compile This Program. In the previous lesson, I told you about GNU GCC Compiler.

First, save the project as a C Source file you can use any filename it’s no matter but, don’t forget to use “.c” as a source file extension. Once done saving process. We can use following commands to compile your C Source Code to Compiled Binary file.

gcc source_filename.c -o executable_filename
  • source_filename – Our C Source Filename.
  • executable_filename – That mean name for our compiled binary file. gcc -o means output.

This is the way how to compile C Source Code or C Source file to Executable File. After Compilation will be done, now you can run this executable file on your system. In Linux systems before running this executable, you must set the file permission on your system. Let’s check how to set up file permissions on your Linux System.

chmod +x executable_filename

chmod means Change Mode. This command will be set execution permissions on your system in your file. Done setting up permissions now you can execute.

./executable_filename

In Windows you may no need to set file permissions. you can simply type your executable_filename and hit enter now the result will be displaying. Looks like this.

executable_filename

If you had some questions, leave comment. Hope you guys enjoy with this tutorial see you back soon in a another Episode of Express Learning Programming with GNU C Programming Language.

5 thoughts on “Express Learning Programming – First Program in C

Leave a Reply

Your email address will not be published. Required fields are marked *