atof conflict while compiling code - c

I have just begun my journey with C programming. I have a problem with a lib conflict. Please find my sample code below.
#include <stdio.h>
#include <stdlib.h>
#define MAXLINE 100
//int getline declaration is here (removed for simplicity)
int main()
{
double sum, atof(char []);
char line[MAXLINE];
int getline(char line[], int max);
sum=0;
while (getline(line,MAXLINE)>0)
printf("\t%g\n", sum += atof(line));
40mi return 0;
}
When I run this code I get a following compilation error:
Error: conflicting types for 'atof'
I'm using CodeBlocks + mingw c compiler
Edit (in response to comment):
I want to download one argument and may return a double value , atof works without stdlib.h , but can not define getline

You don't need to declare atof if you include stdlib. It is declared in stdlib. The definition of atof in stdlib is double atof(const char *nptr). It is different of yours and this explains the conflict.
getline is defined in stdio.h and expects three arguments: ssize_t getline(char **lineptr, size_t *n, FILE *stream). You can't read from standard input with getline as you do. you have to use getline(line, MAXLINE, stdin). And don't declare it in main because it is already declared in stdio.h.

The declaration of atof() in <stdlib.h> is:
double atof (const char* str);
And you are declaring it as
double atof(char []);
The two declarations are different, hence, the error.

Related

Atof is not working in C and without atof is also not working in debug

I have problems with Atof function. I am trying to convert string to float but it is not giving any error when I try in Coocox software in Debug section, Output is not showing anything. I tried two functions Atoi and Atof. When I use Atoi there is no output.When I use Atof The program starting restart. I put stdlib.h definition for atof in here.But it is atoff for float value in here.I tried same code in Dev C++ in C it is working very well. Other things I use without working Atof but this time again the program is restarting. This is working on Dev C. But not in Coocox. How can I solve the problem? There is only difference atoff! What can it be related? I used stdlib.h and there is no error in compilation!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main ()
{
float c;
int b;
char *array[1] = {"52.43525"};
b=(atoi(array[0])/100);
c=((atof(array[0]))/100.0)*100.0;
c/=60;
c+=b;
printf ("%f\n", c);
return 0;
}
-----stdlib.h----
double _EXFUN(atof,(const char *__nptr));
#if __MISC_VISIBLE
float _EXFUN(atoff,(const char *__nptr));
#endif
int _EXFUN(atoi,(const char *__nptr));
int _EXFUN(_atoi_r,(struct _reent *, const char *__nptr));
long _EXFUN(atol,(const char *__nptr));
long _EXFUN(_atol_r,(struct _reent *, const char *__nptr));
------------------------------------
after correcting all the compiler warnings, this was the resulting code:
Note: since the array feature was not used, I changed it to a simple pointer. This made no difference int the output.
#include <stdio.h> // printf()
//#include <string.h> -- contents not used
#include <stdlib.h> // atoi(), atof()
int main ()
{
float c;
int b;
char *array = {"52.43525"};
b = atoi(array);
c = ( (float)( atof(array) ) / 100.0f ) * 100.0f;
c /= 60.0f;
c += (float)b;
printf ("%f\n", c);
return 0;
}
running the program resulted in:
52.873920
So if your compiler is not finding atof() it is a problem with the compiler.

ASCII/count printing C program?

I am trying to make a program that will read characters from standard input until EOF (the end-of-file mark) is read.
And after that function I have:
#include <stdio.h>
int main(int agc, char *agv[]) {
int x;
int count = 0;
while ((x = getchar()) != EOF){
count++;
In your main(), the lines marked are function declarations, not function calls. You will probably need to replace them with calls to the functions.
int main(int argc, char *argv[]) {
int x;
int count = 0;
while ((x = getchar()) != EOF){
count++;
}
prHeader(FILE *out); // Declaration
prCountStr(FILE *out, int code, char *str, int count); // Declaration
prCountChr(FILE *out, int code, char chr, int count); // Declaration
prTotal(FILE *out, int count); // Declaration
return 0;
}
You are also going to need to have an array (counters, for sake of argument) of 256 integers, all initialized to zero, and your loop will increment the entry in counters corresponding to the character just read. Fortunately, getchar() returns a positive value for every possible input character.
In C, you should seldom define a function in a header; in your case, you should not define the functions in a header. The declarations should be there, but the definitions should not be there. You should have another source file, presumably common.c, which defines the functions. You would then need to compile both the file containing the main() function and common.c, and you'd need to link both object files to create the program. (In the short term, you can avoid that by leaving the functions in the header and simply compiling the code that defines the main() function, but that is subverting the point of a header file.) The majority of the variable definitions should be in common.c and not in common.h too.
You have other work to do too; the BADFILE and related macros are not yet used. It also appears you might need to parse the command line options.

How to use scanf() without including stdio.h

Is there any possible methods to write a C program without including stdio.h as a header file. It was suggested that it can be implemented by declaring extern int scanf(char* format, ...);
#include <stdio.h> //I want this same code to work without including this line
int main ()
{
char str [80];
scanf ("%s",str);
return 0;
}
You can declare the scanf function with:
extern int scanf(const char *format, ...);
The extern keyword is optional but I like to include it as a reminder of the fact that the function is defined elsewhere.
Your example would then look like:
extern int scanf(const char *format, ...);
int main ()
{
char str [80];
scanf ("%s",str);
return 0;
}
In C-89, that code would compile without the #include, as function prototypes are optional.
Having said which, it comes under the list of 'really bad things to do' - scanf may be a macro, it might have one or more required parameters, ...
So you can do it, but it's like driving at night without any lights. You're liable to crash, even if you think you know the road.

What is wrong with usage of atof function?

int main()
{
char str[10]="3.5";
printf("%lf",atof(str));
return 0;
}
This is a simple code I am testing at ideone.com. I am getting the output as
-0.371627
You have not included stdlib.h. Add proper includes:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[10]="3.5";
printf("%lf",atof(str));
return 0;
}
Without including stdlib.h, atof() is declare implicitly and the compiler assumes it returns an int.
It could be undefined behavior.

usage of strdup

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *s;
s = strdup("foo");
printf("%s\n", s);
getchar();
return 0;
}
Looks pretty harmless, doesn't it ?
But my IDE, which is Dev-C++, gives my the following warning:
warning: assignment makes pointer from integer without a cast
The warning disappears if you would change the code like this:
char *s;
s = (char*)strdup("foo");
Can anyone help me explain this?
You're using Dev-C++, but strdup is not part of the C or C++ standard, it's a POSIX function. You need to define the proper (according to your IDE's documentation) preprocessor symbols in order for strdup to be declared by the header file ... this is necessary in order for the header file not to pollute the name space when included into conforming C or C++ source files.
For a simple portable alternative, consider
char* mystrdup(const char* s)
{
char* p = malloc(strlen(s)+1);
if (p) strcpy(p, s);
return p;
}
Or, if you know strdup is actually in the library, you can copy its declaration from string.h into your own source file or header ... or use the simpler declaration from the man page:
char *strdup(const char *s);
That's not right. strdup returns char * already. Something else is wrong. Probably because you did not include the right header file that declares the true return type for this function.
#include <string.h>
You're missing #include <string.h>. In the absence of function signatures, strdup is assumed by the compiler to return an int, hence the warning.
man strdup
you will get following things
#include<string.h>
char* strdup(const char * s);
so strdup() returns char* there shuld not be any problem
Actually in your case it takes implicit declaration of strdup() so by default return type is int hence you get this warning
Either include<string.h>
or
give forward declaration char* strdup(const char *);
Also don't forget to free(s) in last when all usage are done

Resources