简体   繁体   中英

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)

I am still new to programming and very very new to C programming, so please pardon me if my question seems ridiculous.

I am learning about functions and function calls and I am trying to follow what the instructor is doing but with a little addition. The instructor did the following:

  • The instructor defined a function cough -- (void cough(int N))
  • Called the function in main using only one line of code
  • The program prints out cough a hard coded number of times on screen ie cough(3) will print cough 3 times.

I am trying to:

  • Define a function cough -- (void cough(int))
  • Call the function in main using only one line of code
  • The program should print out cough N number of times on screen, but should be gotten when program starts with a prompt eg "How many coughs?"

The C(c99) I am using has a library cs50.h (from online course cs50), that provides a code to get integer input. After searching and trying I have successfully done as below:

After including the cs50 library int N = get_int("How many coughs") cough(N)

And also cough(get_int("How many coughs"))

I am only wondering why can't I successfully use below code in C without getting an error. Why can't N be generated from the function?

The error I got is --- use of undeclared identifier '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");
    }
}

Thank you for reading the long story and providing guidance.

I am only wondering why can't I successfully use below code in C without getting an error.

use of undeclared identifier 'N'

because in

int main(void)
{
    cough(N);
}

N is not defined in main as a local variable, and it is not a global variable too, so you cannot compile your code

but should be gotten when program starts with a prompt eg "How many coughs?"

so

N = get_int("How many coughs?\n");

must be moved in main

Finally you want something like that:

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");
    }
}

Note your printf prints a constant string, so you can replace it by puts("cough"); which is a little faster because printf search for '%' for nothing in that case

You can try this, but I didn't use 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");
    }
}

When you write a program in C, you can only identifiers that have been defined previously... or, of course, when you are defining them.

So let's work with your code line by line

void cough(int N);

void already defined: a language keyword
cough it's being (tentatively) defined here
int already defined: a language keyword N it's being (tentatively) defined here. This definition is valid until the next close parenthesis because it's a parameter definition

int main(void) {

main is being defined here

    cough(N);

cough was (tentatively) defined before, so it's OK: compiler knows it is a function, the previous tentative definition and the actual definition cannot change that. N OOPS,,: not defined before, not a keyword, compiler stops with error :)


You could define cough() to take no arguments, though

#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");
    }
}

You need to define N in main (which, by the way, is totally independent of N in cough . For example:

int main(void)
{
    int N = 5;
    cough(N);
{

As an alternative, you can use cough(5) or define N to be global.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM