(C Note): function, the random number, the pointer has shown signs

Function
Function Category:
c * program is made up of functions, we write the code are the main function main () begins execution. C function is the basic program module, the program code means for accomplishing specific tasks.
From the viewpoint of function definition function can be divided into user-defined functions and system functions in two ways:
1: system function, i.e., library function : This is provided by the compiler system, the user does not customize these functions, they can be used directly, as we often print function printf ().
2: User-defined functions: to solve the user's special needs.
Using the function:
using a function of rewriting the code may be omitted, reducing the repetition rate of the code redundancy.
Function can make the program more modular, thus contributing to read, modify and improve the program.
If we write a program to achieve the following functions: reading a column of figures; ordering these numbers; find them average: print a histogram. If we take these actions directly written in the main () function inside, so may give users the feeling of the code a little messy, but, if we use the function, so you can make the program more clear and modular.
Calling the function: generates a random number
when calling the function, to be concerned about the 5 elements
header files: a header file that contains the specified
function name: function name and the name of the header file must be declared as
parameters: the need to know what to do just call this function
returns Found: receiving a return value as appropriate
Here Insert Picture Description

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main()
{
 //time_t timer=time(NULL);
 //srand((size_t)timer);
 //添加随机数种子
 srand((sizet_t)time(NULL))for(int i=0;i<100;i++)
 {
  printf("5d\n",rand()%51=50);//50~100
 }
}
return 0;

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
No parameters of the function call : If you are calling a function with no parameters, you can not add "argument", but parentheses can not be omitted.
Actual argument list comprising a plurality of arguments, parameters are separated by commas between
the return value of the function: can not write keyword void if the return value of the function is not defined, the function call, the function call can not receive a return value of a function,
the function Disclaimer:
If you use a user-defined function, and its function (ie the calling function) function call and not in the same file, or
defined position after calling function
before, you must call this function to be called function do declare.
The so-called function declaration, that is, in the case of the function has not been defined in advance information about the function of the system informs the compiler, the equivalent of telling the compiler, the function is defined in the back so that the compiler can perform normally.
* Note: * A function can only be defined once, but can be declared multiple times.
The difference between declarations and definitions :
declaring variables do not need to create a volume, such as: extern int a;
define variables need to create storage space, such as: int b;
broad perspective is included to define statement, i.e., statement definition is a special case , so not all declarations are defined:
int b it is the declaration is defined
under normal circumstances, the establishment of storage space declaration called the "definition", and the statement does not need to create a volume called "statement" .
Primary and exit functions

#include <stdio.h>
#include <stdlib.h>
int fun1()
{
 printf("hello world\n");
 printf("hello world\n");
 //终止程序执行
 exit(0);
 //return 0;
 printf("hello world\n");
 printf("hello world\n");
 return 0;
}
int main()
{
 fun1();
 return 0;
}
运行结果:
hello world
hello world

多文件编程
Here Insert Picture Description
编程实例:
步骤:以编程软件visdual studio为例:
创建一个项目(设置为启动项目),然后创建一个空项目的源文件起名为:main.c ;然后再创建一个源文件命名为:01fun.c ;然后点击头文件创建一个头文件的项目起名为:head.h ;附图:
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
指针
内存
存储器:计算机的组成中用来存储程序和数据,辅助CPU进行运算处理的重要部分。
内存:内部存贮器,暂存程序/数据–掉电丢失SRAM,DRAM,DDR,DDR2,DDR3。
外存:外部存储器,长时间保存程序/数据–掉电不丢失ROM,ERROM,FLASH,(NAND,NOR)‘硬盘、光盘。
内存是沟通CPU与硬盘的桥梁:
暂存放CPU中的运算数据
暂存与硬盘等外部存储器交换的数据
和存储地址空间
物理存储器:实际存在的具体存储器芯片
主板上装的内存条
显示卡上的显示RAM芯片
各种适配卡上的RAM芯片和ROM芯片
存储地址空间:对存储器编码的范围。我们在软件上常说的内存是指这一层含义。
编码:对每个物理存储单元(一个字节)分配一个号码。
寻址:可以根据分配的号码找到相应的存储单元,完成数据的读写。
内存地址
将内存抽象成一个很大的一维字符数组。
编码就是对内存的每一个字节分配一个32位或者64位的编号(与32位或者64位处理器相关)
The number of memory we call memory address. A respective address is assigned to each data memory:
such as:
char: an address assigned one byte
int: 4 bytes allocated four addresses
float, struct, function, array, etc.

#include <stdio.h>
int main()
{
 int a=0xaabbccdd;
 printf("%p\n%P",&a,a);
 return 0;
}
运行(式例)结果:
000000000062FE1C
00000000AABBCCDD

Defining and using a pointer variable

#include <stdio.h>
 void swap(int* pa, int* pb);//定义指针形参
int main()
{
    int a = 6, b = 5;
    swap(&a, &b);   //传递实参地址值
    printf("a=%d b=%d", a, b);//打印数据
    return 0;
}
void swap(int *pa,int *pb)//交换值
{
    int t;
    t = *pa;
    *pa = *pb;
    *pb = t;
}
运行结果:
a=5 b=6

Null pointer and the pointer field

// & fetch address is a character dimension l
// operator * is the value of the dimension is
Here Insert Picture Description

#include <stdio.h>
int main()
{
 //不建议将一个变量的值赋值给野指针
 //野指针 --》指向一个未知的空间
 int* p = 100; //程序中允许存在野指针
 //操作系统将0~255作为系统占用不允许访问操作
 //操作野指针对应的内存空间可能报错
 printf("%d\n", *p);
 return 0;
}

Refers to a null pointer to the memory address space 0 of
the operation corresponding to the null pointer will be given spatial
null pointer can be used as a condition judgment
defined as a null pointer: int * p = NULL;
universal pointer (void pointer):

#include <stdio.h>
int main()
{
 int a=10;
 void *p=&a;
 *(int *)p=100;
 printf("%d\n",a);
 printf("%d\n",*(int *)p);
 return 0;
}
运行结果:
100
100

Universal pointer to receive any type of variable memory address
need to find the corresponding pointer type variable universal values through the pointer variable can modify
Here Insert Picture Descriptionthe contents hereof are incorporated by reference in conjunction with my other blog

Published an original article · won praise 1 · views 87

Guess you like

Origin blog.csdn.net/weixin_45744986/article/details/104068419