How to write C program and compile execution under Linux

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.