Hello World program in C | How to write, compile and run // Unstop (formerly Dare2Compete) (2023)

OnC languageIt first appeared in 1972 and has been one of the most popular programming languages ​​ever since. This general-purpose language is among the most widely used programming languages ​​in the world and is the basis for many other languages. So there should be no argument against the claim that mastering C will make it easier for you to learn other languages ​​with similar syntax, such asJava,Python,C++, i inni.

In this article, we've explained in detail every step you need to take to write and run a hello world program in C. Beginners love Hello World! and when programming is first learned, it is often the first program a person learns to write. This is probably the simplest program that displays everythingBasics of programingclear, such as the use of variables and the presentation of data on the screen. So let's get started!

The basic structure of a C program

A C program has a specific structure. First, let's look at the structure of a C program.

1

#switch on

heading

2

int main()

Main

3

{

4

printf("Hello world");

declaration

5

return 0;

To give back

6

{

Here is a detailed description of the individual components of the program:

Header file: line 1 (#include)

The first and most important element to include in a C program is the header files. The term "header file" refers to a file with the extension ".h" that contains common C function declarations and macro definitions. The preprocessor program, which is the program called bycompiler, process all lines starting with #. The preprocessor copies the preprocessed code fromto our file in the example above. In C, .h files are called header files.

Main declaration: line 2 (int main())

This is followed by the declaration of the main() function of the C program. At the beginning of the main() function, a door is opened for the program to enter and execution of the program begins. main takes no parameters as indicated in the empty parentheses. The return type of the main() function is indicated by the number of ints written before the main function. The value of the main function is returned, representing the step that closes the program.

Body - Line 3 to 6 ({})

Another part of the basic program are declarations, which are a component of functions in a C program and are called function bodies. Modifications, searches, classifications, etc. are allowed. A pair of parentheses represents the body of the function. Note that each independent function must begin and end with curly braces.

Instruction: Line 4 (printf("Hello world");)

Declarations are sent to the compiler as instructions. A semicolon (;) always marks the end of a sentence in the C language. In this particular case, the task of telling the compiler and its associated hardware that certain text should be visible on the screen is accomplished by the so-called printf function, which is the printf( ). The text content displayed here is "Hello world".

return statement: line 5 (returns 0 ;))

The return statement is the final component of any C function. The return statement refers to the values ​​returned by the function. The return type of a function determines the return value and this return statement. In our program, main() is returned by the return statement, which in this case is return 0.Operating systemyou can use the returned value to determine if the program will exit. Usually 0 means successful completion.

Hello World program in C | How to write, compile and run // Unstop (formerly Dare2Compete) (1)

Compiling a program in C

Successful execution of a C program depends on the availability of a unique compiler designed to compile C code. A compiler is software that is used to create and run programs. There are two different methodologies for installing the C compilera programming languageon any personal computing device such as a desktop computer or laptop. They are-

1. Download a complete IDE that includes a C compiler, such as Turbo C++, Microsoft Visual C++, or DevC++.

2. Alternatively, you can download the C compiler separately, change the program files with any text editor, and then run the C program from the command line.

Hello World program in C | How to write, compile and run // Unstop (formerly Dare2Compete) (2)

Steps to run Hello World in C from an IDE (e.g. Turbo C++)

1. Launch Turbo C IDE (Integrated Development Environment), select File and click New.

Hello World program in C | How to write, compile and run // Unstop (formerly Dare2Compete) (3)

2. Write a program called Hello World. (As shown below, remember to add the header fileand getch(). There is a custom library function called getch() for the C computer language in the conio.h header file. This is mainly used by MS-DOS compilers such as Turbo C.)

Hello World program in C | How to write, compile and run // Unstop (formerly Dare2Compete) (4)

3. To compile the code, click Compile in the Compile menu or press Alt + F9.

Hello World program in C | How to write, compile and run // Unstop (formerly Dare2Compete) (5)

4. To run the code, you can click the "Run" function or just press Ctrl + F9. Note that C programs are first compiled to produce object code, which is then executed.

Hello World program in C | How to write, compile and run // Unstop (formerly Dare2Compete) (6)

5. Finally, the result of the program will be displayed in the results window.

Hello World program in C | How to write, compile and run // Unstop (formerly Dare2Compete) (7)

Steps to run a Hello World program in C without an IDE

There is an alternative way to run a hello world program in C without using an IDE. To do this, follow these steps:

(Video) Dare2Compete IGNITE - Sales and Marketing(FMCGs) Session

1. Download the C compiler from the GCC website if you don't want to configure the IDE and prefer a manual process.

2. Just open any text editor, copy and paste the code of the C program (hello world program here) and save it as helloworld.c, just like you save any other file with a name after downloading it. and install the gcc compiler.

Use- The extension must be .c

3. Go to the directory where you saved the helloworld.c program file and open a command prompt or terminal.

Hello World program in C | How to write, compile and run // Unstop (formerly Dare2Compete) (8)

4. To compile the code, type gcc.\helloWorld.c. Assuming the code is error free, it will compile and create a file with the standard name: a.exe. This process involves executing an ordered set of instructions written in the C language in various stages to produce executable files.

Hello World program in C | How to write, compile and run // Unstop (formerly Dare2Compete) (9)

5. Now type .\a.exe to run the program and Hello world will appear on the screen.

Hello World program in C | How to write, compile and run // Unstop (formerly Dare2Compete) (10)

A simple C program that displays "Hello world!"

An introductory tutorial program known as "Hello World!" The exercise is a simple lesson that provides valuable information for understanding programming languages. It works not only as the first step in learning any language, but also turns out to be one of its simplest elements.

Here is a C program to print Hello World.-

#include int main() {printf("¡Hola mundo!");return 0;}

Production:

Hello World!

Like "Hello world!" Does the C program work?

The message "Hello world!" The program in the above code is simple. The printf() function generates output on a screen that displays "Hello world!" as its leitmotif.

  • The #include preprocessor commandat the beginning of the program tells the compiler to put the standard input/output library in the code.
  • The program's entry point is the int main() function, which supplies the operating system with an integer value.
  • "Hello World!" is an expression created with the printf() function.
  • To print a data stream to the standard console, use the printf() function.
  • The output device gets a display containing everything inside double quotes ("").
  • Finally, the program is terminated and the operating system is informed that the program was successfully executed by returning 0; statement.

Program Hello World in C using functions

Let's see how the same program is executed with a user-defined function. In the following program, we have integrated a function called printMessage() that prints a message to the screen. The function is called in the main() function.

Function definition syntax:

return_type function_name(if any arguments) {

...

function_content (code)

...

}

Hello World C program with a user-defined function-

#include void printMessage() {printf("¡Hola mundo!");}int main() {printMessage();return 0;}

Production:

(Video) Product Management Webinar

Hello World!

Explanation-

In the example above, the printMessage() operation prints the message "Hello world!" to the screen. The lack of a returned value is signaled by inserting "void" in front of the function name.

So the program entry point is the int main() function, which supplies the operating system with an integer value. This program uses the printMessage() function to print "Hello world!" on the screen. The program is terminated and the operating system is informed that the program was successfully executed by returning 0; statement.

Program Hello World in C using string variables

We've already seen how to compile and run a hello world program in C with and without the IDE, and with user-defined functions. Now let's look at how to run the same program with string variables. We'll first look at the syntax of this, then we'll look at an example.

Syntax for declaring variables in C:

variable_type variable_name = initial_value;

Hello World! can be displayed in the output using char variables. Single-character values ​​are stored in variables of the char data type. To print the message on the output screen in the program's printf() function, the format specifiers will be used (%c for char variables). So we can say Hello world! it's just a string made up of several char variables. Using char variables, we can quickly create Hello world! next to the exit.

Program Hello World in C using string variables:

#include int main(){char a = 'H', b = 'e', ​​​​c = 'l', d = 'o';char e = 'w', f = 'r', g = ' d' ;printf("%c%c%c%c%c %c%c%c%c%c", a, b, c, c, d, e, d, f, c, g);return 0; }

Production:

Hello World

Explanation:

In the initial program declaration there is a preprocessor directive/command marked #include.

  • This particular directive instructs the compiler to integrate standard I/O library functions into that computer programming language.
  • The program's entry point is the int main() function, which supplies the operating system with an integer value. In the context of this computer program, there are seven unique character variables: a, b, c, d, e, f, and g.
  • Initially, in his statement, he is permeated with the message "Hello world!".
  • The message is printed to the screen using the printf() function. To print a character, use the %c format specifier.
  • The variables a, b, c, d, e, f, and g are used in this particular order to form the sentence "Hello world!"

How to run a Hello World program in C a certain number of times

One possible approach to implementing the recursive result of the phrase "Hello world!" in the C programming language, this is done using loops. The program asks the user how often they want to display the message, and then displays it repeatedly the specified number of times using a for loop.

The syntax of a recursive hello world program in C:

for (initialization statement; test expression; update statement)

{

// instructions inside the body of the loop

}

Empty world recursive program code in C:

#include int main() {int n, i;printf("Enter the number of prints: ");scanf("%d", &n);for (i = 0; i < n; i++) {printf("Hello , world!\n");}return 0;}

Production:

Enter how many times you want to print the message: 7

Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!

(Video) Mondelez Maestros Launch

Explanation:

The program's entry point is the int main() function, which supplies the operating system with an integer value. Here we first declare two integer variables, n and i.

  • The print() function then displayed the phrase "Enter the number of message prints:" on the screen.
  • When the user enters a number from the keyboard, it will be read by the scanf() method and stored in the variable n.
  • Then the for loop prints "Hello world!" a predetermined number of times.
  • This number is initially set to 0 when the loop starts and runs until i is less than n.
  • First, a sequence of characters is displayed on the screen. The printf() function then generates the message "Hello world!".
  • The process leading to this result involves executing the necessary code steps. Each message is followed by a new line, which is added with the \n character.
  • At the end of each loop, i is incremented by 1 using the i++ instruction.
  • Finally, the program is terminated and the operating system is informed that the program was successful by returning 0; statement.
  • Please note that the result of the program operation depends on how many times a given phrase is to be repeated by the user.

Running Hello World in C indefinitely

We can use a while loop that never ends in C to print "Hello world!" indefinitely.

Syntax:

while (condition) {

reports);

}

implementable code:

#include int main() { while (1) { printf("Hello world!\n");}return 0;}

Production:

Hello World!
Hello World!
Hello World!
Hello World!
...

Explanation:

Note that the program example above can be stoppedpressing (Ctrl + C).

As you can see in the example above, the #include statement, a preprocessor directive, tells the compiler to include the standard input/output library in the program first.

The entry point, the int main() function, then supplies the operating system with an integer value. This program uses an infinite while loop. The loop continues as long as condition 1 is true, which is always true. When the printf() function is called, a message is displayed consisting of the greeting "Hello world!" is displayed on the screen. Using this particular method means an output effect that displays those particular characters with relative ease and efficiency. Each message is followed by a new line, which is added with the \n character. The program is terminated and the operating system is informed that the program was successfully executed by returning 0; statement.

Application

C is a popular language, often considered the mother of all programming languages. We saw how to run one of the most basic C programs, the "Hello World" program. There are many ways to run a hello world program in C, including using the IDE, without using the IDE, using string variables, and using user-defined functions. We also know how to run a program a certain number of times or even indefinitely.

In C, the main() function is used as the starting point for program execution and must return an integer value. The printf() function prints the message "Hello world!" on the screen. And finally, the return 0 statement means that the program was successfully executed.

Frequently asked questions

Q. How to print a Hello World program in C?

We mainly use printf() to print "Hello World" in C. However, we can also use a user-defined function for the same purpose. Below is a sample program showing the implementation of printf():

#include int main() {printf("¡Hola mundo!");return 0;}

Production:

Hello World!

The message "Hello world!" The program in the above code is simple. Using the printf() function, the output is generated on the screen.

Q. How do I declare and initialize variables in C?

First we need to declare the variable in C and then initialize the value. The course of the program is as follows: after declaring a variable, the compiler is informed about its name and type. We then assign the variable its initial value before using it. This process of initializing a variable is called initializing it.

The syntax for initializing and declaring variables in C is as follows:

// Declaration: save variable_name;

internal age;

// Initialization: variable_name = value;

age = 16 years;

// Combination of declaration and initialization: type variable_name = value;

pi float = 3,14159;

Here we first declare the "Age" variable in the first line of code. We then use the assignment operator (=) to set the integer value as the initialization.

We declare and initialize the floating point variable pi in the third line. Again, we use the assignment operator (=) to combine declaration and initialization on one line.

Q. How do I write an if-else statement in C?

In C programming, the if-else statement plays a key role. This particular decision statement determines whether certain parts of the code should be executed according to the specified conditions of the test expression. The code in the block is executed if the given condition is true; otherwise, the code in the else block is executed.

If-else statement syntax in C

if (condition) {

// code executed when condition is true

}

rest {

// code executed when condition is false

}

Q. How to write a for loop in C?

The for loop has a particularly structured methodology where it initializes the condition first, then tests the condition, then executes the conditional statements before updating the data.

C for loop syntax

for (initialization; condition; increment)

{

// multiple declarations

}

Q. How to print Hello World in C, 7 times?

To print "Hello world" 7 times in C, we can use a for loop that will do 5 iterations. An implementable program for the same is-

#include int main() {int i;for (i = 0; i < 7; i++) {printf("Hola mundo\n");}return 0;}

Production:

Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World

Q. What are the 4 types of functions in C?

The four main types of functions used in the C programming language are as follows:

  • integrated functions
  • User-defined functions
  • recursive functions
  • function indicators

Q. What is the main function in C?

Execution of a C program starts with the main() function and is therefore often called the program entry point. Every C program must use this predefined function. The main() function starts program execution and sends an integer value to the operating system.

By now, you should know everything about writing, running, and executing a Hello World program in C. Here are some articles to help quench your thirst for knowledge:

  1. What is the difference between a struct and a union in C?
  2. What is C programming? Learn the history, basics and meaning
  3. The difference between decoded C and embedded C!
  4. Advantages and disadvantages of the C programming language

References

Top Articles
Latest Posts
Article information

Author: Golda Nolan II

Last Updated: 09/06/2023

Views: 5899

Rating: 4.8 / 5 (78 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Golda Nolan II

Birthday: 1998-05-14

Address: Suite 369 9754 Roberts Pines, West Benitaburgh, NM 69180-7958

Phone: +522993866487

Job: Sales Executive

Hobby: Worldbuilding, Shopping, Quilting, Cooking, Homebrewing, Leather crafting, Pet

Introduction: My name is Golda Nolan II, I am a thoughtful, clever, cute, jolly, brave, powerful, splendid person who loves writing and wants to share my knowledge and understanding with you.