I have a struct that contains a float var. I am trying to read the value using a pointer to a struct. Here's the code:
struct mas {
float m;
};
int main(void)
{
struct mas *ms;
ms=(struct mas*)malloc(sizeof(struct mas));
scanf("%f",&(ms->m));
printf("%f",ms->m);
return 0;
}
But running the program produces the following error:
scanf floating point formats not linked
The compiler used is Borland Turbo C++ (3.0) on a Windows PC. Why is this so?
This might be helpful: http://www.faqs.org/faqs/msdos-programmer-faq/part2/section-5.html
From the article:
Borland's compilers try to be smart and not
link in the floating- point (f-p) library unless you need it. Alas, they
all get the decision wrong. ... (To force them to link it) define this function somewhere in a source file but don't
call it:
static void forcefloat(float *p)
{
float f = *p;
forcefloat(&f);
}
Also:
If you have Borland C++ 3.0, the README file documents a slightly less
ugly work-around. Insert these statements in your program:
extern unsigned _floatconvert;
#pragma extref _floatconvert
Why is this so..
Because there's a bug in your ancient, useless compiler. Upgrade to a newer one that properly handles floating point operations.
The latest version of GCC is a good choice, or you can download Microsoft's Visual C++ Express package for free, which bundles their compiler with a world-class IDE.
I am able to compile and run this code under GCC 4.2.1:
#include <stdlib.h>
#include <stdio.h>
struct mas{
float m;
};
int main()
{
struct mas *ms;
ms=malloc(sizeof(struct mas));
scanf("%f",&(ms->m));
printf("%f\n",ms->m);
return 0;
}
Are you missing any #include statements? I don't think you need to cast the result from malloc().
Related
I have a structure defined in quaternions.c
typedef struct quaternion{
double arr[4];
} quaternion;
with header file
#ifndef QUATERNIONS_H
#define QUATERNIONS_H
typedef struct quaternion{
double arr[4];
} quaternion;
#endif
Since this is my first time using C structures, I tried testing it out.
#include <stdio.h>
#include "quaternions.h"
int main(){
quaternion a;
a.arr[1]=2.5;
printf("%d\n",a.arr[1]);
return 0;
}
As far as I can tell, what I'm doing is almost identical to this article. However, when I compile and run this, it prints a random gibberish number. What am I doing wrong?
The correct printf conversion format specifier for a double is %f, not %d. See the documentation for that function for further information.
You should change the line
printf("%d\n",a.arr[1]);
to:
printf("%f\n",a.arr[1]);
By using the wrong conversion format specifier, your program is invoking undefined behavior. This explains why your program is printing "random gibberish" instead of the desired output.
Most good compilers will warn you if you use the wrong conversion format specifier. If your compiler doesn't warn you, then I suggest that you make sure that you have all warnings enabled. See this question for further information:
Why should I always enable compiler warnings?
my gcc (9.1.0) struggle with following structure definition
struct foo_v1 : foo_v2 { a; };
i guess it could be a matter of coding version such as ansi or c99.
reading 9.4.0 i could not find <struct a : b { c; };> notation.
please could anyone give me a hint where to find standard defining such a notation or how to compile with gcc.
have a great day
how to compile with gcc.
It is not valid C syntax. It is not possible to compile it with gcc as a C source code. It is also not a common extension in common C compilers.
It may be possible to compile the following with a C++ compiler (which makes the line compile-able after adding two lines and adding -xc++ to gcc):
struct foo_v2 { int stuff; };
#define a int variable
struct foo_v1 : foo_v2 { a; };
The presented line in the question, on its own, is not valid C++ code anyway.
I'm trying to learn how to use the GMP library in C by just writing a simple program to add some floating point numbers together, but at runtime it complains:
GNU MP: Cannot allocate memory (size=140735132293330)
Aborted (core dumped)
Here is the code:
#include <gmp.h>
#include <stdio.h>
int main(){
mpf_set_default_prec(64);
mpf_t sum;
mpf_init(sum);
mpf_set_ui(sum,0);
unsigned int i = 0;
while (i < 4) {
mpf_add_ui(sum,sum,i);
i++;
}
mpf_out_str(stdout,10,sum);
printf ("\n");
mpf_clear(sum);
}
I was able to do this with just the GMP mpz functions without issue, but when I try this with floats I'm stuck. The documentation doesn't really show any real examples for float functions so maybe I'm initializing or assigning the values incorrectly.
From the documentation,
it is a good idea to include stdio.h before gmp.h, since that will allow gmp.h to define prototypes for these functions
This way you get an error because you are calling the function with the wrong number of arguments. The reason you are not getting any warning for the lack of declaration is because mpf_out_str is a macro, defined in gmp.h which on your machine is installed in /usr/include and thus considered a system header, so the warning is disabled (use -Wsystem-headers to see it). This feels like a misfeature in gcc...
You must have not checked your compiler warnings properly, but the simple error is that you're calling mpf_out_str with the wrong number of arguments, which you can look up in the documentation:
size_t mpf_out_str (FILE *stream, int base, size_t n_digits, const mpf_t op)
// ^^^^^^^^^^^^^^^^
I'm rewriting some Mac code that embeds a freeware library originally written in C. The compiler is complaining that since I'm using long double, I should use fabsl rather than fabs. So I went and changed them.
However, reading a few pages on the topic it seems that there should be no difference, that ever since C99, there is a type generic macro that inserts the correct call based on type.
So perhaps I am using the wrong dialect?
Does anyone know what the Compiler Default is in xcode7, and whether it has the generic macro?
The generic macro is defined in <tgmath.h>, so you need to #include it, as shown in the following snippet:
#include <tgmath.h>
#include <stdio.h>
int main() {
long double ld = 3.14;
double d = 3.14;
float f = 3.14f;
printf("%Lf %lf, %f\n",fabs(ld), fabs(d), fabs(f));
return 0;
}
It compiles flawlessly with
gcc -Wall -Wextra a.c -oa -std=c99
I'm hoping someone can help me out with this. I'm a Linux & Eclipse noob, but I'm pretty familiar with C/C++, though its been a while since I've used them. When I try to compile I get strange errors. No matter what I do to fix them they don't seem to go away.
You can see the there's a simple main function with a little bit of code. There's only 15 lines of code but if you look at the errors they are in external libraries, stdio.h. In main it says there's one error at line 11 but that one doesn't make sense. I assume it's an Eclipse settings problem, but I have no idea what to do to fix it. Any help would be very appreciated. By the way I'm using SciLinux and Eclipse Indigo Service Release 2. Thanks
Code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int *ptr;
int a;
a = 20;
ptr = &a;
int b;
b = *ptr;
printf(" ptr is %d\n",b);
return 0;
}
int *ptr;
int a;
int b; //<- move to block top declaration
a = 20;
ptr = &a;
Some of the previous compiler have this weird problem relating to C they only accept variables which are declared in the beginning of the function.
So most probably the error is because you have not declared the variable b at the starting of the block , i suggest you try using a different compiler or be prepared to declare all the variables at the beginning.
As other answers say, mixing code and declarations is illegal in old fashioned plain C. See:
Variable declaration placement in C
How to enforce C89-style variable declarations in gcc?
In eclipse, the standard version used will depend on the compiler flags passed to the C compiler gcc: either -std=c89 or -std=c99. Depending on how the project is set up, will either be in the Eclipse project properties or a Makefile.