Review one of the C series (start programming in Linux)

Source: Internet
Author: User

After graduation for several years, I have been using C at work, but the more I use it, the more I feel that I have mastered it too superficial. I will repeat the basic knowledge in my spare time.

Time: 2010-12-09

Topic: start programming in Linux

Main content:

1. Build the compilation environment;

2. Start the firstProgram, How to compile, link and run

3. Notes

 

I. Build the compilation environment

Because my system is a Linux environment, I will not make any special instructions in the subsequent articles on the basis of the Linux environment.

Generally, as long as the Linux system is installed, the compiling environment is available in most cases, but there are also basic systems with nothing. In this case, only the Linux system is installed. In the Debian family, APT package management tools are used, and RedHat is used with RPM management tools. For Ubuntu, enter the following during installation:

Sudo apt-Get install build-essential

You can.

If there is no debug tool, install GDB, which is very powerful:

Sudo apt-Get install GDB

 

2. Start the first program

The classic "Hello World" should not be used. After all, after learning for so many years, you should change it.

Find a text editor. We recommend using vim or ulipad. Enter the following:

# Include <stdio. h> <br/> # include <stdlib. h> <br/> int main (INT argc, char * argv []) <br/>{< br/> int I, J; <br/> printf ("I = % d, j = % d/N", I, j); </P> <p> return 1; <br/>}

After that, save it as main. C. You can also use other names to see what you like. Next, let's talk about how to compile, link, and run it. You need to take a closer look. GCC is used for compilation. Simple compilation and direct link input:

GCC main. c

A. Out is generated. Run./A. Out in shell to output the result. Output on my machine: I = 16470004, j = 134513723. We all know that the entire process includes preprocessing, compilation, and linking. However, the process is completed in one sentence. Is it missing. The answer is no. GNU gcc has helped us, but we will discuss the process of LD in detail in the future. Here we will let you know how to run a program.

 

Iii. Notes:

In order to have a good programming habit, we are opposed to the above practice. To prevent bugs from being hidden in the program, we suggest opening-wall-werror. these two options indicate as many warning messages as possible, and treat the warning information as an error. If the above options are added, the following output will be generated during compilation:

C0: warnings being treated as errors
Main. C: In function 'main ':
Main. C: 24: Error: 'I' is used uninitialized in this function
Main. C: 24: Error: 'J' is used uninitialized in this function

 

Okay, let's modify the program, initialize I, j, and then compile it. No error is prompted. The running result is: I = 0, j = 0.

# Include <stdio. h> <br/> # include <stdlib. h> <br/> int main (INT argc, char * argv []) <br/>{< br/> int I = 0, j = 0; <br/> printf ("I = % d, j = % d/N", I, j); <br/> return 1; <br/>}

 

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.