[英]simple C program crashes when run
hi i'm having some trouble with my program when i run it it crashes and i have to force it closed and i'm having some trouble determining what's causing it any help will be greatly appreciated 嗨,我在运行程序时遇到问题,它崩溃了,我不得不强行关闭它,在确定是什么原因导致的麻烦时,我将不胜感激
#include <stdio.h>
void myprint(char let, int num, int line)
{
int countL=0,countT=0,numb, lin;
char abc;
numb=num;
line=lin;
abc=let;
while(countL<lin)
{
while(countT<numb)
{
printf("%s",abc );
countT++;
}
printf("\n");
countL++;
}
}
int main(int argc, char const *argv[])
{
int times = 5, lines = 3;
char letter = 'a';
myprint(letter, times, lines);
return 0;
}
The trouble is that you have a single character and are trying to print it as a string: 问题是您只有一个字符,并试图将其打印为字符串:
printf("%s",abc );
In C strings are sequences of characters terminated by the null character \\0
. 在C字符串中,是由空字符\\0
终止的字符序列。 So this printf
will attempt to keep printing characters until it finds that null byte. 因此,此printf
将尝试继续打印字符,直到找到该空字节为止。 Your program probably keeps trying to read memory until it finds a bad segment, then it crashes. 您的程序可能一直尝试读取内存,直到找到错误的段,然后它崩溃了。
What you may want to do instead is just specify a character in your printf
: 您可能要做的只是在printf
指定一个字符:
printf("%c",abc );
Another problem is that you have the variables reversed in the assignment: 另一个问题是您在分配中将变量取反:
line=lin;
lin
has no initialized value. lin
没有初始化值。
I think the line that reads line=lin;
我认为读取line=lin;
should be lin=line
应该是lin=line
Try inverting this initialization : 尝试反转此初始化:
line=lin;
to 至
lin=line;
Otherwise, lin
is undefined. 否则, lin
是不确定的。
printf("%s",abc );
打印一个字符串,abc是一个字符。
Problem 1: 问题一:
variable "lin" is not initialize properly, 变量“ lin”未正确初始化,
line=lin; 线=林;
so variable "line" and "lin" contains garbage value. 因此变量“ line”和“ lin”包含垃圾值。
Problem 2: 问题2:
change 更改
printf("%s",abc ); printf(“%s”,abc);
to 至
printf("%c",abc ); printf(“%c”,abc);
as abc is a char. 因为abc是一个字符。
在while(countL<lin)
循环中,您永远不会重置countT
变量。
Try to replace 尝试更换
while(countT<numb)
{
printf("%s",abc );
countT++;
}
with 与
while(countT<numb)
{
printf("%c",abc );
countT++;
}
You are using printf
incorrectly. 您使用的printf
错误。
You passed %s
as format, which requires char*
as argument, not char
. 您以格式传递%s
,它需要char*
作为参数,而不是char
。
THE ERROR IN YOUR CODE IS SIMPLE 您的代码中的错误很简单
"line=lin" is the problem of the code “ line = lin”是代码的问题
here the variable lin is not initialised hence holds garbage value 这里变量lin没有初始化,因此保留了垃圾值
this garbage value is passed on to the variable line and it screws up everything 这个垃圾值被传递到变量行,它搞砸了一切
i think the proper code would be lin=line 我认为正确的代码将是lin = line
another bug i found was printf('%s',abc); 我发现的另一个错误是printf('%s',abc); the type specifier is wrong since abc has been declared as character and type specifier must be %c 类型说明符错误,因为已将abc声明为字符,并且类型说明符必须为%c
I also found out that the unused command arguments args and argv which can be only used to pass argument running from command promt 我还发现未使用的命令参数args和argv只能用于传递从命令promt运行的参数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.