Basic knowledge of C + + easy to overlook

Source: Internet
Author: User

As a Java developer, because the actual need to start contact with C + + Recently, this is a recent contact with C + + some of the difficulties and error points, organized as follows, for themselves and other C + + Beginners

Through this article, you can learn:

1) overflow and underflow of floating-point values

2) floating-point rounding error

3) using%d to display the float value does not convert float to an approximate int value, but instead displays the garbage value

4) scanf reading habits

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

6) Conversion of negative numbers to unsigned and values greater than 255 to characters equivalent to modulo

7) Prinf read the number in the stack is read by%


1. Overflow and underflow of floating-point values:

For example, assume that the maximum value of float for the system is 3.4E38, and do the following:

float Toobig =3.4e38*100.0f;print ("%e\n", Toobig);
This will occur overflow (overflow), the value shown by the pronf () function is INF or infinity (or another name for this meaning)
When divided by a very small number, such as 0.1233E-10 divided by 10, the result will lose one precision, and become 0.0123E-10, this phenomenon is called underflow (underflow).

In addition, if the inverse sine value is returned with ASIN (), but the input parameter is greater than 1 o'clock return Nan (not-a-number)


2. Floating-Point rounding error

Add a number plus 1 minus the original number, and the result is 1. If you use floating-point calculations, there may be other results, such as:

#include <stdio.h>int main (void) {float A, b;b = 2.0e20 + 1.0;a = b-2.0e20;printf ("%f\n", a); return 0;}
Results:

This result is due to the fact that the computer lacks sufficient decimal digits for proper operation. 2.0E20 is 2 followed by 20 0. If you add 1, the change is actually 21st place, if you need to calculate correctly, you need to store at least 21 digits, but float as long as 6.7 digits are valid. The above error results will vary depending on the platform.


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


4.SCANF reading habits

/* praise1.c--uses an assortment of strings */#include <stdio.h> #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;}
Results:

What ' s your name?

Tony KK

Hello,tony.what a Super Marvelous name!

This is caused by the fact that scanf () with%s only reads the first word and does not read the entire statement as a string. C can use other read functions (such as Get ()) to handle a generic string.


5.strlen () function and sizeof ():

The sizeof operator, given the size of the data in bytes, the strlen () function gives the length of the string in a string.

#include <stdio.h> #include <string.h>      /* provides strlen () prototype */#define PRAISE "What a super Marve Lous name! " int main (void) {char name[40];p rintf ("What ' s Your name?\n"), scanf_s ("%s", name);p rintf ("Hello,%s.%s\n", name, praise); printf ("Your name of%d letters occupies%d memory cells.\n", strlen (name), sizeof name);p rintf ("The phrase of praise has% D letters ", strlen (Praise));p rintf (" 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 memory cells.

The phrase of praise has letters and occupies memory cells.

Explain:

For the name array, sizeof displays the array length, and strlen () shows the length of the array in use. The string praise,sizeof is 1 more than strlen () because it calculates the end of the.


6. Negative numbers converted to unsigned and values greater than 255 converted to characters equivalent to modulo

#include <stdio.h> #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 of all, 336 is not converted to 336 as we expected, but instead becomes 65200. The reason is that short int 0 to 32767 means they themselves, 42768 to 65535 for negative numbers, 65535 for -1,65534 to 2, and so on, 65200 means-336. So don't expect%u to keep numbers and symbols separate.

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

The 2 binary 336 in memory is 0000000101010000, but the character can only be converted to a number less than or equal to 255, otherwise it is just a conversion, the value is changed. This is true when we try to print a number larger than the short int.


7.prinf read the number in the stack is read by%

#include <stdio.h>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;}
Results:

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 becomes a double), so N2 occupies 8 bytes and N3,n4 occupies 4 bytes respectively. The control is then transferred to the Prinf () function, and Prinf () is read according to the conversion specifier. %ld reads 4 bytes, which is the first half of the N1, and the next%ld reads 4 bytes, the second half of the N1, so the final result is wrong.





Basic knowledge of C + + easy to overlook

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.