C Programming Question

Status
Not open for further replies.

wuchy143

Member
Hi All,

I'm relatively new to C and was wondering what was going on with this piece of code(it's just a function)

Code:
U16 read_zero_push_state(void)
{
    U16 return_val;

    //disable all interrupts for var read
	//INTCONbits.GIE = 0;

    return_val = (zero_push & DEBOUNCE_MASK);
    
    //enable all interrupts
	//INTCONbits.GIE = 1;

    return(return_val);
}

I thought functions are to be declared such that:

Code:
My_Function(void)
         {
          do something
         }

How come there is a U16 and then a space in the original code I posted. What am I missing?
 
U16 is the declaration of the return variable. In this case I think it is a 16 bit unsigned int (not familiar with the compiler). If there is no return value, the function should be declared "void My_Function(void)"
 
oh ok. So the result(return_val in this case) gets stuffed into the U16 register once the function completes?

so when you say

Code:
void My_Function(void)
 {
  do something
 }

by putting the void in the beginning it implies that this specific function does not return a value?
 
Last edited:
I don't think this applies in this thread but it's a noob question so I'm going to ask it

Code:
static U8 tension_output_mode = TENSION_MODE;

I understand what a static variable is but when the say "static" then "U8" I get confused. What does that mean?
 
Last edited:
U8 is the data type and static means that it is only created once and is never destroyed.

If "static tension_output_mode = TENSION_MODE;" is valid then the data type is being implied (probably as int or similar, depends on compiler)
 
ok. so when you say, "static U8 tension_output_mode = TENSION_MODE;"

what you're really saying is that there is a static variable who is U8. This static variable(U8) is equal to whatever TENSION_MODE is equal to?
 
What I can understand from your code is, "read_zero_push_state" this is the function name which is going to return U16 data type this should be declared in the any one of your .h file and the function argument is void.
 
U8 is the data type and static means that it is only created once and is never destroyed.

If "static tension_output_mode = TENSION_MODE;" is valid then the data type is being implied (probably as int or similar, depends on compiler)

That depends on where it is declared, static can have diffirent meanings...

Code:
void Foo(void)
{
static int i = 5;

   //Some code here
}

In this case, static means the integer i is created only once, and assigned the initial value 5. It will not be destroyed when function Foo exits and recreated when Foo is entered again like a normal automatic variable but it will keep its value at all times troughout the program.

Code:
static int Global_i = 5;

Here, Global_i is declared as global variable (outside of a function). This means it can be accessed anywhere within that file.
Here, static means Global_i will have internal linkage. Wich means it will only be visible within the module where it is created.
 
I thought that the only difference there was the scope. Can you explain the difference between an ordinary global and a static global?
 
Say you are creating a LCD library for others to use. Your code uses a global variable called i.
Code:
int i;

Now someone else uses your LCD.c and LCD.h files in his project (or even the already compiled object file if it is closed source). His own code contains
a I²C library wich happens to also declare a global variable i in the same way. During linking you will get an error, because i is declared twice.

By making i static, it will only be seen within its own module, solving the problem.
Ofcourse you can only make the variable static if you don't intend to use it within another module, meaning that if you want to use it somewhere else (with extern) then you can't make it static.
 
So the scope of a global is "everywhere" and the scope of static global is the file it was declared in? (How do you define module?)
 
By module i mean object file , wich are the input files for the linker.
So yes, in C that will typically mean the file it was declared in. But weird constructions where multiple source files compile into a single object file are possible.
 
I thought functions are to be declared such that:

Code:
My_Function(void)
         {
          do something
         }

This is a function declaration:
Code:
void My_Function(void);

this is a function definition:
Code:
void My_Function(void)
{
do something
}

I know you hate me for this post, but it is important to use the correct terms. Especially when asking for advice on a forum like this. (and yes, I teach embedded programming. That's why I'm being 'pedantic').
 
Last edited:
oh ok. So the result(return_val in this case) gets stuffed into the U16 register once the function completes?

No, the U16 tells you that the function returns an unsigned 16 bit integer. The memory location where the return value is placed depends on how you call the function.

This places the return value in a variable called "var":
Code:
U16 var;
var = read_zero_push_state();

This discards the return variable completely. You can do this if you don't need the return value for anything.
Code:
read_zero_push_state();
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…