数据先放在寄存器里,由控制器一点一点交给运算器处理,处理完在放回寄存器中,最后
回到 MEM
(打完游戏,游戏数据保存在硬盘里)
C varible
\n 表示空行
#include <stdio.h>
int main() {
float mynum = 10.23;
char word = 'e';
int a = 19;
float R = a + mynum;
printf("%f\n", R);
printf("%f\n",mynum);
printf("%c\n",word);
return 0;
}
. 加数字表示小数点后留几位数
Type conversion:
Implicit conversion
Explicit conversion
手动带上小数点后
加上(float)
Constants 值是定值
Const + type +
Operator
Assignment operators
十进制转二进制,再二进制转十进制
逢二进一
二进制时的位置,几位数的 2 平方的值 = 十进制的值
#include <stdio.h>
int main() {
int x = 5;
int y = 3;
// Returns 1 (true) because 5 is greater than 3 AND 5 is less than 10
printf("%d", x > 3 && x < 10);
return 0;
}
Run:1
Sizeof Operator
The memory size (in bytes) of a data type or a variable can be found
with the operator:sizeof
#include <stdio.h>
int main() {
int myInt;
float myFloat;
double myDouble;
char myChar;
printf("%lu\n", sizeof(myInt));
printf("%lu\n", sizeof(myFloat));
printf("%lu\n", sizeof(myDouble));
printf("%lu\n", sizeof(myChar));
return 0;
}
4
4
8
1
C Booleans
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and
condition2 is true
} else {
// block of code to be executed if the condition1 is false and
condition2 is false
}
C Short Hand If Else
满足条件,则 printf 前者
反之如异
Switch Statement
Note: Do not forget to increase the variable used in the condition ( i+
+), otherwise the loop will never end!
int i = 0;
do {
printf("%d\n", i);
i++;
}
while (i < 5);
For Loop
When you know exactly how many times you want to loop through a
block of code, use the for loop instead of a while loop:
To demonstrate a practical example of the for loop, let's create a
program that prints the multiplication table for a specified number:
Example
int number = 2;
int i;
// Print the multiplication table for the number 2
for (i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", number, i, number * i);
}
return 0;
Break
You have already seen the statement used in an earlier chapter of this
tutorial. It was used to "jump out" of a switch statement.break
The statement can also be used to jump out of a loop.break
This example jumps out of the for loop when is equal to 4:i
Contine
The statement breaks one iteration (in the loop), if a specified
condition occurs, and continues with the next iteration in the
loop.continue
This example skips the value of 4:
Change an Array Element
int myNumbers[] = {25, 50, 75, 100};
myNumbers[0] = 33;
printf("%d", myNumbers[0]);
// Now outputs 33 instead of 25
Set Array Size
Another common way to create arrays, is to specify the size of the
array, and add elements later:
// Declare an array of four integers:
int myNumbers[4];
// Add elements
myNumbers[0] = 25;
myNumbers[1] = 50;
myNumbers[2] = 75;
myNumbers[3] = 100;
Get Array Size or Length
int myNumbers[] = {10, 25, 50, 75, 100};
int length = sizeof(myNumbers) / sizeof(myNumbers[0]);
printf("%d", length); // Prints 5
C Multidimensional Arrays
C Strings
Access Strings
Since strings are actually arrays in C, you can access a string by
referring to its index number inside square brackets .[]
This example prints the first character (0) in greetings:
char greetings[] = "Hello World!";
printf("%c", greetings[0]); //output H
Modify Strings
To change the value of a specific character in a string, refer to the
index number, and use single quotes:
Example
Strings - Special Characters
Because strings must be written within quotes, C will misunderstand
this string, and generate an error:
char txt[] = "We are the so-called "Vikings" from the north.";
char txt[] = "We are the so-called \"Vikings\" from the north.";
We are the so-called "Vikings" from the north.
The sequence inserts a single quote in a string:\'
char txt[] = "It\'s alright.";
It's alright.
The sequence inserts a single backslash in a string:\\
Example
char txt[] = "The character \\ is called backslash.";
Try it Yourself »
The character \ is called backslash.
String Length
For example, to get the length of a string, you can use
the strlen() function:
Example
char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
printf("%d", strlen(alphabet));
In the Strings chapter, we used sizeof to get the size of a
string/array. Note that sizeof and strlen behaves differently,
as sizeof also includes the \0 character when counting:
Example
char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
printf("%d", strlen(alphabet)); // 26
printf("%d", sizeof(alphabet)); // 27
It is also important that you know that sizeof will always return the
memory size (in bytes), and not the actual string length:
Example
char alphabet[50] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
printf("%d", strlen(alphabet)); // 26
printf("%d", sizeof(alphabet)); // 50
Try it Yourself »
Concatenate Strings
To concatenate (combine) two strings, you can use
the function:strcat()
Example
char str1[20] = "Hello ";
char str2[] = "World!";
// Concatenate str2 to str1 (result is stored in str1)
strcat(str1, str2);
// Print str1
printf("%s", str1);
Copy Strings
To copy the value of one string to another, you can use
the function:strcpy()
Example
char str1[20] = "Hello World!";
char str2[20];
// Copy str1 to str2
strcpy(str2, str1);
// Print str2
printf("%s", str2);
Compare Strings
To compare two strings, you can use the function. strcmp()
It returns if the two strings are equal, otherwise a value that is not 0: 0
Example
char str1[] = "Hello";
char str2[] = "Hello";
char str3[] = "Hi";
// Compare str1 and str2, and print the result
printf("%d\n", strcmp(str1, str2)); // Returns 0 (the strings
are equal)
// Compare str1 and str3, and print the result
printf("%d\n", strcmp(str1, str3)); // Returns -4 (the strings
are not equal)
User Input
You have already learned that printf() is used to output values in C.
To get user input, you can use the scanf() function:
The function takes two arguments: the format specifier of the variable
( in the example above) and the reference operator (), which stores
the memory address of the variable.scanf()%d&myNum
Tip: You will learn more about memory addresses and functions in the
next chapter.
Type a number AND a character and press enter: 5b
Your number is: 5
Your character is: b
Note: When working with strings in scanf(), you must specify the size
of the string/array (we used a very high number, 30 in our example,
but atleast then we are certain it will store enough characters for the
first name), and you don't have to use the reference operator ( &).
char fullName[30];
printf("Type your full name: \n");
fgets(fullName, sizeof(fullName), stdin);
printf("Hello %s", fullName);
// Type your full name: John Doe
// Hello John Doe
Note: The memory address is in hexadecimal form (0x..). You will
probably not get the same result in your program, as this depends on
where the variable is stored on your computer.
You should also note that &myAge is often called a "pointer". A pointer
basically stores the memory address of a variable as its value. To print
pointer values, we use the %p format specifier.
You will learn much more about pointers in the next chapter.
43
0x7ffe5367e044
0x7ffe5367e044
Pointers & Arrays
C Function
When a parameter is passed to the function, it is called
an argument. So, from the example above: is a parameter,
while , and are arguments.nameLiamJennyAnja
Fahrenheit: 98.80
Convert Fahrenheit to Celsius: 37.11
Function Declaration and Definition
A function consist of two parts:
Declaration: the function's name, return type, and parameters
(if any)
Definition: the body of the function (code to be executed)
void myFunction() { // declaration
// the body of the function (definition)
}
For code optimization, it is recommended to separate the declaration
and the definition of the function.
You will often see C programs that have function declaration above ,
and function definition below . This will make the code better organized
and easier to read:main()main()
// Function declaration
void myFunction();
// The main method
int main() {
myFunction(); // call the function
return 0;
}
// Function definition
void myFunction() {
printf("I just got executed!");
}
C Recursion