How can i read a number in c with a function and use it in main?

Gradin98

i try to read a number from keyboard with a function, but it doesn't work. This is the code

#include <stdio.h>
#include <stdlib.h>

void write(int *n);

int main()
{
    int *n,*m,p;
    write(&n);
    write(&m);
    p = *n + *m;

    printf("p = %d",p);
}

void write(int *n)
{
    scanf("%d",&n);
}
user3629249

applying all the comments to the posted code and following the axiom:

only one statement per line and (at most) one variable declaration per statement.

and checking the returned value from scanf() to assure the operation was successful

results in:

#include <stdio.h> // scanf(), printf(), fprintf()
#include <stdlib.h> // exit(), EXIT_FAILURE

void getInteger(int *n);

int main( void )
{
    int n;
    int m;
    int p;
    getInteger(&n);
    getInteger(&m);
    p = n + m;

    printf("p = %d",p);
}

void getInteger(int *n)
{
    if( 1 != scanf("%d",n) )
    {
        fprintf( stderr, "scanf failed" );
        exit( EXIT_FAILURE );
    }
}

and when run with an input of: '1 3' the output is

p = 4

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I get fout to work in a function that is not main?

From Dev

How can I create a PowerShell "main" function?

From Dev

How can I use stringstream as a parameter for a c++ function?

From Dev

how can I use c empty parentheses function, in c++?

From Dev

How can I read a number in between 2 numbers?

From Dev

How can I read this C code?

From Dev

How do I have the main function use another function?

From Dev

How can I main use other codes?

From Dev

How can I use a list in Where of lambda function c#?

From Dev

How can I use Google Test to call specific test functions at specific places in the main() function?

From Dev

How can I use two sockets in the main using Netty

From Dev

How can I use Angular service function?It throws Error:"Cannot read property of undefined"

From Dev

how i can get 'argc' and 'argv' from main function to QGLWidget

From Dev

How can I read an hex number with dlmread?

From Dev

How can I use map function with random number

From Dev

In a C++ function how do I read from an input file that was opened in main()?

From Dev

how can I use c empty parentheses function, in c++?

From Dev

how can I read a number with file (magic)?

From Dev

How can I read this C code?

From Dev

In Excel, how can I use a function to split an unknown number of terms into different cells on a separate sheet?

From Dev

How can I pass a struct and an int from an function and back to main? C

From Dev

How can I use pointers received from a function correctly? (in C)

From Dev

How can I get the number of bytes a C function is compiled to?

From Dev

How can I go to the beginning of main function from a sub function in C programing language?

From Dev

How can I use operator overloading function with a class in C++?

From Dev

How can I use a pointer function to write/read an array

From Dev

How can I use one function from main application and bootloader? (embedded)

From Dev

sublime text 3 C compiler (how can i use functions with header file in main.c)

From Dev

c++ : how can I call a method in main function without creating an object

Related Related

  1. 1

    How can I get fout to work in a function that is not main?

  2. 2

    How can I create a PowerShell "main" function?

  3. 3

    How can I use stringstream as a parameter for a c++ function?

  4. 4

    how can I use c empty parentheses function, in c++?

  5. 5

    How can I read a number in between 2 numbers?

  6. 6

    How can I read this C code?

  7. 7

    How do I have the main function use another function?

  8. 8

    How can I main use other codes?

  9. 9

    How can I use a list in Where of lambda function c#?

  10. 10

    How can I use Google Test to call specific test functions at specific places in the main() function?

  11. 11

    How can I use two sockets in the main using Netty

  12. 12

    How can I use Angular service function?It throws Error:"Cannot read property of undefined"

  13. 13

    how i can get 'argc' and 'argv' from main function to QGLWidget

  14. 14

    How can I read an hex number with dlmread?

  15. 15

    How can I use map function with random number

  16. 16

    In a C++ function how do I read from an input file that was opened in main()?

  17. 17

    how can I use c empty parentheses function, in c++?

  18. 18

    how can I read a number with file (magic)?

  19. 19

    How can I read this C code?

  20. 20

    In Excel, how can I use a function to split an unknown number of terms into different cells on a separate sheet?

  21. 21

    How can I pass a struct and an int from an function and back to main? C

  22. 22

    How can I use pointers received from a function correctly? (in C)

  23. 23

    How can I get the number of bytes a C function is compiled to?

  24. 24

    How can I go to the beginning of main function from a sub function in C programing language?

  25. 25

    How can I use operator overloading function with a class in C++?

  26. 26

    How can I use a pointer function to write/read an array

  27. 27

    How can I use one function from main application and bootloader? (embedded)

  28. 28

    sublime text 3 C compiler (how can i use functions with header file in main.c)

  29. 29

    c++ : how can I call a method in main function without creating an object

HotTag

Archive