Simple cube and square sum function won't run (C) [duplicate] - c

This question already has answers here:
What will happen if '&' is not put in a 'scanf' statement?
(6 answers)
Closed 4 years ago.
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?

scanf needs a pointer:
scanf("%d",&a);
instead of
scanf("%d",a);

int a;
scanf("%d",a);
^
must be &a. d conversion specifier for scanf expects a pointer to int argument.

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

You have not given address of "a" in scanf function.
Please use:
scanf("%d", &a);

Related

The different statements cause the type conversion to be different. c/gcc [duplicate]

This question already has answers here:
Unspecified number of parameters in C functions - void foo()
(5 answers)
Closed 1 year ago.
If I add "double" into statement then it will print 60.00
else the code will print 0.0000..
#include <stdio.h>
int main(void)
{
void foo();// if i add "double" into statement then will printf 60.00
char c = 60;
foo(c);
return 0;
}
void foo(double d)
{
printf("%f\n", d);//now printf 0.0000
}
Yor code invokes Undefined Behaviour as the caller does not know the parameters of foo. Your prototype says that the function will take an unspecified number of parameters. It does not say anything about the types of those parameters. So it does the implicit conversion to int and passes int to foo.
foo expects the double parameter.
What to do?
Add the function prototype befoe the function call.
void foo(double);
int main(void)
{
char c = 60;
foo(c);
return 0;
}
void foo(double d)
{
printf("%f\n", d);//now printf 60.0000
}

C - Scanf straight into an function argument?

So, if I have the function foo:
float foo(float a, float b){
return a*b;
}
and I call it from another function, how can I call it like this?
void main(){
foo(scanf("%d"), scanf("%d");
}
scanf doesn't return the input string, I don't want to create a bunch of temp variables. Is that possible, and if so how?
You can't directly return a value from scanf. But you can create a function yourself that returns a value from scanf, but it'll still use temp variables inside of it:
int getInteger() {
int input;
scanf("%d", &input);
return input;
}
Then you can use it like:
foo(getInteger(),getInteger());
However, if you really don't wanna use variables, you can just use get_int() from cs50 library. More info here.
#include <cs50.h> // include cs50 to use get_int
int main(void)
{
foo(get_int("Number 1: "),get_int("Number 2:"));
return 0;
}

Why this failed when I use a function to malloc memory [duplicate]

This question already has answers here:
Using Double Pointers after memory allocated within function
(3 answers)
Closed 7 years ago.
#include <stdio.h>
#include <stdlib.h>
char** mlc(char** f){
int count=10;
int size=10;
f=(char**)malloc(count*sizeof(char*));
for(int i=0;i<count;i++){
f[i]=(char*)malloc(size*sizeof(char));
}
return f;
}
int main()
{
char** f;
f=mlc(f);
f[0][0]='1';
f[0][1]='\0';
printf("%s",f[0]);
return 0;
}
I use this code can work perfectly ,But when I use the following code ,It will get segmentation fault:
#include <stdio.h>
#include <stdlib.h>
void mlc(char** f){
int count=10;
int size=10;
f=(char**)malloc(count*sizeof(char*));
for(int i=0;i<count;i++){
f[i]=(char*)malloc(size*sizeof(char));
}
return f;
}
int main()
{
char** f;
mlc(f);
f[0][0]='1';
f[0][1]='\0';
printf("%s",f[0]);
return 0;
}
So ,the main difference is the first code I return the pointer, why the second code get fault?
In C, function arguments are passed by value. So, from inside the function, you cannot change the value of a parameter and expect the change to be reflected onto the variable passed in by the caller.
In simple words, in your case, from inside mlc(), you can change the value of *f, and that will be reflected onto the *f in main(), but you cannot change the value of f itself.
You first case works, because after allocating memory and pointing the parameter f at it, you return said pointer and retrieve it into f in main(). So, in main(), f is perfectly valid for usage.
Your second case fails, because after returning from the function, f in main() remains uninitialised.

Why the following code gives "hello5" as output in C? [duplicate]

This question already has answers here:
what is the return type of printf [closed]
(2 answers)
Closed 7 years ago.
Follwing code in C gives the output "hello5" ...how?
#include<stdio.h>
int main(){
int f = fun();
printf("%d",f);
return 0;
}
void fun(){
printf("hello");
}
What you see is undefined behavior.
The value f is never initialized and you are printing uninitialized variable which will lead to undefined behavior.
printf() returns number of characters successfully printed out so the count here is 5(hello) for printing hello.
You need to return this value if you want then you have defined behavior because you are initializing the variable f in main()
int func()
{
int j;
j = printf("hello");
return j;
}

float with array of structures? [duplicate]

This question already has answers here:
scanf: floating point format not linked [duplicate]
(3 answers)
Closed 8 years ago.
I am getting a runtime error while running the following code.
#include<stdio.h>
int main()
{
struct emp
{
char name[20];
float sal;
};
struct emp e[10];
int i;
for(i=0; i<=9; i++)
scanf("%s %f", e[i].name, &e[i].sal);
return 0;
}
Runtime error is floating point formats not linked.
some body please help me to run this program.
Add the following anywhere outside main after all the includes.
void dummy(float *a) {
float b=*a; //perform some floating access
dummy (&b); //calling a floating point function
}
You need not call this function

Resources