First C Program

General Overview of a Simple C Program's Structure:

The general architecture of a simple C program typically consists of several vital components. Below is an outline of the essential elements and their purposes:

1)Header Files:

The #include directives at the beginning of the program are used to include header files. Header files provide function prototypes and definitions that allow the C compiler to understand the functions used in the program.

2)Main Function:

Every C program starts with the main function. It is the program's entry point, and execution starts from here. The main function has a return type of int, indicating that it should return an integer value to the operating system upon completion.

3)Variable Declarations:

Before using any variables, you should declare them with their data types. This section is typically placed after the main function's curly opening brace.

4)Statements and Expressions:

This section contains the actual instructions and logic of the program. C programs are composed of statements that perform actions and expressions that compute values.

5)Comments:

Comments are used to provide human-readable explanations within the code. They are not executed and do not affect the program's functionality. In C, comments are denoted by // for single-line comments and /* */ for multi-line comments.

6)Functions:

C programs can include user-defined functions and blocks of code that perform specific tasks. Functions help modularize the code and make it more organized and manageable.

7)Return Statement:

Use the return statement to terminate a function and return a value to the caller function. A return statement with a value of 0 typically indicates a successful execution in the main function, whereas a non-zero value indicates an error or unexpected termination.

8)Standard Input/Output:

C has library functions for reading user input (scanf) and printing output to the console (printf). These functions are found in C programs and are part of the standard I/O library (stdio.h header file). It is essential to include these fundamental features correctly while writing a simple C program to ensure optimal functionality and readability.

Additional Information:

There is some additional information about the C programs. Some additional information is as follows:

Preprocessor Directives:

C programs often include preprocessor directives that begin with a # symbol. These directives are processed by the preprocessor before actual compilation and are used to include header files, define macros, and perform conditional compilation.

Data Types:

C supports data types such as int, float, double, char, etc. It depends on the program's requirements, and appropriate data types should be chosen to store and manipulate data efficiently.

Control Structures:

C provides control structures like if-else, while, for, and switch-case that allow you to make decisions and control the flow of the program.

Error Handling:

Robust C programs should include error-handling mechanisms to handle unexpected situations gracefully. Techniques like exception handling (using try-catch in C++) or returning error codes are commonly employed.

Modularization:

As programs grow in complexity, it becomes essential to modularize the code by creating separate functions for different tasks. This practice improves code reusability and maintainability. Remember, the architecture and complexity of a C program can vary significantly depending on the specific application and requirements. The outline is a general overview of a simple C program's structure.

Explain the First C program:

Basic C Program

</> Code Example

                        #include     
                            int main(){    
                            printf("Hello C Language");    
                            return 0;   
                            } 
                    

Output

Hello C language

Let us first study the various parts of this C program:

#include :


In this line, the program includes the standard input/output library (stdio.h) due to the preprocessor directive. For input and output tasks, the stdio.h library contains methods like printf and scanf.

int main() { ... }:


It is the main function which is the entry point of the C program. The program starts executing from the beginning of the main function.

printf("Hello World!n");:printf("Hello World!");:


Use the printf() function to print formatted output to the console. In this example, the string "Hello, C Language" is printed, followed by a newline character (n) which moves the pointer to the following line after the message is displayed.

return 0;


When the return statement is 0, the program has been completed. When determining the state of a program, the operating system frequently uses the value returned by the main function. A return value of 0 often indicates that the execution was successful.
After compilation and execution, this C program will quit with a status code 0 and output "Hello, C Language" to the terminal.
The "Hello, C Language" program is frequently used as an introduction to a new programming language since it introduces learners to essential concepts such as text output and the structure of a C program and provides a rapid way to validate that the working environment is correctly set up.

To write, compile, and run your first C program, follow these steps:


Step 1: Open a text editor


Open a text editor of your choice, such as Notepad, Sublime Text, or Visual Studio Code. It will be where you write your C code.

Step 2: Write the C program

Now, copy and paste the following code into the text editor:
</> Code Example

                                    #include   
                                        int main() {  
                                        printf("Hello, C Language");  
                                        return 0;  
                                        } 
                                

Output

Hello C language

Step 3: Save the file

After that, save the file with a .c extension such as first_program.c. This extension indicates that it is a C source code file.

Step 4: Compile the program

Now, compile the program in the command prompt.

Step 5: Run the program

After successful compilation, you can run the program by executing the generated executable file. Enter the following command into the terminal or command prompt:
</> Code Example

                                        ./first_program  
                                    

Output

Hello C language
The program will execute, and you will see the output on the console:
output
Hello, C Language

How to compile and run the C program


There are two ways to compile & run the c program by menu and by shortcut.
By Menu
  1. Now click on the compile menu, then compile sub-menu to compile the c program.
  2. Then click on the run menu and the sub-menu to run the c program.
By shortcut
  1. Or, press the ctrl+f9 keys to compile and run the program directly.
  2. You will see the following output on the user screen.
  3. You can view the user screen any time by pressing the alt+f5 keys.
  4. Now press Esc to return to the turbo c++ console.

Conclusion:

Finally, the first C program introduces the C programming language and its fundamental structure. It illustrates the necessary components for writing, compiling, and running a C program. The program contains the standard input-output library (stdio.h), which includes routines for output operations such as printf(). The main() function is the program's entry point, from which execution begins. The printf() function is used within the main() method to print the message "Hello, C Language" to the console. A C compiler such as GCC is required to compile the program. The code is stored in a text file with the .c extension, and the compiler is started by typing gcc, followed by the names of the input and output files. The compilation process converts machine-readable instructions from human-readable C code. Once the program has been successfully constructed, it may be started by double-clicking the resultant executable file. In a terminal or command prompt, the executable is called by its file name, followed by ./. After that, the program is performed, and the "Hello C Language" output is shown on the console. By following these instructions, you will get a basic grasp of developing, constructing, and running a C program. It offers the groundwork for further investigating more complex ideas and developing more sophisticated applications using the C programming language.

Compilation process in c |

What is a compilation?

The compilation is a process of converting the source code into object code. It is done with the help of the compiler. The compiler checks the source code for the syntactical or structural errors, and if the source code is error-free, then it generates the object code. The c compilation process converts the source code taken as input into the object code or machine code. The compilation process can be divided into four steps, i.e., Pre-processing, Compiling, Assembling, and Linking. The preprocessor takes the source code as an input, and it removes all the comments from the source code. The preprocessor takes the preprocessor directive and interprets it. For example, if , the directive is available in the program, then the preprocessor interprets the directive and replace this directive with the content of the 'stdio.h' file. The following are the phases through which our program passes before being transformed into an executable form:
  • Preprocessor
  • Compiler
  • Assembler
  • Linker

Preprocessor

The source code is the code which is written in a text editor and the source code file is given an extension ".c". This source code is first passed to the preprocessor, and then the preprocessor expands this code. After expanding the code, the expanded code is passed to the compiler.

Compiler

The code which is expanded by the preprocessor is passed to the compiler. The compiler converts this code into assembly code. Or we can say that the C compiler converts the pre-processed code into assembly code.

Assembler

The assembly code is converted into object code by using an assembler. The name of the object file generated by the assembler is the same as the source file. The extension of the object file in DOS is '.obj,' and in UNIX, the extension is 'o'. If the name of the source file is 'hello.c', then the name of the object file would be 'hello.obj'.

Linker

Mainly, all the programs written in C use library functions. These library functions are pre-compiled, and the object code of these library files is stored with '.lib' (or '.a') extension. The main working of the linker is to combine the object code of library files with the object code of our program. Sometimes the situation arises when our program refers to the functions defined in other files; then linker plays a very important role in this. It links the object code of these files to our program. Therefore, we conclude that the job of the linker is to link the object code of our program with the object code of the library files and other files. The output of the linker is the executable file. The name of the executable file is the same as the source file but differs only in their extensions. In DOS, the extension of the executable file is '.exe', and in UNIX, the executable file can be named as 'a.out'. For example, if we are using printf() function in a program, then the linker adds its associated code in an output file.
Let's understand through an example. hello.c

#include int main() { printf("Hello javaTpoint"); return 0; }

Now, we will create a flow diagram of the above program: In the above flow diagram, the following steps are taken to execute a program:
  • Firstly, the input file, i.e., hello.c, is passed to the preprocessor, and the preprocessor converts the source code into expanded source code. The extension of the expanded source code would be hello.i.
  • The expanded source code is passed to the compiler, and the compiler converts this expanded source code into assembly code. The extension of the assembly code would be hello.s.
  • This assembly code is then sent to the assembler, which converts the assembly code into object code.
  • After the creation of an object code, the linker creates the executable file. The loader will then load the executable file for the execution.

printf() and scanf() in C

The printf() and scanf() functions are used for input and output in C language. Both functions are inbuilt library functions, defined in stdio.h (header file).

printf() function

The printf() function is used for output. It prints the given statement to the console. The syntax of printf() function is given below: printf("format string",argument_list); The format string can be %d (integer), %c (character), %s (string), %f (float) etc.

scanf() function

The scanf() function is used for input. It reads the input data from the console. scanf("format string",argument_list);

Program to print cube of given number

Let's see a simple example of c language that gets input from the user and prints the cube of the given number.

#include int main(){ int number; printf("enter a number:"); scanf("%d",&number); printf("cube of number is:%d ",number*number*number); return 0; }

output

enter a number:5 cube of number is:125 The scanf("%d",&number) statement reads integer number from the console and stores the given value in number variable. The printf("cube of number is:%d ",number*number*number) statement prints the cube of number on the console.

Program to print sum of 2 numbers

Let's see a simple example of input and output in C language that prints addition of 2 numbers.

#include int main(){ int x=0,y=0,result=0; printf("enter first number:"); scanf("%d",&x); printf("enter second number:"); scanf("%d",&y); result=x+y; printf("sum of 2 numbers:%d ",result); return 0; }

output

enter first number:9 enter second number:9 sum of 2 numbers:18