a c question
a c question
Sorry, not the place of here but just searching a quick answer,
I wonder if c lets default value for function parameters. I know c++ lets but c?
I wonder if c lets default value for function parameters. I know c++ lets but c?
Re:a c question
Strcitly not. However I wouldn't be suprised if some C compilers allow it anyway.
Pete
Pete
Re:a c question
btw I have an optimization question sometimes in my C code i need to get the the quotient as well as the remainder part of a division. right now i do this in this way
i was just wondering is there a more efficient way in C to do this in a single op or at least does the compiler optimize this in any way?
also if the divisor is a power of 2 then what will be the effect on the code. i.e if the divisor is 8 i could use then if i use can this be optimized.
Code: Select all
int some1,some2;
some1= dividend/divisor;
// may or may not have statements here
some2= dividend%divisor;
also if the divisor is a power of 2 then what will be the effect on the code. i.e if the divisor is 8 i could use
Code: Select all
divisor>>3
Code: Select all
some2=dividend%divisor;
Only Human
Re:a c question
The C runtime provides div and ldiv, which return the quotient and remainder like this. The x86 provides the DIV and IDIV instructions which could be used to implement these routines. But hopefully the compiler will see that you're taking both the quotient and remainder of two numbers, and optimize appropriately. Don't worry about it.
Any recent compiler will optimize multiplies, divides and mods by powers to 2 to shifts and masks. So don't worry about that either.
Any recent compiler will optimize multiplies, divides and mods by powers to 2 to shifts and masks. So don't worry about that either.
Re:a c question
another GGC and NASM question
when i declare a variable static in 'C' i can't access it in NASM. so if i wanted to, how do i do so? (other than removing the 'static' keyword of course)
also the memory map shows these static variables are placed in the COMMON section of the final file. Why does this happen and what is the COMMON section for?
when i declare a variable static in 'C' i can't access it in NASM. so if i wanted to, how do i do so? (other than removing the 'static' keyword of course)
also the memory map shows these static variables are placed in the COMMON section of the final file. Why does this happen and what is the COMMON section for?
Only Human
Re:a c question
The only way to do this is to remove the static keyword. static says, "don't let any other module access this variable/function".Neo wrote: another GGC and NASM question
when i declare a variable static in 'C' i can't access it in NASM. so if i wanted to, how do i do so? (other than removing the 'static' keyword of course)
This is probably the same as .bss, i.e. variables which aren't initialised with any value, so which get the value 0 when the program is loaded. If you assigned them a value other than 0 they would get put in .data. Note that the default ld script puts all sections called COMMON into .bss.also the memory map shows these static variables are placed in the COMMON section of the final file. Why does this happen and what is the COMMON section for?
Re:a c question
you sure bout that definition of static?
i had always thought it meant for like globals.
eg: if you declare a static in a function, it retains its value.
static declars a 'local global' variable if you will...
kinda like the opposite of declaring something volatile.
(obviously volatile has totally different semantics but you get what i mean....)
i had always thought it meant for like globals.
eg: if you declare a static in a function, it retains its value.
static declars a 'local global' variable if you will...
kinda like the opposite of declaring something volatile.
(obviously volatile has totally different semantics but you get what i mean....)
-- Stu --
Re:a c question
The static keyword is overloaded many times in C++ for different reasons.
Code: Select all
static int var1; // var1 can only be accessed by this source file
// func1 can only be called by this source file
static int func1()
{
/* local can only be accessed by this function,
* but retains its value between calls */
static int local;
return 42;
}
class Class1
{
public:
static int member1;
static int method1();
};
static int Class1::member1; // Class1::member1 is shared between all instances of Class1
int Class1::method1()
{
/* Class1::method1 can be called on any instance
* of Class1, or on no instance. It is unable to use 'this'. */
return 42;
}
Re:a c question
right. i never do c++. hate it. so i dunno. i was only talking in a c context
i always thought applying static to a function meant it couldnt be unloaded type thing, i fi wanted a function to be private, i just didnt declare it in the header file, thus no outside functions could call it......
mm i learn something everyday!
i always thought applying static to a function meant it couldnt be unloaded type thing, i fi wanted a function to be private, i just didnt declare it in the header file, thus no outside functions could call it......
mm i learn something everyday!
-- Stu --
Re:a c question
Remove all the Class:: stuff from and it applies to C. I guarantee that static outside of a function prevents other modules from accessing the function or variable it applies to. They could have called it private but they didn't .
Re:a c question
NO sir,
-----------------------------------------------------
Sir i've something to ask u..
Can you please help me to make my own toy OS..
if yes please mail me back at
[email protected]
please sir,,,,,,....
-----------------------------------------------------
Sir i've something to ask u..
Can you please help me to make my own toy OS..
if yes please mail me back at
[email protected]
please sir,,,,,,....
Re:a c question
i have another c question.
If i have a file say "super.c" with its own "super.h" and 2 other files say "a.c" and "b.c" with their own "a.h" and "b.h" resply. is there any way i can make the functions of "super.h" implemented in "super.c" be avialble to both the files "a.c" and "b.c" by just including the "super.h" in the header files of both. if not then how do i do this?
If i have a file say "super.c" with its own "super.h" and 2 other files say "a.c" and "b.c" with their own "a.h" and "b.h" resply. is there any way i can make the functions of "super.h" implemented in "super.c" be avialble to both the files "a.c" and "b.c" by just including the "super.h" in the header files of both. if not then how do i do this?
Only Human
Re:a c question
even if the functions in super.h are static?
i know i should try them out to see if they work. but i just wanted to know
i know i should try them out to see if they work. but i just wanted to know
Only Human