Basic knowledge that is easy to ignore in C ++

Source: Internet
Author: User

Basic knowledge that is easy to ignore in C ++

As a java developer, due to the actual needs, it has recently started to contact C ++, which is a difficult and error-prone problem recently encountered in C ++, for myself and other C ++ beginners ····

Through this article, you can learn:

1) floating point overflow and Underflow

2) floating point number rounding error

3) using % d to display the float value will not convert the float to an approximate int value, but will display the garbage value.

4) reading habits of scanf

5) Difference between strlen () function and sizeof ()

6) converting a negative number to unsigned and a value greater than 255 to a character is equivalent to a modulo.

7) prinf reads the number in the stack by %.

 

1. Overflow and underflow of floating point values:

For example, if the maximum float value of the system is 3.4E38, perform the following operations:

 

float toobig =3.4E38*100.0f;print("%e\n",toobig);
This will cause overflow. The value displayed by the pronf () function is inf or infinity (or another name of this meaning)
When dividing by a small number, for example, 0.1233E-10 divided by 10, the result will lose one precision and change to 0.0123E-10. This phenomenon is called underflow ).

 

In addition, if asin () is used to return the inverse sine value, but if the input parameter is greater than 1, NaN (Not-a-Number) is returned)

 

2. Floating Point Number rounding error

Add 1 to a number and then subtract the original number. The result is 1. If floating point is used, there may be other results, such:

 

#include 
 
  int main(void){float a, b;b = 2.0e20 + 1.0;a = b - 2.0e20;printf("%f\n", a);return 0;}
 
Result:

 

The reason for this is that the computer lacks sufficient digits in decimal format required for correct calculation. 2.0e20 is followed by 2 with 20 zeros. If 1 is added, the change is actually 21st bits. If correct calculation is required, at least 21 bits must be stored, but float only needs 6.7 valid digits. The above error results may vary with the platform.

 

3. Using % d to display the float value will not convert the float to an approximate int value, but will display the garbage value.

 

4. scanf reading habits

 

/* praise1.c -- uses an assortment of strings */#include 
 
  #define PRAISE "What a super marvelous name!"int main(void){    char name[40];    printf("What's your name?\n");    scanf("%s", name);    printf("Hello, %s. %s\n", name, PRAISE);      return 0;}
 
Result:

 

What's your name?

Tony KK

Hello, Tony. What a super marvelous name!

This is because scanf () with % s will only read the first word rather than the entire statement as a string. C can use other reading functions (such as gets () to process General strings.

 

5. strlen () Functions and sizeof ():

The sizeof operator is used to specify the data size in bytes. The strlen () function is used to specify the length of a string in bytes.

 

#include 
 
  #include 
  
         /* provides strlen() prototype */#define PRAISE "What a super marvelous name!"int main(void){char name[40];printf("What's your name?\n");scanf_s("%s", name);printf("Hello, %s. %s\n", name, PRAISE);printf("Your name of %d letters occupies %d memory cells.\n",strlen(name), sizeof name);printf("The phrase of praise has %d letters ",strlen(PRAISE));printf("and occupies %d memory cells.\n", sizeof PRAISE);return 0;}
  
 
Interaction:

 

What's your name?

Morgan Buttercup

Hello, Margan. What a super marvelous name!

Your name of 6 letters occupies 40 memory cells.

The phrase of praise has 28 letters and occupies 29 memory cells.

Explanation:

For the name array, sizeof displays the length of the array, and strlen () shows the length of the used array. For the string PRAISE, sizeof is 1 more than strlen (), because it calculates the ending \ 0.

 

6. Converting a negative number to unsigned and a value greater than 255 to a character is equivalent to a modulo.

 

#include 
 
  #define PAGES 336#define WORDS 65618int main(void){    short num = PAGES;    short mnum = -PAGES;    printf("num as short and unsigned short:  %hd %hu\n", num,            num);    printf("-num as short and unsigned short: %hd %hu\n", mnum,            mnum);    printf("num as int and char: %d %c\n", num, num);    printf("WORDS as int, short, and char: %d %hd %c\n",            WORDS, WORDS, WORDS);   return 0;}
 
Output:

 

First,-336 is not converted to 336 as we expect, but 65200. the reason is that 0 to 32767 in the short int indicates themselves, 42768 to 65535 indicates negative, 65535 indicates-1, 65534 indicates-2, and so on, 65200 indicates-336. So do not expect % u to separate numbers from symbols.

The second line shows that if you want to convert a value greater than 255 to a character, it is equivalent to modulo:

The binary 336 value is 0000000101010000 in the memory, but the character can only be converted into numbers smaller than or equal to 255. Otherwise, it is just a piece of conversion, and the value changes. This is also true when we try to print a number larger than the short int.

 

7. prinf reads the number in the stack by %.

 

#include 
 
  int main(void){    float n1 = 3.0;    double n2 = 3.0;    long n3 = 2000000000;    long n4 = 1234567890;    printf("%.1e %.1e %.1e %.1e\n", n1, n2, n3, n4);    printf("%ld %ld\n", n3, n4);    printf("%ld %ld %ld %ld\n", n1, n2, n3, n4);       return 0;}
 
Result:

 

The Computer puts these values into the value stack based on the type of the variable rather than the conversion specifier. Therefore, n1 occupies 8 bytes in the stack (float is changed to double). Likewise, n2 occupies 8 bytes, and n3 and n4 occupy 4 bytes respectively. Then the control is transferred to the prinf () function, and prinf () is read according to the conversion instruction. % Ld reads four bytes, that is, the first half of n1. The next % ld reads four more bytes and the second half of n1. Therefore, the final result is incorrect.
 

 



 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.