First, Hello, world!
Enter under Linux: (Take hello.c as an example)
First select the path to save the file (for example: CD work)
VI hello.c (file name to edit)
Input program:
# include<stdio.h>
int main (void)
{
printf ("hello,world!\n");
return 0; Note: There should be a space between return and 0
}
Exit and save the program:
In VI mode, after pressing ESC, press: Wq plus enter
Using GCC to compile the program:
(first to find out which folder the program exists in)
Ls
CD work
Gcc-wall hello.c (or gcc hello.c-o hello) Note: It is best to add the-wall option at compile time so that the hidden warning can also be displayed, and the first letter of the Wall is capitalized.
Output program Results:
./hello (or./a.out)
After execution you can get:
Hello, world!.
II. compilation instructions when referencing a mathematical function
When a mathematical function is used in a program, the-LM option should be appended to GCC when compiling in VI, because the math functions are saved with the Libm.so library file (usually in the/lib directory), and the option-LM is to tell the program that the math function is to be found in this library. such as procedures:
#include <stdio.h>
#include <math.h>
{
int a=0;
int b;
b= cos (a);
printf ("The value is%d", b)
return 0;
}
You should enter Gcc-lm-wall math.c at compile time
How to write C program and compile execution under Linux