You are on the right track. This is the approach I would use. Although bitwise operations will also work, something needs to be said for readability.
#include <stdio.h> int main(int argc, char **argv) { int numbers[4] = {1, -1, -1, -1}; for (int i = 0; i < (sizeof(numbers) / sizeof(numbers[0])); i++) { printf("%s ", (numbers[i] < 0 ? "-" : "+")); } printf("\n"); return 0; }
Or you can build a line with sprintf() / snprintf() ; Not sure if you want to get it out or use it elsewhere.
source share