How should I continue this code? What keyword should I use for the 2nd part of this assignment?

StackOverflow https://stackoverflow.com/questions/21391987

  •  03-10-2022
  •  | 
  •  

سؤال

I am writing a code that initially displays my credentials then I am supposed to prompt the user to enter values for certain variables. I do not know how to finish the second part of the program. I don't know whether to use a specific keyword for starting the second part of the program. I tried to attempt to use (int main(void)) again after the } but my program failed. What do I use instead of (int main(void))?

#include <stdio.h>

int main(void)

{
    printf("name\n");
    printf("email\n");
    return 0;
}

int main(void)

{
    printf(Enter value for a(in feet): \n");
    scanf(%f\n");
    return 0;
}
هل كانت مفيدة؟

المحلول

Look up functions in C. In fact reading the first few pages of a C tutorial will take you a long way. In short a function does some operation then returns the result or it may not return anything. So lets break down what you've got and what it does.

When you write int main(void) you're telling the compiler that you have a function that returns an integer (that's the int part). The name of this function is main. And you do not need to provide it anything to get started (void). You can't have more than one function of the same name because the compiler doesn't know which to use. main is a special function that is called automatically when the program runs.

To prompt a user to input a value, you need to print the prompt (which you almost did with printf(Enter value for a(in feet): \n"); except you forgot " before Enter). Then you need to read the user's input from the keyboard. In C, the keyboard input comes from a File Descriptor known as stdin. To read from stdin, one option is to use fscanf as in this example. That should get you going. Good luck and happy reading.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top