Explanation of problem examples:
T1.
#include <stdio.h> int main() { int a, b, c; a = 5;//a=5 c = ++a;//c=6 a=6 b = ++c, c++, ++a, a++; //b=7 c=8 a=8 b += a++ + c; //b=23 a=9 c=8 printf(" a = %d\n b = %d\n c = %d\n:", a, b, c);//9 23 8 return 0; }
T2.
Count the number of 1 in binary int count_number_of_1( int m) { int c = 0; while (m) { if (m % 2 == 1) { c++; } m /= 2; } return c; } int main() { int n = 15;//n the number of 1 in binary of the complement placed in memory int ret = count_number_of_1(n); printf("%d\n", ret); return 0; }
For negative numbers, it will be invalid!
Solution 1 for negative numbers:
When unsigned is used, N in the main function is - 1, which is a signed bit, but m in the definition function does not want to be a signed bit. Therefore, when passing n to m, unsigned is used. By default, the complement of an unsigned integer is passed, and all its bits are significant bits.
Note: if the absolute value of - 1 is not used, the logic will not work if the number of complement 1 is solved!
int count_number_of_1(unsigned int m) { int c = 0; while (m) { if (m % 2 == 1) { c++; } m /= 2; } return c; } int main() { int n = -1;//n the number of 1 in binary of the complement placed in memory //10000000000000000000000000000001 //11111111111111111111111111111110 //11111111111111111111111111111111111111111111111111111111111 - > - 1 Complement int ret = count_number_of_1(n); printf("%d\n", ret); return 0; }
Solution 2 for negative numbers:
int count_number_of_1(int m) { int c = 0; int i = 0; for (i = 0; i < 32; i++) { if ((m & 1) == 1) { c++; } m >>= 1; } return c; } int main() { int n = -1;//n the number of 1 in binary of the complement placed in memory //10000000000000000000000000000001 //11111111111111111111111111111110 //11111111111111111111111111111111111111111111111111111111111 - > - 1 Complement int ret = count_number_of_1(n); printf("%d\n", ret); return 0; }
Solution 3 for negative numbers:
int count_number_of_1(int m) { int c = 0;//Counter while (m) { m = m & (m - 1); c++; } return c; } int main() { int n = -1;//n the number of 1 in binary of the complement placed in memory //10000000000000000000000000000001 //11111111111111111111111111111110 //11111111111111111111111111111111111111111111111111111111111 - > - 1 Complement int ret = count_number_of_1(n); printf("%d\n", ret); return 0; }
To judge whether a number m is the k-th power of 2, you can use M & (m-1) = = 0
Online question brushing form:
T3.
int count_diff_bit(int m, int n) { int i = 0; int c = 0;//Counter for (i = 0; i < 32; i++) { if ((m & 1) != (n & 1)) { c++; } m >>= 1; n >>= 1; } return c; } int main() { int m = 1999; int n = 2299; int ret = count_diff_bit(m, n); printf("%d\n", ret); return 0; }
Method 1:
Method 2:
//XOR operator //The same is 0 and the difference is 1 int count_diff_bit(int m, int n) { int i = 0; int c = 0;//Counter int tmp = m ^ n; //How many 1's are there in the binary bits that compute tmp while (tmp) { tmp = tmp & (tmp - 1); c++; } return c; } int main() { int m = 1999; int n = 2299; int ret = count_diff_bit(m, n); printf("%d\n", ret); return 0; }
Keep learning every day and the brain structure will improve!
T4.
//Title: //Print odd and even bits of integer binary //Title Content: //Get all even and odd bits in an integer binary sequence, and print out the binary sequence respectively // void print(int m) { //Print odd digits int i = 0; for (i = 30; i >= 0; i -= 2) { printf("%d ", (m >> i) & 1); } printf("\n"); //Print even digits for (i = 31; i >= 1; i -= 2) { printf("%d ", (m >> i) & 1); } } int main() { int m = 0; // //00000000000000000000000011001001 // scanf("%d", &m); print(m); return 0; }
go language!
T5.
int i;//Global variables, if not initialized, default to 0 int main() { i--;//-1 if (i > sizeof(i))//-1 > 4 { printf(">\n"); } else { printf("<\n"); } return 0; }
- When 1 is treated as an unsigned bit, its value is 4294967295
T6.
int main() { int n = 0; //EOF - end of file // while (scanf("%d", &n) != EOF) { if (n % 2 == 1) { printf("Odd\n"); } else { printf("Even\n"); } } return 0; }
T7.
In Visual Stdio 2019 operation example:
Enter A and b The order is: enter A first and then Space (\ n) and then enter b,
When scanf reads, the first character is read normally, but it will be read in the buffer \ N and passed to & ch, but \ n is neither vowel nor consonant, the total output result is inconsistent with the expectation!
Method 1: use getchar()
Because it is a single input, the getchar loop is not used:
int main() { char v[] = { 'a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U' }; char ch = 0; while (~scanf("%c", &ch)) { int i = 0; for (i = 0; i < 10; i++) { if (ch == v[i]) { printf("Vowel\n"); break; } } if (i == 10) printf("Consonant\n"); //Clear buffer getchar();//\n } return 0; }
Method 2: scanf uses% c Only take away when taking characters \ n, not for printing integers! Because taking characters will treat everything in the buffer as characters!
Method 3: see the figure below
Practice in large enterprises!
Evaluate something correctly!
1.1 basic knowledge of structure
struct tag { member-list; } variable-list;
For example, describe a student:
typedef struct Stu { char name[20];//name int age;//Age char sex[5];//Gender char id[20];//Student number }Stu;//Semicolons cannot be lost
struct Point { int x; int y; }p1; Defining variables while declaring types p1 struct Point p2; Define structure variables p2 Initialization: define variables and assign initial values at the same time. struct Point p3 = {x, y}; struct Stu Type declaration { char name[15]; name int age; Age }; struct Stu s = {"zhangsan", 20}; initialization struct Node { int data; struct Point p; struct Node* next; }n1 = {10, {4,5}, NULL}; Structure nesting initialization struct Node n2 = {20, {5, 6}, NULL}; Structure nesting initialization
struct Stu { char name[20]; int age; float score; }s1, s2; s1,s2 It is 2 structural variables and is global int main() { int a = 0; int b = 0 struct Stu s = { "zhansan", 20, 95.5f }; Define a structure variable, local printf("%s %d %f\n", s.name, s.age, s.score); return 0; }
struct S { int a; char c; double d; }; struct Stu { struct S ss; char name[20]; int age; float score; }s1, s2;//S1 and S2 are two structural variables, which are global int main() { int a = 0; int b = 0; struct Stu s = { {100, 'w', 3.14}, "zhansan", 20, 95.5f};//Define a structure variable, local printf("%d %c %lf %s %d %f\n",s.ss.a, s.ss.c, s.ss.d, s.name, s.age, s.score); return 0; }
For example:
struct Stu { char name[20]; int age; }; struct Stu s;
struct S s; strcpy(s.name, "zhangsan"); use.visit name member s.age = 20; use.visit age member
struct Stu { char name[20]; int age; }; void print(struct Stu* ps) { printf("name = %s age = %d\n", (*ps).name, (*ps).age); Use structure pointers to access members that point to objects printf("name = %s age = %d\n", ps->name, ps->age); } int main() { struct Stu s = {"zhangsan", 20}; print(&s); Structure address transfer parameter return 0; }
3. Structure transmission parameters
struct S { int data[1000]; int num; }; struct S s = {{1,2,3,4}, 1000}; Structural transmission parameters void print1(struct S s) { printf("%d\n", s.num); } Structure address transfer parameter void print2(struct S* ps) { printf("%d\n", ps->num); } int main() { print1(s); Transmission structure print2(&s); Transmission address return 0; }
struct S { int arr[1000]; float f; char ch[100]; }; void print(struct S tmp) { int i = 0; for (i = 0; i < 10; i++) { printf("%d ", tmp.arr[i]); } printf("\n"); printf("%f\n", tmp.f); printf("%s\n", tmp.ch); } int main() { struct S s = { {1,2,3,4,5,6,7,8,9,10}, 5.5f, "Hello,World!" }; print(s); return 0; }
struct S { int arr[1000]; float f; char ch[100]; }; void print1(struct S tmp) { int i = 0; for (i = 0; i < 10; i++) { printf("%d ", tmp.arr[i]); } printf("\n"); printf("%f\n", tmp.f); printf("%s\n", tmp.ch); } void print2(struct S* ps) { int i = 0; for (i = 0; i < 10; i++) { printf("%d ", ps->arr[i]); } printf("\n"); printf("%f\n", ps->f); printf("%s\n", ps->ch); } int main() { struct S s = { {1,2,3,4,5,6,7,8,9,10}, 5.5f, "hello bit" }; //print1(s); print2(&s); return 0; }
int Add(int x, int y) { int z = 0; z = x + y; return z; } int main() { int a = 10; int b = 20; int c = Add(a, b); printf("%d\n", c); return 0; }
Function call process analysis - the creation and destruction of function stack frames. See the notes in the next section!