IOS Development Series-basic knowledge of c language, ios-c

Source: Internet
Author: User

IOS Development Series-basic knowledge of c language, ios-c
Overview

The current trend of mobile development is unstoppable. I hope to talk about my personal opinions on IOS development in this series. The IOS series plans to talk about IOS development from several perspectives:

  • C Language
  • OC Basics
  • IOS development (iphone/ipad)
  • Swift

In this case, there is still a lot of content to be supplemented, but today we start with the most basic C language, and I will divide it into several chapters, today, let's take a brief look at some basic knowledge of C. More advanced content will be included in the following articles.

Today's basic knowledge is divided into the following parts (Note: loop and condition statements are not described here ):

Hello World

Since it is an IOS development series, first let's take a look at the running of C in Mac OS X.

Open Xcode

Select the command line program

Enter the project name and select the C Language

Select save directory

Automatically generate the following code:

OK. In Xcode, we compile our own program as follows:

//// Main. c // c language basics /// 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 program, we need to explain the following points:

Note: # There are two methods to include files: <> and "". The difference is that <> contains only the compiler library function file, so it is applicable to include library functions. "" contains the current directory of the program first. If not, it searches for the library function path, this applies to custom files;

Running Process

The C language runs in two major steps: Compilation and linking.

  • Compile: during the compilation phase, the corresponding xxx. c source file (ASCII format) compiled into the target file xxx. obj, which is in binary format (of course we usually have multiple. the c file will generate multiple corresponding. obj); pre-processing (for example, the # include command) is required before compilation, and syntax check is also required during compilation; generated. the obj file cannot be executed independently, because each. obj is associated, and they also reference the C language library functions;
  • Link: the link process is the process of combining various. obj files and C language library functions to generate an executable file;
Extension

In large-scale project development, it is unrealistic to write all the code in the program to a file. We usually divide a sub-operation into two files:. c file and. h file. In. the c file implements the corresponding functions in. h, so that as long as the corresponding header file is included in the top of the main function, sub-operations can be separated without considering the order. For example, the example of rewriting "Hello World" (note that the. c and. h file names corresponding to the message can be completely different, but we still get the same file name for the purpose of standardization ):

Message. h

//// Message. h // C language basics /// Created by Kenshin Cui on 14-7-12. // Copyright (c) 2014 cmjstudio. all rights reserved. // void showMessage ();

Message. c

//// Message. c // c language basics /// 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 basics /// 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 ;}

We can find that the program can still run normally, but we have a question: if we do not divide the program into two files, can we directly include message. c In the main function file to run normally? The answer is no, because the two files generated by compilation are main. obj and message. obj will find main. obj already contains message. the showMessage function defined in obj throws the "Duplicate identifier" error.

Data Type

Type Modifier

We can clearly see the data type structure of C language. Of course, we also have some type modifiers (or qualifiers) for these types)

  • Short type, modifying int and double
  • Long TYPE, modifying int and double
  • Signed, modifying int and char
  • Unsigned type, modifying int and char

The Type modifier must be explained as follows:

Of course, sometimes we must know the bytes occupied by each type. The following table lists the storage space occupied by common data types.

Note: the char type is the smallest data type unit. In any type of compiler, it occupies 1 byte, char variables can be directly assigned a value equal to a character or an integer (corresponding ASCII value ).

Operator

There are 34 operators in C language, which are not much different from C #, Java and other languages. Here we will list some precautions.

See the following examples for the above points:

//// Main. c // c language basics /// 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; // The operation result printf ("% d, % d \ n", a, B) is not saved; // result: 1, 0 if (c) {// you can use printf ("true. \ n ");} if (d) {// failed to pass printf (" false \ n ");} printf (" % d \ n ", e ); // result: 5 return 0 ;}
Common functions: printf ()

The printf () function is used to output data to the standard output device. It can be used with a format character to complete the powerful output function. We have used this function in the above example.

Normally, our output does not contain fixed content but contains certain variables. In this case, a format character is required. The common format characters are as follows:

We can precisely control the output width of the format operator and the decimal point of the floating point.

//// Main. c // c language basics /// 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 running result is as follows:

From the running results, it is not difficult to find that front-end completion can be set for positive numbers before the formatter %, and backend alignment can be set for negative numbers. If the total length of data exceeds the modified length, it is displayed according to the actual length; the integer after the decimal point is used to control the length of the decimal place after the decimal point.

Scanf () function

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

//// Main. c // c language basics /// 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", &, & B, & c); // enter: 1, 2, 3, and press ENTER printf ("a = % d, B = % d, c = % d \ n ", a, B, c); return 0 ;}

Requirements for scanf () Functions


Ios development only has the C language basics. How long can I learn it?

I think that I was familiar with ios development in one semester, but I should not only have the c foundation, but I think I should be able to master the basics in one or two months.

C language or C ++ is used for IOS development

Objective-c, an extended version of C, object-oriented

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.