How to create a string-type variable in C

后端 未结 8 1356
没有蜡笔的小新
没有蜡笔的小新 2021-02-04 08:42

Question

How to declare a string variable in C?

Background

In my quest to learn the basics of c, I am trying to port on

8条回答
  •  囚心锁ツ
    2021-02-04 09:02

    It's easy!

    Just put this line below, atop of your main() function.

    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:

    #include 
    
    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.

提交回复
热议问题