0

I am trying to print the value of a local variable inside of main, from another function without using global variables. What would be the best way to do so?

#include <stdio.h>

int function1();

int main(void) {
  int hello=10;
  printf(function1());
}

int function1(int ip){
  printf("hello%d",ip); 
}

I am expecting the 10 to be printed next to the "hello" but instead get a 0.

3
  • 3
    You didn't pass a value to function1. Commented Nov 8, 2022 at 23:47
  • 2
    You forgot to return a value from function1(). Commented Nov 9, 2022 at 0:05
  • printf(printf(...)) really doesn't make any sense. Are you thinking of printf() operating on a buffer populated by sprintf? Commented Nov 9, 2022 at 0:15

3 Answers 3

3

You need to call the function passing the value (or variable) required.

int function1(int);

int main(void) 
{
  int hello=10;
  function1(hello);
  function1(130);
}

int function1(int ip)
{
  return printf("hello - %d\n",ip); 
}

https://godbolt.org/z/97o3dPWzj

Sign up to request clarification or add additional context in comments.

Comments

2

change

int function1();

to

int function1(int);

and

 printf(function1());

to

 function1(hello);

Comments

0

"I am expecting the 10 to be printed next to the "hello" but instead get a 0."

There are already examples of working versions of your code in the other answers, but little is said in the way of explanation. I will try to do that here, and offer some general suggestions.

Although your code compiles, it does so with warnings and when run, produces run-time errors. The information resulting from both of these, if heeded would likely provide a guide to addressing most of your issues. (If using GCC for example, you can set warnings to -Wall). To illustrate, here is an image showing line numbers and the warning that correspond with them on my system (not GCC, but warning are turned on)

enter image description here

Build warnings:
enter image description here
And finally, run-time error:
enter image description here

From these you can begin to derive some of the following observations (others listed are my own.)

  • The signature of a function prototype must match its implementation. (for function1, they do not)
  • If the prototype of a function is non-void, then it should return a value.
  • If a function returns a value, its return value should not be ignored.
  • The warning of an unused variable pointed out that you probably intended to use it as an argument here: function1()

See comments below for other clarifications:

int function1();//this prototype is incomplete, and is inconsistent
                //with the signature of its implementation

int main(void) {
  int hello=10;
  printf(function1());//because function1 is not returning a value, your code outputs a 0
                      //Function1 prototype requires it to pass an argument
  // non-void function should return a value here
}

int function1(int ip){//signature of this implementation disagrees with its prototype
                      //because there is no value passed, ip can be any value 
  printf("hello%d",ip); 
  // non-void function should return a value here
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.