One-sentence knowledge in C Language

Source: Internet
Author: User

 

1. Differences between class and struct

The usage of class is no different from that of struct, except that the default member variable of class is private, while that of struct is public by default.

2. Do not forget to use % s when printing a string in printf.

You can see the code:

Int main ()

{

Char string [] = "Hello C programmers % dd ";

Printf (string );

Return 0;

}

3. Benefits of inline

In real-time systems, the speed of code is the most important thing. Therefore, you can use the inline keyword to reduce the time needed to call a function and return it. For example:

Inline double cube (Double X)

{

Return x * X;

}

4. Gets is dangerous.

Char string [100];

Printf ("Enter sentence :");

Gets (string );

This code is dangerous and may overflow the array. Use fgets instead. Fgets has two parameters to control.

5. define abstract classes in C ++

A class wants to program an abstract class. Its member functions plus virtual have no subject (= 0). Such classes cannot be instantiated and can only be inherited.

6. copy constructors

Class copiableclass

{

Public:

Copiableclass (const copiableclass & other); // copy the constructor

};

The copy constructor should copy all the data in the class and its parent class. If your class has pointers, You need to allocate space. if the pointer is of a polymorphism type, you should use the polymorphism replication method.

7. In C ++, the pure abstract base class is a contract.

In C ++, the pure abstract base class is like a contract between the implementation class and the user. The specific implementation is determined by the Implementation class.

Class contract

{

Virtual short Foo (void) = 0;

}

Class A: public class contract

{

Short Foo (void) {return 2 ;}

}

Class B: public class contract

{

Short Foo (void) {return 4 ;}

}

 

Int main (INT argc, char * argv [])

{

Contract * pcontract = new B;

STD: cout <"foo =" <pcontract-> Foo () <STD: Endl;

Delete pcontract;

Return 0;

}

8. Differences between float and double

Float a = 3.14f;

If (A = 3.14)

{

Cout <"Hello" <Endl;

}

Else

{

Cout <"bye" <Endl;

}

The output must be bye. Because the default value is double.

9. Swap functions without temporary variables

Void swap (Int & A, Int & B)

{

A ^ = B;

B ^ =;

A ^ = B;

}

To ensure that the two variables do not overlap, change to the following:

Void swap (Int & _ restrict a, Int & _ restrict B)

{

Assert (&! = & B );

A ^ = B;

B ^ =;

A ^ = B;

}

10. Swap functions without temporary variables

Int A, B;

Scanf ("% d", & A, & B );

B = (a + B)-(A = B );

Printf ("% d", a, B );

11. Division logic error

Float ans = 5/10; // The value of ANS is 0, not 0.5.

12. Type comparison Problems

Unsigned int u = 10;

For (INT I =-1; I <= u; I ++) {/* no execution at a time */}

13. Assert () yourself

# Include <cassert> or # include <assert. h>

Assert (theptrthatshouldneverbenull! = NULL );

I don't want to compare it. # define ndebug. All assert instances are invalid.

14. Do not use strlen in loop judgment

For (int ix = 0; ix <strlen (a_str); ix ++)

{

A_str [ix] = tolower (unsigned char) a_str [ix]);

}

Every cycle is calculated and optimized.

15. int * P Definition

Int * p, q is a pointer and a variable.

Int * P, * q is two pointers.

16. Optimization: use multiplication instead of Division

The multiplication speed is much faster than the Division speed.

17. Optimization: ++ A and A ++

For (x = 0; x <10; X ++) is not as fast as for (x = 0; x <10; ++ X.

For (G = g_list.begin (); G! = G_list.end (); ++ g) for (G = g_list.begin (); G! = G_list.end (); G ++) fast.

18. Do not add a positive or negative number before sizeof

Cout <-sizeof (char); // The result is 4294967295.

Because the return value of sizeof is unsigned Int.

19. convert a number to a string

C language:

# Include <math. h>

# Include <stdio. h>

Char * ITOA (INT num)

{

Int size = log10 (Num) + 1;

Char * x = malloc (size );

Snprintf (x, size, "% d", num );

}

C ++ language:

# Include <iostream>

# Include <sstream>

# Include <string>

String ITOA (INT num)

{

Stringstream converter;

Converter <num;

Return converter. STR ();

}

20. After the free and delete operations are complete, execute pointer = NULL.

21. Optimization: Use <to optimize

A * = 100;

A = (a <6) + (A <5) + (A <2 );

22. Do not use void main ()

23. Clean up after CIN

# Include <iostream>

Int main (INT argc, char * argv [])

{

Int age;

STD: cout <"Enter your age :";

STD: CIN> age;

STD: cin. Ignore (); // This sentence is critical.

STD: cout <"you entered" <age <STD: Endl;

STD: cin. Get ();

}

24. strcpy is not safe. Use strncpy whenever possible.

25. Optimization: Use a ++, a -- to replace a + = 1, A-= 1

26. Gets may overflow. Use fgets instead.

27. Use STL containers to replace the C Array

28. # include <stdlib. h> system function execution system commands

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.