当 N 是 C 的输入并且它也由输入函数定义时如何在 C 中定义变量 "N" 类型(N 将提示用户输入)
How to define a variable "N" type in C when N it is an input to C and its also defined by an input function( N will prompt the user for input)
我对编程还是很陌生,对 C 编程也很陌生,所以如果我的问题看起来很荒谬,请原谅我。
我正在学习函数和函数调用,我正在尝试按照讲师的操作进行操作,但要进行一些补充。
讲师做了以下事情:
- 讲师定义了一个函数 cough -- (void cough(int N))
- 仅用一行代码就调用了main中的函数
- 该程序在屏幕上打印出硬编码的咳嗽次数,即咳嗽(3) 将打印咳嗽 3 次。
我正在尝试:
- 定义一个函数 cough -- (void cough(int))
- 一行代码调用main函数
- 程序应该在屏幕上打印 N 次咳嗽,但应该在程序启动时得到提示,例如“咳嗽多少次?”
我正在使用的 C(c99) 有一个库 cs50.h(来自在线课程 cs50),它提供了一个代码来获取整数输入。
经过搜索和尝试,我已经成功完成如下操作:
包含cs50库后
int N = get_int("咳嗽了多少次")
咳嗽(N)
还有
咳嗽(get_int("咳嗽多少次"))
我只是想知道为什么我不能在 C 中成功使用下面的代码而不会出现错误。为什么不能从函数生成N?
我得到的错误是 --- 使用未声明的标识符 'N'
void cough(int N);
int main(void)
{
cough(N);
}
void cough(int N)
{
N = get_int("How many coughs?\n");
for (int i = 0; i < N; i++)
{
printf("cough\n");
}
}
感谢您阅读长篇大论并提供指导。
你可以试试这个,但我没用get_int()
void cough(int N);
int main(void)
{
int N = 0;
scanf( "%d", &N );
cough(N);
}
void cough(int N)
{
printf("How many coughs?\n");
for (int i = 0; i < N; i++)
{
printf("cough\n");
}
}
当您用 C 语言编写程序时,您只能使用之前定义过的标识符...或者,当然,在您定义它们时。
所以让我们逐行处理您的代码
void cough(int N);
void
已定义:语言关键字
cough
此处(暂定)定义
int
已定义:一个语言关键字
N
在这里(暂定)定义它。此定义在下一个右括号之前有效,因为它是参数定义
int main(void) {
main
正在此处定义
cough(N);
cough
之前(暂定)定义过,所以没关系:编译器知道它是一个函数,之前的暂定定义和实际定义不能改变它。
N
糟糕!!!之前未定义,不是关键字,编译器因错误而停止:)
您可以定义 cough()
不带任何参数,但是
#include <stdio.h>
#include "cs50.h"
int cough(void);
int main(void)
{
cough();
}
void cough(void)
{
int N = get_int("How many coughs?\n");
for (int i = 0; i < N; i++)
{
printf("cough\n");
}
}
I am only wondering why can't I successfully use below code in C without getting an error.
use of undeclared identifier 'N'
因为在
int main(void)
{
cough(N);
}
N在main中没有定义为局部变量,也不是全局变量,所以你不能编译你的代码
but should be gotten when program starts with a prompt e.g "How many coughs?"
所以
N = get_int("How many coughs?\n");
必须移动到main
你终于想要这样的东西了:
void cough(int N);
int main(void)
{
cough(get_int("How many coughs?\n"));
return 0;
}
void cough(int N)
{
for (int i = 0; i < N; i++)
{
printf("cough\n");
}
}
请注意,您的 printf
打印了一个常量字符串,因此您可以将其替换为 puts("cough");
,这会更快一些,因为 printf
在这种情况下搜索 '%' 是空的
您需要在main
中定义N
(顺便说一句,它完全独立于cough
中的N
。例如:
int main(void)
{
int N = 5;
cough(N);
{
作为替代方案,您可以使用 cough(5)
或将 N
定义为全局。
我对编程还是很陌生,对 C 编程也很陌生,所以如果我的问题看起来很荒谬,请原谅我。
我正在学习函数和函数调用,我正在尝试按照讲师的操作进行操作,但要进行一些补充。 讲师做了以下事情:
- 讲师定义了一个函数 cough -- (void cough(int N))
- 仅用一行代码就调用了main中的函数
- 该程序在屏幕上打印出硬编码的咳嗽次数,即咳嗽(3) 将打印咳嗽 3 次。
我正在尝试:
- 定义一个函数 cough -- (void cough(int))
- 一行代码调用main函数
- 程序应该在屏幕上打印 N 次咳嗽,但应该在程序启动时得到提示,例如“咳嗽多少次?”
我正在使用的 C(c99) 有一个库 cs50.h(来自在线课程 cs50),它提供了一个代码来获取整数输入。 经过搜索和尝试,我已经成功完成如下操作:
包含cs50库后 int N = get_int("咳嗽了多少次") 咳嗽(N)
还有 咳嗽(get_int("咳嗽多少次"))
我只是想知道为什么我不能在 C 中成功使用下面的代码而不会出现错误。为什么不能从函数生成N?
我得到的错误是 --- 使用未声明的标识符 'N'
void cough(int N);
int main(void)
{
cough(N);
}
void cough(int N)
{
N = get_int("How many coughs?\n");
for (int i = 0; i < N; i++)
{
printf("cough\n");
}
}
感谢您阅读长篇大论并提供指导。
你可以试试这个,但我没用get_int()
void cough(int N);
int main(void)
{
int N = 0;
scanf( "%d", &N );
cough(N);
}
void cough(int N)
{
printf("How many coughs?\n");
for (int i = 0; i < N; i++)
{
printf("cough\n");
}
}
当您用 C 语言编写程序时,您只能使用之前定义过的标识符...或者,当然,在您定义它们时。
所以让我们逐行处理您的代码
void cough(int N);
void
已定义:语言关键字
cough
此处(暂定)定义
int
已定义:一个语言关键字
N
在这里(暂定)定义它。此定义在下一个右括号之前有效,因为它是参数定义
int main(void) {
main
正在此处定义
cough(N);
cough
之前(暂定)定义过,所以没关系:编译器知道它是一个函数,之前的暂定定义和实际定义不能改变它。
N
糟糕!!!之前未定义,不是关键字,编译器因错误而停止:)
您可以定义 cough()
不带任何参数,但是
#include <stdio.h>
#include "cs50.h"
int cough(void);
int main(void)
{
cough();
}
void cough(void)
{
int N = get_int("How many coughs?\n");
for (int i = 0; i < N; i++)
{
printf("cough\n");
}
}
I am only wondering why can't I successfully use below code in C without getting an error.
use of undeclared identifier 'N'
因为在
int main(void)
{
cough(N);
}
N在main中没有定义为局部变量,也不是全局变量,所以你不能编译你的代码
but should be gotten when program starts with a prompt e.g "How many coughs?"
所以
N = get_int("How many coughs?\n");
必须移动到main
你终于想要这样的东西了:
void cough(int N);
int main(void)
{
cough(get_int("How many coughs?\n"));
return 0;
}
void cough(int N)
{
for (int i = 0; i < N; i++)
{
printf("cough\n");
}
}
请注意,您的 printf
打印了一个常量字符串,因此您可以将其替换为 puts("cough");
,这会更快一些,因为 printf
在这种情况下搜索 '%' 是空的
您需要在main
中定义N
(顺便说一句,它完全独立于cough
中的N
。例如:
int main(void)
{
int N = 5;
cough(N);
{
作为替代方案,您可以使用 cough(5)
或将 N
定义为全局。