#include <math.h>
#include <stdio.h>
int main(void)
{
double x = 4.0, result;
result = sqrt(x);
printf("The square root of %lf is %lfn", x, result);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
此代码不起作用,因为它采用变量的平方根.如果你改变了sqrt(x),to sqrt(20.0),代码工作正常,为什么?请解释.
另外,我如何获得变量的平方根(这是我真正需要的)?
OUTPUT:
matthewmpp@annrogers:~/Programming/C.progs/Personal$ vim sqroot1.c
matthewmpp@annrogers:~/Programming/C.progs/Personal$ cc -c sqroot1.c
matthewmpp@annrogers:~/Programming/C.progs/Personal$ cc -o sqroot1 sqroot1.c
matthewmpp@annrogers:~/Programming/C.progs/Personal$ ./sqroot1
4.472136
matthewmpp@annrogers:~/Programming/C.progs/Personal$ vim sqroot2.c
matthewmpp@annrogers:~/Programming/C.progs/Personal$ cc -c sqroot2.c
matthewmpp@annrogers:~/Programming/C.progs/Personal$ cc -o sqroot2 sqroot2.c
/tmp/ccw2dVdc.o: In function `main':
sqroot2.c:(.text+0x29): undefined reference to `sqrt'
collect2: ld returned 1 exit status
matthewmpp@annrogers:~/Programming/C.progs/Personal$
Run Code Online (Sandbox Code Playgroud)
注意:sqroot1是20.0的sqroot.sqroot2是变量的sqroot.
matthewmpp@annrogers:~/Programming/C.progs/Personal$ cc …Run Code Online (Sandbox Code Playgroud) matthewmpp@annrogers:~/Programming/C.progs/Personal$ cat prime4.c
/*
* File: main.c
* Author: matthewmpp
*
* Created on November 7, 2010, 2:16 PM
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
/*
prime numbers.
version4
should tell whether a number is prime or not prime.
by using other prime numbers.
*/
int input_func ()
{
char line[100];
int n_input;
while (1) {
printf("Please enter a whole number.\n");
fgets(line, sizeof (line), stdin);
sscanf(line, "%d", &n_input);
if (n_input >= 0)
break;
return (n_input);
}
} …Run Code Online (Sandbox Code Playgroud) /*\n * File: main.c\n * Author: matthewmpp\n *\n * Created on November 7, 2010, 2:16 PM\n */\n\n#include <stdio.h>\n#include <string.h>\n#include <stdlib.h>\n#include <math.h>\n\n/*\nprime numbers.\nversion4\nshould tell whether a number is prime or not prime.\nby using other prime numbers.\n */\n\nint input_func() {\n char line[100];\n int n_input;\n\n while (1) {\n printf("Please enter a whole number.\\n");\n fgets(line, sizeof (line), stdin);\n sscanf(line, "%d", &n_input);\n\n if (n_input >= 0)\n break;\n\n return (n_input);\n }\n}\n\nint ifstatements_func(n_ifstate)\nint n_ifstate;\n{\n if (n_ifstate == 0) {\n printf("The number, %d, is not prime and has …Run Code Online (Sandbox Code Playgroud) c ×3