C基础-输入输出

1、输入输出
scanf_s("%d", &d); scanf_s("%s", s, LEN);

printf("%d",d);
规定符用法介绍,抄的百科
在这里插入图片描述
2、从字符串中读入,或者写出
sscanf_s()读取格式化的字符串ip中的数据,如果是字符串需要跟一个大小s, LEN, s1 LEN1
sprintf_s()将数据格式化输出到字符串buf里面,注意申请的大小不够大会报错哦

https://www.cnblogs.com/xingzhensun/p/9915277.html
sscanf_s(line, "%s%s%lf%lf%lf%lf%lf%d",
         name, 64, time, 64, &px, &py, &pz,
         &rmsH, &rmsV, &nSta);

大牛写的蛮好了,就不加工了

unsigned long ip2long(char *ip)
{
    
    
    unsigned long a, b, c, d; //必须使用ul,否则左移24位会越界
    sscanf_s(ip, "%lu.%lu.%lu.%lu", &a, &b, &c, &d);
    return ((a << 24) | (b << 16) | (c << 8) | (d));
}
void long2ip(unsigned long ipNum, int post, char *buf)
{
    
    
    unsigned long tmp[4] = {
    
    0};
    for (int i = 0; i < 4; i++)
    {
    
    
        tmp[i] = ipNum & 0xff;
        ipNum >>= 8;
    }
    sprintf_s(buf, sizeof(buf), "%lu.%lu.%lu.%lu/%d", tmp[3], tmp[2], tmp[1], tmp[0], post);
    return;
}
作者:fatmouse
链接:https://leetcode-cn.com/problems/ip-to-cidr/solution/c-4-ms-73-mb-by-fatmouse/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

3、附录常用的size 两个整数相减,如果要考边界,需要转换为long long相减。

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_45554139/article/details/104865965