Check if bit is changed - c

I want transmit the state of a bit. When it is set, it should be transmetted for 10 secondes even if its status changes.
here is my solution:
unsigned long Time;
unsigned char State;
unsigned char Flag;/*It is set by an other function*/
unsigned char Bit;
#define BITDETECTION 1
#define COUNT 2
void My_Function ()
{
Bit = (Flag == 0)?0:1;
switch(State)
{
case BITDETECTION:
if(Bit == 0) Transmitte(Bit);
else {State = COUNT; time = GetTime();/*Get the current time*/}
break;
case COUNT:
if( GetTime() - time) <= 10 ) Transmitte(Bit);
else State = BITDETECTION;
break;
default:break;
}
}
Is it correct?

Here is a proposal:
void My_Function ()
{
Bit = (Flag == 0)?0:1;
switch(State)
{
case COUNT:
if( GetTime() - time) <= 10 )
{
Transmitte(Bit);
break;
}
else
{
State = BITDETECTION;
/* fall through to next case */
}
case BITDETECTION:
if(Bit != 0)
{
State = COUNT;
time = GetTime();/*Get the current time*/
}
Transmitte(Bit);
break;
default: abort();
}
}

Related

QMK RGB saturation bottoms out

RGB keycodes RGB_HUI, RGB_HUD, RGB_SAI, RGB_SAD, RGB_VAI, and RGB_VAD allow me to increment or decrement hue, saturation, and value on my RGB underglow lights. But I wanted to be able to hold down a key to change the color continually. So I made these changes:
In config.h:
#ifdef RGBLIGHT_ENABLE
#define RGBLIGHT_HUE_STEP 1
#define RGBLIGHT_SAT_STEP 1
#define RGBLIGHT_VAL_STEP 1
#endif
And then in keymap.c I made the custom keycodes:
enum custom_keycodes {
KC_HUI = SAFE_RANGE,
KC_HUD,
KC_SAI,
KC_SAD,
KC_VAI,
KC_VAD
};
And defined some variables:
// flags. 0 = no change, 1 = increment, -1 = decrement.
int8_t change_hue = 0;
int8_t change_saturation = 0;
int8_t change_value = 0;
// timers to control color change speed
uint16_t hue_timer = 0;
uint16_t saturation_timer = 0;
uint16_t value_timer = 0;
// seconds it takes to cycle through 0-255 or back
// technically 1 = 1.024 seconds, yielding even multiples of 4ms per tick (1024 / 256).
const int8_t hue_seconds = 5;
const int8_t saturation_seconds = 5;
const int8_t value_seconds = 5;
Then I use the keycodes to set the flags and timers:
bool process_record_user(uint16_t keycode, keyrecord_t *record) {
switch (keycode) {
case KC_HUI:
if (record->event.pressed) {
hue_timer = timer_read();
change_hue = 1;
} else {
change_hue = 0;
}
break;
case KC_HUD:
if (record->event.pressed) {
hue_timer = timer_read();
change_hue = -1;
} else {
change_hue = 0;
}
break;
case KC_SAI:
if (record->event.pressed) {
saturation_timer = timer_read();
change_saturation = 1;
} else {
change_saturation = 0;
}
break;
case KC_SAD:
if (record->event.pressed) {
saturation_timer = timer_read();
change_saturation = -1;
} else {
change_saturation = 0;
}
break;
case KC_VAI:
if (record->event.pressed) {
value_timer = timer_read();
change_value = 1;
} else {
change_value = 0;
}
break;
case KC_VAD:
if (record->event.pressed) {
value_timer = timer_read();
change_value = -1;
} else {
change_value = 0;
}
break;
}
return true;
}
Then I use a matrix scan to change the color using those flags and timers:
void matrix_scan_user(void) {
switch (change_hue) {
case 0:
break;
case 1:
if (timer_elapsed(hue_timer) > (hue_seconds * 4)) {
hue_timer = timer_read();
rgblight_increase_hue();
}
break;
case -1:
if (timer_elapsed(hue_timer) > (hue_seconds * 4)) {
hue_timer = timer_read();
rgblight_decrease_hue();
}
break;
}
switch (change_saturation) {
case 0:
break;
case 1:
if (timer_elapsed(saturation_timer) > (saturation_seconds * 4)) {
saturation_timer = timer_read();
rgblight_increase_sat();
}
break;
case -1:
if (timer_elapsed(saturation_timer) > (saturation_seconds * 4)) {
saturation_timer = timer_read();
rgblight_decrease_sat();
}
break;
}
switch (change_value) {
case 0:
break;
case 1:
if (timer_elapsed(value_timer) > (value_seconds * 4)) {
value_timer = timer_read();
rgblight_increase_val();
}
break;
case -1:
if (timer_elapsed(value_timer) > (value_seconds * 4)) {
value_timer = timer_read();
rgblight_decrease_val();
}
break;
}
}
According to the docs, each of those should wrap around at the max/min hue/saturation/value, respectively.
That seems to work exactly as expected with KC_HUI and KC_HUD: If I hold the key down long enough it rounds the bend and hue just keeps on cycling through.
KC_SAI, KC_VAI, and KC_VAD work at least normal-ish. I can increase saturation or value to the max and it will just stop there, and likewise if I decrement the value it stops at zero.
KC_SAD is the problem. When I hold it down long enough (just past white), the keyboard bombs out. It turns a weird orange color and nothing works.
For reference, here is the GitHub commit where I made all the changes, along with the rest of my code of course.
Any idea why KC_SAD would bomb out rather than either stopping or wrapping around?
Any idea why both saturation and value just stop, rather than wrapping around as I'd expect based on my read of the docs?
Might I do better to try this whole thing a different way (question to more seasoned users)?
I experimented with this some more, and came up with a different implementation which is much more efficient in terms of lines of code and bytes added to the firmware size. Note that instead of defining new custom keycodes, instead it hijacks the existing RGB_... ones for this new and improved purpose:
#include <lib/lib8tion/lib8tion.h>
// flags. 0 = no change, 1 = increment, -1 = decrement.
int8_t change_hue = 0;
int8_t change_sat = 0;
int8_t change_val = 0;
// timer to control color change speed
uint16_t change_timer = 0;
const uint16_t change_tick = 15;
void matrix_scan_user(void) {
if (change_hue != 0 || change_val != 0 || change_sat != 0) {
if (timer_elapsed(change_timer) > change_tick) {
HSV hsv = rgblight_get_hsv();
hsv.h += change_hue;
hsv.s = change_sat > 0 ? qadd8(hsv.s, (uint8_t) change_sat) : qsub8(hsv.s, (uint8_t) -change_sat);
hsv.v = change_val > 0 ? qadd8(hsv.v, (uint8_t) change_val) : qsub8(hsv.v, (uint8_t) -change_val);
rgblight_sethsv_noeeprom(hsv.h, hsv.s, hsv.v);
change_timer = timer_read();
}
}
}
bool process_record_user(uint16_t keycode, keyrecord_t *record) {
if (record->event.pressed) {
switch (keycode) {
// clang-format off
case RGB_HUI: change_timer = timer_read(); change_hue = 1; return false;
case RGB_HUD: change_timer = timer_read(); change_hue = -1; return false;
case RGB_SAI: change_timer = timer_read(); change_sat = 1; return false;
case RGB_SAD: change_timer = timer_read(); change_sat = -1; return false;
case RGB_VAI: change_timer = timer_read(); change_val = 1; return false;
case RGB_VAD: change_timer = timer_read(); change_val = -1; return false;
// clang-format on
}
} else {
bool rgb_done = false;
switch (keycode) {
case RGB_HUI:
case RGB_HUD:
change_hue = 0;
rgb_done = true;
break;
case RGB_SAI:
case RGB_SAD:
change_sat = 0;
rgb_done = true;
break;
case RGB_VAI:
case RGB_VAD:
change_val = 0;
rgb_done = true;
break;
}
if (rgb_done) {
HSV final = rgblight_get_hsv();
rgblight_sethsv(final.h, final.s, final.v);
}
}
return true;
}
This is a really nice bit of functionality!
I copied your code into one of my keyboards, and I am not able to reproduce the "bomb out" behavior that you are seeing on KC_SAD.
The documentation is incorrect. The increase/decrease functions for saturation and value do not wrap around, and to my knowledge, they are not intended to wrap. They max out at 255, and are bottom limited at 0. I'll be filing a PR to fix the documentation.
The only significant problem that I see with your implementation is that it results in VERY many writes to eeprom, and that isn't healthy (they wear out over time). You should use, for instance, rgblight_increase_sat_noeeprom() to adjust the saturation (and its counterparts for hue / saturation, and for decrease). Then when you release the key, only then should you write the current HSV setting to eeprom (using rgblight_sethsv() to set the final value into eeprom.

No output from the program

In our student class we are building a small program to process the data from Oil & Gas Industry. The program at a rough condition, but we learn a lot through the project.
The program is compiled this way
In the below sections you can find the whole code of the source files. Here is the compilation routine:
gcc -fPIC -c parser.c -o parser.o
gcc -shared parser.c -o parser.dll
gcc -c well.c - o well.o
gcc -c main.c -o main.o
gcc main.o well.o -L. -lparser -o well.exe
Then we execute it this way: well.exe "WELL_DATA.csv" "NUM OF WELL TO FIND" >> well.log
The problem the execution starts, but the output from fprintf appears just sometimes, but usually not... We hope you can help us resolve our problem
main.c file content
#include"well.h"
FILE* input;
struct WELL* head = NULL; //the head of the list
struct TNode* tree = NULL;
int main(int argc, char *argv[3]) {
CBUFFER* buffer = malloc(sizeof(CBUFFER));
buffer->len = BUFFER_SIZE;
input = fopen(argv[1], "r");
struct WELL* instance = NULL;
while(buffer->len != 0){
flushBuffer(buffer);
getNextLine(input, buffer);
if (buffer->len == 0)
break;
parseData(buffer);
instance = createWell(buffer);
addWell(instance, &head);
}
instance = getWell(argv[2], head);
if (instance != NULL)
fprintf(stdout,"NAME = %s\tLIFT_TYPE = %i\tMONTHLY_OIL_FLOW_RATE = %.2f\n", instance->name, instance->prodMethod, instance->oil.monthly);
else fprintf(stdout,"No such well\n");
freeList(head);
fclose(input);
return 0;
}
In the following sections you can find the rest of the code. Here is the parser.c which processes the data:
#include "parser.h"
unsigned short getNextLine(FILE* input, CBUFFER* buffer){
//printf("Getting next line\n");
unsigned short len = 0;
char symbol = fgetc(input);
if (!input || !buffer)
return 0;
while ((symbol != '\n' && (symbol != EOF))){
//
*((buffer->data) + len) = symbol;
symbol = fgetc(input);
len++;
}
*((buffer->data) + len) = '\n';
buffer->len = len;
buffer->index = 0;
return buffer->len;
}
unsigned short parseData(CBUFFER* buffer){
char *temp = malloc(sizeof(char) * (buffer->len + 1));
unsigned short i = 0;
unsigned short j = 0;
while (*((buffer->data) + i) != '\n'){
if (*((buffer->data) + i) == '\0' || *((buffer->data) + i) == '\r' || *((buffer->data) + i) == '\v' ||
*((buffer->data) + i) == '\t' || *((buffer->data) + i) == '\f')
i++;
else{
if (*((buffer->data) + i) == ';')
*(temp + j) = '\0';
else
*(temp + j) = *((buffer->data) + i);
j++;
}
i++;
}
*(temp + j) = '\n';
flushBuffer(buffer);
i = 0;
while (*(temp + i) != '\n'){
*((buffer->data) + i) = *(temp + i);
i++;
}
*((buffer->data) + i) = '\0';
free(temp);
buffer->len = i;
return buffer->len;
}
char* getValue(CBUFFER* buffer)
{
int i = buffer->index;
int j = buffer->index;
char c;
for (; i <= buffer->len; i++)
{
c = buffer->data[i];
if (c == '\0')
{
char* newValue = (char*)malloc(sizeof(char) * (i - buffer->index + 1));
for (; j <= i ; j++)
{
newValue[j - buffer->index] = buffer->data[j];
}
newValue[j - buffer->index] = '\0';
buffer->index = i + 1;
return newValue;
}
}
return NULL;
}
DATE getDate(char* input, char delimeter){
DATE date;
unsigned short number = 0;
char c = input[0];
for(unsigned short i = 1, j = 0; i < 12; i++){
number = number * 10 + atoi(&c);
c = input[i];
if((c == delimeter) || (c == '\0')){
if (j == 0) date.day = number;
if (j == 1) date.month = number;
if (j == 2){
date.year = number;
break;
}
number = 0;
j++;
}
}
return date;
}
int flushBuffer(CBUFFER* buffer) {
unsigned short i = 0;
while( i < buffer->len){
*((buffer->data)+i) = '\0';
i++;
}
buffer->len = 0;
return 0;
}
And the part which defines the well-object we work with well.c:
#include "well.h"
struct WELL* createWell(CBUFFER* pbuffer) {
struct WELL* instance = malloc(sizeof(struct WELL));
char * nextValue = getValue(pbuffer);
unsigned i = 0;
while ( nextValue != NULL){
setProperty(instance, i, nextValue);
i++;
nextValue = getValue(pbuffer);
}
instance->next = NULL;
return instance;
}
unsigned setProperty(struct WELL* instance, unsigned prop_index, char* value) {
switch(prop_index){
case 0:
setWellName(instance, value);
break;
case 1:
setWellDate(instance, getDate(value, '/'));
break;
case 2:
setProductionMethod(instance, value);
break;
case 3:
setFormation(instance, value);
break;
case 4:
setPump(instance, value);
break;
case 5:
setProduction(instance, OIL, MONTH, atof(value));
break;
case 6:
setProduction(instance, OIL, YEAR, atof(value));
break;
case 7:
setProduction(instance, OIL, COMMULATIVE, atof(value));
break;
case 8:
setProduction(instance, WATER, MONTH, atof(value));
break;
case 9:
setProduction(instance, WATER, YEAR, atof(value));
break;
case 10:
setProduction(instance, WATER, COMMULATIVE, atof(value));
break;
// PAY ATTENTION THAT cases from 11 to 13 are skipped!
// THIS IS BECAUSE WE DON'T NEED m3 PARAMETERS as it is
// AND CAN CALCULATE THEM from tons using density parameter
case 14:
setProduction(instance, MIX, MONTH, atof(value));
break;
case 15:
setProduction(instance, MIX, YEAR, atof(value));
break;
case 16:
setProduction(instance, MIX, COMMULATIVE, atof(value));
break;
case 17:
setProduction(instance, MIX, MONTH, atof(value));
break;
case 18:
setProduction(instance, MIX, YEAR, atof(value));
break;
case 19:
setProduction(instance, MIX, COMMULATIVE, atof(value));
break;
}
return 0;
}
unsigned addWell(struct WELL* instance, struct WELL** head) {
if (*head == NULL) {
*head = instance;
} else {
struct WELL* tmp = *head;
while (tmp->next){
tmp = tmp->next;
}
tmp->next = instance;
}
return 0;
}
struct WELL* getWell(char* key, struct WELL* head) {
if (head == NULL) return NULL;
struct WELL* currentWell = head;
while (strcmp(currentWell->name, key) != 0) {
if (currentWell->next == NULL) return NULL;
currentWell = currentWell->next;
}
return currentWell;
}
char* setWellName(struct WELL* instance, char* value) {
strcpy(instance->name, value);
return instance->name;
}
char* setFormation(struct WELL* instance, char* value) {
strcpy(instance->devFormation, value);
return instance->devFormation;
}
char* setPump(struct WELL* instance, char* value) {
strcpy(instance->pump, value);
return instance->pump;
}
DATE setWellDate(struct WELL* instance, DATE date) {
instance->startDate.day = date.day;
instance->startDate.month = date.month;
instance->startDate.year = date.year;
return instance->startDate;
}
WLIFT_TYPE setProductionMethod(struct WELL* instance, char* value) {
if (!strcmp(value, "flush"))
instance->prodMethod = FLUSH;
if (!strcmp(value, "gaslift"))
instance->prodMethod = GAS_LIFT;
if (!strcmp(value, "pump"))
instance->prodMethod = PUMP;
return instance->prodMethod;
}
double setProduction(struct WELL* instance, WFLUID fluid, WPERIOD period , double value) {
if (fluid == OIL) {
if (period == MONTH) {
instance->oil.monthly = value;
return instance->oil.monthly;
} else if (period == YEAR) {
instance->oil.year = value;
return instance->oil.year;
} else if (period == COMMULATIVE) {
instance->oil.commulative = value;
return instance->oil.commulative;
}
} else if (fluid == GAS) {
if (period == MONTH) {
instance->gas.monthly = value;
return instance->gas.monthly;
} else if (period == YEAR) {
instance->gas.year = value;
return instance->gas.year;
} else if (period == COMMULATIVE) {
instance->gas.commulative = value;
return instance->gas.commulative;
}
} else if (fluid == WATER) {
if (period == MONTH) {
instance->water.monthly = value;
return instance->water.monthly;
} else if (period == YEAR) {
instance->water.year = value;
return instance->water.year;
} else if (period == COMMULATIVE) {
instance->water.commulative = value;
return instance->water.commulative;
}
} else if (fluid == MIX) {
if (period == MONTH) {
instance->liquid.monthly = value;
return instance->liquid.monthly;
} else if (period == YEAR) {
instance->liquid.year = value;
return instance->liquid.year;
} else if (period == COMMULATIVE) {
instance->liquid.commulative = value;
return instance->liquid.commulative;
}
}
}

Escape delay with a button in a game

I have a problem with a piece of code for a game. I would like to know, how to break the time delay if I press the button.
List of files
At the end, the game writes your end score on the LCD for 10 seconds. And goes back to the wait for start. I want to keep that as it is - the only thing that I want to include is that if I press the button within those 10 seconds I want it to go immediately back to the wait for start.
Function code is here
void DrawGameScore()
{ //&RFONT_8X12 &RFONT_16X26
//char key;
//key = KBD_GetKey();
char txt[10];
UG_FontSelect(&RFONT_16X26);
UG_SetForecolor(C_WHITE);
switch (level){
case 1:
UG_PutString(60,130,"Peasant Score:");
sprintf(txt,"%d",score);
UG_PutString(120,160,txt);
_delay_ms(10000);
break;
case 2:
UG_PutString(60,130,"Knight Score:");
sprintf(txt,"%d",score);
UG_PutString(120,160,txt);
_delay_ms(10000);
break;
case 3:
UG_PutString(60,130,"Queen Score:");
sprintf(txt,"%d",score);
UG_PutString(120,160,txt);
_delay_ms(10000);
break;
case 4:
UG_PutString(60,130,"King Score:");
sprintf(txt,"%d",score);
UG_PutString(120,160,txt);
_delay_ms(10000);
break;
case 5:
UG_PutString(60,130,"God Score:");
sprintf(txt,"%d",score);
UG_PutString(120,160,txt);
_delay_ms(10000);
break;
}
//UG_PutString(60,130,"Final Score:");
//sprintf(txt,"%d",score);
//UG_PutString(120,160,txt);
//_delay_ms(1000);
}
When the game is over this function draws your score based on your level. In this next function i am calling drawgamescore function, which is in dsip_end_score makro.
int EndOfGame()
{
char key;
//static int state;
int result = 1;
key = KBD_GetKey();
display_msg = DSIP_END_SCORE;
if (key == BTN_OK){
//display_msg = DISP_REFRESH_ALL;
//display_msg = DSIP_END_SCORE;
return result;
}
return result;
}
Here is the main game function
void Game()
{
static int state;
int result;
switch (state)
{
case 1: //waiting for the game start
result = WaitForStart();
if (result == 1) state++;
break;
case 2: //Play the game
result = PlayTheGame();
if (result == 1) state++;
break;
case 3: //Display the score
result = EndOfGame();
if (result == 1) state=0;
break;
default: //0 or unknown value: reset the game
state = 1;
break;
}
}
Thanks.
I tried
int EndOfGame()
{
char key;
static int state;
int result = 1;
key = KBD_GetKey();
display_msg = DSIP_END_SCORE;
if (key == BTN_OK){
//display_msg = DISP_REFRESH_ALL;
//display_msg = DSIP_END_SCORE;
return result;
}
return result;
}
/*
display_msg = DSIP_END_SCORE;
if (key != 0){
switch (key)
{
case BTN_OK:
return result;
break;
}
}
*/
/*
switch (state)
{
case 0:
display_msg = DSIP_END_SCORE;
state++;
break;
case 1:
key = KBD_GetKey();
if (key != 0){
switch (key)
{
case BTN_OK:
return result;
break;
}
break;
}
}
return result;
*/
//display_msg = DISP_REFRESH_ALL;
//display_msg = DSIP_END_SCORE;
//return result;
/*
char key;
int result=0;
//static int state;
key = KBD_GetKey();
if (KBD_isKeyStatePressed(BTN_OK))
{
display_msg = DSIP_END_SCORE;
result = 1;
} else {
result = 1;
}
//return result;
*/
/*
switch (state){
case 0:
KBD_flush(); //empty buffer
//display_msg = DISP_REFRESH_ALL;
display_msg = DSIP_END_SCORE;
return result;
state++;
break;
case 1:
key = KBD_GetKey(); //kbd read dela background services
if (key == BTN_OK){
//display_msg = DISP_REFRESH_ALL;
display_msg = DSIP_END_SCORE;
return result;
break;
}
break;
}
return result;
*/
/*
key = KBD_GetKey();
if (key == BTN_OK) {
//display_msg = DISP_REFRESH_ALL;
return result;
}else{
display_msg = DISP_REFRESH_ALL;
display_msg = DSIP_END_SCORE;
//TODO: write the program
return result;
}*/
I found a solution!
Here is the code if anyone is interested.
int EndOfGame()
{
int result = 0;
char key;
static uint32_t timeStamp;
static int state = 0;
display_msg = DISP_REFRESH_ALL;
switch (state)
{
case 0: //Show end score
display_msg = DSIP_END_SCORE;
timeStamp = GetSysTick();
state++;
break;
case 1: //read keys after 2 seconds
if (Has_X_MillisecondsPassed(2000, &timeStamp)
{
state++;
KBD_flush();
break;
}
break;
case 2:
key = KBD_GetKey();
if (key != 0)
{
switch (key)
{
case BTN_OK:
state = 0;
result = 1;
break;
}
}
//finish after 10 seconds
if (Has_X_MillisecondsPassed(10000, &timeStamp))
{
result = 1;
state = 0;
break;
}
break;
}
return result;
}
If you want to know about the
Has_X_millisecondsPassed();
function you are welcome to see it in systime.c in the File list hyperlink in the question above. For the reference i'm using AtMega328pb microcontroller.

C error C2660: 'Menu' : function does not take 3 arguments

I am new to programming and do not understand this error.
I have the same arguments in the Menu () function and when I call the same function in the menu_principal () function.
In function menu_principal(), I want execute the switch-case statement by the function Menu() with 'option' variable.
Can you help please?
int main()
{
void menu_principal();
return 0;
}
void menu_principal()
{
bool stop = true;
int option;
const char *title = "MENU PRINCIPAL";
const char *options_menu[] = { "ARTIGOS", "CLIENTES", "ORCAMENTOS", "SAIR" };
int n_options = 4;
do
{
option = Menu(title, options_menu, n_options);
switch (option)
{
case 1:
Menu_Item();
break;
case 2:
Menu_Client();
break;
case 3:
Menu_Billing();
break;
case 4:
stop = false;
break;
}
} while (stop);
}
int Menu(const char *title1, const char *options_menu1[], int n_options1)
{
int OptionSelected= 1;
int key;
bool stop = true;
do
{
system("cls");
gotoxy(5, 3 + OptionSelected); printf(">>");
gotoxy(15, 2); printf("%s", title1);
for (int i = 0; i < n_options1; i++)
{
gotoxy(10, 4 + i);
printf("%s ", options_menu1[i]);
}
do
{
key = _getch();
} while (key != KEY_UP && key != KEY_DOWN && key != KEY_ENTER );
switch (key)
{
case KEY_UP:
OptionSelected--;
if (OptionSelected < 1)
{
OptionSelected = n_options1;
}
break;
case KEY_DOWN:
OptionSelected--;
if (OptionSelected > n_options1)
{
OptionSelected = 1;
}
break;
case KEY_ENTER:
stop = false;
break;
}
} while (stop);
return OptionSelected;
}
The compiler reads your program top to bottom, so it sees:
option = Menu(title, options_menu, n_options);
On this line, you call a previously unknown function, Menu.
Because the function is unknown, the compiler assumes it will be int Menu(void). (takes no parameters, returns an int).
That assumption is obviously different from how Menu eventually gets declared.
To fix this, declare the function properly near the top of your file:
int Menu(const char *title1, const char *options_menu1[], int n_options1);
Then, when the compiler encounters your function-call, it will not assume a declaration, it will use the declaration you already provided (takes 3 parameters, and returns an int)

error : expected identifier or '(' in global variable

I got this errror during compile processing.
I already saw this error in this site before, I think this error can occur when the ; or something are in wrong place. but I can't find.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#define MAX 80
typedef struct assemble
{
int op;
int ni;
int xbpe;
int addr;
}Assemble;
void get_token(char *bp);
int hTod(char *dp);
int bTod(char *dp);
void codeBreak(char *cp);
void result(int t);
Assemble asm;
int type;
int main()
{
FILE *fp;
char buf[MAX];
if( (fp = fopen("inst.txt","r")) == NULL ){
fprintf(stderr, "file not found...\n"); exit(1);
}
while(fgets(buf,sizeof(buf),fp) != NULL){
get_token(buf);
}
fclose(fp);
return 0;
}
void get_token(char *bp)
{
char *cp;
int i = 1;
for(cp=strtok(bp, " \t\n"); cp != NULL; )
{
if( (*(cp+0) == '0') && (*(cp+1) == 'x') )
{
if( i == -1 )
asm.addr = hTod(cp);
if( i == 1)
asm.op = hTod(cp);
i *= -1;
}
else
{
codeBreak(cp);
}
cp = strtok(NULL, " \t\n");
}
result(type);
}
void codeBreak(char *cp)
{
if ( strlen(cp) == 2 ) // ni 판단
{
asm.ni = bTod(cp);
}
else if( strlen(cp) == 1 )
{
asm.xbpe = bTod(cp)*pow(2,3);
type = 1;
}
else if( strlen(cp) == 4 )
{
if( *(cp+3) == '1')
{
asm.xbpe = bTod(cp);
type = 2;
}
else
{
asm.xbpe = bTod(cp);
type = 3;
}
}
}
void result(int t)
{
switch(t)
{
case 1 : printf("0x%x%x\n", (asm.op+asm.ni), asm.addr); break;
case 2 : printf("0x%x%x%.5x\n", (asm.op+asm.ni), asm.xbpe, asm.addr); break;
case 3 : printf("0x%x%x%.3x\n", (asm.op+asm.ni), asm.xbpe, asm.addr); break;
default : break;
}
}
/* Hex to decimal */
int hTod(char *dp)
{
int i;
int dec = 0;
for( i=2 ; i < strlen(dp) ; i++)
{
switch (*(dp+i))
{
case 'a' : *(dp+i) = 58; break;
case 'b' : *(dp+i) = 59; break;
case 'c' : *(dp+i) = 60; break;
case 'd' : *(dp+i) = 61; break;
case 'e' : *(dp+i) = 62; break;
case 'f' : *(dp+i) = 63; break;
}
dec += (*(dp+i)-48) * pow( 16, (strlen(dp)-(i+1)));
}
return dec;
}
/* binary to decimal*/
int bTod(char *dp)
{
int i;
int dec = 0;
for( i = 0; i < strlen(dp) ; i++)
{
dec += (*(dp+i)-48) * pow( 2, (strlen(dp) - (i+1)));
}
return dec;
}
asm is a keyword. Instead of
Assemble asm;
use something different, such as:
Assemble asm1;
The problem reported by the compiler is different though. You seem to have used
ASSEMBLY asm;
in the code you compiled.

Resources