i am trying to write a function that repeatedly adds 0.001 to 't' and then plugs it into 'y' until 't' reaches 0.3 however the numbers come out wrong, but i've noticed that if i change float to int and change the numbers to integer, the fuction works.. what should i change so the function works properly
#include <stdio.h>
#include <math.h>
void main(void)
{
float t,y,dt;
dt = 0.001;
y = 1;
t = 0;
while (t <= 0.3)
{
y = y + dt*(sin(y)+(t)*(t)*(t));
t = t + dt;
}
printf("y is %d when t is 0.3\n" , y);
return 0;
}
i've noticed that if i change float to int and change the numbers to integer, the fuction works.. what should i change so the function works properly
as said in a remark the problem is the way you (try to) print the value in
printf("y is %d when t is 0.3\n" , y);
%d suppose the corresponding argument is an int and prints it as an int, but y is a float. Note that there is no conversion from float to int in that case because the arguments are managed through a varargs
just do
printf("y is %f when t is 0.3\n" , y);
Also change
void main(void)
to
int main()
After the changes, compilation and execution :
/tmp % gcc -pedantic -Wall -Wextra f.c -lm
/tmp % ./a.out
y is 1.273792 when t is 0.3
Note that all the calculations are done in double, so better to replace float to double to type your vars
(edit) Compiling your initial code with gcc and the option -Wall signals your problems :
/tmp % gcc -Wall f.c -lm
f.c:4: warning: return type of 'main' is not 'int'
f.c: In function 'main':
f.c:18: warning: format '%d' expects type 'int', but argument 2 has type 'double'
f.c:19: warning: 'return' with a value, in function returning void
To use both -Wall and -Wextra is the better option
Related
#include <stdio.h>
int arredonda (double x)
{
int arredondado;
if (x - (int)x >= 0.5)
arredondado = (int)x + 1;
else
arredondado = (int)x;
return arredondado;
}
int main()
{
double num;
scanf("%f", &num);
printf("%d", arredonda(num));
return 0;
}
This is a function that rounds a number to upper or to lower depending of the decimal part. The problem is that it keeps returning 0 with all values.
%lf must be used to input a double.
Make sure to enable your compiler's warnings!
a.c: In function ‘main’:
a.c:16:13: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double *’ [-Wformat=]
scanf("%f", &num);
~^ ~~~~
%lf
I use -Wall -Wextra -pedantic with gcc and clang.
As for the rounding itself, there is a better solution: round from math.h.
This is my full code, and its printing random negative values each time I run it not sure what is wrong.
using Ubuntu to run and "gcc -Wall -Wextra test.c"
#include <stdio.h>
int main () {
unsigned int x = 10;
unsigned int y = 16;
unsigned int p = x + y;
printf("%d\n", &p);
return 0;
}
You are passing the address of p. You need to pass the value.
printf("%d\n", p);
As you have it, your code is printing the address of p, whatever that happens to be.
In addition, since you are using unsigned int, you probably want to use the %u formatter insted of %d.
When I declare a variable as float and subtract two hexadecimal numbers, I keep getting different answer everytime I compile and and run it. Where as the if I declare an integer variable the result stays the same everytime I compile and run the code. I don't understand why storing the result in float changes everytime I compile with the difference of the same two numbers (0xFF0000 - 0xFF7FF)
int main()
{
float BlocksLeft = 0xFF0000 - 0xFF7FF;
int BLeft = 0xFF0000 - 0xFF7FF;
printf("%08x\n", BlocksLeft);
printf("%08x\n", BLeft);
}
The following line is incorrect:
printf("%08x\n", BlocksLeft);
%x format will indicate compiler the argument you give is an int. This lead to undefined behavior. I tried to compile your code and I got:
>gcc -Wall -Wextra -Werror -std=gnu99 -o stackoverflow.exe stackoverflow.c
stackoverflow.c: In function 'main':
stackoverflow.c:15:4: error: format '%x' expects argument of type 'unsigned int', but argument 2 has type 'double' [-Werror=format=]
printf("%08x\n", BlocksLeft);
^
Please, try to compile with stronger warning level, at least -Wall.
You can correct your program this way, for instance:
#include <stdio.h>
int main()
{
float BlocksLeft = 0xFF0000 - 0xFF7FF;
int BLeft = 0xFF0000 - 0xFF7FF;
printf("%08x\n", (int) BlocksLeft); // Works because BlocksLeft's value is non negative
// or
printf("%08x\n", (unsigned int) BlocksLeft);
// or
printf("%.8e\n", BlocksLeft);
printf("%08x\n", BLeft);
}
I'm playing around with some code in K&R just for fun but have run into an error which I can't explain myself. I'm modifying code on page 9 in section 1.2, i.e. the temperature conversion program:
#include <stdio.h>
/* converts a range of fahrenheit temperatures to celsius
and displays them in a table*/
int main(int argc, char *argv[]){
float fahr, celsius;
float lower, upper, step;
if(argc != 4){
printf("Usage: ./tempConvert lower upper step\n");
return 1;
}
// note: atof is bad?
lower = atof(argv[1]); // lower limit of temperature
upper = atof(argv[2]); // upper limit of temperature
step = atof(argv[3]); // step size
//printf("%f %f %f",lower, upper, step);
fahr = lower;
printf("F \t C \n");
while(fahr <= upper){
celsius = 5.0*(fahr-32.0)/9.0; // if this were int, 5/9=0 because int division
printf("%3.1f \t %6.1f\n", fahr, celsius);
fahr += step;
}
return 0;
}
When run, I get an infinite loop. However, when I change atof to atoi, it works perfectly fine other than the fact that I wanted float precision instead of just having integers. Printing out the values right after entering them also gives garbage instead of the numbers I entered. What is causing this difference between using atoi and atof to read in numbers?
You didn't include <stdlib.h>, so your compiler assumes that atof() returns an int, but it doesn't.
You aren't compiling with enough warnings enabled! You should insist that the compiler warns you when you call a function for which there is no prototype in scope. Note that C99 mode will warn you if there's no declaration at all for the function, but it still permits non-prototype declarations.
With GCC, I routinely use this (or -std=c11 and the other options):
gcc -g -O3 -std=c99 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes \
-Wold-style-definition -Wold-style-declaration -Werror ...
Your code would not compile under those options.
I wish to generate random numbers between 0 and 1. (Obviously, this has application elsewhere.)
My test code:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int main() {
double uR;
srand(1);
for(int i=0;i<5;i++){
uR = rand()/(RAND_MAX+1.000);
printf("%d \n", uR);
}
}
And here's the output after the code is compiled with GCC:
gcc -ansi -std=c99 -o rand randtest.c
./rand
0
-251658240
910163968
352321536
-528482304
Upon inspection, it turns out that casting the integer RAND_MAX to a double has the effect of changing its value from 2147483647 to -4194304. This occurs regardless of the method used to change RAND_MAX to type double; so far, I've tried (double)RAND_MAX and double max = RAND_MAX as well.
Why does the number's value change? How can I stop that from happening?
You can't print a double with %d. If you use %f, it works just fine.
You are printing a double value as a decimal integer - which is causing you confusion.
Use %.6f or something similar.
You are passing a double (uR) to printf when it expects a signed int. You should cast it or print with %f
printf("%d \n", (int)uR);