atof reads string as 0 - c

I suppose I am just stupid (I tried all). Help me out of here:
#include <stdio.h>
#include <string.h>
int main()
{
const char * y = "747.0";
double x;
x = atof(y);
printf("%.5f\n",x);
printf("%e\n",x);
return 0;
}
result:
0.00000
0.000000e+00

You need to include stdlib.h to offer atof() a prototype, without proper prototype, compiler will suppose its return value be an int.

Try including stdlib.h because I think that is the package you need for atoi in C.

Related

How do i tell if an output an integer or not in C

i Have this homework about a superhero triangle, from what i got if the Area an integer data, then it's a superhero triangle, if it's not then it's not a superhero triangle.
The problem is, how do i determine if the output an integer or not
Thank you
Note : English is not my main language, sorry for the lack of vocabulary to describe it
Well I believe your calculation is all with doubles, because you are not sure of the result maybe an int maybe double. but you just need to know if it's really a double or an int, in your case 2.0 is an int of course, so I made this simple example for you
#include <iostream>
int isFloat(double n) {
return n - int(n) > 0;
}
int main(void) {
double x = 2.1;
if(isFloat(x)) {
std::cout << "it's a float";
}else {
std::cout << "it's an int";
}
return 0;
}
output
it's a float
Assuming you are just trying to check if a number is an integer (not necessarily the type but just that it has no decimal values... ex: 5.00 is an integer)
You could use the math.h standard library like this:
#include <stdio.h>
#include <math.h>
int main()
{
double output_value = 1.27;
if(fmod(output_value, 1.00) != 0)
//This is not an integer
else
//This is an integer
}
Clearly, if a number is not evenly divisible by 1, it is not an integer.
This seems like the simplest solution to me.

c variable turning into -17918 when incremented

hi i am making a programming language that will run on the nintendo gameboy in c
which is why you will see some functions like waitpad();
but this question is unrelated the the gameboy librarys
for some reason when ever i try to increment a certain variable in my main.c file:
#include <stdio.h>
#include <gb/gb.h>
#include "convert.h"
#include "display.h"
#include "input.h"
#include "functions.h"
#include "interpreter.h"
unsigned char cnt[5] = {1,2,3,4,5};//cnt is short for counters
unsigned char k = 0;
unsigned char running = 1;
unsigned char memory[2048];
unsigned char code[2048];
int main()
{
Clear_mem();
Clear_code();
while(running == 1) {
display(cnt[0],cnt[1],cnt[2],cnt[3],cnt[4]);
printf("cnt[0] = %d\n", cnt[0]);
cnt[0]++;//turns into -17918
printf("Press Start To Enter Next Character\n");
waitpad(J_START);
code[k] = input();
interpret(code[k]);
k++;
}
return 0;
}
cnt[0] turns into -17918
can anyone see any problem that would cause it to behave this way?
You ask if anyone sees a problem, well - yes, here is a problem:
unsigned char k = 0;
unsigned char running = 1;
unsigned char code[2048];
while(running == 1) {
code[k] = input();
k++;
}
If k >= 2048, then code[k] = ... will cause a memory-override.
After a memory-override, pretty much anything can happens (undefined behavior).
Having said that, the value of k can be larger than 2047 only if CHAR_BIT is larger than 11.
Add #include <limits.h> to your program and make sure that CHAR_BIT is not larger than 11.
You have to convert it to an integer, because that's what you're trying to print:
printf("cnt[0] = %d\n", (int) cnt[0]);
When you're using a variadic function like printf, you have to make sure you're passing the right type. Check your compiler warning settings, new compilers can easily detect these kind of problems.
if you want to print the character value of your character variable you should print it like this:
printf("cnt[0] = %c\n", cnt[0]);
If you print it using %d the expansion of the character to a size of int could be negative for characters over half a character's size (0x80 and up).
If you insist on printing it as an int cast the variable like this:
printf("cnt[0] = %d\n", (int)cnt[0]);

Why is printf not using scientific notation?

I understand that this is a common problem. However I can't find a solid straight answer.
16 ^ 54 = 1.0531229167e+65 (this is the result I want)
When I use pow(16,54), I get:
105312291668557186697918027683670432318895095400549111254310977536.0
Code is as follows:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
void main(){
double public;
double a = 16;
double b = 54;
public = (pow(a,b));
printf("%.21f\n", public);
}
Code executed with:
gcc main.c -lm
What I'm doing wrong?
What am I doing wrong?
Several things:
Use %.10e format for scientific notation with printf for a printout with ten digits after the dot,
Return an int from your main,
Consider not using public to name a variable, on the chance that your program would need to be ported to C++, where public is a keyword.
Here is how you can fix your program:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main(){
double p;
double a = 16;
double b = 54;
p = (pow(a,b));
printf("%.10e\n", p);
return 0;
}
Demo on ideone.
Have you tried:
printf("%e\n", public);
The %e specifier is for scientific notation, as described in the documentation
If you need scientific notation you need to use the %e format specifier:
printf("%e\n", public);
^^
Also, public is a keyword in C++ and so it would be a good idea to avoid that and any other keywords in case this code needs to be portable.

What is the use of * in printf?

I have a this code:
#include <stdio.h>
#include <conio.h>
void main()
{
int n = 5;
clrscr();
printf("n=%*d", n);
getch();
}
The output which I got is: n= 5. Why is there a space? How is it generated? What is the use of * in the code?
When in doubt, read the docs:
*:
The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.
However, you appear to be using it wrong. The proper way to use it would be like this:
printf("n=%*d", 2, n);
It is clearly mentioned in the C Manual.
The answer is already given by Richard J. Ross III. Just quoting again what is said from the manual.
The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.
Consider this code:
#include<stdio.h>
main()
{
int a,b;
float c,d;
a = 15;
b = a / 2;
printf("%d\n",b);
printf("%3d\n",b);
printf("%03d\n",b);
c = 15.3;
d = c / 3;
printf("%3.2f\n",d);
}
The output would be:
7
7
007
5.10
You can see here, how the printf function can be used for formatting output. Hope it helps. :)
With this *, you can set the width of your print with a variable.

strcspn() stopping at a period

I'm writing a function that should parse a string containing a description of a dice roll, for instance "2*1d8+2". I extract the four values OK when they are integers, but I want to be able to use floats as well for the multiplier and the addition at the end. Things get nasty when I try to parse such a string: "1.8*1d8+2.5".
I have determined that the problem is with the function strcspn. I ask it to parse the input string s (which contains the dice string) and stop at either an asterisk or an 'x':
const char * s = "1.8*1d8+2.5";
size_t l = strcspn(s,"*x");
The function should return 3, as the asterisk is at the 4th position. However, it seems to stop on the decimal separator (period) and returns 1.
It's not that I can't continue writing my function without this, as there are other ways to get things done, but still I'm curious why such a thing would happen. Has anyone ever encountered this problem before?
[EDIT]
Nevermind, I've found the answer, and it was my stupidity rather than the compiler playing tricks on me. I used this code:
if (l = strcspn(s,"*x") < strlen(s)) {
...
which returned 1 (or true) because strcspn(s,"*x") < strlen(s) evaluates to true - and was assigned to the l variable. I should have added parentheses:
if ((l = strcspn(s,"*x")) < strlen(s)) {
...
Thanks for your answers nonetheless, particularly #sleske, who made me analyse my code more deeply (which led to finding the answer).
There must be an error somewhere else. I wrote a test program:
#include <stdio.h>
#include <string.h>
void main(){
const char * s = "1.8*1d8+2.5";
size_t l = strcspn(s,"*x");
printf("l: %d\n", l);
}
and compiled it with gcc on Linux. On my system, it outputs "3".
Please post a complete, working example that exhibits the problem. Then we'll see...
Your code works just as you posted it for me1.
#include <stdio.h>
#include <string.h>
int main(void) {
const char * s = "1.8*1d8+2.5";
size_t l = strcspn(s,"*x");
printf("%zd (%.*s)\n", l, (int)l, s);
return 0;
}
so ross$ ./a.out
3 (1.8)
1. Mac OS X 10.6.4
I'd use sscanf instead of manually finding stop points and parsing myself. You can get your integers and floats easily out of the string with that.
This code:
#include <stdio.h>
#include <string.h>
int main(void)
{
const char * s = "1.8*1d8+2.5";
size_t l = strcspn(s,"*x");
printf("<<%s>> %zd <<%s>>\n", s, l, s+l);
return 0;
}
produces this answer on MacOS 10.6.4 (GCC 4.5.1):
<<1.8*1d8+2.5>> 3 <<*1d8+2.5>>
If your compiler and library does not produce the same answer, get a fixed/upgraded version of the software.
(NB: The 'z' modifier in the printf() format string is a C99 feature - it indicates that the type of the parameter is size_t.)

Resources