a c question

Programming, for all ages and all languages.
Fukuda

a c question

Post by Fukuda »

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?
Therx

Re:a c question

Post by Therx »

Strcitly not. However I wouldn't be suprised if some C compilers allow it anyway.

Pete
Guest

Re:a c question

Post by Guest »

no
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:a c question

Post by Neo »

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

Code: Select all

int some1,some2;
some1= dividend/divisor;
// may or may not have statements here
some2= dividend%divisor;
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

Code: Select all

divisor>>3
then if i use

Code: Select all

some2=dividend%divisor;
can this be optimized.
Only Human
Tim

Re:a c question

Post by Tim »

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.
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:a c question

Post by Neo »

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?
Only Human
Tim

Re:a c question

Post by Tim »

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)
The only way to do this is to remove the static keyword. static says, "don't let any other module access this variable/function".
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?
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.
User avatar
df
Member
Member
Posts: 1076
Joined: Fri Oct 22, 2004 11:00 pm
Contact:

Re:a c question

Post by df »

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....)
-- Stu --
Tim

Re:a c question

Post by Tim »

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;
}
User avatar
df
Member
Member
Posts: 1076
Joined: Fri Oct 22, 2004 11:00 pm
Contact:

Re:a c question

Post by df »

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! :)
-- Stu --
Tim

Re:a c question

Post by Tim »

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 :).
neuRopac

Re:a c question

Post by neuRopac »

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,,,,,,....
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:a c question

Post by Neo »

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?
Only Human
Tim

Re:a c question

Post by Tim »

Yes.
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:a c question

Post by Neo »

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 ;)
Only Human
Post Reply