首页 > 解决方案 > 在C中的两个给定字符之间打印所有ASCII字符

问题描述

我制作了一个程序来打印两个给定字符之间的所有 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);
    }
}

标签: 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);
    }
}

推荐阅读