簡體   English   中英

在C中兩個給定字符之間打印所有ASCII字符

[英]Printing all ASCII characters between two given characters in C

我編寫了一個程序,以打印出兩個給定字符之間的所有ASCII字符,但隨后將其寫入函數中。 這兩個程序的輸出是不同的。 我嘗試使用指針通過引用傳遞變量,但是輸出不太可能像第一個程序那樣。 如何正確制作?

這是使用線性編程的C語言中的第一個程序。

#include <stdio.h>>
int main() 
{
    char a,b,tmp;
    int d;
    scanf("%c%c",&a,&b);
    if(a>b) 
    {
        tmp=a;
        a=b;
        b=tmp;
    }
    d = b - a;
    for (char c = a+1;c<b;c++) 
    {
        printf("%c : %d, %o, %X\n",c,c,c,c);
    }
}     

這是功能中的其他程序。

#include <stdio.h>>

void ascii(char a,char b);

int main() 
{
    char a,b,tmp;
    int d;
    printf("Enter 2 character => ");
    scanf("%c%c",&a,&b);
    ascii(&a,&b);
}

void ascii(char a,char b)
{
    int d;
    if (a>b) 
    {
        char tmp= a;
        a=b;
        b=tmp;
    }
    d=b-a;
    for (char c=a+1;c<b;c++) 
    {
         printf("%c : %d, %o, %X\n",c,c,c,c);
    }
}

您不應該將變量的地址傳遞給函數,正確的是

#include <stdio.h>>

void ascii(char a,char b);

int main() 
{
    char a,b,tmp;
    int d;
    printf("Enter 2 character => ");
    scanf("%c%c",&a,&b);
    ascii(a,b);
}

void ascii(char a,char b)
{
    int d;
    if (a>b) 
    {
        char tmp= a;
        a=b;
        b=tmp;
    }
    d=b-a;
    for (char c=a+1;c<b;c++) 
    {
         printf("%c : %d, %o, %X\n",c,c,c,c);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM