Is it possible to use for loop to reduce code in C?

Posted on

Question :

I have the following function: A .
Is it possible to use the B for loop to generate declarations as in the A function?
What to use instead of printf to become a declaration?



void functionX(unsigned int buf) {
    H8_3687_pulse(EEPROM_MASK(buf, EEPROM_A15));
    H8_3687_pulse(EEPROM_MASK(buf, EEPROM_A14));
    H8_3687_pulse(EEPROM_MASK(buf, EEPROM_A13));
    H8_3687_pulse(EEPROM_MASK(buf, EEPROM_A12));
    H8_3687_pulse(EEPROM_MASK(buf, EEPROM_A11));
    H8_3687_pulse(EEPROM_MASK(buf, EEPROM_A10));
    H8_3687_pulse(EEPROM_MASK(buf, EEPROM_A9 ));
    H8_3687_pulse(EEPROM_MASK(buf, EEPROM_A8 ));
    H8_3687_pulse(EEPROM_MASK(buf, EEPROM_A7 ));
    H8_3687_pulse(EEPROM_MASK(buf, EEPROM_A6 ));
    H8_3687_pulse(EEPROM_MASK(buf, EEPROM_A5 ));
    H8_3687_pulse(EEPROM_MASK(buf, EEPROM_A4 ));
    H8_3687_pulse(EEPROM_MASK(buf, EEPROM_A3 ));
    H8_3687_pulse(EEPROM_MASK(buf, EEPROM_A2 ));
    H8_3687_pulse(EEPROM_MASK(buf, EEPROM_A1 ));
    H8_3687_pulse(EEPROM_MASK(buf, EEPROM_A0 ));    


void functionX(unsigned int buf) {
    int i;
    char ad1[]="H8_3687_pulse(EEPROM_MASK(buf, EEPROM_A";
    char ad2[]="));";

    for (i=15; i>=0; i--) {


Answer :

How about something like this:

void functionX( unsigned int buf )
    int i;

    int addr[16] = { EEPROM_A15, EEPROM_A14, EEPROM_A13, EEPROM_A12,
                   EEPROM_A11, EEPROM_A10, EEPROM_A9, EEPROM_A8,
                   EEPROM_A7, EEPROM_A6, EEPROM_A5, EEPROM_A4,
                   EEPROM_A3, EEPROM_A2, EEPROM_A1, EEPROM_A0 };

    for( i = 0; i < 16; i++ ) {
        H8_3687_pulse( EEPROM_MASK( buf, addr[i] ) );



While I usually follow Lacobus’s response, this is a good place to remember that the tcc compiler has a dynamic-link library which allows the compilation of dynamic code:

#include "libtcc.h"

void (*)(unsigned int)
compilar_funcao(TCCState * s) {
char buffer[1024];
char * ptr = buffer;

/* monta o texto do programa */
ptr += sprintf(ptr,
"#include "header.h""n"" // cabeçalho com as definições de EEPROM_MASK e EEPROM_A*

Leave a Reply

Your email address will not be published. Required fields are marked *