0

Learning C, trying to code a program that outputs the sum of the cube and square of an inputted number.

#include <stdio.h>

main()
{
int a;
scanf("%d",a);
printf("%d",cube(a)+sqr(a));
}

cube(int x)
{
return(x*x*x);
}

sqr(int arg)
{
return(arg*arg);
}

When I run the program it outputs some seemingly random string of numbers after I input a number. Any way to fix it without changing the usage of returns to assign variables?

Cœur
  • 37,241
  • 25
  • 195
  • 267
freshc
  • 3
  • 1
  • 4

4 Answers4

5

scanf needs a pointer:

scanf("%d",&a);

instead of

scanf("%d",a);
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
hivert
  • 10,579
  • 3
  • 31
  • 56
  • Thanks lol was such a simple mistake – freshc Jul 23 '13 at 10:03
  • 2
    Your compiler should have warned you about this one. Are you compiling with ``-Wall`` (gcc/linux) ? – hivert Jul 23 '13 at 10:05
  • don't know what that means.. I'm using codeblocks and it ran the program but crashed after input – freshc Jul 23 '13 at 10:09
  • 2
    In "project -> Build options -> Compiler settings -> Compiler flags" you should check "enable all compiler warnings" and pay attention to those warnings. – hivert Jul 23 '13 at 10:13
4
int a;
scanf("%d",a);

          ^

must be &a. d conversion specifier for scanf expects a pointer to int argument.

ouah
  • 142,963
  • 15
  • 272
  • 331
0

You need to define a return type to your functions !

#include <stdio.h>

int main()
{
  int a;
  scanf("%d",a);
  printf("%d",cube(a)+sqr(a));
  return 0;
}

int cube(int x)
{
  return(x*x*x);
}

int sqr(int arg)
{
  return(arg*arg);
}
dotixx
  • 1,490
  • 12
  • 23
0

You have not given address of "a" in scanf function.

Please use:

scanf("%d", &a);
stev
  • 182
  • 1
  • 11