小编Kam*_*aze的帖子

当我定义具有相同名称的宏时会发生什么

当我#define在一个源文件中有多个具有相同名称的s 时会发生什么:

例如:

#define Dummy 1
#define Dummy 2
Run Code Online (Sandbox Code Playgroud)

我不打算使用它,但在生产代码中看到类似的东西.这是否符合标准?

c language-lawyer c-preprocessor

6
推荐指数
2
解决办法
632
查看次数

如何在C中计算crc8?

我已经在 C 中看到了 crc8 实现的多种实现,但我无法弄清楚 polynomial(x8,x5,x4,1) ie0x31和 initialization 0xFF

还反映输入 = False,反映输出 = False 和最终 XOR = 0x00

我尝试了其中的几个,我希望这样CRC(0x00)=0xACCRC(0xBEEF)=0x92

我见过类似的实现,但没有真正奏效。我在http://www.sunshine2k.de/coding/javascript/crc/crc_js.html中看到了确切的功能,我可以在其中进行初始化、反映输入、反映输出和最终异或。但是有人可以指出我在 C 中的实现。我知道最初我们需要将 crc 指定为 0xFF,但到目前为止对我来说没有任何效果。

请找到我尝试附加的示例代码:

#include <stdio.h>
#include <stdint.h>

uint8_t crc8(uint16_t input);

int main()
{
    uint8_t temp1;
    uint16_t temp2 = 0xBEEF;

    printf("CRC input is 0x%X\n", temp2);

    temp1 = crc8(temp2);

    printf("CRC output is 0x%X\n", temp1);

    return 0;
}

uint8_t crc8(uint16_t input)
{
    uint8_t crc[8] = { };
    uint8_t i;
    uint8_t …
Run Code Online (Sandbox Code Playgroud)

c crc

4
推荐指数
2
解决办法
3万
查看次数

如何在C编程中将代码划分为函数

我有一个完美的运行代码,但这项作业的标准之一是它至少具有两个不同的功能。我怎样才能将此代码用于其他功能?

我想将alarmClock()函数排序为更多函数。那里有很多事。也许是updateTime()函数。我尝试过类似的操作,但这不起作用:

#include <stdio.h>

void alarmClock(int, int);
void updateTime(int, int, int);

int main() {
  int present_time;
  int time_for_alarm;

  printf("Time indicates in HHMMSS! \nPresent time: ");
  scanf("%d", &present_time);

  printf("Time for alarm: ");
  scanf("%d", &time_for_alarm);

  if (present_time == time_for_alarm)
    printf("ALARM!");
  else
    alarmClock(present_time, time_for_alarm);

  return 0;
}

void alarmClock(int presT, int alarmT) {
  int presentHH = presT / 10000;
  int presentMM = (presT / 100) % 100;
  int presentSS = presT % 100;

  int combineTime;

  while (presT != alarmT) {
    printf("%02d:%02d:%02d \n", presentHH, …
Run Code Online (Sandbox Code Playgroud)

c function

3
推荐指数
1
解决办法
86
查看次数

c- recvfrom错误22

好的,首先是下面的代码:

int recvMast_sock;
struct sockaddr_in serv_addr, cli_addr;
socklen_t cli_len;
if ((recvMast_sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
  {
    critErr("listen:socket=");
  }

fillSockaddrAny(&serv_addr, UDP_NODE_LISTEN_PORT);// fills the sockaddr_in works fine elsewhere

if ((bind(recvMast_sock, (struct sockaddr*) &serv_addr, sizeof serv_addr)) < 0)
  {
    critErr("listen:bind recv_mast_sock:");
  }
recvReturn_i = recvfrom(recvMast_sock, &recvBuff[0], (size_t)1, 0, (struct   sockaddr*) &cli_addr, &cli_len);
if(recvReturn_i <0)
        printf("recv error%d\n",errno);
Run Code Online (Sandbox Code Playgroud)

critErr是处理错误的函数,还包括错误的打印和exit

如果有关联,它将在线程中运行。如果我在具有ARM Cortex A9和Linaro Linux(基于精确Ubuntu)的Zedboard(ZYNQ-7000 SoC)上编译并运行它。它显示错误,22但仍具有的接收值recvBuff[0]

使用xubuntu在我的VM中运行它可以正常工作。

错误22等于EINVAL被描述为无效参数。

recvfrom(2)它的手册页中,状态EINVAL表示MSG_OOB …

c sockets linux linaro

2
推荐指数
1
解决办法
2294
查看次数

将一个大浮点数存储到一个整数中(强制转换,不投出)

标准是什么(标准中有差异?)说关于将整数范围内的浮点数分配给这个整数?

那么这里应该发生什么,假设16位短,以保持数字小 (USHRT_MAX == 65535)

float f = 100000.0f;
short s = f;
s = (short) f;
unsigned short us = f;
us = (unsigned short) f;
Run Code Online (Sandbox Code Playgroud)

c

2
推荐指数
1
解决办法
27
查看次数

是否定义了添加逻辑运算结果的行为

是否可以(定义行为)来添加逻辑运算的结果(因为它们应该是0或者1)?

如果我想计算大于零的数字,我可以这样做吗?(或者有更好的方法吗?)

int a[3] = {1,-5,3};
int result  = 0;
for( int i = 0 ; i<3; i++)
{
    result += a[i]>0;
}
Run Code Online (Sandbox Code Playgroud)

c relational-operators

2
推荐指数
1
解决办法
63
查看次数

为什么预处理器宏忽略括号中的语句

关注我的(重复)问题(并由StoryTeller建议)

为什么预处理器宏忽略括号中的函数名?

#include  <stdio.h>
#include <stdlib.h>
#define abs(x) ((x))

int main(void)
{
    printf("%d\n", abs(-1)); // output: -1
    printf("%d\n", (abs)(-1)); // output: 1
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是在标准中定义的吗?

c c-preprocessor

2
推荐指数
1
解决办法
197
查看次数

在 C 中将浮点数传递给字符串

如何将变量添加到字符串中?我想传输具有实际值的字符串depth。那么如何将其添加到消息中呢?

float depth = ((pressure) / (g)) * 100;
char message[] = "$DBS,,f,depth,M,,,F*hh<CR><LF>";  //Put actual depth here
Run Code Online (Sandbox Code Playgroud)

c string

2
推荐指数
1
解决办法
141
查看次数

如何在我的struct中使用库

struct date
{
  int day;
  int month;
  int year;
  void input()
  {
      std::cin>>day>>month>>year;
  }
};

#include<iostream>

main()
{
  date d1;
  d1.input();
}
Run Code Online (Sandbox Code Playgroud)

cin除非包含iostream,否则无效.但是我们不能将结构声明为全局,除非我们在main函数之前定义它.

如何在我的struct中使用库函数.

c++

-2
推荐指数
1
解决办法
45
查看次数