# C language learning log

```int binary_search(int* arr,int num,int k)
{
int left = 0;
int right = k - 1;
int mid;
while (left <= right)
{
mid = (left + right) / 2;
if (arr[mid] < num)
{
left = mid + 1;
}
else if (arr[mid] > num)
{
right = mid - 1;
}
else if (arr[mid] == num)
{
return mid;
}
}
return -1;
}
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8 };
int x, num = 5, y;
y = sizeof(arr) / sizeof(arr);
x = binary_search(arr, num, y);
printf("%d", x);
}```

This is a binary search program, which encountered some problems but also solved them.

First of all, this code can't be written in subroutines to calculate the length of arrays, because int binary_ In search (int * arr, int num, int k), the first pointer address of the array is passed from the array to the formal parameter, not the entire array.

`y = sizeof(arr) / sizeof(arr);`

Judgment of prime numbers

```int is_prime(int x)//Judgment of prime numbers
{
int j;
for (j = 2; j < x; j++)
{
if (x % j == 0)//If any previous number is divided by i and the remainder is 0, then it is not a prime number
{
return 0;
}
}
return 1;
}
int main()
{
int i, count = 0;
for (i = 100; i <= 200; i++)
{
if (is_prime(i) == 1)
{
count++;
printf("%d ", i);
}
}
printf("\ncount=%d", count);
return 0;
}```

Initial knowledge of memset function

```int main()
{
char str[] = "zzy daizi";
memset(str,'*', 3);//Although the second parameter of memset is int type, the symbol is converted into ascii code for storage in the computer, and can only be converted into '' instead of ''. The last parameter is the number of bytes from the address
printf("%s", str);
return 0;
}```

Memset is a memory function. I read several blogs about memset(str,1,4), but I made a mistake. At first, I thought that my computer's integer type occupied different bytes. When I used the sizeof function, I found that the integer type also occupied four bytes. I don't know why I can't. I have to use single quotation marks' 'to type 1. But such a 1 is actually a character type, accounting for only one byte.

strcpy_s

```int main()
{
char a[] = "ewqyu";
char b = "##########";
strcpy_s(b,5,a);//Set the length as the initial string length + 1
printf("%s", b);
return 0;
}```

This function has one more parameter than the previous strcpy function, which is the intermediate integer. Its meaning is to identify the length. Generally, the length is set to the initial string length + 1.

System instructions

```int main()
{
char input = {0};
system("shutdown -s -t 60");//Space is very important. Turn off at 60
printf("Please enter: my next up Must be crooked, or your computer will shut down in one minute\n");
while (1)
{
scanf_s("%s",input,20);//scanf_s: the input array needs to indicate the length of the buffer
if (strcmp("I'm next up Must be crooked", input) == 0)
{
system("shutdown -a");
break;
}
}
return 0;
}```

system("shutdown -s -t 60"); Shutdown - S - t 60 in parentheses is a system instruction. It is generally used on the cmd command line, but if you want to use it in c, you need to use the system() function. shutdown -a is an instruction to cancel shutdown. The space between system instructions is very important, otherwise the instruction is invalid. It will be very interesting to send this thing to friends, but I don't set up automatic startup here. But after I sent it to my friends, I found that none of my friends could open it. After searching, I found that the. exe file in debug could not be opened on computers without vs installed. However, when we change debug to release, all the. exe friends in release can open it. At this time, I suddenly thought about whether it would be interesting to associate it with yesterday's number guessing game, so I improved yesterday's number guessing game. You can combine this with some other ideas to make friends, but don't overdo it.

```#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
{
printf("###############1.Start################\n");
printf("###############2.sign out################\n");
}
void game()
{
int tmp, guess;
tmp = rand()%(199-100+1)+100;//The random number range is 0-32767, and the random number generated by different random number seeds is also different, generating a random number of 
while (1)
{
printf("Please enter the number you guessed[100,199]: ");
scanf_s("%d", &guess);
if (guess == tmp)
{
printf("Congratulations! You guessed right\n");
system("shutdown -a");
break;
}
if (guess < tmp)
{
printf("Guess it's a little smaller\n");
}
if (guess > tmp)
{
printf("Guess it's a little bigger\n");
}
}
}
int main()
{
int i;
srand(time(NULL));//Set random number seed
printf("Please finish the game within 1 minute, or your computer will shut down automatically\n");
system("shutdown -s -t 60");
do
{
scanf_s("%d", &i);
switch (i)
{
case 1:
game();
break;
case 2:
system("shutdown -a");
break;
default:
printf("Invalid input");
}
} while (i!=2);
return 0;
}``` Tags: C

Posted by sella2009 on Sat, 03 Sep 2022 21:14:03 +0300