C programming notes day08 ~ Day10 code specification, compilation, library functions, namespaces, function calls, and dummy Functions

Source: Internet
Author: User

Code compiling specifications, command line compilation, database function introduction, namespace introduction, function calling introduction, and dummy functions Introduction


Generally, a. c implementation file should have a. h header file. All functions in the. c file should be declared in the. h file.

Command Line compilation method:
1. GCC directly generates executable files with all. c files-O output file names
2. The GCC-c. c file-O ***. o file is generated in sequence.
Then all the. O files in GCC-O output files generate executable files
Static global variables in the program cannot be used by statements in other files.
In the. c file, use the global variable: extern variable type variable name in the. h file;
. H file should be used # ifdef ...... # Define ...... # Endif inclusion to prevent repeated references
Experience:
When the struct is defined, the larger the value of the variable vertex byte should be declared at the end to reduce space waste.

In the struct, student Stu; Student * pstu;

You can directly use & (pstu-> member) and & Stu. member to get the variable address. You can use pstu-> Member, Stu. Member



Heap memory usage:
The address allocated by malloc is local or global. If it is not free, it can be used elsewhere.
Calloc (sizeof (INT), n); // allocate space of N Integers
Malloc (N * sizeof (INT); // allocate space of N Integers


Sorting library function: qsort () header file: stdlib. h
The prototype of the qsort function is void _ cdecl qsort (void * base, size_tnum, size_t width, INT (_ cdecl * comp) (const void *, const void *))
Parameter 1: array name
Parameter 2: Number of Elements
Parameter 3: data type size, for example, sizeof (INT)
Parameter: Comparison function:
For example, int comp (const void * a, const void * B)
{
Return strcmp (* (const char **) A, * (const char **) B );
}
Int comp (const void * a, const void * B)
{
Return * (int *) A-* (int *) B;
}
Library Function usage:
Atoi, atol, atoll, atof -- convert the character into a digital header file: stdlib. h
Char STR [50] = "123"; int id = 0;
Sscantf (STR, "% d", & ID); // put the information read from the string into the variable
Sprintf (STR, "Hello % d", 123); // output the variable value to the string
Fscanf (File *, "% d", & ID); // input from file to variable
Fprintf (File *, "% d", ID); // output the variable to the file


Namespace declaration:
Namespace NS1 {
// Code, Function
Void fun1 (){}
}
// Default namespace Function
Void fun1 (){}
Call:
For the default namespace, you can directly call fun1 (); or: fun1 ();
Functions in NS1 can be called using NS1: fun1 ();
Overload:
It is related to the function name and parameter list, and has nothing to do with the return value (the same scope, different parameters, the same function name can constitute an overload)
Reload parsing: Security matching> constant conversion> Upgrade conversion> standard conversion> Custom conversion> ellipsis matching
Functions with the same name in different namespaces can constitute an overload when visible within the scope, but not necessarily an overload.


View the modules in the. o file:
NM ***. o
Differences between C ++ and C functions:
The name of the function generated by the C compiler remains the same, and the name of the function generated by the C ++ compiler changes (the C ++ naming principle). If C ++ calls the C function, by adding exterm "C" [function declaration] before declaring a function, you can avoid renaming C ++ during calling. C does not support overloading.
When defining a function, you can specify the default value of the parameter or include the default parameter when declaring the function.

Definition of a dummy and non-function: it has a parameter type and no parameter variable. Passing any parameter value does not affect the function call result, just to overload the function.



Related Article

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.