简体   繁体   English

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

[英]How to create a string-type variable in C

Question问题

How to declare a string variable in C?如何在C中声明一个字符串变量?

Background背景

In my quest to learn the basics of , I am trying to port one of my oldest programs, Bob , to C. In the program, the script asks the user for information on him or herself, and then spits out responses.为了学习的基础知识,我尝试将我最古老的程序之一Bob移植到 C。在程序中,脚本要求用户提供有关他或她的信息,然后给出响应。 Almost all of these variables use raw_input for their information - the variables are strings.几乎所有这些变量都使用raw_input作为它们的信息——变量是字符串。 But, I have found no way to declare C variables.但是,我找不到声明 C 变量的方法。

Code代码

So far, I have tried to declare the variable as of type char and int.到目前为止,我已经尝试将变量声明为charint.类型int. Here is the code, switch the type at your leisure.这是代码,您可以随意切换类型。

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, Xcode returns some weird stuff.当我运行这段代码时, Xcode返回一些奇怪的东西。 This part of the globidty-gloop is highlighted. globidty-gloop 的这一部分被突出显示。

0x7fff96d2b4f0:  pcmpeqb(%rdi), %xmm0

Lasty, this Yahoo Answer said that I had to use something called a character array .最后, 这个 Yahoo Answer说我必须使用一种叫做character array东西。 It was posted 5 years ago, so I assumed that there was a better way.它是 5 年前发布的,所以我认为有更好的方法。

EDIT编辑

I am following the tutorial at C Programming .我正在关注C Programming的教程。

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

Edit: restricted input length to 59 characters (plus terminating 0):编辑:限制输入长度为 59 个字符(加上终止 0):

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

The int your putting is not a string, a string looks like "char myString[20]".您放置的 int 不是字符串,字符串看起来像“char myString[20]”。 Not like "int name", that's an integer and not a string or char.不像“int name”,它是一个整数,而不是一个字符串或字符。 This is the code you want:这是你想要的代码:

         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;
}

In C you can not direct declare a string variable like Java and other language.在 C 中你不能像 Java 和其他语言一样直接声明字符串变量。 you'll have to use character array or pointer for declaring strings.您必须使用字符数组或指针来声明字符串。

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);

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]='\0'; //null
}

Instead int is used for int number (1, 2, 3, ecc.);相反, int用于 int number (1, 2, 3, ecc.); For floating point number instead you have to use float对于浮点数,您必须使用float

C does not have a string variable type. C 没有字符串变量类型。 Strings can be stored as character arrays (char variable type).字符串可以存储为字符数组(char 变量类型)。 The most basic example I would add up to the rest is:我要加起来的最基本的例子是:

int main()
{
   char name[] = "Hello World!";
   printf("%s",name);
   return(0);
}

It's easy!这简单!

Just put this line below, atop of your main() function.只需将此行放在下面,在main()函数的顶部。

typedef string char*;

This allows you to create a string variable as you do with integers or characters in C. After that, your program should look like this:这允许您像在 C 中处理整数字符一样创建字符串变量。 之后,您的程序应如下所示:

#include <stdio.h>

typedef char* string;

int main(void) {
    string a = "Hello";
    printf("%s\n", a);  // %s format specifier for String
    return 0;
}

For a live demonstration , visit this REPL.it .如需现场演示,请访问此 REPL.it。

Normally we use "&" in scanf but you shouldn't use it before variable "name" here.通常我们在 scanf 中使用“&”,但你不应该在变量“name”之前使用它。 Because "name" is a char array.因为“名称”是一个字符数组。 When the name of a char array is used without "[]", it means the address of the array.当char数组的名称不带“[]”时,表示数组的地址。

replace int name;替换 int 名称; to--.到 - 。 char name[60];字符名称[60];

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

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

  return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM