打印char数组

时间:2017-04-19 23:12:55

标签: c string binary

好的,所以我在C中编写了一个简单的程序,将数字从十进制转换为二进制,它工作正常,直到我尝试转换128.一些奇怪的符号出现,它可能与ASCII有关。有人可以解释为什么这会发生128号及以上的数字。这是我的代码和图像;
screenshot from terminal

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char const *argv[]){

    int a = 128;
    int counter = 0;
    char array[512];
    char c;

    while(a!=0){
        c = a%2 + '0'; // Gives me eather 1 or 0 and converts to char
        array[counter] = c;
        a /= 2;
        counter++;
    }
    for(int i=strlen(array)-1; i>=0; i--){
        printf("%c", array[i]); //printing in reverse
    }
    printf("\n");



    return 0;
}

0 个答案:

没有答案
相关问题