-3
char zk[] = "All is great";

I have to write two possibilities printf commands to print out all is great, starting from third position. I have following:

1. 
    printf("%s\n", &zk[2]);
2.
    for (int x = 2; x < strlen(zk); x++) printf("%c", zk[x]);

Both working for me, but I think i have to avoid a for loop, and just use the print command, is there any other way to print out All is great from the third position?

Panther
  • 15
  • 1
  • 6
  • @Cool Guy &Alter Mann edited. – Panther Apr 13 '15 at 14:32
  • 5
    Strings traditionally go into `char` arrays, too, not `int` arrays... – Crowman Apr 13 '15 at 14:33
  • Can you define how it's "working for you", because I get both `error: array of inappropriate type initialized from string constant` and `warning: passing argument 1 of 'printf' from incompatible pointer type`? – Jongware Apr 13 '15 at 14:39
  • @Jongware which variant #1 or #2 ? – Panther Apr 13 '15 at 14:48
  • All the variants that contained your original line `int zk[] = "All is great";`. The error and warning were issued by gcc, using `-pedantic -Wall` ("Check strictly, and everything is an error"); I'd strongly recommend to look up similar settings for your own setup. – Jongware Apr 13 '15 at 15:45

2 Answers2

3
printf("%s", string + offset)

And it will print your string starting from position offset.

Stefan Golubović
  • 1,225
  • 1
  • 16
  • 21
0

You can use the string as the first argument:

printf(&zk[2]);

This is ok as long as the string doesn't contain a %. Why? Read this to know it.

Community
  • 1
  • 1
Spikatrix
  • 20,225
  • 7
  • 37
  • 83