C programming language - Functions

Function (the code itself, the program execution function parameters on the stack first, and then run the function code)


int main(int argc, char** argv);

indicates the number of parameters argc, argv inside the respective storage parameter
; the second parameter argv [0] of its own directory path and name of the program to run, argv [. 1] refers to the first argument, argv [2] refers to the second parameter

#include <stdio.h>
int main(int argc, char *argv[ ])
{
printf("%s\n",argv[0]);
return 0;
}

Entrance main function is not really a program!
When the compiler to compile your project file, it is to find your main function, then the main function according to the type, the period of pre-compiled prefix entry program, and then let it go call the main function. when you need the main function parameters, it has compiled main parameters of the function call, and vice versa compiled main function is called with no arguments.
so compiler automatically recognize your main function type!

void main no return value, can be part of the compiler.

Default Library

int fprintf ( FILE *fp, char * format, … );

Function declaration: int printf(const char *format, ...)
function call:printf("<格式化字符串>", <参量表>);

format - string (or character array pointer), containing the text to be written to stdout of.
It can contain embedded tag format, format tag value may be assigned additional parameters subsequent replacement, and demand-driven format.

    转换说明符:
  %c 字符   %s 字符串
      %d 、%i        有符号十进制整数 %4d 四个为单位,右对齐
      %u             无符号十进制整数
      %f             浮点数(包括float和doulbe)
      %o   八进制整数     %x  十六进制整数
      %p   指针

int fscanf ( FILE *fp, char * format, … );

scanf ( "%?", & n);
the received data is stored to the variable n in memory,
if the variable is an array or a pointer, then no &



putchar()、puts()、gets()、getc()

putchar (): an output character data, puts () data output string.

gets()、getc()

The file stream fgetc (), fpuc ()

Read the file into memory

int fgetc (fp): fp obtained from the file referred to in the next character, an error is returned EOF, otherwise the characters read.

int fputc(int char, FILE *stream);

fgets * char (char * buf, int the bufsize of, FP);
* buf: a pointer character, storing the resulting data address pointing to. bufsize: integer data, indicating the size of stored data.

fpintg (fp, ''%? ", the variable name)

#include <stdio.h>
int main()
{
    FILE *fp; //文件流。
if((fp=fopen("test.txt","r"))==NULL)
    printf("%s","错误");
//char c;
//while((c=fgetc(fp))!=EOF)
  //  printf("%c",c);

char f[10];
fgets(f,10,fp);
puts(f);

fclose(fp);
        return 0;
}

size_t strlen (char const * str): size_t is an unsigned integer

Calculating the length of string str specified, but not including the end character (i.e., null character: '\ 0')


LT less than: LE GT greater than or less than or equal GE EQ Equal NE Not equal


math.h

sqrt (): calculates the square root of the non-negative real numbers

abs (int x); x is an integer of seeking the absolute value
fabs (double x); absolute value of the floating-point number x.

pow (a, n): a power of n, a, and n as well as the result is a double return



string.h

strcpy (old and new): the string copy, accepts two parameters, a character string is copied, another new string


strcmp (str1, str2)
String Compare (string comparison), used to compare two strings and returns the integer result of the comparison.

Two strings are compared character by character from left to right (in ASCII value compared to the size), or until a different character encountered so far '\ 0'

When s1 <time s2, return is negative;
when S1 = s2, return value = 0;
when s1> s2, it returns a positive number.



stdlib.h

malloc / realloc / free application, the release of memory:

a malloc specified amount of memory
void * malloc (long NumBytes): NumBytes bytes allocated, and returns a pointer to a pointer to the memory. Allocation failure returns a null pointer.


realloc is based on the original in several open space

Pointer name = (Elemtype *) realloc (original name of the pointer, the new size)
① to release the original pointer memory region
② in accordance with the new size of the space reallocation
③ from beginning to end of the original data is copied to the newly allocated memory region
④ and return the first address of the memory area. I.e. reallocation memory blocks.

void free (void * ptr): function to release memory space in the C language.

Often used in conjunction with the application memory space functions malloc (), the memory space can be released by the malloc (), calloc (), realloc () function and the like of the application.




(Elemtype *); type can be returned by malloc (void *)
 cast (Elemtype *) Type

Usage: Instruction name = (elemType ) the malloc (n- the sizeof (elemType))

Pointer names ⇔ pointer to the first memory address (such as an array base address pointer)



Published 46 original articles · won praise 15 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_41850194/article/details/105315890