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.
C Language OC Basic iOS Development (Iphone/ipad) 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):
Hello World
Run process
Data type
Operator
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
Fill in the project name and choose to use the C language
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 processing before translating), it actually does is to copy the corresponding file to the specified location, the content can be any type of file, not just. h file; 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, At the same time to compile the grammar check; the generated. obj files 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 with the C language library function to generate an executable file;
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.
Data type
Type modifiers
From the above we can clearly see the C language data type structure, of course, for these types we also have some type modifiers (or called qualifiers)
Short type, modifier int, double long, modifier int, double signed signed, modifier int, char unsigned unsigned, modifier int, char
For type modifiers you need to explain the following
These modifiers are often used to modify the int, which can be omitted when the int type is decorated; short and long change the length of the int, the length of the different compiler items is not the same, but the short length is not longer than the int,int length is not longer than long; singed, Unsigned does not change the type length, only indicates whether the highest position is a sign bit, and signed represents a positive number greater than or equal to 0;
Of course, sometimes we have to be aware of the bytes occupied by each type, and the following table lists the storage space used by common data types
Note: The char type is the smallest data type unit that occupies 1 bytes under any type of compiler, and a variable assignment of a char type can be assigned directly to a character or to an integer (the corresponding ASCII value).
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 true, and returns 0 for false; In the conditional language, 0 is true (negative, positive), only 0 is 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
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)