C Programming Math Functions #include <stdlib.h> abs() [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I start learing C programming from "Beginning Programming with C For Dummies" by Dan Gookin.
I have a problem with understanding "C Math Functions" - my question is how to use #include <stdlib.h> and abs() function. Only explanation in the book is this:

Here is a simple example:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i = -42;
int j = abs(i);
printf("i = %d, j = %d\n", i, j);
return 0;
}
LIVE DEMO

Besides the examples given above,functions work just like a mathematical function
in that y = f(x) you put in x and it returns a y(of course some dont return anything and some dont take in anything)
now what you need to do is to catch that value when it returns it by storing it into some memory location which is called a variable that you declare
its also important that you know what the return type is so that you dont truncate or lose some part of the result
for example if the result is a floating point number then you need to return the value into a float/double variable and if it is a integer you can store it in either int or double/float
also if it is a char you would probably want it to be returned to a char variable and so on and so forth
So any function that you write or that you use from somebody elses library/header is going to work like that
It takes in an argument(sometimes it can even take no arguments and you just use it by calling it with parentheses, parentheses must always be typed because otherwise it will look like a variable or something else and convention says so) and it returns some result(if it does return a result) or does something else unrelated to the calling functions variables/values
So hopefully that explains what functions are, and how you can use them
Now all of this described also applies to the functions you posted about, they have a set algorithm that they do which somebody else wrote, and you just give it the argument, and catch the return type and it will do what it says it intended to do i.e abs() gives you the absolute value, pow() returns the square of some base and so on

The line #include <stdlib.h> gives your code access to certain functions and abilities that are within that library. One of which is the abs() function.
The abs() function return the absolute value of an integer, by that we mean it will always convert it to a positive number.
Example:
#include <stdio.h> /* printf */
#include <stdlib.h> /* abs */
int main ()
{
int n,m;
n=abs(23);
m=abs(-11);
printf ("n=%d\n",n);
printf ("m=%d\n",m);
return 0;
}
Edit & Run
Output:
n=23
m=11

Related

Write a function receives two arguments and returns the difference [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Write a C-program that calls a function minus(). This function receives two arguments and returns the difference (regular subtraction, not absolute). This difference should be printed on screen.
My code is:
int minus(int a,int b)
{
int c = a - b;
return c;
}
int main()
{
int a = 4; int b = 5;
minus(a,b);
printf("%d", minus);
return 0;
}
I have two questions:
1.why a and b in
int minus(int a,int b)
are grey in Visual Studio? "int" is blue but a and b are grey.
2. I got this result but it should be -1.
Could someone help me please
a and b are grey because the editor is automatically coloring the code to help illustrate the program syntax. This may look funny at first, but, over time, your brain will become accustomed to it, and things that are the wrong colors will stand out. This will help you find mistakes in your program—when you make a mistake typing, something that should be a keyword will be colored like a parameter name, and you may notice it is the wrong color and take a closer look at what you typed.
In printf("%d", minus);, the minus is just the function. It is not the value returned by the function. To print the value returned by the function, use printf("%d", minus(a, b));.
You have undefined behavior here printing the function pointer using %d format specifier. (You have used the wrong format specifier that's why the Undefined behavior).
And the most probable way you would like is to printf("%d", minus(a,b));. You wanted to print the result of the subtraction not the function pointer itself.
You are missing an assignment
int minus(int a,int b)
{
int c = a - b;
return c;
}
int main()
{
int a = 4; int b = 5;
int d = minus(a,b);
printf("%d", d);
return 0;
}
you called the function minus() but you did not take the value returned by function in any variable so when you try to print minus then it will return the pointer value of function. so to get the correct answer hold the return value in variable and then print it int c = minus(a,b);
printf("%d", c); or you can call the minus function inside the print function like this printf("%d", minus()); as a beginner i will suggest you to implement the first suggestion it will increase the capacity to use statements in c.

C uninitialized int has a value of 1 instead of 0 [duplicate]

This question already has answers here:
Initializing variables in C
(10 answers)
Closed 6 years ago.
#include <stdio.h>
#include <string.h>
#include "prac.h"
#define MYNAME "Butter"
int main() {
int numberOfKids;
int weight;
int shirt;
printf("If I eat a Watermelon I will weigh %d lbs \n", weight + numberOfKids+ shirt );
return 0;
}
I compiled and ran the program and the result was 1; although I expected it to be 0. When I checked the value of each variable individually, the weight variable's value was 1. Can someone explain why that specific variables result was not 0? I am new to C and want to experiment with the basics to get a deeper understanding of the nuances of C. Any help would be appreciated.
Variables inside a function in C are not guaranteed to be set to anything by default. In memory, whatever was last stored there (which might not be flushed/erased to be 0) will be what the int is initialized to.
This is answered in Initializing variables in C
EDIT: As chux has stated below, local static variables are initialized to 0 if they aren't given an initial value. Also covered Is un-initialized integer always default to 0 in c?

Code output explanation [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Question 1.
#include <stdio.h>
int main(void)
{
int c;
while((c=getchar())!='\0')
{
putchar(c);
}
}
Input
Hello C.
Tell me about you.
Output
Hello C.
Tell me about you.
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
and it continues with status-time limit exceeded.
Question 2.
#include <stdio.h>
int main(void)
{
float a;
a=46.43253;
printf("\n%d",a);
printf("\n%f",a);
return 0;
}
Output
536870912
46.432529
Output- 536870912
46.432529
In general using incorrect format specifier triggers undefined behavior - which is what you have when you use %d in printf for printing float. In this case, you can expect any output usually.
However, it may also be the case that since you have specified to read the float number as integer (e.g. by using %d specifier), it simply interpreted the result as integer - hence the strange number (since floats and integers are stored differently).
If you are interested why the second printf prints a number slightly different from yours, this may help you.
This block is fine:
float a;
a=46.43253;
This block is also fine:
printf("\n%f",a);
The problem is with this block:
printf("\n%d",a);
Particularly this part:
"\n%d"
Please keep in mind you declared a float and using the integer syntax to output it. That's why you are getting the negative output
If it is a case where you don't want to change the "%d," then simply cast it as a float before output

Unclear about return value of a void function in C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
The following C program has a function test(), which despite having no return statement, returns either:
The number of characters in the format of printf()
The ASCII value of the character, if a single character is present in the format of printf()
Value of one of its arguments for printf("").
How can a function without a return statement return a value and why the variability in results?
#include <stdio.h>
#include <stdlib.h>
int test(int, int);
int main(int argc, char **argv)
{
printf("\n%d\n", argc);
if (argc > 1)
{
printf("\n%d\n", test(atoi(*++argv), 2));
printf("\n%d\n", test(2, atoi(*argv)));
}
return 0;
}
int test(int a, int b)
{
printf("0");
}
The result in this situation is based on whats on top of the stack when test() is returned. The behaviour is undefined.
You should compile your code with warnings enabled:
gcc main.c -Wall
Also, altering your argv pointer is a bit dirty. De-referencing argv directly communicates your intentions in a clear way:
printf("\n%d\n", test(atoi(*++argv), 2));
printf("\n%d\n", test(2, atoi(*argv)));
Should be:
printf("\n%d\n", test( atoi(argv[1]), 2) );
printf("\n%d\n", test( 2, atoi(argv[1]) ) );
The way values are returned from a function is different in different architectures. In most of them, it is the value of some CPU register. If you don't specifically use return, the content of that register is what the compiler "sees" as the return value of the function. Sometimes, it is the return value of the last function called, but sometimes it can be other things. The behaviour is undefined.
you should look at this slideshare page 22
Values are tossed around in registers.. and that makes this behaviour undefined.

Write recursive function that prints 1 and n zeroes in C [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have trouble writing a recursive function in C:
void func(int n)
which for a given number n prints "1" and n zeroes after it.
for example:
func(3);
prints: 1000
func(5);
prints: 100000
No global variables (outside the function) are allowed and argument count must not be increased. No other helper functions allowed.
Just some pointers:
Why do you think you must print "1" only on the first call of the function?
Why can't you print it on the last call of the recursive function and then print all the 0's?
How will you determine if a call to the function is the last call in the recursive way? (Can the value of "n" hold the answer?)
I would suggest you look into static variables. you could set a static variable to tell if your function is being called recursively or not. If you're not allowed to use global variables, it's just a tricky work-around. And when your recursion is done, just set it back for the next call.
You can solve this problem using two functions, for example func and func_rec. func will print the 1 and it will once call the recursive function func_rec which will print only zeros (recursively).
#include <stdio.h>
void func(int k){
if(k==0){
printf("1");
return;
}
func(k-1);
printf("0");
}
int main(){
func(3);
return 0;
}
#include <stdio.h>
void func(int n)
{
static one = 1;
if (one == 1){
printf("1");
one--;
func(n - 1);
}
else {
if (n < 0){
printf("\n");
return;
}
else {
printf("0");
func(n - 1);
}
}
}
int main() {
func(5);
return 0;
}
The code is very trivial so, even it seems that its a homework, i'm giving out the code.
Your earlier question suggests that you are not so much into c coding. What i'm trying to do is to reduce your frustration by giving out the code, but with a twist.
Please answer us why the code i've given is not a "pure" function? why this solution should be discarded with ones which have global storage.
You should learn this stuff, its pretty interesting :)

Resources