Hi, I am using Keil uVision3 and have written code in C which includes three files as key.c,key.h and main.c. when I compile my code as error C129: missing ';' before '.' for the line exactly where I initialize my structure element to zero in key.c
My code is as follows:
File main.c
#include <REG51.H> #include "key.c" void main() { key_task(); while(1); }
File key.h
#ifndef KEYPAD_H_INCLUDED #define KEYPAD_H_INCLUDED sbit ke1=P2^0; sbit ke2=P2^1; sbit ke3=P2^2; sbit ke4=P2^3; sbit ds0=P3^4; sbit ds1=P3^5; sbit ds2=P3^6; sbit ds3=P3^7; typedef struct { unsigned char key_pres_cnt,key_rls_cnt,key_pres_flg,key_rls_flg,key_type,ket_status; }key; extern key k1,k2,k3,k4; enum key_stat{no_key_pres,key_pres}; void key_task(); #endif /*KEYPAD_H_INCLUDED*/
File key.c
#include <REG51.H> #include "key.h" key k1,k2,k3,k4; k1.key_pres_cnt=0; k1.key_pres_flg=0; k1.key_rls_cnt=0; k1.key_rls_flg=0; void key_task() { //Logic for first key if(ke1==no_key_pres) { if(k1.key_pres_cnt<10) k1.key_pres_cnt++; else { if(k1.key_pres_cnt) k1.key_pres_cnt--; } if(k1.key_pres_cnt>=10) { k1.key_pres_cnt=0; k1.key_pres_flg=1; } if(k1.key_pres_flg==1) ds0=0; if(ke1==key_pres) { if(k1.key_rls_cnt<10) k1.key_rls_cnt++; else { if(k1.key_rls_cnt) k1.key_rls_cnt--; } if(k1.key_rls_cnt>=10) { k1.key_rls_cnt=0; k1.key_rls_flg=1; k1.key_pres_flg=0; } if(k1.key_rls_flg==1) ds0=1; } } }
Please help me
Thanks in advance
How about picking up a good book about programming in C?
The extension *.h means "header".
So you should #include "key.h" and not #include "key.c".
"key.c" on the other hand, should be added to your project, so the compiler will compile it into an object file, and so that the generated object file will be included when linking the project.
The poor compiler now see the source lines in key.c but haven't seen the data type "key" that is in "key.h".
Oh I made horrible mistake it won't be repeated again. Thanks Per but actual problem I am facing is in initializing structure member as I want to initialize only few of them and not all. As the syntax varies from compiler.
Not sure why you think italic is a good choice for your questions. Formatting options are there to allow people to put special focus on some parts of text.
Anyway - it's a question of C language standard if the compiler supports the designated initializer format, or if you are limited to the classical concept where n values will be assigned to the n first elements of a struct and any remaining elements will be set to zero. And whatever you do - avoid any non-standard gcc extensions you might have seen.
But your attempt:
k1.key_pres_cnt=0;
is neither the classical construct or the C99 designated initializer format. This is how you assign values to a field when inside a function while the program is running. There is a big difference between assigning initial values to variables and assigning values at run time.
See some discussion about it here: stackoverflow.com/.../c-struct-initialization-using-labels-it-works-but-how-documentation
Hi, Per I have changed the initialization part as per you said rest of the code is as it is, I tried it in two ways
First Case
key k1={.key_pres_cnt=0,.key_pres_flg=0,.key_rls_cnt=0,.key_rls_cnt=0};
for which I got the error as
error C141: syntax error near '.' for the same line
Second Case
key k1={key_pres_cnt:0,key_pres_flg:0,key_rls_cnt:0,key_rls_cnt:0};
for which I got the errors as
error C202: 'key_pres_cnt': undefined identifier
error C141: syntax error near ':'
error C231: 'key_task': redefinition
error C141: syntax error near '{'
error C231: 'no_key_pres': redefinition
error C231: 'key_pres_cnt': redefinition
error C248: aggregate initialization needs curly braces
error C129: missing ';' before '.'
So you didn't spend time and read the information I linked to?
The ":" form was specifically described in that link to be an old gcc extension. And I said you should stay away from any gcc extensions you might have seen.
And the "." form is C99 - which requires that the compiler supports the C99 language standard, and that you have enabled C99.
Note that you - for some very strange reason - have selected "Toolset = None" instead of selecting C51 which the used header file seems to indicate that you are using.
http://www.keil.com/support/docs/1893.htm
See specifically
Information in this article applies to: [...] C51 All Versions
and the note
Keil C compilers are based on C90. We added some language extensions as practical concessions to the architectural peculiarities of the microcontrollers we support and the needs of embedded systems programmers.
So unless Keil has failed to keep that linked document up to date, you don't get C99 support with the C51 compiler.
Thanks a lot Per, problem solved