First C Program
Here's an example of a "Hello, World!" program in C:
#include
int main() {
printf("Hello, World!\n");
return 0;
}
Explanation:
- The #include <stdio.h> line is a preprocessor directive that includes the standard input/output library, which contains functions like printf.
- The int main() function is the entry point of a C program. It is where the execution of the program begins.
- printf("Hello, World!\n"); is a function call that prints the text "Hello, World!" to the console. The \n represents a newline character, which adds a line break.
- The return 0; statement indicates that the program has executed successfully and returns the value 0 to the operating system.
To run this program:
- Save the code into a file with a .c extension (e.g., hello.c).
- Open a command prompt or terminal.
- Navigate to the directory where the file is saved.
- Compile the program using a C compiler. For example, if you have GCC installed, run the command: gcc hello.c -o hello.
- If the compilation is successful, an executable file named hello (or hello.exe on Windows) will be generated in the same directory.
- Run the program by executing the generated executable: ./hello (or hello.exe on Windows).
- The program will output "Hello, World!" to the console.
Congratulations! You have successfully written and executed your first "Hello, World!" program in C.