Sure! Here’s an example of a simple “Hello, World!” program in C:
#include <stdio.h> int main() { printf("Hello, World!\n"); return 0; }
In this program, the #include <stdio.h>
statement is a preprocessor directive that tells the compiler to include the standard input/output library, which provides functions like printf()
for displaying output on the screen.
The main()
function is the entry point for the program, and it returns an integer value to indicate whether the program executed successfully or encountered an error. In this case, the function simply calls printf()
to display the message “Hello, World!” on the screen, and then returns the value 0
to indicate successful completion.
When you compile and run this program, it should output the message “Hello, World!” on the screen.
How to compile and run the c program:
To compile and run a C program, you can follow these steps:
- Write the C code in a text editor and save it with a
.c
file extension. For example, you could save the “Hello, World!” program I showed you earlier ashello.c
. - Open a command prompt or terminal window and navigate to the directory where you saved the
.c
file. - Compile the program using a C compiler such as
gcc
orclang
. To compilehello.c
withgcc
, you would enter the following command:
gcc -o hello hello.c
This command tells gcc
to compile hello.c
and output an executable file called hello
.
4. If there are no errors during the compilation process, you can run the program by entering its name followed by any required command-line arguments. To run hello
, you would enter the following command:
./hello
This command tells the shell to execute the hello
executable that was created during the compilation process.
5. If everything worked correctly, you should see the output of the program printed to the console. In the case of the “Hello, World!” program, you should see the message “Hello, World!” printed on the screen.