user1911575
user1911575

Reputation: 454

Convert Char to String in C

How do I convert a character to a string in C. I'm currently using c = fgetc(fp) which returns a character. But I need a string to be used in strcpy

Upvotes: 30

Views: 215998

Answers (9)

Adam
Adam

Reputation: 1099

To answer the question without reading too much else into it I would

char str[2] = "\0"; /* gives {\0, \0} */
str[0] = fgetc(fp);

You could use the second line in a loop with whatever other string operations you want to keep using chars as strings.

Upvotes: 23

John
John

Reputation: 436

Here is a working exemple :

printf("-%s-", (char[2]){'A', 0});

This will display -A-

Upvotes: 2

Floriel
Floriel

Reputation: 41

This is an old question, but I'd say none of the answers really fits the OP's question. All he wanted/needed to do is this:

char c = std::fgetc(fp);
std::strcpy(buffer, &c);

The relevant aspect here is the fact, that the second argument of strcpy() doesn't need to be a char array / c-string. In fact, none of the arguments is a char or char array at all. They are both char pointers:

strcpy(char* dest, const char* src);

dest : A non-const char pointer
Its value has to be the memory address of an element of a writable char array (with at least one more element after that).
src : A const char pointer
Its value can be the address of a single char, or of an element in a char array. That array must contain the special character \0 within its remaining elements (starting with src), to mark the end of the c-string that should be copied.

Upvotes: 1

Omar M. Hussein
Omar M. Hussein

Reputation: 45

//example
char character;//to be scanned
char merge[2];// this is just temporary array to merge with      
merge[0] = character;
merge[1] = '\0';
//now you have changed it into a string

Upvotes: 2

Ħđ Ťǿųfik
Ħđ Ťǿųfik

Reputation: 59

I use this to convert char to string (an example) :

char c = 'A';
char str1[2] = {c , '\0'};
char str2[5] = "";
strcpy(str2,str1);

Upvotes: 5

LihO
LihO

Reputation: 42133

Using fgetc(fp) only to be able to call strcpy(buffer,c); doesn't seem right.

You could simply build this buffer on your own:

char buffer[MAX_SIZE_OF_MY_BUFFER];

int i = 0;
char ch;
while (i < MAX_SIZE_OF_MY_BUFFER - 1 && (ch = fgetc(fp)) != EOF) {
    buffer[i++] = ch;
}
buffer[i] = '\0';  // terminating character

Note that this relies on the fact that you will read less than MAX_SIZE_OF_MY_BUFFER characters

Upvotes: 7

Taiki
Taiki

Reputation: 639

A code like that should work:

int i = 0;
char string[256], c;
while(i < 256 - 1 && (c = fgetc(fp) != EOF)) //Keep space for the final \0
{
    string[i++] = c;
}
string[i] = '\0';

Upvotes: 2

Utkan Gezer
Utkan Gezer

Reputation: 3069

You could do many of the given answers, but if you just want to do it to be able to use it with strcpy, then you could do the following:

...
    strcpy( ... , (char[2]) { (char) c, '\0' } );
...

The (char[2]) { (char) c, '\0' } part will temporarily generate null-terminated string out of a character c.

This way you could avoid creating new variables for something that you already have in your hands, provided that you'll only need that single-character string just once.

Upvotes: 9

FYI you dont have string datatype in C. Use array of characters to store the value and manipulate it. Change your variable c into an array of characters and use it inside a loop to get values.

char c[10];
int i=0;
while(i!=10)
{
    c[i]=fgetc(fp);
    i++;
}

The other way to do is to use pointers and allocate memory dynamically and assign values.

Upvotes: 0

Related Questions