The C language _ios of IOS development Basics

Source: Internet
Author: User
Tags reserved

Why study the development of iOS to learn C language first, learn C language on the role of iOS development.

Now more and more iOS development interests have been put into iOS training, some already employed staff, some students are still studying, and some are completely 0 of the students, so for them to start from the basis of learning, first from the C language learning.

Because C language is a language foundation, it is widely used because of its simplicity and flexibility, and plays an important role in various development environments. So, Why learn iOS development to learn C language first?   

Because in the development of iOS, will often use C language and assembly, and assembly language is relatively jerky, C language thus become the programmer's first choice. And as a qualified C-language engineer, understand the most basic GCC compilers, data types, variables and constants, structure, union and enumeration, C standard library, heap memory allocation, IO and so on, which for the future of C, embedded and so on language learning has laid a solid foundation, flexible response to various development work, Be a good software engineer.

Therefore, C language engineers have a flexible and practical expression, so that they can be in the program or some large database low-level core programs, professional graphics processing programs, video image processing programs and game design and other applications in the application of the distribution of light and heat.

Overview

The current trend of mobile development is overwhelming, and the series is hoping to talk about personal insights into iOS development: The iOS series plans to talk about iOS development from several perspectives.

1.C language

2.OC Foundation

3.IOS Development (IPHONE/IPAD)

4.Swift

So there is a lot of content to continue to add, but today we start from the most basic C language, C language section I will be divided into several chapters to say, today we have a simple look at some of the basic knowledge of C, more advanced content I will put in the later article.

Today's basic knowledge is divided into the following points (note: Loops, conditional statements do not repeat here):

1.Hello World

2. Operation Process

3. Data type

4. Operator

5. Common functions

Hello World

Since it's the iOS Development series, first look at the operation of C in Mac OS X.

Open Xcode

Select a command line program

Select Save Directory

Automatically generate the following code

OK, on the Xcode we write our own program as follows


//MAIN.C
//C language Base////
Created by Kenshin Cui on 14-7-12.
Copyright (c) 2014 Cmjstudio. All rights reserved.
#include <stdio.h>
void ShowMessage () {
 printf ("hello,world!\n");
}
int main (int argc, const char * argv[]) {
 showmessage ();
 return 0;
}

In the above procedure we need to explain a few things:

The main function is a program entry, a program can only have one main () function, need to have an integer return value (in fact, the return value int can be omitted, but this does not mean not to return the value, but the default is int; we can also not provide returns in the main () function. This is because the C language grammar requirements are not strict enough);
#include是预处理指令, which is used to include the specified file (note that it is processed before translation), and it actually does the job of copying the corresponding file to the specified location; it can be any type of file, not just. h files;
The ShowMessage function above must be written on the main () function and, if written below, must be declared before the main () function;
Note: There are two ways to #include include files: Use <> and "". The difference is that <> contains only find compiler library function files, so it applies to include library functions, and "" contains the current directory of the program, if not found, find the library function path, so it applies to custom files;

Run process

The C language runs in two big strides: compiling and linking

Compiling: The compile phase compiles the corresponding XXX.C source file (ASCII format) into the target file xxx.obj, which is a binary format (of course, we will have multiple. c files, and multiple corresponding. obj); Preprocessing (for example, #include directives) before compiling, A grammar check is also performed while compiling, and the generated. obj file cannot be executed separately because each. obj is associated, and they also refer to the C language library function;
Links: The process of linking each. obj file and C language library function together to generate an executable file;

Extended

It is not realistic to write all the code in a program in a large project development to a file, and we typically divide a sub operation into two files:. C and. h files. Implement the corresponding function in the. c file, and declare the function in. h so that you can separate the child operations from the sequence problem as long as the corresponding header file is included above the main function. For example, rewrite the example "Hello World" (note that the. c and. h file names corresponding to the message can be completely different, but for the purposes of the specification we still take the same file name):

Message.h

Message.h
//C language Base
//Created by Kenshin Cui on 14-7-12.
Copyright (c) 2014 Cmjstudio. All rights reserved.
void ShowMessage ();

Message.c

MESSAGE.C
//C language Base
//Created by Kenshin Cui on 14-7-12.
Copyright (c) 2014 Cmjstudio. All rights reserved.
#include <stdio.h>
void ShowMessage () {
 printf ("hello,world!\n");
}

Main.c

MAIN.C
//C language Base
//Created by Kenshin Cui on 14-7-12.
Copyright (c) 2014 Cmjstudio. All rights reserved.
#include <stdio.h>
#include "Message.h"
int main (int argc, const char * argv[]) {
 showmessage ();
 return 0;
}

You can find that the program still works, but we're thinking about a problem: if we don't divide into two files, does it work correctly if we include message.c directly in the main function file? The answer is no, because the two files generated by the compilation of Main.obj and Message.obj in the link will find that there is already a showmessage function defined in Message.obj, which throws a "marker repeat" error.

Note: The char type is the smallest data type unit that occupies 1 bytes under any type of compiler, and the variable assignment of a char type can be assigned directly to a character or to an integer (the corresponding ASCII value); You can use two long to decorate an integer (that is, a 8-byte cosmetic long long) that is often used, but a two long cannot modify double and there is no two short, or compile a warning; a floating-point constant if the following is followed by the F compiler to consider it a float type, Otherwise, the double type, for example 10.0 is the double type, and the 10.0f is the float type.

Operator

C language has 34 in the operator, with C #, Java and other languages are not much different, this refers to the list of some considerations

The relational operator returns 1 for real, and 0 for false, and 0 for true (negative and positive) in the conditional language and only 0 for false;
The C language can not save the value of the relational operator;
The final value of the comma expression is the value of the last expression;

See the following examples for the above points

MAIN.C
//C language Base
//Created by Kenshin Cui on 14-7-12.
Copyright (c) 2014 Cmjstudio. All rights reserved.
#include <stdio.h>
int main (int argc, const char * argv[]) {
 int a=2>1,b=2<1,c=99,d=0;
 int f=0,g=0,h=0,e= (f=3,g=4,h=5);
 a>0;//did not save the result of the Operation
 printf ("%d,%d\n", a,b);//Result: 1,0
 if (c) {//Can be passed
  printf ("true.\n");
 }
 if (d) {//Cannot pass
  printf ("false\n");
 }
 printf ("%d\n", e);//Result: 5 return
 0;
}

Common functions

printf () function

The printf () function is used to output data to a standard output device, and the format character can perform a powerful output function, which we have already used in the example above.

Usually our output is not fixed content, but contains some variables, we need to use the format character, the common format characters are as follows

We can accurately control the output width of the format character and the decimal position of the floating-point number.

MAIN.C
//C language Base
//Created by Kenshin Cui on 14-7-12.
Copyright (c) 2014 Cmjstudio. All rights reserved.
#include <stdio.h>
int main (int argc, const char * argv[]) {
 int a=16;
 float b=79.3f;
 printf ("[a=%4d]\n", a);
 printf ("[a=%-4d]\n", a);
 printf ("[b=%10f]\n", b);
 printf ("[b=%.2f]\n", b);
 printf ("[b=%4.2f]\n", b);
 return 0;
}

The results of the operation are as follows

Runresult

From the results of the operation we can find the positive number before the format character is set to the front, the negative set the back-end alignment, if the total length of the data exceeds the set decorated length, then the actual length is displayed; the integer after the decimal point is used to control the length of the decimal place after the point

scanf () function

The scanf () function is used to receive input data from a standard input device

MAIN.C
//C language Base
//Created by Kenshin Cui on 14-7-12.
Copyright (c) 2014 Cmjstudio. All rights reserved.
#include <stdio.h>
int main (int argc, const char * argv[]) {
 int a,b,c;
 scanf ("%d,%d,%d", &a,&b,&c);//At this point you need to enter: 1,2,3 then enter
 printf ("a=%d,b=%d,c=%d\n", a,b,c);
 return 0;
}

For the scanf () function We need to emphasize a few points

Parameter receive end operation with carriage return

If you need to receive more than one parameter, the delimiter between multiple parameters is arbitrary, but if the delimiter is "space", the delimiter can make the space, tab, and carriage return (the last carriage return is considered Terminator)

Well, the content of this article to introduce you so much, I believe that we learn from the above content has learned to learn C language on the role of iOS development. I hope the above mentioned everyone likes.

Related Article

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.