We Will Discuss About First C Program

C is a popular programming language that is widely used for system programming, application development, and more. If you’re new to programming and want to learn C, the first step is to write your first C program. In this article, we will guide you through the process of writing your first C program.

 

 

Here’s a classic first C program that prints “Hello, world!”:

#include <stdio.h>

int main() {
    printf("Hello, world!n");
    return 0;
}

Explanation:

  1. #include <stdio.h>: This line includes the standard input/output header file, which provides functions like printf() for printing output.
  2. int main(): This is the main function of the program, where execution begins. The int return type indicates that the function returns an integer value.
  3. printf("Hello, world!n");: This line calls the printf() function to print the message “Hello, world!” to the console. The n character adds a newline.
  4. return 0;: This line returns the value 0 to the operating system, indicating successful program execution.

How to Compile and Run:

  1. Save the code: Save the code as a .c file (e.g., hello.c).
  2. Compile: Use a C compiler like GCC (GNU Compiler Collection) to compile the code:
    Bash
    gcc hello.c -o hello
    

    This will create an executable file named hello.

  3. Run: Execute the compiled program:
    Bash
    ./hello
    

    This will output:

    Hello, world!
    
 

Key Points:

  • The main function is the entry point of a C program.
  • printf() is used to print output to the console.
  • The n character adds a newline to the output.
  • The return 0; statement indicates successful program execution.

To write, compile, and run your first C program, follow these steps:

Step 1: Open a text editor

Open a text editor of your choice, such as Notepad, Sublime Text, or Visual Studio Code. It will be where you write your C code.

Step 2: Write the C program

Now, copy and paste the following code into the text editor:

  1. #include <stdio.h>
  2. int main() {
  3. printf(“Hello, C Language”);
  4. return 0;
  5. }

Step 3: Save the file

After that, save the file with a .c extension such as first_program.c. This extension indicates that it is a C source code file.

Step 4: Compile the program

Now, compile the program in the command prompt.

Step 5: Run the program

After successful compilation, you can run the program by executing the generated executable file. Enter the following command into the terminal or command prompt:

  1. ./first_program

The program will execute, and you will see the output on the console:

Output:

Hello, C Language

Related Posts

Leave a Reply