• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

For C programmers...

BCYL

Diamond Member
I am taking an OS course and all our assignments have to be done in C... I am quite familiar with C++, but have not use C much, so I am having some trouble...

One of my problems is I can't figure out which standard libraries I should include. I have already include stdio.h, math.h, stat.h, types.h, and dir.h. What other ones should I include?

Also, whenever I use some flow control statements like for, while, if, etc I get a parse error... Is this because I did not include a standard library which contains these functions?

Thanks a lot!
 
One of my problems is I can't figure out which standard libraries I should include. I have already include stdio.h, math.h, stat.h, types.h, and dir.h. What other ones should I include?

You haven't said what functions you have a problem with so I can't tell you which header file(s) you need.

Also, whenever I use some flow control statements like for, while, if, etc I get a parse error... Is this because I did not include a standard library which contains these functions?

Are you sure you're a C++ programmer? Since when are flow control statements functions?

You really need to post some programs up which you are having problems with so I can help you. Saying "I get a parse error" isn't very useful.
 
Parse error is a grammatical error 🙂

and use _only_ the libraries you need, don't include excess headers.


 
I think I know why you get parse errors with your control flow.

In C, you can't define/create new variable in the middle of
functions/scope.

Thus a code like this doesn't work for C and will give
parse error :

/**** START CODE ****/
for (int i=0; i<10; i++)
a = i;

/**** END CODE *****/

It works fine for C++ code though.


To make the above code works, change to the following :

Declare/define int i at the beginning of the function then :

/***** START CODE ****/

for (i=0; i<10; i++)
a = i;
/***** END CODE ******/


edit : REMEMBER in C, you have to define all your variables
in the beginning of the scope/functions
(you can't do it in the middle of the scope/functions,
which is allowed in C++).

🙂
 
br0wn:

Thanks for your suggestion... Here is part of my code for my *.c file:

for ( i = 0; i < headCAP; i++ ) {
freeheads{i}= &amp;listheads{i};
}
Note: I use {i} so that the forum won't automatically change it... it's an array...

where headCAP is an integer I defined in my *.h file using:
#define headCAP 2

I also put int i; in my header file to initialize i...

However, this for loop is still giving me parse error... Also note that this for loop is contained right at the start of the *.c file, to initialize the array my program is to work on...

The only standard library I have included is stdio.h...

Thanks for your help!
 
how about upload the file that won't compile somewhere,
so we can look at the complete picture ?

From part of your code, it looks like you do it correctly,
so the errors might be somewhere else.

It is not an issue with the header files,
this won't have anything to do with parse error
(unless you include header file that has error 🙂)

 
This is my List.c file:

#include &quot;List.h&quot;

HEAD listheads[ headCAP ];
HEAD *freeheads[ headCAP ];

NODE listnodes[ nodeCAP ];
NODE *freenodes[ nodeCAP ];

for ( i = 0; i < headCAP; i++ ) {
freeheads = &amp;listheads;
}


int freeheadcnt = headCAP;


for ( i = 0; i < nodeCAP; i++ ) {
freenodes = &amp;listnodes;
}


int freenodecnt = nodeCAP;


LIST *ListCreate()
{
if( freeheadcnt > 0 )
{
LIST *tmp;
LIST templist;
tmp = &amp;templist;
tmp->headptr = freeheads[ freeheadcnt ];
freeheadcnt = freeheadcnt - 1;
tmp->currptr = NULL;
tmp->count = 0;
return tmp;
}
else {
return NULL;
}
};


And this is my List.h file:

#include <stdio.h>

#define headCAP 2
#define nodeCAP 10

typedef struct List {
struct Head *headptr;
struct Node *currptr;
int count;
} LIST;

typedef struct Head {
struct Node *ptr;
} HEAD;

typedef struct Node {
void *item;
struct Node *prevptr;
struct Node *nextptr;
} NODE;

int i;

LIST *ListCreate();
 
Now we know what your errors are 🙂

You can't put for loop like that, it has
to be in a function (maybe create initialize
function for it).

In fact, you can't put any statements like that
(outside of functions), unless it is a declaration
or definition.
 
Thanks a lot br0wn! It's working now 🙂

You are so much more helpful than my school's TAs! 😀
 
Back
Top