[Interesting things in C language learning] _ 29 _ C programming language _ secnod_edition _ exercise

Source: Internet
Author: User

I received a Chinese version of K & R the day before yesterday. I was a little excited about the long-awaited book.

[Exercise 1-1]

In "Hello World"ProgramManufacturing chaos.

I don't really want to do this, because I am not a confused person, although sometimes writingCodeI can't control it by myself, but I hope to find the ladybug in the code.

Here let's look at something else:

1. Elf

After compiling the hello World Program in Linux, the running process is very simple. However, I have been trying to find out what happened here. If you are proficient in machine code

Od can help you solve this problem, but the problem is that I don't understand, so I need to "save the country by curve ".

I compiled it like this: gcc-wall hello. C-O hello

Fortunately, there is no error. I can run my program, but I don't want to check my running results. I did this:

Less hello. As a result, Linux tells me that this file may be a binary file and asks if it is still opened. Of course, it will be opened here. Why don't I open it for your Linux brother.

Let's take a look at the output result. Let's first look at the output line:

I am not interested in a bunch of ^ A and ^ @. I am interested in the first four characters:

What do we mean by Elf? Let's take a look at the content from Wikipedia:

Http://zh.wikipedia.org/wiki/%E5%8F%AF%E5%9F%B7%E8%A1%8C%E8%88%87%E5%8F%AF%E9%8F%88%E6%8E%A5%E6%A0%BC%E5%BC%8F

That is to say, elf indicates that this file can be connected and executable, that is, it is a binary file, and we can compile a function separately.

For the ELF format, the connection is briefly described.

2. connected database

We know that in the assembler. translate the ASM file. after the o file, you need to add. o file is linked to the final executable file. The pre-compiled library file is used here. How can we

Which library files are linked? Don't worry. Although Tang Miao was caught by the mouse, they could not be close to me. As I said, we can see the following output:

Okay, see no, kiss, here pointed out the library file we used when the link,/lib/ld-linux.so.2, you can search, in your computer (if you install

Select to install the development tool) You should be able to find this file.

This file is a dynamic connector file that needs to be used when linking. From the LD prefix, we can see that it is related to the linker lD. In fact, many other library files are prompted.

3. GNU

There are three more striking letters in the output, that is: GNU. I didn't expect it. There are actually three GNU letters in the target file you compiled.

4. Hello World string

There is also a striking point in the entire file: Hello world, which can be seen here

This is the character we will output.

5. system information, compiler information, and file section information

Here we can clearly see the keyword information of. Text,. Data,. BSS,. comment,

Let's check whether my GCC version information is the same as shown here.

Obviously, the information displayed here is consistent with the hello file we use less to view.

6. readelf Program

In fact, Linux not only provides a binary file viewing tool OD, but also provides a more visual viewing tool readelf. Let's take a look at the differences between readelf and less.

Here we can see that there are many similarities between the output content and the less command output. If you are familiar with readelf, can you better understand it with the less command?

7. objdump

If you want to view more specific content, you can also use the objdump tool. For example, to view the content of all segments, you can: objdump-x hello

The output result is as follows:

It can be seen that the information provided by objdump is more detailed than that provided by readelf. If you are interested in the analysis of the ELF file, you can go to this link to see: http://wenku.baidu.com/view/5ca21b28915f804d2b16c164.html

[Exercise 1-2]

Use '\ C' In the printf function to see what the output result is. What do you mean? What will be output?

We can see that when printf is output, the string literal value is not output as we normally understand. Many people think so. Actually? I once said this question when I first [learning interesting things in C Language]

I can guess that when many of us are learning, your teacher said this: the content of the first parameter of the printf function, if not formatted control and escape sequence, is output as is. Think about whether your teacher said so.

Why? La

[Exercise 1-3]

[Exercise 1-4]

[Exercise 1-5]

Note the division of integers in these three exercises.

 

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.