整数和小数
Created|Updated
|Post Views:
int & float
前提
我们已经知道int是整形(只能输出整数),float是浮点型(能输出小数)
仍需知道一点
在两个int类型进行运算的时候,C默认会把计算结果变成int。那么:
1
print("%f", 1/2); //0.000000
为什么结果是小数,但又是0呢?
因为先算
1/2=0,再将int类型的0,变成float1
printf("%f\n", 1.0/2); //0.500000
这里的1.0是double型,在计算前先把2变成double,再计算。那么就是得到double型的值0.500000。
总结
计算的时候要留意数字的类型,防止结果的出乎意料
Related Articles
2020-12-23
Hiding of the reference
Today, I made a serious error, which is allocating the memory of reference variable repeatedly. However, if I did it, the complier will throw a error. Why did I create two references that have the same names but the complier didn’t throw the error? The reason is that I create a reference outside the method and the other reference that has the same name was created inside the method. This may be fatal. Because the first reference would be hided while the program was running in the...
2020-10-10
函数的调用问题
Synopsis先来看两个错误 报错 'insertList' was not declared in this...
2020-05-22
指针
不要使用没初始化的指针即只能使用指向已经分配空间的指针。否则可能会擦拭掉数据或代码。 可以,因为第二行就让指针指向字符串ferry12char *str;str = "ferry" 不可以,scanf()是把信息拷贝到指定的地址上12char *str;scanf("%s", str); 不可以,跟上面情况一样,不要解引用未初始化的指针12int *a;a = 2; 时刻记得指针指向的地址(位置)当我们用指针遍历数组的时候,遍历完以后就已经不再指向数组首元素地址了 123int *i;int arr[10];i = arr;
2020-12-26
数组名的传递
1234567891011121314void t(int* a){ cout<<sizeof(a)<<endl;//8}int main(int argc, char const *argv[]){ int a[5]={1,2,3,4,5}; t(a); cout<<sizeof(a)<<endl;//20 return 0;} 很明显cout的那么两行,是同样的,但为什么输出一个是8,一个20呢?我们已知64位的gcc中一个pointer是由8bytes的。所以,传递数组名(地址)给函数,sizeof计算的是a的指针大小,不是整个数组的大小。 此技巧仅适用于数组,不适用于指针: 1sizeof(b) / sizeof(b[0])
2020-05-02
链表中结构指针的指针
关于结构指针 struct Node *ps定义一个Node类型的结构指针。注意,此时并没有指向Node的某个结构 必须malloc分配结构空间,并返回结构地址,给ps 这样ps才能有类似的ps->next操作 想要在函数中改变某个指针的指向的时候通常是,函数中malloc返回给指针的情况 显然,如果我们想通过函数改变某个变量的值,那么我们可以传递这个值的指针给函数。让这个指针指向另外一个值 同样地,如果我们想要改变某个指针,那么就要传递指针的指针给函数 123456789101112typedef struct Node *List;void InitList(List *L){ *L = (PtrToNode)malloc(sizeof(Node)); if(*L == NULL) { fprintf(stderr, "out of space."); exit(1); } (*L)->next =...
Contents
