1, c error handling
errno, Perror () and Sterror ()
The perror () function displays the string you passed to it, followed by a colon, a space, and a textual representation of the current errno value.
The strerror () function returns a pointer to the text representation of the current errno value.
Error removed by 0
Program Exit status
Normally, a program that successfully executes an operation normally exits with a value of exit_success. Here, Exit_success is a macro, which is defined as 0. If there is an error condition in the program, when you exit the program, it will have a status value of Exit_failure, which is defined as-1.
2, C recursion
Factorial of the number
int factorial (unsigned int i) { if (I <= 1) { return 1; } return I * factorial (i-1);}
Fibonacci sequence
int Fibonaci (int i) { if (i = = 0) { return 0; } if (i = = 1) { return 1; } Return Fibonaci (i-1) + Fibonaci (i-2);}
3. variable Parameters
You need to use the stdarg.h header file, which provides functions and macros that implement the variable parameters feature. The steps are as follows:
- Define a function, the last argument is an ellipsis, and the one preceding the ellipsis is always int, indicating the number of arguments.
- Create a va_list type variable in the function definition that is defined in the Stdarg.h header file.
- Use the int parameter and the va_start macro to initialize the va_list variable as a list of arguments. The macro va_start is defined in the Stdarg.h header file.
- Use the VA_ARG macro and the va_list variable to access each item in the argument list.
- Use the macro va_end to clean up the memory assigned to the va_list variable.
#include <stdio.h> #include <stdarg.h>double average (int num,...) { va_list valist; Double sum = 0.0; int i;
/ * Initialize valist for num parameters * / Va_start
(Valist, num);
/* Access all parameters assigned to Valist */
for (i = 0; i < num; i++) {
Va_arg
(valist, int); }
/* Clean up the memory reserved for Valist */
va_end (valist); return sum/num;} int main () { printf ("Average of 2, 3, 4, 5 =%f\n", Average (4, 2,3,4,5)); printf ("Average of 5, ten,%f\n", Average (3, 5,10,15));}
4. Memory Management
Memory management functions
| Function |
Describe |
| void *calloc (int num,int size) |
The function assigns an array with function allocates an array of num elements, each of which is of size byte . |
| void free (void *adress) |
This function releases the block of H memory pointed to by address. |
| void *malloc (int num) |
The function allocates an array of num bytes and initializes the |
| void *realloc (void *adress,int newsize) |
This function re-allocates memory to extend memory to newsize |
Dynamically allocating memory
Without knowing the length of the text that needs to be stored, you need to define a pointer to a character that does not define the size of the memory you are learning, and then allocate memory on demand.
... char *description;
Description = malloc (* sizeof (char));
5. command Line Parameters
When executing a program, you can pass a value from the command line to the C program. These values are called command-line arguments , and they are important to the program, especially if you want to code them externally, rather than hard-coding them within the codes.
Command-line arguments are handled using the main () function parameter, whereargc refers to the number of arguments passed in, andargv[] is an array of pointers that point to each parameter passed to the program.
int main (int argc, char *argv[]) { if (argc = = 2) { printf ("The argument supplied is%s\n", argv[1]); c11/>} else if (argc > 2) { printf ("Too Many arguments supplied.\n"); } else { printf ("One Argument expected.\n");} }
Using a parameter to compile and execute the above code, it produces the following results:
The argument supplied is testing
Using two parameters, compile and execute the above code, which produces the following results:
Too many arguments supplied.
Without passing any parameters, compile and execute the above code, which produces the following results:
One argument expected argv[0] the name of the stored program,argv[1] is a pointer to the first command-line argument, *argv[n] is the last parameter. If no arguments are supplied, ARGC will be 1, otherwise if a parameter is passed,argc will be set to 2.
Multiple command-line arguments are separated by a space, but if the parameter itself has a space, the parameter should be passed in double quotation marks "" or single quote "Inside
C Review notes (DAY4)