Write and compile your first simple C program
Learning every programming language begins with writing your first basic program, and the typical way to begin learning C is with the “Hello, World!” example. The fundamental structure of a C program and how to turn your code into a runnable application are covered in this program.
This is a typical illustration of the “Hello, World!” C program:
#include <stdio.h>
int main(void)
{
printf("Hello, World!\n");
return 0;
}
Let’s examine this code’s key components using the sources:
#include <stdio.h>: This directive comes from the preprocessor. Prior to the code being compiled, lines that begin with # are handled. The preprocessor is instructed to incorporate the contents of the supplied file with the #include directive. The Standard Input/Output Header file is located at . The C standard library has it. The information required for common input/output functions, such as printf(), is contained in this header file. Usually, when using printf() or scanf(), it must be included.
int main(void): There must be a main() function in every C program. The main() method is always used to start a program. The function is intended to return an integer value, as indicated by the term int before main. When main is executed, it does not accept any arguments or data, as shown by the void enclosed in parenthesis. It is advised to express the purpose of the primary function in a comment that comes before it.
{ and }: The body of the main function begins with the opening curly brace { and ends with the closing curly brace }. Between these brackets are all of the function’s statements.
printf(“Hello, World!\n”);: This line is an action-taking remark. A common library function used for formatted output is printf(). The string literal “Hello, World!” is the data that is supplied to printf for display. After printing, the n sequence, which is a newline character, advances the cursor to the following line. A semicolon (;) must come at the end of each statement in C.
return 0;: The main() function is terminated with this statement. It returns a value in this case, 0 to the operating system that invoked the application. Returning 0 often means that the application ran correctly.
Comments
Comments are lines that begin with // or that contain text between /*… */. The compiler ignores them; they are used to improve readability and document the program.
Compiling and Executing
Editing, preprocessing, compilation, linking, loading, and execution are the usual steps involved in creating and executing a C program.
Create the Source File (Edit): The code is typed in a text editor. A.c extension, such as hello.c, should be used when saving the file. Word processors introduce concealed formatting, so stay away from them.
Compile: Machine code must be created from the source code. A compiler like gcc (GNU C Compiler) or cc does this. A linker links the code with required library functions to create an executable file after the compiler manages preprocessing (such as #include) and translates the code. An executable won’t be produced if the compiler finds syntax mistakes, which are violations of language rules.
Execute: You launch the executable file when it has successfully compiled. Usually, you type the name of the executable (such as a.out or hello on Windows) on the command line. The program is loaded by the operating system, and main() is where execution begins. “Hello, World!” will appear on the console after the printf statement. The program ends when the return 0; signifies successful completion.
C programming is based on this writing, compiling, and running cycle. The most efficient method of learning is to write and debug programs.