Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello . I am learning C and I am just a beginner. My editor is vs code and I have a simple piece of code but why identifier "string" is undefined ?
My code :
// Prints a string

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


int main(void)
{
    string s = "HI!";
    printf("%s\n", s);
}


What I have tried:

I have added the header files but I don't know why it still does not work. It works fine when I use char[] instead of string , but I only wanted to know how can I fix the error.
I will be grateful for your help and advice.
Posted
Updated 14-Sep-21 1:17am
v2
Comments
The Other John Ingram 14-Sep-21 6:34am    
there is no string type in c.
the string.h gives you functions to do string manipulations.
strings are really char arrays.
Aylin Naebzadeh 14-Sep-21 6:40am    
😅Ok, thanks.

1 solution

C does not have a string type: just char, char pointer, and char array along with literal strings which resolve to a char pointer:
C
char *prompt = "Please enter your name: ";

Try this:
C
#include <stdio.h>

int main()
    {
    char *hi = "Hello World";
    printf("%s\n", hi);
    return 0;
    }
Or more readably:
C
#include <stdio.h>

int main()
    {
    char *hi = "Hello World";
    printf(hi);
    printf("\n");
    return 0;
    }
Or better:
C
#include <stdio.h>

int main()
    {
    char *hi = "Hello World\n";
    printf(hi);
    return 0;
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900