[英]C : how do I printf "the square root of 1764 is 42 and * in ascii"?
ok so I am learning C and I try to use simple functions to understand basics and here I am stuck whith a segmentation fault I can't manage to make this code working h3lp please thanks you all !!!好的,所以我正在学习 C,我尝试使用简单的函数来理解基础知识,但在这里我遇到了分段错误,我无法使此代码运行 h3lp,谢谢大家!!!
#include <stdio.h>
#include <stdlib.h>
int ft_sqrt(int nb) //square root
{
unsigned int i;
i = nb;
while (nb < (i * i))
i--;
if (nb == (i * i))
return (i);
if (nb > (i * i))
return (0);
}
void ft_strcpy(char *d, char *s) // string copy
{
while((*d++ == *s++))
;
}
int ft_strlen(char *s) // string length
{
int i = 0;
while(s[i] != '\0')
i++;
return (i);
}
char *ft_itoa(int n) // integer to ascii
{
char *s;
s = (char *)malloc(99);
s += ft_strlen(s);
*s = 0;
while((*--s == n % 10 + '0') && (n /= 10))
;
return (s);
}
int ft_atoi(char *s) //ascii to integer
{
int i = 0;
while(*s)
i = 10 * i + *s++ - '0';
return (i);
}
int main()
{
int ft_sqrt(int nb);
void ft_strcpy(char *d, char *s);
char *ft_itoa(int n);
int ft_atoi(char *s);
int ft_strlen(char *s);
int a, *x;
a = 0;
char c[40], d[4];
c[40] = 0;
d[4] = 0;
a = ft_sqrt(1764); //42 in a
ft_strcpy(d, ft_itoa(a)); // a in d
ft_strcpy(c, "The square root of 1764 is: ");
x = ft_atoi(d);
printf("\n\n\t%s%sand%cin ascii\n\n\n", c, d, x);
return 0;
}
Just hack my code just wanna learn !!只是破解我的代码只是想学习!
There are many bugs in your code你的代码有很多错误
When the nb is not a perfect square number
, then it will give 0
always.当nb不是一个perfect square number
,它总是会给出0
。 Try it yourself.自己试试吧。 Use sqrt()
instead (available under math.h header file)使用sqrt()
代替(在math.h头文件下可用)
Your ft_strcpy()
is not properly framed, use strcpy()
under string.h
header file instead.您的ft_strcpy()
框架不正确,请改用string.h
头文件下的strcpy()
。 Prototype : void strcpy(char *str1, char *str2)
,here the content of str2
will be copied to the str1
.原型: void strcpy(char *str1, char *str2)
,这里str2
的内容将被复制到str1
。
char *ft_itoa(int n)
may not properly work because you have provided wrong value to ft_strlen()
which may turn out wrong value to be added to the pointer *s
( Why ?. Think your self , i'm not gonna tell you that elementary concept ). char *ft_itoa(int n)
可能无法正常工作,因为您为ft_strlen()
提供了错误的值,这可能会导致将错误的值添加到指针*s
(为什么?。想想你自己,我不会告诉你那个基本概念)。
Do re-code your program and let me know if still can't fix the Error.请重新编码您的程序,如果仍然无法修复错误,请告诉我。
Hey @BLUEPIXY thanks for your help you fixed my atoi !嘿@BLUEPIXY 感谢您帮助修复我的 atoi! @psyco thank you here is my code fixed btw with the same value to ft_strlen() for *ft_itoa(int n). @psyco 谢谢你这里是我的代码修复 btw 与 ft_strlen() 相同的值用于 *ft_itoa(int n)。
#include <stdio.h>
#include <stdlib.h>
int ft_sqrt(int nb)
{
unsigned int i;
i = nb;
while (nb < (i * i))
i--;
if (nb = (i * i))
return (i);
if (nb > (i * i))
return (0);
}
void ft_strcpy(char *d, char *s)
{
while((*d++ = *s++))
;
}
int ft_strlen(char *s)
{
int i = 0;
while(s[i] != '\0')
i++;
return (i);
}
char *ft_itoa(int n)
{
char *s;
s = (char *)malloc(99);
s += ft_strlen(s);
*s = 0;
while((*--s = n % 10 + '0') && (n /= 10))
;
return (s);
}
int ft_atoi(char *s)
{
int i = 0;
while(*s)
i = 10 * i + *s++ - '0';
return (i);
}
int main()
{
int ft_sqrt(int nb);
void ft_strcpy(char *d, char *s);
char *ft_itoa(int n);
int ft_atoi(char *s);
int ft_strlen(char *s);
int a;
a = 0;
char c[40], d[4];
c[40] = 0;
d[4] = 0;
a = ft_sqrt(1764);
ft_strcpy(d, ft_itoa(a));
ft_strcpy(c, "The square root of 1764 is: ");
printf("\n\n\t%s%s and %c in ascii\n\n\n", c, d, ft_atoi(d));
return 0;
}
BugFixed thank you all已修复,谢谢大家
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.