谁来帮我看看我的这个程序在gcc下调试出现 "段错误"
//用三种循环求1+2+3...+100#include<stdio.h>#include<string.h>intmain(intargc,char**argv){in...
//用三种循环求1+2+3...+100
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
int i, sum = 0;
i = 1;
if (argc < 2)
printf( "input './ for' or './ while' or './ goto' or './ dowhile'");
//for 循环
if (strcmp(argv[1], "for") == 0)
{
for (; i <= 100; i++)
{
sum += i;
}
fprintf(stdout, "'for':the sum is %d\n", sum);
}
//while 循环
else if (strcmp(argv[1], "while") == 0)
{
while (i <= 100)
{
sum += i;
i++;
}
fprintf(stdout, "'while':the sum is %d\n", sum);
}
//goto 循环
else if (strcmp(argv[1], "goto") == 0)
{
loop:if (i <= 100)
{
sum += i;
i++;
goto loop;
}
fprintf(stdout, "'goto':the sum is %d\n", sum);
}
//do while 循环
else if (strcmp(argv[1], "dowhile") == 0)
{
do
{
sum += i;
i++;
} while (i <= 100);
fprintf(stdout, "'do while':the sum is %d\n", sum);
}
else
{
fprintf(stdout, "input './ for' or './ while' or './ goto' or './ do while'");
}
return 0;
}
if (argc < 2)
printf( "input './ for' or './ while' or './ goto' or './ dowhile'");
我是问当我 ./ 不带参数的时候为什么出现段错误
程序应该是这样
//用三种循环求1+2+3...+100
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
int i, sum = 0;
i = 1;
if (argc < 2)
{
printf("input './ for' or './ while' or './ goto' or './ dowhile'\n");
return -1;
} 展开
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
int i, sum = 0;
i = 1;
if (argc < 2)
printf( "input './ for' or './ while' or './ goto' or './ dowhile'");
//for 循环
if (strcmp(argv[1], "for") == 0)
{
for (; i <= 100; i++)
{
sum += i;
}
fprintf(stdout, "'for':the sum is %d\n", sum);
}
//while 循环
else if (strcmp(argv[1], "while") == 0)
{
while (i <= 100)
{
sum += i;
i++;
}
fprintf(stdout, "'while':the sum is %d\n", sum);
}
//goto 循环
else if (strcmp(argv[1], "goto") == 0)
{
loop:if (i <= 100)
{
sum += i;
i++;
goto loop;
}
fprintf(stdout, "'goto':the sum is %d\n", sum);
}
//do while 循环
else if (strcmp(argv[1], "dowhile") == 0)
{
do
{
sum += i;
i++;
} while (i <= 100);
fprintf(stdout, "'do while':the sum is %d\n", sum);
}
else
{
fprintf(stdout, "input './ for' or './ while' or './ goto' or './ do while'");
}
return 0;
}
if (argc < 2)
printf( "input './ for' or './ while' or './ goto' or './ dowhile'");
我是问当我 ./ 不带参数的时候为什么出现段错误
程序应该是这样
//用三种循环求1+2+3...+100
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
int i, sum = 0;
i = 1;
if (argc < 2)
{
printf("input './ for' or './ while' or './ goto' or './ dowhile'\n");
return -1;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询