programming笔记
programming笔记
回到 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;
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;
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
This example jumps out of the for loop when is equal to 4:i
Contine
printf("%d", myNumbers[0]);
// Add elements
myNumbers[0] = 25;
myNumbers[1] = 50;
myNumbers[2] = 75;
myNumbers[3] = 100;
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 .[]
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.
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
Example
Example
Try it Yourself »
Concatenate Strings
To concatenate (combine) two strings, you can use
the function:strcat()
Example
// Print str1
printf("%s", str1);
Copy Strings
To copy the value of one string to another, you can use
the function:strcpy()
Example
// 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
User Input
You have already learned that printf() is used to output values in C.
Tip: You will learn more about memory addresses and functions in the
next chapter.
char fullName[30];
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
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();
// Function definition
void myFunction() {
printf("I just got executed!");
}
C Recursion