First C program and C Program Structure

first c program and program structure

First Program in C, So how do you create your first ever program using the C programming language? Here, we are going to archive that one today with this brand-new article. If you have already read our latest article, I think you already know about the background and history of the C programming language and how to get started. So let’s take the experience with our second lesson of the C programming language, the Express Learning Programming series.

Before getting started with this lesson, if you haven’t read that article yet, I highly recommend you take your time and read it first. Because it’s crucial to get a better understanding of this series and also a good point for the beginning of your programming journey.

Previous Lesson: Introduction to C Programming Language

To get started with the C programming language, We need some required programs/Software need to be installed in our system. We mentioned in our first lesson what are those and how to install those programs in our system. So I hope you are ready to develop now. So let’s dive into today’s lesson and find out how to create our First Program in C.

The “Hello World” Program

If you are aware of programming, I think you already saw this Hello World program, and I hope you know about it. So let me first explain what this “Hello, World” program is. It’s actually a programming tradition that is used to explain the basic syntax of a programming language. In most programming languages, this is the first lesson that helps you learn about the basic concepts of that language.

If you are more interested in “Hello World” and need more information, follow this Wikipedia article about the “Hello, World!” Program.

C Language File Extensions

There are several extensions you can identify when you are getting started with programming with the C programming language.

Hello World Program in C

So let’s take a look at how to create your first Hello, World program in C and learn about the basic syntax of the C programming language.

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

Now open up your favorite editor Write down the given code and save it. Don’t forget to save it with the .c extension. As I noticed from the beginning of this lesson, it’s the extension for C language source files.

So let’s break down this code to make it easier to understand.

Code Explanation

  • At the first line of code, #include <stdio.h>. stdio. h. This line describes including preprocessor headers in the program. This header is an important header for every C project. In future lessons, I will talk about all things 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 that the main function is the beginning of any Program, written in C. Let’s see what is inside our main function.
  • printf("Hello, World\n"); is the first line we can see inside our program’s main function. It’s one of the standard output functions which is used to print a character or string in C. Inside of the () Brackets, we can identify what we need to print (output).
  • return 0; what does this return indicate? In this code, it indicates the program exited without any error, and 0 denotes the execution without any error, Which means execution is successful.
  • Semicolon, See, every C statement ends with a semicolon (;). It was another main thing about the GNU C programming language. Keep in mind that every C statement 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 are called an escape sequence. These type of things is used to print some special characters in C. For example, “\n” is used to get a new line after printing something. In future tutorials, I will explain more about the escape sequence.

Don’t think too much of these; just get the basic idea of a simple C program. I will step-by-step guide you through all of these functions and concepts in the later lessons.

About printf(); Function

Like I noticed before, it’s one of the basic functions in the C language, and it comes from the studio.h header file. If you have already read our previous lesson, I mentioned some C standards. So thisprintf(); function belongs to the ANSI C standard library. Just get that first line inside of our main function.

printf("Hello, World\n");

printf("Hello, World\n"); printf(); It is one of the functions of C used to get a print of some string, character, or, some other type of data in the console window. “Hello, World” is a string constant. If something is inside double quotes, C Programming Language will treat it as a string type of data.

Latest Article: Top 5 Linux Distros in 2023, Let’s Learn about Linux

Compile & Run Our First C Program

Finally, our program must be compiled before running. in our previous lesson, We mentioned C is a Compiled Programming Language, and I told you we need some Compiler program to compile our Source Code before executing it. Now in this lesson series, we are using The GNU GCC Compiler for Compile This Program. In the previous lesson, I told you about the 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, the saving process. We can use the following commands to compile your C Source Code to Compile a Binary file.

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

This is the way to compile C Source Code or C Source file to Executable File. After Compilation is 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 set execution permissions on your system in your file. Done setting up permissions now you can execute.

./executable_filename

In Windows, you may not need to set file permissions. you can simply type your executable_filename and hit enter Now the result will be displayed. Looks like this.

executable_filename.exe

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

2 thoughts on “First C program and C Program Structure

Leave a Reply

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