无法在c中连接int和string

时间:2014-10-20 15:32:51

标签: c

我不能在这些字符串中输入字符串。

void main
{
    char buffer[10];
    int degrees=9;
    sprintf(buffer,"%d",degrees);
    string completeMessage=(("turnAnticlockwise(%s);",buffer));
    printf(completeMessage);
}

任何帮助都会很棒!

2 个答案:

答案 0 :(得分:4)

也许你想要这个:

#include <stdio.h>

void main()
{
    char buffer[30];  // note it's 30 now, with 10 the buffer will overflow
    int degrees=9;
    sprintf(buffer, "turnAnticlockwise(%d)",degrees);
    printf("%s", buffer);
}

这个小程序将输出:

turnAnticlockwise(9)

答案 1 :(得分:1)

请参阅: http://www.cesarkallas.net/arquivos/faculdade/estrutura_dados_1/complementos%20angela/string/conversao.html 特别是:

#include <stdio.h>

int main() {
  char str[10]; /* MUST be big enough to hold all 
                  the characters of your number!! */
  int i;

  i = sprintf(str, "%o", 15);
  printf("15 in octal is %s\n",   str);
  printf("sprintf returns: %d\n\n", i);

  i = sprintf(str, "%d", 15);
  printf("15 in decimal is %s\n", str);
  printf("sprintf returns: %d\n\n", i);

  i = sprintf(str, "%x", 15);
  printf("15 in hex is %s\n",     str);
  printf("sprintf returns: %d\n\n", i);

  i = sprintf(str, "%f", 15.05);
  printf("15.05 as a string is %s\n", str);
  printf("sprintf returns: %d\n\n", i);

  return 0;
}