为什么我的代码有时会给出不同的结果 [英] Why do my code gives different result at times
问题描述
我正在试图找出为什么我的代码给了我不同的结果是运行时
例如在第一次测试时工作
第二次测试完全不同的结果
第三次测试没有任何结果
以下是我的代码:
char * Amy [] = {Olivia \\\
Emma \\\
Ava \\\
Mia \\\
Charloth \ nn Noah \ Liam \ Ben \ n Oliver \\ \\ n将};
printf(\ n Amy's Profile \ n);
printf(学生姓名:\ n%s,* Amy,\ n);
我尝试了什么:
我没有尝试过多,因为我看不太清楚我的错误
您将不得不更准确地解决问题发生时使用的代码:如果我将代码粘贴到在线编译器中并尝试它:
#include < stdio.h >
int main()
{
printf( Hello World \ n);
char * Amy [] = { Olivia \\\
Emma \\\
Ava \\\
Mia \\\
Charloth \ nn Noah \ Liam \ Ben \ n Oliver \ n will};
printf( \ n Amy的个人资料\ n);
printf( 学生姓名:\ n%s,* Amy, \ n);
return 0 ;
}每次运行它都会做同样的事情:
Hello World
Amy's个人资料
学生姓名:
Olivia
Emma
Ava
Mia
Charloth
Noah
Liam
Ben
Oliver
将这是我的期望。
我建议对你的代码进行一次改进:
< pre lang =c ++> printf( 学生姓名:\ n%s,Amy [ 0 ], \ n );通常是比使用指针更好的访问数组的方法。它对你的代码输出没有任何影响。
引用:char * Amy [ ] = {Olivia \\\
Emma \\\
Ava \\\
Mia \\\
Charloth \ nn Noah \ Liam \ Ben \ n Oliver \ n will will}};以上line定义了一个只有一个项目的指针数组(字符)。
虽然技术上不是一个错误,但它可能不是你想要的。
Quote:printf(学生姓名:\ n%s,* Amy,\ n);
以上行不正确(请参阅 printf文档 [ ^ ]):您的格式字符串只指定一个参数(
%s
),但你提供两个(amy [0]
和\ n
) 。一个不错的编译器会产生警告。
我会这样写的:
#include < stdio.h >
int main()
{
const char * Amy = Olivia \ n Emma \\\
Ava \\\
Mia \\\
Charloth \ Noah \ Liam \ Ben \\ n奥利弗\ n将;
printf( \ n Amy的个人资料\ n);
printf( 学生姓名:\ n%s \ n,Amy) ;
return 0 ;
}
I'm trying to figure out why does my code gives me different results are runtime
for instance on first test it works
second test a totally different result
third test no result at all
below is my code :
char *Amy[] = {"Olivia \n Emma \n Ava\n Mia\n Charloth\n Noah \n Liam \n Ben \n Oliver \n will"};
printf("\n Amy's Profile\n");
printf("Student Names :\n %s",*Amy,"\n");
What I have tried:
I haven't try much at all since I don't quite see my mistake
You're going to have to be more exact about the code you use when the problem occurs: If I paste your code into an online compiler and try it:
#include <stdio.h> int main() { printf("Hello World\n"); char *Amy[] = {"Olivia \n Emma \n Ava\n Mia\n Charloth\n Noah \n Liam \n Ben \n Oliver \n will"}; printf("\n Amy's Profile\n"); printf("Student Names :\n %s",*Amy,"\n"); return 0; }It does the same thing each time I run it:
Hello World Amy's Profile Student Names : Olivia Emma Ava Mia Charloth Noah Liam Ben Oliver willWhich is what I expect.
I would suggest one improvement to your code though:
printf("Student Names :\n %s", Amy[0], "\n");Is normally a better way of accessing an array than using a pointer. It won't make any difference to your code output though.
Quote:char *Amy[] = {"Olivia \n Emma \n Ava\n Mia\n Charloth\n Noah \n Liam \n Ben \n Oliver \n will"};
The above line defines an array of pointers (to characters) having just one item.
While not technically a mistake, it is probably not what you intended.
Quote:printf("Student Names :\n %s",*Amy,"\n");
The above line is incorrect (see the printf documentation[^]): your format string specifies just one argument (
%s
), but you are providing two (amy[0]
and"\n"
). A decent compiler would produce a warning.
I would have written it this way:
#include <stdio.h> int main() { const char *Amy = "Olivia \n Emma \n Ava\n Mia\n Charloth\n Noah \n Liam \n Ben \n Oliver \n will"; printf("\n Amy's Profile\n"); printf("Student Names :\n %s\n", Amy); return 0; }
这篇关于为什么我的代码有时会给出不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!