【问题标题】:Using a binary number in for loop comparison [duplicate]在for循环比较中使用二进制数[重复]
【发布时间】:2019-12-02 15:52:51
【问题描述】:
#include<stdio.h>
#include<stdlib.h>

typedef struct node {
  int data;
  struct node* left, *right;
}  node;

node* createNode(int data) {
  node* x = (node*) malloc(sizeof(node));
  x->data = data;
  x->left = x->right = NULL;
  return x;
}

node * addNode(node* head, int data) {
  if (head == NULL)
    return createNode(data);

  node* temp = head;
  while (temp->right)
    temp = temp->right;

  temp->right = createNode(data);
  temp->right->left = temp;
  return head;
}

void printList(node* head) {
  node* temp = head;
  while (temp) {
    printf("%d ", temp-> data);
    temp = temp->right;
  }
}

int main() {

  node* head = NULL;
  for (int i = 0; i < 010; i++)
    head = addNode(head, i + 1);
  printList(head);
  return 0;
}

以下代码产生输出 1 2 3 4 5 6 7 8

我无法理解 for 循环的工作原理。我只想检查编译器在比较时如何处理前导零。如果我在前面放更多零,我会在链表中附加更多节点。附加的节点数始终是 2 的幂。 提前致谢。

【问题讨论】:

标签: c for-loop binary


【解决方案1】:

C 没有二进制整数文字。

以零开头的整数文字是octal 数字。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多