任何人都可以告诉我为什么我不能让这个菜单让我做出选择?它跳过让我选择正确的"错误选择".在此先感谢您的帮助,我一定会感谢您的亲自.
printf("Welcome to the Inventroy Control System");
printf("\nPlease make a selection");
printf("\n\n\ta) Display Inventory");
printf("\n\tb) Add New Items");
printf("\n\tg) Exit");
printf("\nSelect what you would like to do");
printf("\nOption Chosen: ");
fflush(stdin);
scanf("%c", &input);
input = toupper(input);
switch(input)
{
case 'A':
{
funa(j,a);
break;
}
case 'B':
{
j = funb(amount,a);
break;
}
case 'G':
{
fung();
break;
}
default:
{
printf("\nWrong Selection");
}
}
Run Code Online (Sandbox Code Playgroud) 我希望程序获取用户的身高和体重,然后将其输入到bmi公式中,然后输出结果.
高度问题工作正常,但是当您在重量问题中输入一个数字并按下回车键时,它只会换行.输入另一个数字后,它会将其设置为bmi并打印出来.
#include<stdio.h>
int main()
{
int h, w, bmi;
printf("What is your height (in inches)? ");
scanf("%d", &h);
printf("What is your weight? ");
scanf("%d", &w);
bmi = (w/(h*h))*703;
scanf("%d", &bmi);
printf("\t\tBMI\n");
printf("US: %d\n", bmi);
}
Run Code Online (Sandbox Code Playgroud) 单个百分号的输出应该是什么?
#include <stdio.h>
void main()
{
printf("%");
}
Run Code Online (Sandbox Code Playgroud)
“未知格式代码”与“未知转义序列”不同。
对不起,我的英语不好。
我在 M1 Pro MacBook Pro 中使用 macOS 12.3
我想了解C标准库中printf的源代码。
我可以在 usr/include 目录中找到 stdio.h 文件。
但我在同一目录中找不到 printf.c 或 stdio.c 文件。
而且我认为 Apple Open Source 的 printf.c 太复杂,看起来不像 macOS 中的真实代码,对我没有帮助。Apple 开源 printf.c 代码真的在 macOS 中使用吗?
https://opensource.apple.com/source/xnu/xnu-201/osfmk/kern/printf.c.auto.html
我无法弄清楚这个程序有什么问题.我试过用
strncpy(text,array[ ],sizeof(text))
Run Code Online (Sandbox Code Playgroud)
已经但是没有解决任何问题.我需要的是一种复制字符串的简单方法,如pascal语言,其中可以使用简单的等号来复制字符串(或者可以执行此操作的生成函数).这是源代码:
#include <stdio.h>
#include <string.h>
int x,y;
char array[10][10];
int choice;
char text[5];
int main()
{
for(x=0;x<5;x++)
{
printf("ENTER text: ");
scanf("%s", text);
strcpy (text,array[x]);
}
for (y=0;y<5;y++)
{
printf("\n");
printf("%s", array[y]);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出应该是这样的;
"*string*"
"*string*"
"*string*"
"*string*"
"*string*"
Run Code Online (Sandbox Code Playgroud)
但我得到的是五个空格,没有字符串.任何方案?
请帮助我使用右对齐的哈希值和空格正确打印高度“n”的金字塔。我已在下面发布了代码本身。该程序正确地要求用户输入,但不会构建右对齐的金字塔。如果有人可以解决这个问题,请帮忙。
#include <stdio.h>
#include <cs50.h>
int main(void)
{
int i=0;
int n=0;
do
{
printf("Height:");
n=GetInt();
} while (n<0 || n>23);
for (i=0; i<n; i++)
{
printf(" ");
for (int x=0; x<i+2; x++)
{
printf("#");
}
printf("\n");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我们知道C是一种编译语言.根据C语言维基百科,它说:
它被设计为使用相对简单的编译器进行编译,以提供对内存的低级访问,提供有效映射到机器指令的语言结构,并且需要最少的运行时支持.它还说,通过设计,C提供了有效映射到典型机器指令的结构,因此它已经在以前用汇编语言编写的应用程序中得到了持久的使用,包括操作系统,以及用于计算机的各种应用软件.超级计算机到嵌入式系统.
但是当我读到这篇文章时,根据Bruce Eckel的C++ 2中的Thinking,它在第2章中标题为Iostreams :(我省略了一些部分)
最大的障碍是用于变量参数列表函数的运行时解释器.这是在运行时解析格式字符串并从变量参数列表中获取和解释参数的代码.出于四个原因,这是一个问题.
因为解释在运行时发生,所以你无法摆脱性能开销.令人沮丧的是因为编译时格式字符串中存在所有信息,但直到运行时才会对其进行求值.但是,如果您可以在编译时解析格式字符串中的参数,则可以进行硬函数调用,这些函数调用可能比运行时解释器快得多(尽管printf()函数系列通常已经过很好的优化).
这个链接还说:
更类型安全:有了I/O的对象类型,编译器静态地知道.相反,cstdio使用"%"字段动态地计算出类型.
所以在阅读本文之前,我认为解释器不像C那样用于编译语言,但是在C程序执行期间运行时解释器是否也可以使用?读这篇文章之前我错了吗?与Iostream相比,这种运行时解释真的有这么多开销吗?