简体   繁体   中英

Assign an integer value to a string in C

I have a small problem assigning a string variable in C. Let's say we have char string[100] and I want to assign a string with an integer to this variable like in here.

What I tried was..

strcpy(string, "The tile in %d,%d is occupied: ", row, col);

But I get an error

Unresolved externals symbol message.

Is there any other way that I could do it?

 char string[100];
 sprintf(string, "The tile in %d,%d is occupied: ", row, col);

What you've got to understand is that a construct like "The tile in %d,%d is occupied: ", row, col is not some "magic" construct of the C language that always applies with strings. The reason for why you might think that, is that most beginners guides and books don't care to mention, what a line like printf("You name is %s", name) actually does.

It's not magic done by the C language, it's the how printf (and whole print , scanf family of function) were design in the first place.

In C a string "Your name is Mike" and "Your name is %" are just two strings (a series of characters terminated with \\0 ), the second one is no more special than the first one from the C perspective. The C language has also a special feature (some call it ellipses) that allow you to pass an undefined amount of arguments.

Take a look at this:

void foo(int a, int b);

With this declaration, there is only one way to call foo : with too arguments. If however foo would be declared as

void foo(int a, int b, ...);

then you can call foo so:

struct something k;
foo(1,2);
foo(1,2,3,4,5,6,7);
foo(2, 3, "This is a string", k, &k);

Using va_list (Variable Argument Lists) foo can access the rest of the parameters passed to it that come after the last named arguments, in this case b .

printf is a function that allows this flexibility, you can call printf with many arguments (always at least one).

// man 3 printf
#include <stdio.h>

int printf(const char *format, ...);
int fprintf(FILE *stream, const char *format, ...);
int dprintf(int fd, const char *format, ...);
int sprintf(char *str, const char *format, ...);
int snprintf(char *str, size_t size, const char *format, ...);

As you can see, all these function are declared that way. The basic way that printf work, is that it reads the first argument and parses it. When it encounters a substring like %d it knows that the user has passed an integer. It retrieves the integer using va_list and prints the value of the integer instead of %d . Then in continues until it reaches the end of the format -String, always replacing the % s with values retrieved using va_list .

strcpy is declared as char *strcpy(char *dest, const char *src); , it doesn't use the "ellipse trick" as printf does, so your line is wrong and the compiler complains about it.

There are other function that use similar strategies as printf , but the vast majority of function accepting strings do not implement this "trick". For those cases you can use sprintf .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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