How to write C language programs with OS X Xcode

Source: Internet
Author: User

Statement : The following content is not original, reproduced elsewhere. Take out just to share to the FY, do not like to spray! Original address http://blog.yorkxin.org/posts/2009/03/15/fundamental-c-with-xcode/
How to write C language program with the OS X Xcode (this is for novice friends to see)

If you're accustomed to using Visual C + + or dev-c++ in Windows, you may suddenly not know how to write your program to Mac OS X, especially if you've used Visual C + + debugger to get started. If this article is just to teach you how to press compile, then I have to cheat the article number, so the content also includes how to use Xcode debugger. The first time you open Xcode you will see the Welcome to Xcode screen as the first attempt, please press create a new Xcode Project first.

Welcome to Xcode if you do not want to see this screen later, you can uncheck the Show this window when the Xcode launches to permanently close, in order to open a new scenario, you can press the file→new→project of the menu. .. 。

New C Language program

Just said press "create a new Xcode project", then you will jump out of a window and ask what you plan to do. For the general C program job, it is categorized in OS X's command-line tool, so the application in the left OS X, and then the command line tool on the right.

Then you will be asked to enter the scheme name, Organization name to write your name on the line, company Identifier I do not know what to do (I do not specifically write OS X software), but it does not affect the operation of the next, so as I fill a look like a decent on the line. The bottom type can be C or C + +, and other objective-c-based frameworks, here I directly choose c.

finally press Next, you will find a place to put this plan, you can find a place to do it.

Understanding the Xcode IDE

The interface of the Xcode IDE is not the same as Visual Studio and dev-c++, and people from Windows may not get used to it, but it doesn't matter, just know something. But before you begin, please go to xcode→preferences ... Inside the behaviors, choose Running→starts, the Show debugger open, and the debug area open, in the View→debug area→show debug area. This preset is not open, but will be used next, very important, so first open.

Next, let's meet the basic configuration of the Xcode Project window:

please know first:

    • "Execute" button (run), like the music software Play, press down is to execute the program
    • the Stop button, which looks like a stop of the music software, can be forced to stop when the program executes
    • status bar, at the top, appears as if the success of compilation or execution
    • "left sidebar", now displays the list of files (other lists can be toggled)
    • "Main work area", now inside is not understand the thing, wait will switch to program code edit
    • "In addition to the wrong zone", so that you can easily debug the code, I will especially say this area.
first-time execution stroke

In addition to writing the program code itself, the most important thing is to run the program to see the results. Just introduced the "Execute" button, see can press it to execute the program, then press to see it. As you press down, you'll see the state of the message change, prompting Building, Build Success, Running, and so on, and finally you'll see this on the right side of the Debug area:

Well, the program can execute and you can see the output.

Modify the program for the first time

But now that you haven't seen the code, just say the left side of the linked fields is switched to "file list", that is, the file is hidden inside, please find main.c , click to open, the main work area will become code:

you should be familiar with this code, which is the normal C language Hello world.

Auto Complete

Next, please try to modify the program. If I want to change to 10 times the content of Hello world, presumably you will be in//insert code here Plus for loop:

[Color= #2aa198!important]int [color= #268bd2!important]i; [Color= #cb4b16!important]for ([color= #268bd2!important]i = [color= #2aa198!important]0; [Color= #268bd2!important]i < [color= #2aa198!important]10; [color= #268bd2!important]i+ +) {[color= #268bd2!important]printf ([color= #2aa198!important] "Hello, world[ color= #dc322f!important]\n[color= #2aa198!important] ");}

When you hit halfway, you should be doing something like this:

This feature, called " auto Complete," is one of the most useful features of Xcode, and if you're not unfamiliar with Visual Studio, you're halfway through, Xcode will automatically prompt you to write the Code and press TAB to To type by jumping to the frame. You can try it, press Tab to switch, and then press Enter to confirm. Self-completion is no longer a reality, in addition to the self-Syntax, you can also show the variable name, function name (identifiers), hints. h files can include, hint struct structure. The identifers example is that you want to use fputs to play FP, it will automatically show all the FP-open functions (because there is an include stdio.h, so grab), press the key to select the key, in addition to the right side of the linked fields appear simple to explain. If you press more, there will be complete documentation, so you don't need to check the file.

Another secret sincerity, if you want to move your trigger, you can press ESC. For example, I first announced var1, VAR2, VAR3, want to specify a value for one, hit half only var ran to the other side, and then back, you can press ESC after the Var, will jump out of the self-completion:

with a mention, the size of the play, it also recognizes. you can try it and you should feel "he seems smart".

Automatic Bug Tips

I changed it, oops, as if forgetting something?

program write wrong, not to the editor to know, Xcode has been self-editing, check whether your code can be translated through, and self-labeling wrong, if you press the line next to the red sighs, it will tell you where the wrong:

Yes , forget to say, after the patch, this bug message disappears. Wrong message In addition to the programming of the wrong code, there will also be a warning to the editor, for example, there is a change declared but not used:

how, is it convenient?

execute the travel and input information

right now run again, this time do not move the mouse, please press Command + R on the keyboard, will run "Execute":

If you're coming from Visual Studio or dev-c++, you might be wondering why it's not a familiar black-and-white screen? In fact, Xcode does not open a new terminal program, but directly in its own Console to input output, I guess this reason is because Xcode is the GUI application of the main direction, so the Console is simple, and because OS X is a kind of UNIX, and the The operating system, which has a natural input and output direction, can be directly connected to Xcode (this is taught in the System program). It seems that Eclipse is like this. but , it's not going to be done automatically when you start the Console, you have to turn it on, so I'll ask you to open the Debug area first. then try to execute a simple program with input output, enter an integer n, and Output n times "Hello, world!\n".

[Color= #2aa198!important]int [color= #268bd2!important]Main([color= #2aa198!important]int [color= #268bd2!important]argc, [color= #cb4b16!important]const [color= #2aa198! Important]char*[color= #268bd2!important]argv[]) {[color= #2aa198!important]int [color= #268bd2!important]i, [color= #268bd2!    Important]n; [Color= #cb4b16!important]if ([color= #268bd2!important]fscanf (color= #268bd2!important]stdin, [color= #2aa198! Important] "%d",&[Color= #268bd2!important]n)==[color= #2aa198!important]1) {[color= #cb4b16!important]for ([color= #268bd2!important]i=[Color= #2aa198!important]0; [Color= #268bd2!important]i<[Color= #268bd2!important]n; [Color= #268bd2!important]i++{[Color= #268bd2!important]printf ([color= #2aa198!important] "Hello, world![    color= #dc322f!important]\n[color= #2aa198!important] "); }} [color= #cb4b16!important]return [color= #2aa198!important]0;}

Press Run, then enter 3 in the Console, it will enter the integer n = 3, and print 3 times Hello, world!, consistent with what we want.

If you do half of the program you want to get rid of, just press Stop.

using Debugger

As with Visual Studio, the professional IDE must have a perfect Debugger integration, and Xcode certainly has it, which is a great feature for me, a person unfamiliar with command-line debugging. General Command line debugger you want to make your own point (tell it in the first few lines), you can command it yourself, but with Xcode, you just move the mouse. Here's an example of a simple applet:

[Color= #93a1a1!important]#include <stdio.h>[Color= #93a1a1!important]/ * Global variables * /[Color= #2aa198!important]int [color= #268bd2!important]i_am_a_global_variable=[Color= #2aa198!important]999; [Color= #93a1a1!important]/ * functions * /[Color= #2aa198!important]void [color= #268bd2!important]another_function([color= #2aa198!important]int*[color= #268bd2!important]a) {(*[Color= #268bd2!important]a)++; [Color= #268bd2!important]i_am_a_global_variable+= *[Color= #268bd2!important]a; [Color= #cb4b16!important]return;} [Color= #2aa198!important]int [color= #268bd2!important]some_function([color= #2aa198!important]int [color= #268bd2!important]a) {[Color= #2aa198!important]int [color= #268bd2!important]some_local_var=[Color= #268bd2!important]a; [Color= #268bd2!important]printf ([color= #2aa198!important] "Some_local_var have been changed to%d[color= #dc322f!  important]\n[color= #2aa198!important] ", [color= #268bd2!important]some_local_var); [Color= #268bd2!important]another_function (&[color= #268bd2!important]some_local_var); [Color= #268bd2!important]printf ([color= #2aa198!important] "Some_local_var have been changed to%d[color= #dc322f!  important]\n[color= #2aa198!important] ", [color= #268bd2!important]some_local_var); [Color= #cb4b16!important]return [color= #2aa198!important]0;} [Color= #2aa198!important]int [color= #268bd2!important]Main([color= #2aa198!important]void)  {[Color= #2aa198!important]int [color= #268bd2!important]number;  [Color= #268bd2!important]printf ([color= #2aa198!important] "Enter Number:"); [Color= #cb4b16!important]if ([color= #268bd2!important]fscanf (color= #268bd2!important]stdin, [color= #2aa198! Important] "%d",&[Color= #268bd2!important]number)==[color= #2aa198!important]1) {[color= #268bd2!important]some_function ([color= #268bd2!important]number); [Color= #268bd2!important]printf ([color= #2aa198!important] "You ve entered%d[color= #dc322f!important]\n[color=#  2aa198!important] ", [color= #268bd2!important]number); } [color= #cb4b16!important]else {[color= #268bd2!important]printf ([color= #2aa198!important] "No number entered. Bye.  [color= #dc322f!important]\n[color= #2aa198!important] "); } [color= #cb4b16!important]return [color= #2aa198!important]0;}

The point of definition is "go back to debugger" before you perform this line, that is, if you put the interrupt on line 12th, then it will pause the program before the 12th line and enter the debugger. " The method of setting the point is very simple, just click on the left mouse button on the line. You can move it and drag it with the mouse. The break can be canceled temporarily, that is, a little bit to turn it into a shallow blue color. You can delete it, just drag it to the travel area, just like the Dock. Now I'm setting the line in Some_function (number).

Then execute the program, first enter the number in the Console, and then press Enter to input into the program. Then, the program pauses immediately, and you see the code inside, which shows which row to stop, and the Debug area on the left side will also show the current region changes. The Debug area has a tool column with a few important buttons on it, such as:

Here's a first introduction to the usual Debugger directives:

    • Continue (continued): Leave Debugger and continue to execute the program, which may be interrupted at the next point.
    • Step Over (skip): Skip this line and stop in the next line
    • Step into (jump): Currently in this line has a function, jump into
    • Step out: The current line is in a function, jumping out to the calling function program (that is, return to finish)

familiarize yourself with these four instructions and you will be able to walk through the code. Then I'm going to set up two more points, i_am_a_global_variable + = and another_function (&some_local_var), two lines (don't have to stop the program first, Directly press the left mouse button to add a point). Then press Continue, and when it does another_function this line, it will stop to enter the Debugger. You will notice that there is also a change in the left side, as it enters the Stack of Function call. You can switch between different stacks, and there will be different Local Variables on the left, and the way to do this is to press the function name of the Debugger navigation column.

then click on the Continue and run into the another_function, and you'll see that the left pane will show the memory address of the target and the memory inside the reference, as well as the function (reference). "--" To the global variable i_am_a_global_variable, so Xcode will also list itself:

Another small example, this time is array:

[Color= #93a1a1!important]#include <stdio.h>[Color= #2aa198!important]int [color= #268bd2!important]Main([color= #2aa198!important]void) {[Color= #2aa198!important]int [color= #268bd2!important]array[]={[color= #2aa198!important]1, [color= #2aa198!important]2, [color= #2aa198!important]3, [color= #2aa198!important]4, [  Color= #2aa198!important]5};  [Color= #2aa198!important]int [color= #268bd2!important]i; [Color= #cb4b16!important]for ([color= #268bd2!important]i=[Color= #2aa198!important]0; [Color= #268bd2!important]i<[Color= #2aa198!important]5; [Color= #268bd2!important]i++{[Color= #268bd2!important]printf ([color= #2aa198!important] "array #%d is%d[color= #dc322f!important]\n[color= #2aa  198!important] ", [color= #268bd2!important]i, [color= #268bd2!important]array[[color= #268bd2!important]i]); } [color= #cb4b16!important]return [color= #2aa198!important]0;}

at the same line of printf, and then execute it, you'll find that it lists the contents of the array (as you can):

What if it's an animal array? We know that malloc, Calloc, ReAlloc is coming back with the opening address of the memory it was assigned to, and that Xcode would not be smart about it as an array? We modify the above program to Calloc:

[Color= #93a1a1!important]#include <stdio.h>[Color= #93a1a1!important]#include <stdlib.h>[Color= #2aa198!important]int [color= #268bd2!important]Main([color= #2aa198!important]void) {[Color= #2aa198!important]int*[Color= #268bd2!important]array=([color= #2aa198!important]int*[Color= #268bd2!important]calloc ([color= #2aa198!important]5, [color= #cb4b16!important]sizeof ([color= #2aa198!)  Important]int));  [Color= #2aa198!important]int [color= #268bd2!important]i; [Color= #cb4b16!important]for ([color= #268bd2!important]i=[Color= #2aa198!important]0; [Color= #268bd2!important]i<[Color= #2aa198!important]5; [Color= #268bd2!important]i++) {[color= #268bd2!important]array[[color= #268bd2!important]i]=[Color= #268bd2!important]i+[Color= #2aa198!important]1; [Color= #268bd2!important]printf ([color= #2aa198!important] "array #%d is%d[color= #dc322f!important]\n[color=#  2aa198!important] ", [color= #268bd2!important]i, [color= #268bd2!important]array[[color= #268bd2!important]i]);  } [color= #268bd2!important]free ([color= #268bd2!important]array); [Color= #cb4b16!important]return [color= #2aa198!important]0;}

set the point in the free (array) line and execute, and you will notice that the Debugger does not list the contents of the array, but only the indicator:

From the graph we know two things: The array is declared int *, so Xcode grabs its memory address. it uses int to read the memory inside the *array point, so it gets the first value of 1, because the contents of the array are 1, 2, 3, 4, 5. what if you want to see array[1] or something? It's time to use Expression Monitor, right-click the Add expresssion in this variable list, and then enter Array[1]. In addition, since it is Expression, you can enter equations such as array[1] + 2.

Debugger I will use the function probably just like this ... But I think that's enough, and with that, I'm going to be able to catch up on logically's mistakes.

Font Setting

We look at code every day, and we always want them to keep their eyes open before they can see it. Xcode can also adjust the font. Enter Xcode's Preferences settings, in Fonts & Colors pages. Each item is divided, to change the words, is the first Command + A full selection, and then press the T icon, you can change all at once. The font of the Console is in the "console" page of the same painting.

How to write C language programs with OS X Xcode

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.