c - 在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);
}
}
解决方案
您不应该将变量的地址传递给您的函数,正确的是
#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);
}
}
推荐阅读
- php - 当从 PHP 服务器发送 curl 请求时超时,但从 localhost 可以正常工作,是的 curl 已启用并在其他域上工作
- c# - 根据其他参数设置可选参数默认值
- python - 依赖解析树的最低共同祖先
- google-compute-engine - Google Compute Engine 和 Glassfish 无法从外部访问
- python - 计算时间序列中连续几个月的客户平均未偿金额
- mongodb - MongoDB 副本集 TLS/SSL
- string - 在bash中的文件中计算带有变量子字符串的字符串的出现次数
- javascript - 如何在javascript中复制一个const数组
- java - 单声道对象未在邮递员中显示
- docker - Kubernetes Nginx Ingress Controller 暴露 Nginx Webserver