C语言 如何在 C 中创建字符串类型的变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15420696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:42:38  来源:igfitidea点击:

How to create a string-type variable in C

cstringvariable-declaration

提问by xxmbabanexx

Question

How to declare a string variable in C?

如何在C中声明一个字符串变量?

Background

背景

In my quest to learn the basics of c, I am trying to port one of my oldest pythonprograms, Bob, to C. In the program, the script asks the user for information on him or herself, and then spits out responses. Almost all of these variables use raw_inputfor their information - the variables are strings. But, I have found no way to declare C variables.

在我学习c的基础知识的过程中,我试图将我最古老的Python程序之一移植Bob到 C。在该程序中,脚本要求用户提供有关他或她的信息,然后给出响应。几乎所有这些变量都raw_input用于它们的信息——变量是字符串。但是,我找不到声明 C 变量的方法。

Code

代码

So far, I have tried to declare the variable as of type charand int.Here is the code, switch the type at your leisure.

到目前为止,我已经尝试将变量声明为类型charint.这是代码,您可以随意切换类型。

int main(int argc, const char * argv[])
{

    int name;
    printf("What is your name?");
    scanf("%s",&name);
    printf("Your name is %s", name );

    return 0;
}

Error Message

错误信息

When I run this code, Xcodereturns some weird stuff. This part of the globidty-gloop is highlighted.

当我运行这段代码时,会Xcode返回一些奇怪的东西。globidty-gloop 的这一部分被突出显示。

0x7fff96d2b4f0:  pcmpeqb(%rdi), %xmm0

Lasty, this Yahoo Answersaid that I had to use something called a character array. It was posted 5 years ago, so I assumed that there was a better way.

Lasty,这个 Yahoo Answer说我必须使用一个叫做character array. 它是 5 年前发布的,所以我认为有更好的方法。

EDIT

编辑

I am following the tutorial at C Programming.

我正在关注C Programming的教程。

回答by Valeri Atamaniouk

char name[60];
scanf("%s", name);

Edit: restricted input length to 59 characters (plus terminating 0):

编辑:限制输入长度为 59 个字符(加上终止 0):

char name[60];
scanf("%59s", name);

回答by TheGuyWhoForgetsALot

The int your putting is not a string, a string looks like "char myString[20]". Not like "int name", that's an integer and not a string or char. This is the code you want:

您放置的 int 不是字符串,字符串看起来像“char myString[20]”。不像“int name”,它是一个整数,而不是一个字符串或字符。这是你想要的代码:

         int main(int argc, const char * argv[])
{

char name[9999];
printf("What is your name?\n");
scanf("%s", name);
system("cls");
printf("Your name is %s", name);

return 0;
}

回答by Tushar Gaurav

In C you can not direct declare a string variable like Java and other language. you'll have to use character array or pointer for declaring strings.

在 C 中,您不能像 Java 和其他语言一样直接声明字符串变量。您必须使用字符数组或指针来声明字符串。

char a[50];
printf("Enter your string");
gets(a);

OR

或者

char *a;
printf("Enter your string here");
gets(a);

OR

或者

char a[60];
scanf("%59s",a);

回答by Mitro

TESTED ON XCODE

在 Xcode 上测试

You can do so:

你可以这样做:

int main(int argc, const char * argv[])
{

    int i;
    char name[60]; //array, every cell contains a character

    //But here initialize your array

    printf("What is your name?\n");
    fgets(name, sizeof(name), stdin);
    printf("Your name is %s", name );

    return 0;
}

Initialize the array, is good to avoid bug

初始化数组,有利于避免bug

for(i=0;i<60;i++){
      name[i]='
int main()
{
   char name[] = "Hello World!";
   printf("%s",name);
   return(0);
}
'; //null }

Instead intis used for int number (1, 2, 3, ecc.); For floating point number instead you have to use float

而是int用于 int number (1, 2, 3, ecc.); 对于浮点数,您必须使用float

回答by Mykhailo Badiuk

C does not have a string variable type. Strings can be stored as character arrays(char variable type). The most basic example I would add up to the rest is:

C 没有字符串变量类型。字符串可以存储为字符数组(char 变量类型)。我要加起来的最基本的例子是:

#include <stdio.h>
int main()
{
  char name[648];
  printf("What is your name?");

  scanf("%s", name);
  printf("Your name is %s", name );

  return 0;
}

回答by Ceylan

Normally we use "&" in scanf but you shouldn't use it before variable "name" here. Because "name" is a char array. When the name of a char array is used without "[]", it means the address of the array.

通常我们在 scanf 中使用“&”,但你不应该在变量“name”之前使用它。因为“名称”是一个字符数组。当char数组的名称不带“[]”时,表示数组的地址。

回答by nemer tamimi

replace int name; to--. char name[60];

替换 int 名称;到 - 。字符名称[60];

##代码##