如何将字符串复制到另一个变量并使用 if else 语句输出
how can i copy string to another variable and output with if else statement
#include<stdio.h>
#include<string.h>
int main(void)
{
//Assigning string to name1//
char name1[8] = "Aaeric";
//will be used to copy name1 to copyname//
char copyname[8];
//Assigning string to name2//
char name2[7] = "Mudus";
//prompt for input//
int i;
printf("Choose 1 or 0: ");
scanf("%i \n", &i);
}
//for copying string of name1 to copyname//
if (i = 0)
{
strcpy(copyname, name1);
puts(copyname);
return 0;
}
else
puts(name2);
}
I want to copy the string to another variable and output with if-else condition through this way only but I am getting error.
The output is the expected identifier or ‘(’ before ‘if’
你可能想要这个:
#include <stdio.h>
#include <string.h>
int main(void)
{
char name1[8] = "Aaeric";
//will be used to copy name1 to copyname//
char copyname[8];
char name2[7] = "Mudus";
int i;
printf("Choose 1 or 0: ");
scanf("%i", &i);
if (i == 0) // <<<<< use == instead of =
{
strcpy(copyname, name1);
puts(copyname);
return 0;
}
else
puts(name2);
}
- 已删除毫无意义的评论
- 代码片段移至
main
- 代码格式正确
scanf("%i \n", &i);
替换为 scanf("%i", &i);
if (i = 0)
替换为 if (i == 0)
#include<stdio.h>
#include<string.h>
int main(void)
{
//Assigning string to name1//
char name1[8] = "Aaeric";
//will be used to copy name1 to copyname//
char copyname[8];
//Assigning string to name2//
char name2[7] = "Mudus";
//prompt for input//
int i;
printf("Choose 1 or 0: ");
scanf("%i \n", &i);
}
//for copying string of name1 to copyname//
if (i = 0)
{
strcpy(copyname, name1);
puts(copyname);
return 0;
}
else
puts(name2);
}
I want to copy the string to another variable and output with if-else condition through this way only but I am getting error.
The output is the expected identifier or ‘(’ before ‘if’
你可能想要这个:
#include <stdio.h>
#include <string.h>
int main(void)
{
char name1[8] = "Aaeric";
//will be used to copy name1 to copyname//
char copyname[8];
char name2[7] = "Mudus";
int i;
printf("Choose 1 or 0: ");
scanf("%i", &i);
if (i == 0) // <<<<< use == instead of =
{
strcpy(copyname, name1);
puts(copyname);
return 0;
}
else
puts(name2);
}
- 已删除毫无意义的评论
- 代码片段移至
main
- 代码格式正确
scanf("%i \n", &i);
替换为scanf("%i", &i);
if (i = 0)
替换为if (i == 0)