Compile and Run a C Program: Follow these steps If you want to compile and run a C program:
- Open the text editor and add above-mentioned code (or) new codes
- Next, save the file as hello.c (or) a.c
- Open a command prompt and open the directory where you saved the file.
- And use command gcc filename.c and enter to compile your code.
- If no errors found in your code, then command prompt will take you to the next line and generates an a.out executable file.
- And type a.out command to execute your program.
After following these steps. Now, you will be getting the output displayed on the screen.
Output:
$ gcc hello.c
$ ./a.out
Hello, World!
Compile and Run C Program
C Basic Syntax
We have already seen the syntax of C. Now, let us know more about the basic building blocks of C.
Example: Hello World Program
#include <stdio.h>/*Preprocessor command*/ int main( ) /*main function*/ { printf("Hello, World! \n"); return 0; /*terminates main function*/ }
Output: Hello, World!
All C programs must have a main() which contains two parts. And they are as follows:
- Declaration part
- Execution part
The declaration part is used to declare all variables that will be used within the program. There needs to be at least one statement in the executable part, and these two parts are declared within the opening and closing curly braces of the main(). While the execution part begins at the open braces ‘{ ‘. While the sub-program function deals with all user-defined functions that are called from main( ).
Managing I/O in C Language
While the I/O operations are useful for the program to interact with users. And stdlib was the standard library for the C language. In these library operations, there are two types. They are 1)stdin 2)stdout
1) C stdin (standard input)
This was used for taking input from devices such as the keyboard.
2) C stdout(standard output)
It was used for giving output t a device such as a monitor.