Li Hongqiang iOS Development "0 Basic Learning iOS development" "02-c language" 02-First C language Program

Source: Internet
Author: User
Tags ultraedit

Objective

Already nagging so much theoretical knowledge, starting from this, it is necessary to learn the C language grammar by contact code. To learn any language, the first thing to master is certainly the grammar. The purpose of Learning C language grammar is to be able to write programs in C, and then run programs to interact with hardware (computers, mobile phones, and other hardware devices). Since our ultimate goal is to learn iOS development, learning iOS development must be done under the Mac system, so I am developing a C language program in a MAC system environment, not in a Windows environment.

Next, in the MAC system environment to write the first C language program, and finally run the program to do a small interaction with the computer

One, the first C language program written-hello world

Why is the first program called "Hello wolrd"? In fact, the computer industry, the first program to learn any technology can be called "Hello world." "Hello world" literally means "hello", that is to say hello to the world. Our first program was born in this world, and it must be said hello to the world.

1. What is the tool for writing code?

The first thing we have to do is to write the code, in the code to say clearly what you want the computer to do. Actually write the code is like usually writes the article, just writes some text content on the computer, that uses what tool to write the code? Usually we write articles in Windows, you can use Notepad, Word and other text editing tools. In Mac, we can install some text editing tools to write code, such as UltraEdit (click the link to download UltraEdit). Of course, in the actual development, in order to improve the development efficiency, the general use of development tools, the benefits of development tools, I have said in the previous article. However, the development tools screen a lot of operational details and grammatical details, not conducive to beginners intuitive, systematic learning a language. So, here we are temporarily using the text Editing tool UltraEdit to write the C language code.

2. Write code 1> C program is composed of functions

Before you write the code, you must first know that any C program is composed of one or more program segments (small programs), each of which has its own function, which we generally call "functions". So, you can say that the C language program is composed of functions.

For example, if you write a MP3 player program in C, the program structure is as follows:

    • As you can see, a function is a program segment used to implement a function, and each function has its own function. Therefore, you should write the code needed to implement a function in the function. For example, one function is to play MP3, so the code that plays MP3 should be written in this function.
    • When a function is called (executed), the computer executes all of the code in the function sequentially, demonstrating the functionality implemented by the function.
    • In general, we will give different functions to different functions to implement. For example, write the code that pauses the MP3 to a function, and write the code that stops the playback MP3 to another function. Therefore, there may be a lot of functions in a C program.

Entrance to 2> C program

As I said before: A C program can have a lot of functions, so there is a question: when we run the whole program, in a number of functions, the computer will execute which function first? In other words, where is the entrance to a C program? I wrote thousands of lines of code, which line of code should I start first? Do you start with the first line or the last line of code?

In fact, the entrance to the C program is a function called Main, referred to as the main function. (In order to differentiate functions, each function has a name) that is, regardless of how many functions are in the whole program, the main function is executed first. Whether the main function is written in the middle of the file or at the end of the file, the main function is executed first.

It is important to note that:

    • If there is no main function in a C program, then the program does not have the ability to run. Not even the entrance to the program, what else is running?
    • There can be only one main function in a C program. Imagine that if there are more than one main function, what is the main function to execute first? This will make the computer unable to select

3> writing the main function

Now that you want to run a C program, you must have a main function, and then write a main function in the text Editing tool. The format of the main function is roughly as follows:

    • The int of line 1th is not understood at the moment, it is considered as the fixed method of main function.
    • The 1th line of main is the function name, a pair of parentheses after main () is the function of the flag, absolutely cannot be missing! And this pair of parentheses is "English brackets ()", not "Chinese brackets ()"!
    • The 2nd line begins with a pair of curly braces {}, and the code inside the function is written inside the curly braces. Each function has a pair of {},{} contents that can be called the "function body".
    • In line 3rd add a line return 0, and temporarily do not understand its meaning, first think is the main function of the fixed notation.
    • Like Line 3, this code, written in a function, can be called a "statement." After you finish writing a statement, add a semicolon ";" to the end of the statement.

4> Writing output statements

Next, add the code in the main function.

1 #include <stdio.h>2 3 int main ()  4 {  5     printf ("Hello world\n"); 6     return 0;7}
    • In line 5th, a statement was added to printf ("Hello world\n"), and the statement was used to allow the computer to output a double quotation mark "" around the screen: Hello World, followed by "\ n" is an escape character, which means the carriage return line, so the output Hello Wrold This string of content will be wrapped automatically. And why does this sentence have to be written like this? Why does this statement allow the computer to output something? These questions are put on the shelf and will be explained in detail later.
    • In order to ensure the normal use of the 5th line of code, the main function is preceded by a line of code # include <stdio.h>, temporarily do not have to understand its meaning, silently add can, note, here is not added semicolon ";" Of
    • When this program is run, the main function is executed first, and then all the statements in the main function curly braces {} are executed sequentially (line 5th, line 6th)
    • It is important to note that all statements in the main function must be written in line 6th, return 0; Before the statement, do not ask why, in the future will be described in detail

3. Save the source file as a C program

The code is finished, always save it, why do you save the file? Each file has its own extension name, and different extension names represent different types of files, such as. mp3 represents an audio file, and. txt represents a text file. The C language code we wrote earlier should be saved as a file with a. c extension, called the "source code file" of the C language program, also known as a "source file."

Press the shortcut key command + S, enter the filename (I am called one.c), select the file format

In this case, the first C language program is finished, very simple, a little pressure is not

Second, compile the program

Before the program has been written, and can't wait to do things must be running the program to see how the computer will react. Unfortunately, the previously written one.c file is not yet ready to run. The previous article has said that the computer can only recognize the 0 and 1 machine instructions, you write these now what int, main these English, it is not understand. We need to use the C language compiler to translate the source files into only 0 and 1 binaries, the translation process, which we call "compilation".

A compiler called GCC is supported on Mac systems, and GCC supports a variety of programming languages: C, C + +, Objective-c, Java, and so on. Later, Apple itself developed a compiler called clang, the goal is to go beyond GCC. We use the clang compiler to compile the program here. To use the Clang compiler in your Mac, first install a command line tool

1. Download the installation command-line tool

There are 2 ways to install command-line tools.

1> Way one: directly to the apple website download

Click on the "Apple icon" in the upper left corner of the screen to view your Mac system version

    • If your Mac is a Lion system (version <=10.7), please select Download:
    • If your Mac is a mountain lion system (version >=10.8), please select Download:

2> Way Two: first to the Apple website to download and install Xcode, then open Xcode, install the command line tool in Xcode

2. Open the terminal

Once you have installed the command-line tool, you can use the Clang compiler. So how do you use Clang? Start the compiler by entering the appropriate clang directive in terminal.

By default, the path that the terminal points to is the user's personal home folder, my home folder is/users/apple, and my user name is Apple

3. Jump to the path where the one.c is located

For ease of operation, we should switch the path of the terminal to the path where the one.c is located, one.c stored on my desktop, the folder path is/users/apple/desktop

Input command: Cd/users/apple/desktop, then hit enter, the instruction "CD" is to change the meaning of the path.

4. Enter the compilation instructions and compile the one.c file

Input command: Cc-c one.c, then hit enter

The instruction "cc-c" means compiling a source file followed by the name or full path of the source file.

If you do not see too much information after you finish the compilation instructions, you are successful in compiling. After the compilation succeeds, a binary file is generated in the path where the terminal is located, called the "target file", and the extension is named ". O", and the file name is consistent with the source file. The ONE.O file was generated when the one.c file was compiled successfully.

Add: In the development process, it is not possible to write all the code in a. c file, for modular development, it is common to write different functions to different source files. If you want to compile multiple source files at the same time, write this: cc-c one.c two.c three.c. After the source file is compiled, each source file generates a corresponding. o file, such as TWO.C generated TWO.O, THREE.C generated THREE.O

5. Compiler syntax Detection

In addition to compiling the. C source file into a. o target file, the compiler also has a very important function: syntax detection. Like English, C language has its own grammar, if you do not follow the C language syntax to write code, it will not compile successfully. Before the target file is generated, the compiler checks the. c file for syntax errors and, if a syntax error occurs, lists the total number of errors, the cause of the error, and the line number of the error code, which does not produce the target file; the corresponding syntax error must be corrected before the target file is generated.

Next I write the code deliberately wrong, the 3rd line to the int is Intt, the 5th line statement missing a semicolon ";"

1 #include <stdio.h>2 3 Intt main () 4 {  5     printf ("Hello world\n") 6     return 0;7}

Re-save the source file and recompile it in the terminal

It is very obvious: there are 2 errors, the 1th error is on line 3rd of one.c, and 2nd error is in the 5th line of one.c. These error messages (errors) must be modified, as long as an error message exists, it is not possible to compile successfully. You may also encounter some warning messages (warning), which can be ignored and do not affect compilation.

Third, the link Program 1. What is a link

After the source file has been compiled successfully, a. o target file is generated, which is a binary file, but it cannot be run. There are 2 main reasons why a target file cannot be run:

1> in the development process, it is not possible to write all the code in a. c file, for modular development, it is common to write different functions to different source files. After the source file has been compiled, each source file has a corresponding. o file, such as TWO.C generated TWO.O, three.c generated THREE.O, these. o files cannot be run alone, they all have an inseparable relationship and need to combine all associated. o target files.

2> In addition to combining all of the target files, you need to include the C language library in order to generate the executable file.

The process of combining all the associated. O Target files and the C-language function library together to generate the executable file, which we call "links."

2. Link the target file

Enter the command in the terminal: CC ONE.O, then hit enter. If you want to link multiple target files simultaneously, write this: cc ONE.O two.o THREE.O

When the link succeeds, an executable file is generated, and the default name is "A.out". Since we generated the executable file under Mac System, the MAC system is based on UNIX system, so this file can only run on UNIX system. If you are in a Windows environment, the resulting executable file extension is named ". exe".

3. Change the name of the executable file

If you want to change the name of the executable file, you can enter the directive: Cc-o one one.o,-o followed by the name of the executable file, so the name of the executable becomes one

4. Continuous compilation, linking

In fact, it can also be entered directly in the terminal: CC ONE.C, compiled sequentially, linked to two operations.

This command produces an executable program called "A.out". A target file named ONE.O is generated in the middle, but it is deleted when the link is complete.

If you want to modify the name of the executable file, the same as before, the instruction is: Cc-o ABC one.c, the executable file name is ABC

Iv. Operating Procedures

After the first few steps, the executable file is finally generated, and then you can run the program. There are 2 ways of running the program:

1. Enter the command to run the program in the terminal

Enter:./a.out in the terminal and hit enter to run the program. Here a.out is the name of the executable file.

After you've knocked out the instructions, you'll see a "Hello World" output on the screen. In this way, we have a small interaction with the computer, it output a word for us.

2. Double-click the executable file

Directly double-click the a.out file and select Use Terminal to run the program

After successful operation

V. Summary

After some toss, finally the first C program run successfully, a total of 4 steps: Write the program, compile, link, run

It is important to note that:

    • There is an error in the middle of the link, then there is no subsequent operation. such as compile error, then there is no link to this link.
    • If you modify the source code, you need to recompile, link, and then run the program to see the latest effects.

Back to top six, learning suggestions 1. Learning programming is not learning English

After writing this Hello World program, many people will look up the words, see what the meaning of int, see the stdio, return and what it means. In fact, this is very unnecessary practice, we are learning programming, not learning English, there is no need to one word to buckle. Besides, these words are not English words to the computer, just is a symbol, is code!!! The computer can only recognize 0 and 1, so the code is compiled to 0 and 1 before it can be executed by the computer. Computer is not a Youdao dictionary, it is impossible to know any English words. Many of the code is fixed, and there are not too many so-called English meanings.

2. Readability of the program

This Hello world program has very few functions and only a small amount of content, and we occupy several lines of code. One might ask: Does this code have to be wrapped? Do you want to indent a few spaces in front of the 5th line Prinf statement?

In fact, you can absolutely not indent

1 #include <stdio.h>2 3 int main ()  4 {  5 printf ("Hello world\n"); 6 return 0;7}

You can also write all the code in one line (the 1th line of code is special and must be a separate line)

1 #include <stdio.h>2 int main ()  {printf ("Hello world\n"); return 0;}

Although this is not a problem, but the code readability is very poor, imagine also know: The whole file in a line of code, if the program is very large, certainly a lot of code, such code is how disgusting ah! Others may not understand what you are writing, even if you do not understand it. In the work, we will inevitably encounter problems, often have to resort to others, but others to understand our code based on, can help us solve the problem ah. Therefore, the readability of the code is important! Be sure to develop good programming habits.

3. Beginners should not be too root to ask the bottom

When you are not very familiar with a new technology, the first thing you have to do is to learn how to use it, first successfully used it, not from beginning to end to see all the source code, each line of code in each word is clear what meaning, there is absolutely no need. When you have a certain technical basis, the technology is used very well, and the time and ability to allow, it is necessary to root to ask the bottom of the interpretation of all the source code. So, the code of the first C program many do not understand the place, temporarily do not have to tangle, learn the back, you will unconsciously know what they mean.

Back to top VII, clang instruction summary

These instructions do not have to go to rote, there is a general understanding, use the time to check the information can be

1. Compile a single C source file and produce a target file

Cc-c one.c
This command produces a target file named ONE.O.

2. Compile multiple C source files and generate a target file for each file

Cc-c one.c two.c three.c
This command produces 3 target files: one.o, TWO.O, THREE.O

3. Link a single target file

CC ONE.O
This command produces an executable file named A.out.

4. Link multiple target files

CC ONE.O TWO.O THREE.O
This command produces an executable file named A.out.

5. Compile and link a C source file

CC ONE.C
This command produces an executable file named A.out. A target file named ONE.O is generated in the middle, but it is deleted when the link process is complete.

6. Compile and link multiple C source files

CC ONE.C TWO.C three.c
This command produces an executable file named A.out. When more than one source file is compiled, the destination file is not deleted. This allows you to make changes to the program and recompile only the source files that have been changed.

7. Compile a C source file and link it to the existing target file

CC ONE.O TWO.O three.c
This command produces an executable file named A.out.

8. The above can produce executable file instructions can be added to the "-O name" option, the resulting executable is called name

Like Cc-o ABC one.c.
This command produces an executable named ABC.

9. Execute executable file

./a.out
This instruction can execute a executable file named A.out.

Li Hongqiang iOS Development "0 Basic Learning iOS development" "02-c language" 02-First C language Program

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.