How to use printf with format from a string and numbers for string offset in C

1 Answer

0 votes
#include <stdio.h>

int main(int argc, char **argv)
{ 
	char format[] = "%d %c\n", s[] = "abcdefgh"; 
	
	printf(format, 0[s], 1[s + 4]); 
	printf(format, 0[s], 2[s + 4]); 
	printf(format, 1[s], 3[s + 4]); 
	printf(format, 1[s], 3[s + 3]); 
	
	printf("%d %c\n", *(0 + "abcdefgh"), *(1 + "abcdefgh" + 4));
	printf("%d %c\n", *(0 + "abcdefgh"), *(2 + "abcdefgh" + 4));
	printf("%d %c\n", *(1 + "abcdefgh"), *(3 + "abcdefgh" + 4));
	printf("%d %c\n", *(1 + "abcdefgh"), *(3 + "abcdefgh" + 3));
	
	return 0; 
}   
 

/*
run:
 
97 f
97 g
98 h
98 g
97 f
97 g
98 h
98 g
 
*/

 



answered Jan 17, 2019 by avibootz
edited Jan 17, 2019 by avibootz

Related questions

2 answers 284 views
1 answer 111 views
1 answer 113 views
1 answer 165 views
...