First C Program
Let's start with the "Hello World" program, which is commonly used as the first example in any programming language tutorial.
Steps to Write Your First C Program
- Install a C compiler such as GCC or use an IDE that supports C programming.
- Create a file with a .c extension (e.g., first_program.c).
- Write the code inside the file.
- Compile the program using the C compiler.
- Run the compiled program to see the output.
Example: Hello World Program
#include <stdio.h>
int main()
{
printf("Hello, World!\n");
return 0;
}
Output
Hello, World!
Explanation of the Program
Here is a detailed explanation of the code:
- #include <stdio.h>: This is a preprocessor directive that includes the Standard Input Output library, which is necessary for functions like printf().
- int main(): The main() function is the entry point of every C program. It marks where the execution starts.
- printf("Hello, World!n"); This function prints the text "Hello, World!" followed by a newline.
- return 0; This statement ends the program and returns 0 to indicate successful execution.
Compiling and Running the Program
Follow these steps to compile and run your program:
- Open your terminal or IDE.
- Navigate to the directory where your .c file is saved.
- Compile the file using the command: gcc first_program.c -o first_program
- Run the compiled program using the command: ./first_program
- You should see the output: Hello, World!
Congratulations! You have successfully written and executed your first C program.