c语言的二进制数值如何直接输出?

如题所述

1、首先打开vc6.0, 新建一个项目。

2、添加头文件。

3、添加main主函数。

4、定义一个两个数相加的函数binSubtracton。

5、在main函数定义int了性number1,number2, binSub。

6、使用scanf给变量赋值。

7、调用binAddition、binSubtracton。

8、使用printf打印结果。

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2019-09-04

C标准没有输出二进制的,不过用itoa()可以实现到二进的转换。

1、在C语言程序中,可以使用标准库函数中printf()来向屏幕输出信息,或者使用sprintf()向缓冲区输出信息。对整数而言,可以使用%d、%o、%x(或%X)输出十进制形式、八进制、十六进制形式,但貌似缺乏二进制形式。

2、解决方式有两种,一种是利用Windows提供的itoa()函数,另一种自行设计一个新的函数:

代码一:

/** Test.h */
#pragma once


#ifndef Test_H
#define Test_H


/** use itoa() to print integers in the form of binary 
 *  @param[in] n the integer to print
 *  @return void
 */
void printBinaryByItoa(unsigned int n);

代码二:

/** Test.c */
#include <stdio.h>
#include <stdlib.h>


void printBinaryByItoa(unsigned int n)
{
  unsigned char data[33];


  _itoa_s(n, data, 33, 2);
  
  printf("%sb\n", data);
}


void printBinary(unsigned int n, unsigned char separator, unsigned char needPrefixZeros)
{
  unsigned int i = 0;
  unsigned int mask = 0;    /* the mask code to get each bit of n */
  unsigned char data[40];   /* the buffer to store the bits of n in the form of characters */
  unsigned int index = 0;   /* used to access data[] */
  unsigned int start = 0;   /* to point out where and when to store bits of n */
  
  data[39] = '\0';

本回答被网友采纳
第2个回答  2018-02-23
就像楼上说的,二进制是不可以直接输出的,但是可以用程序输出
#include<stdio.h>

void f(int n)
{
if(n) f(n/2);
else return;
printf("%d",n%2);
}

int main()
{
int n;
while(1)
{
scanf("%d",&n);
if(n<0) break;
if(n==0) printf("0");
f(n);
printf("\n");
}
return 0;
}
第3个回答  2020-04-14
void B_print (unsigned char B)//八位二进制输出
{
char j;
for(j=8;j>0;j--){printf("%d\t",(1&(B>>j)));}
}

想要几位改循环次数和就行了

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网