简体   繁体   中英

Using printf inside a string

I'm trying to put printf(" in a string

#include<stdio.h>
#include<conio.h>
main()
{
      char print1="printf(\""; \\i used \ as an escape sequence for "
      printf("%s",print1);
      getch();
}

but it gives me the following warning:

5 [Warning] initialization makes integer from pointer without a cast

and when I execute the code,the output is : <null>

This line

char print1="printf(\""; \\i used \ as an escape sequence for "

should be

 char *print1="printf(\""; \\i used \ as an escape sequence for "

or perhaps

char print1[]="printf(\""; \\i used \ as an escape sequence for "

This is because, your assignment char print1 = "..."; assigns a string to a single character, eg the compiler converts the string to an unsigned number. To resolve this, use char *print1 = "..."; which will result in an array of characters.

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