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.
Related
I would like to execute a function given in the paramater of a function. Let me explain with an example.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int object() {
printf("Hello, World!");
return 0;
}
int run_func( int a_lambda_function() ) {
a_lambda_function();
return 0;
}
int main() {
run_func( object() );
return 0;
}
Now, I want to run "object()" in the parameters of "run_func(int a_lambda_function()").
When I run it, It returns an error. How would I achieve this is full C?
Restrictions I have:
Absolutly No C++ allowed.
Functions can be passed as arguments or stored into variables as function pointers.
The definition for a function pointer compatible with your object function is int (*funcp)() ie: a pointer to a function taking an unspecified number of arguments and returning int.
In modern C, functions with an unspecified number of arguments are not used anymore, and functions taking no arguments must be declared with a (void) argument list.
Here is a modified version:
#include <stdio.h>
int object(void) {
printf("Hello, World!");
return 0;
}
int run_func(int (*a_lambda_function)(void)) {
return a_lambda_function(); // can also write (*a_lambda_function)()
}
int main() {
return run_func(object);
}
I'm trying to use the isalpha function from the <ctype.h> library. I have used other functions from other libraries in different files but I cant get isalpha to work. The error is:
test.c:9:21: error: expected ')'
int isalpha(int c);
^
The code I'm working on:
// Function Declarations
int isalpha(int c);
int main(void) {
char letter = get_char("Letter:");
bool yesno = isalpha(letter);
if (yesno == true) {
printf("True\n");
} else {
printf("False\n");
}
}
The Library is included in the header I just didn't know how to include the header here.
Thanks for the help
You should just include <ctype.h> and not redefine isalpha with an explicit prototype in your code. The reason you get an error is isalpha() is probably defined as a macro in <ctype.h> and this macro gets expanded as the compiler parses your prototype and the result of the expansion is a syntax error.
Note also that isalpha() should not be called with an argument of type char because it has undefined behavior for negative values on platforms where char is signed by default. Cast char arguments as (unsigned char) to avoid this undefined behavior.
Furthermore, the return value of isalpha is an int and the value 0 means false and any other value means true. Comparing isalpha(letter) == true is incorrect. Your code relies on the boolean conversion between int and bool, which is available since C99, but it is error prone to rely on this implicit conversion as this subtlety is probably beyond your skill level.
Here is a modified version:
#include <ctype.h>
#include <stdio.h>
#include <cs50.h>
int main(void) {
char letter = get_char("Letter:");
if (isalpha((unsigned char)letter) {
printf("True\n");
} else {
printf("False\n");
}
return 0;
}
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.
How to use topper and tolower in the C language?
I've tried to run the program that I've made, it runs properly
the problem is since I should submit it to a website to check it whether it's right or wrong, every time I submit it, it says compile error.
I made the code on macbook, using Xcode and it says on my toupper and tolower code -- implicit declaration of function 'toupper' is invalid in C99
#include <stdio.h>
#include <string.h>
int main()
{
int input;
scanf("%d",&input);
int jumlahkata;
char kalimat[100];
for(int i=0;i<input;i++)
{
scanf("%s",kalimat);
jumlahkata=strlen(kalimat);
for(int j=0;j<jumlahkata;j++)
{
if(j%2==0 || j==0)
{
kalimat[j]=toupper(kalimat[j]);
}
else
{
kalimat[j]=tolower(kalimat[j]);
}
}
printf("%s\n",kalimat);
}
return 0;
}
toupper and tolower are defined in ctype.h. Simply include this file with the line #include <ctype.h>.
You need to include header <ctype.h> .
Also int jumlahkata; should be of type size_t as you store result of strlen in it.
Or don't use it (as also pointed out by #iharob Sir ) , it unnecessary. As it is string , just check for null character as a condition in loop.
You are mixing C with C++:
int input;
scanf("%d",&input); // in C, following the first executable statement you may not declare variables until the next block
int jumlahkata; // declaring the variable here is C++
char kalimat[100]; // declaring the variable here is C++
for(int i=0;i<input;i++) // declaring the variable here is C++
I found this example code and I tried to google what (int (*)[])var1 could stand for, but I got no usefull results.
#include <unistd.h>
#include <stdlib.h>
int i(int n,int m,int var1[n][m]) {
return var1[0][0];
}
int example() {
int *var1 = malloc(100);
return i(10,10,(int (*)[])var1);
}
Normally I work with VLAs in C99 so I am used to:
#include <unistd.h>
#include <stdlib.h>
int i(int n,int m,int var1[n][m]) {
return var1[0][0];
}
int example() {
int var1[10][10];
return i(10,10,var1);
}
Thanks!
It means "cast var1 into pointer to array of int".
It's a typecast to a pointer that points to an array of int.
(int (*)[]) is a pointer to an array of ints. Equivalent to the int[n][m] function argument.
This is a common idiom in C: first do a malloc to reserve memory, then cast it to the desired type.