For example:
How to achieve the maximum value of two values: ((a+b) + abs(ab))/2
How to achieve the exchange of two variable values without relying on intermediate quantities:
a = a + b;
b = a - b;
a = a - b;
or
a = a^b;
b = a^b;
a = a^b;
The latter method is more superior.
The difference between sizeof and strlen
Sizeof is an operator, and the result can be determined during the compilation process, but strlen is a function, and the return value can only be obtained during the run time.
The combination of pointers in various forms such as pointers, arrays, and functions is also the focus of the questions.
In programming, the main design involves the processing of strings and linked lists. I will find a few simple programs as training. Implement the conversion problem between integers and strings. This conversion problem is essentially to handle the switching relationship between ASCII codes and numbers, that is, the ASCII code form of numbers is '0'= 0 + 48; '9'= 9 + 48; 0 = '0'-48.
This relationship is the most important issue in the conversion process. Of course, when converting integers to strings, we need to pay attention to the impact of negative numbers. If negative numbers are not handled correctly, some inexplicable results may occur.
First, implement the conversion process from string to integer: This process is relatively easy, because this process can directly determine whether it is a positive number or a negative number. Just determine the content corresponding to the subscript 0. The rest can be achieved by gradually summing and accumulating. The basic conversion process is as follows:
int myatoi(char *src)
{
char flag = 0;
int sum = 0;
int i = 0;
int len = strlen(src);
/*Parameter correctness*/
if(NULL == src)
{
return 0;
}
/*Is there a sign problem*/
if(src[i] == '-')
{
flag = '-';
++ i ;
}
for( ; i < len ; ++ i)
{
/*Judge whether the character is legal*/
if(src[i] < 48 && src[i] > 57)
return 0;
/*Sum the data, pay attention to the conversion of the value src[i] - 48*/
sum = sum *10 + src[i] - 48;
}
/*Return the correct positive and negative number according to the flag bit*/
if(flag == '-')
return -sum;
else
return sum;
}
Integer to string conversion: Actually, it is to separate the bits of an integer and then sort the string. Because the uncertainty of the length of the number makes it difficult to quickly determine the length of the string, it can only be decomposed first and then sorted. At the same time, it is also necessary to pay attention to the existence of symbols. The basic conversion process is as follows:
char* myitoa(int num, char *str)
{
char flag = 0;
int i = 0, count = 0, j = 0;
/*Parameter detection*/
if(NULL == str)
{
return NULL;
}
/*Judge the positive or negative value and set the corresponding sign*/
if(num < 0)
{
str[i ++] = '-';
/************************
At the same time, take the absolute value of the value
to ensure that the subsequent remainder division operation is normal
****************************/
num = -num;
}
while(num / 10)
{
/***************************
count is used to save the actual number of digits,
which is convenient for later sequence exchange
****************************/
++ count;
/*The actual saved order is the reverse sequence*/
str[i++] = num % 10 + 48;
num /= 10;
}
/*The judgment is*/
if(num %10)
{
str[i++] = num %10 + 48;
++ count;
}
/*String terminator*/
str[i] = 0;
/*Achieve better data operation*/
i = 0;
if(str[0] == '-')
{
i = 1;
}
/*The actual subscript interval for exchange is i~j*/
j = count + i - 1;
for(; i < j ; ++ i, --j)
{
/* Better operation of data*/
str[i] = str[i] + str[j];
str[j] = str[i] - str[j];
str[i] = str[i] - str[j];
}
/*Return the actual string*/
return str;
}
The above code implements the conversion problem between strings and integers, and basically realizes conversion in various situations.
Previous article:Analysis of memory space size (sizeof) of classes in C++
Next article:Analysis of strings and character arrays in C language
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Frequency deviation definition and measurement method
- Load Modulation High Efficiency Linear Microwave Circuits—Base Station PA and Doherty
- Compile QT5 application with qt creator
- micropython update: 2021.3
- Show off the Xiaomi mouse I received from the Ti reading activity
- [AutoChips AC7801x motor demo board review] + motor demo board unboxing report and quick new project
- About the problem of L6226 controlling brushless DC motor
- Publish a batch of paper books
- Evaluation of domestic FPGA Gaoyun GW1N-4 series development board - stepper motor control (modified)
- Camel air conditioner fan 220v series 16v capacitor, please help analyze