Initialize a C project using Xcode

  1. Open up Xcode and say “Create a new Xcode project”
  2. Select MacOS /Command Line Tool and click Next
  3. Enter Product Name (Project Name HelloWorld), Organization Identifier(Organization Certification CN.hifelix.ios), and select Language as “C”.
  4. Write the contents in main.c
#include <stdio.h>
int main () {
  printf("This is my first Xcode C project! \n");
  return 0;

Xcode has two common shortcuts

  1. Command + R compiles and executes automatically
  2. Conmand + B auto-compile (check for errors)

Add multiple targets to the Project

Common data types

Int float double char int -2147483648 ~ +2147483647 int float double char int -2147483648 ~ +2147483647 (floating-point type A decimal number with no more than 16 significant bits (the first seven digits can be saved accurately) Character type

Variable declaration

int a;

The assignment of variables

The int type int num; num = 100;

· In C, we write a decimal with a data type of double. · Float can only hold a decimal with no more than 7 significant digits, and does not hold float f1; F1 = 12.12f(double if you write 12.12)

Double double a; A = 12.12

· The char variable can store only a single character, and each character must be enclosed in a single quotation mark. · If too many characters are given, the last space is also a character, so it can store a space. A char variable is one byte) char ch; ch = ‘a’;

Output the value of the variable

· printf(” Output information “); Printf (” format control string “, list of variables) printf different variable type, different placeholder int a; printf(” format control string “, list of variables) a = 18; Print (” value of variable a is %d”, a); %f — float placeholder (default output 6 decimal places) %d — int placeholder (default output 6 decimal places) %lf — double placeholder %c — char placeholder (default output 6 decimal places int a = 10; Float b = 10.12 f; Printf (“int a is value %d, float b is value %f”, a, b);

Declare multiple variables

int a, b, c, d;

int a = 1, b = 2, c = 3, d = 4;

Naming rules for variables

· Variable names can only start with any letter, underscore, $, cannot begin with a number · can be followed by letter, number, underscore, $· cannot use C language keywords · C language is strictly case-sensitive

Use of the scanf function

Syntax format: scanf(” format control character “, variable address list);

What type of data do you want the user to enter, depending on the placeholder in the format controller

%d user enters data of type int %f user enters data of type float %lf user enters double %c user enters data of type char

  1. Declare a variable whose type is the same as the type of data entered by the user
  2. Use scanf to ask the user to enter data a. Negotiate the corresponding placeholder in the format control character. Be sure to enter the corresponding b. To store the user input data into the variable, write the address of the variable at the end of the use of the variable address can be retrieved
Int main () {float f1 = 0.01f; scanf("%f", f1); }
  1. The scanf function is used to receive multiple inputs from the user a. Write multiple placeholders in the format controller. The number of placeholders represents the number of inputs from the user, and the type of placeholder represents the type of input from the user b. Write the address of the variable in which the user enters the data at a later time. C. When the user enters the number, it can use a space or a carriage return to separate multiple data

Clear the buffer of data


The arithmetic operator

The type of the result of an arithmetic expression. The type of the result of two operands of the same type is the type of the operands. B. Two operands of different types have different types. The result is int < float < double

Increment/Decrease expression

The result of the pre-increment/decrement expression is the result of the increment/decrement expression, the result of the post-increment/decrement expression is the variable itself, and then the increment/decrement operation is performed

Boolean value

In C, we use int for Boolean values, 0 for true, and non-0 (1) for false

Logical expression

&& and | | or! non

Shortcut key for formatting code (Ctrl + I)

The scope of a variable

Variables in curly braces are discarded when the code in curly braces completes itself

C language to generate random number method

  1. Start by introducing a system header file

    inclde <stdlib.h>

  2. 0~(n-1) int num = arc4random(10); 0~(n-1) int num = arc4random(10) int num = arc4random(10); A random number between 0 and 9 is generated and assigned to num

Switch case syntax considerations

Switch (expression) {case: 1: printf(" This is case1"); break; case: 2: { int a = 1; Printf ("int a is %d", a)}}

GOTO statements (loops can be implemented)

Syntax: loop is an identifier, which can be named by itself

Note: One line of code below an identifier cannot declare a variable (in C, you cannot declare a variable after an identifier);

int main () {
    print("Mr Chan");
  print("Mr Feng");
  goto loog;

Use of functions

Note: Functions need to be defined outside the main method, otherwise an error will be reported!

Function declaration

void test();                                             # 函数的声明(相当于变量提升)
int main () {
void test() {                                            # 函数的定义

Preprocessing instruction

C code falls into two main categories:

1) c code

2) Preprocess code

# start, such as #include <stdio.h>

Classification of preprocessing instructions

The file contains the directive #include

# define a macro definition

Conditional compilation instruction #if

File include instruction

Function: The specified file contents can be copied to the specified location


#include “file path”

#include < file path >

The difference between the two types of grammar:

Use “”, first go to the current source file in the directory to find this file, if it is included directly, if not, go to the system’s own translator directory to find, if it is included, no error

Use <> include, go directly to the system’s own compiler directory to find, if there is a include, no error

Use: # include “/ User/HiFelix/Desktop / 1. TXT” (will be 1. TXT in the code to copy to the homes of this article

Four base systems in C language


Int num = 0b10001010101010; int num = 0b100010101010;


Int num = 0133; int num = 0133;

The decimal system


Int num = 0x167acde; int num = 0x167acde;

The output

%d prints the data in an integer variable in decimal;

%o prints data in an integer variable in octal;

%x prints the data in an integer variable in octal;

The memory size of the variable

Int 4 bytes

Double 8 bytes

Float 4 bytes

Char 1 byte

An operation

1. Bit and operation &

2. | bitwise or operations

3. Reverse ~3 by bit

4. By bit different or 3^2(same = 0, different = 1)

5. Move left or right by bit << >>

Int type modifier

Short modifier

Short int num = 10 (int = 2)

The short int variable outputs the placeholder % HD

Short num = 10; short num = 10; short num = 10;

Long modifier

On 32-bit operating systems, the long modifier is 4 bytes long

On 64-bit operating systems, the long modifier is 8 bytes long

The long int variable outputs the placeholder %ld

The long long modifier

The long long int variable outputs placeholder % LLD

The long long modifier is 8 bytes on both 32-bit and 64-bit operating systems

Unsigned modifier

Unsigned int num = num unsigned int num = num

The unsigned int variable outputs the placeholder %u

The unsigned short variable outputs the placeholder %hu

The unsigned long variable outputs the placeholder %lu

Unsigned long long output placeholder %llu

An array of

A. 1 array contains multiple data of the same type, which is specified when we create B. 1 array contains multiple data of the same type. An array can hold a fixed amount of data, which is specified when we create it

Array declaration

Int arr[4] –> int arr[4] –>

Stores the data into the array

arr[1] = 100

Array initialization (if only one value is initialized, all other values will be initialized to 0)

int arr[3] = { 10, 20, 30 }

int arr[] = { 10, 20, 30, 40, 50}

int arr[3] = { [1] = 10, [2] = 20 }

Calculates the length of the array

int lenth = sizeof(arr) / sizeof(int);

2 d array

A declaration of a two-dimensional array

Int arr3 (declare an array with three rows and four columns);

Assignment to a two-dimensional array

arr1 = 100;

Initialization of a two-dimensional array

  1. int arr3 = {

    {1, 23, 44},

    {2, 22, 11},

    {12. 45, 75},

    {223. 99, 64},

  2. Rows can be omitted, for example, int arr [] [4] = {{1, 23, 44}, {2, 22, 11}, {12. 45, 75}, {223, 99, 64},}
  3. Int arr3 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
  4. Partial initialization (the first element of the first one-dimensional array is initialized to 10, the first element of the second one-dimensional array is initialized to 10, the first element of the third one-dimensional array is initialized to 10, everything else is initialized to 0) int arr3 = {{10}, {20}, {30}}
  5. Int arr3 = {1, 2, 3, 4} int arr3 = {1, 2, 3, 4}
  6. Initialization of the character array (if only some elements are initialized, then other characters are initialized to \0, \0 is an invisible element and prints out nothing, \0 is a character with ASCII code 0) chat CHR [4] = {‘a’};

String data (in C, string arrays must be enclosed in double quotes)

A declaration of an array of strings

  1. char name[] = { ‘j’, ‘a’, ‘c’, ‘k’, ‘\0’ }
  2. Char name[] = {“jack”} # is automatically followed by \0
  3. The most common method is char name[] = “jack”;
  4. char name[10] = “jack”; It’s going to be followed by 5 \0
  5. A Chinese takes 3 bytes

The output of an array of strings

Format control character %s principle: starting from the specified string array address from the low byte to the high byte byte output, until the end of \0

The length of the string array

We need to use the while loop int len = 0; while (name[len] ! = ‘\0’) { len++ }; At the end of the loop you get the length of the string

String common function

— — — — — — — — — — — — — — — — — — — — — — — the two functions are in the stdio. H file, can be used directly — — — — — — — — — — — — — — — — — — — — — — — — — — — —

  1. The puts() function does: The syntax format for putting a string: puts(string array name) Advantage: When the output is complete, line wrapping disadvantage: Only strings can be output, and no placeholders can be used
  2. The get() function receives a string of data from the console. Syntax format: gets(array name of string). Like scanf, not safe (crashes when the array of strings used to store the data in a string becomes too small)

— — — — — — — — — — — — — — — — — — — — — — — the following four function that is declared in the string. The h in this header file — — — — — — — — — — — — — — — — — — — — — — — — — — — —

  1. The strlen() function (which returns a value of type unsigned long) returns the length of the string stored in a character array
  2. STRCMP () char name1[] = “jack”; char name2[] = “rose”; strcmp(name1, name2); Returns a value of type int, greater than 0, name1 is large, equal to 0, same large, less than 0, name2 is large
  3. strcpy char name1[] = “jack”; char name2[] = “rose”; strcpy(name1, name2); Copy the value of name2 into name1

Strcat () string data concatenation

Clear the printed message –> system(” clear “);

Pointers (Pointers are the addresses of variables)

Pointer to the variable

Declaration of pointer variables

int p1; Int (p1) int (p1) int (p1) int (p1)

It can also be written as int p1; int p1;

Int p1, p2, *p3;

Initialization of pointer variables

int num = 100;

int* p1 = &num;

Int p1 = NULL; int p1 = NULL; int p1 = NULL; int p1 = NULL; int p1 = NULL; int p1 = NULL; int p1 = NULL; int p1 = 0;

Use of pointer variables

p1 = 100; (p1) points to num;

Five areas of memory (no matter which area is used to store data)

Stack area, heap area, BSS section, data section/constant area, code section

The stack area

Used to store local variables

The heap area

Allows the programmer to manually request space from the heap for use


Used to store uninitialized global and static variables

Data segment

Used to store initialized global variables, static variables, and constant data

Code segment

The code/instruction used to store a program

Use fputs to output string data to the specified stream

Stream: Standard output stream -> console

Char *name = "char *name "; fputs(name, stdout); The fopen function creates a pointer to a file. The fopen function takes two arguments to the file. The first argument is the path to the file, which means the created pointer points to the file. Write, r: read, a: append) 2) writes a string to the specified file with fputs(string, file pointer); 3) When you're done, be sure to close the file with fclose()

Gets content from the specified stream using fgets

fgets(name, 10, stdin);

Malloc function/calloc function/realloc function

All three functions are declared in the system header file of stdlib.h

The malloc function

int *p1 = malloc(4); Size_t (unsigned long) returns the value void *, which represents a pointer with no type

Free space (pointer);

Calloc function

1) it is possible to pass sizeof(int) to specify the sizeof the space 2) the garbage value of the space is initialized int *p1 = calloc(4, sizeof(int));

Relloc function (expansion)

A pointer to a function

1) A pointer to a function is not a pointer to any function

2) Syntax return value type (* pointer name)([argument list]) void (*pFuction)(); Int (*pFun)(int num1, int num2); int (*pFun)(int num1, int num2); int (*pFun);

Void test () {} void (*pFunc)() = test;

4) call pFunc (); Or * pFunc () ();

The structure of the body

Syntax format:

Struct new type name {// write that the new type you created is the data type of those variables union 1 small variable name 1 data type 2 small variable name 2 data type 3 small variable name 3}

The sample

  struct Student
     char *name
     int age
     int score
     float height

Declares a variable of a structure type

Struct new type name variable name; struct Student stu;

Initialization of a structure variable

The first way to initialize it: = "Felix "; stu.age = 18; stu.score = 95; stu.height = 180; Struct Student stu = {"felix", 18, 95, 180}; Struct Student stu = {.name = "felix",.age = 18,.score = 95,.height = 180};

Macro definition


#define N 10; #define N 10;


  int a = N + 1;

Conditional compilation instruction


if N > 0

Printf (” ha ha “)

elif N > 0

Printf (” ha ha “)

else N > 0

Printf (” ha ha “)


Ifdef N (if N macro is defined)

Ifndef N (if N macro is not defined)

The static and extern

Static int num = 123; static int num = 123; static int num = 123; / / extern = extern; / / extern = extern; / / extern = extern; / / extern = extern; / / extern = extern