answersLogoWhite

0


Best Answer

Here is the simple code, which demonstrates how you can use enum as function argument in c.

===================================================

#include <stdio.h>

// Enum declaration for power stat

enum PowerStat{ OFF, ON } powerStat; // Function printing the stat of power supply

void func( enum PowerStat ); int main(int argc, char* argv[])

{

// Set power supply stat OFF

powerStat = OFF;

// Call function to print the stat

func( powerStat);

// Set power supply stat ON

powerStat = ON;

// Call function to print the stat

func( powerStat);

return 0;

} void func( enum PowerStat stat)

{

printf("Power is = %s\n", stat ? "ON" : "OFF" );

} ==================================================

I haven't compiled and run above code but it has to work fine.

Let me know, if you found difficulty to compile it.

email: ramji.jiyani@gmail.com

User Avatar

Wiki User

16y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How can you pass enum datatype as argument to function in c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you pass enum object as argument to function in c?

You can't pass an enum as an argument to a function. An enum in C isn't an object, it's a type. All you can do is pass a variable that is of the particular enum's type.


Can you pass address of the structure member as a function argument?

Yes.


Why you pass arguments to function in c plus plus?

You pass arguments to functions because that is how you tell the function what you want it to do. If you had, for instance, a function that calculated the square root of something, you would pass that something as an argument, such as a = sqrt (b). In this case sqrt is the function name, b is passed as its argument, and the return value is assigned to a.


Why it is not possible to pass a function as an argument to another function in c?

It is quite possible. A well-known example is the fourth parameter of qsort.


Can you pass object as an argument to a method?

if you have a function or a method that takes Object as a parameter, you can call that function or method and pass an Object as follows: Let's say you have a class that extends Object and a function as follows public class A extends Object { ..... } void function ( A arg ){ ..... } To call this function, you need an instance of the class A A objectInstance = new A(); function(objectInstance); The next line shows how to pass an instance of the class A to the function.


What does the warning Illegal Argument Exception mean in Java language?

The warning Illegal Argument Exception in Java means that one has attempted to pass a wrong type of argument for a function. For example, we have a function that calculates a sum of two numbers and feed it a text string, which results in Illegal Argument Exception.


What is the advantages of call by reference?

Whenever you pass a value to a function, that value must be copied. If the value is large or complex, this can hinder performance, particularly when the function does not need to alter the value. To improve performance, we use the pass by reference semantic. Rather than passing the value itself, we pass the address of the value. That is, the address is copied, not the value. The function can then refer to the value by dereferencing the address. Ideally, pass by reference should only be used when the function does not need to alter the value. This is achieved by declaring the function's formal argument a pointer to constant type. This makes it clear to the caller the value of the actual argument will not be altered by the function. In some cases, we do require the function to alter the value. In these cases the argument is regarded as being an output parameter because it allows the function to return another value besides the return value. Typically, the caller will allocate some memory for the function to use (perhaps initialising it with a value), and then pass the address of that memory to the function. The function's formal argument is therefore declared a pointer to non-constant type, making it clear to the caller that the function will modify the value being pointed.


Does C even have pass by reference?

Strictly speaking, no. All arguments in C are passed by value. However, when the argument being passed is a memory address, although the address itself is passed by value, we're effectively passing the object that resides at that address -- by reference. Thus when a function's formal argument is a pointer variable (of any type), then it can be taken as read that the function is using the pass by reference semantic rather than the pass by value semantic. Nevertheless, it is important to keep in mind that the formal argument is assigned a copy of the actual argument and is therefore being passed by value.


How do we declare enmurated data type?

Enumerated data types can be declared using the enum keyword followed by the name of the enumeration and a list of possible values enclosed in curly braces. Each value is separated by a comma. For example, enum Color { Red, Green, Blue } declares an enumeration named Color with three possible values: Red, Green, and Blue.


What is pass by value in c functions?

Pass by value is a semantic that describes the way in which the actual argument in the calling code is assigned to the corresponding formal argument of the function being called. In pass by value, the actual argument and the corresponding formal argument are independent of each other; changing the value of the formal argument has no effect whatsoever upon the actual argument's value. In other words, the function receives a copy of the actual argument's value, never the actual argument itself. In C, all arguments are passed by value. However, when the formal argument is declared a pointer, we are effectively declaring a pass by reference semantic. The formal and actual arguments are still independent of each other (the formal argument's pointer value can still be changed without affecting the actual argument's pointer value), but if the formal argument and the actual argument both refer to the same object, changing that object's value via the function changes the same value in the caller. Pass by reference is useful when the value being passed is large or complex and cannot be implicitly or easily copied, such as an array or data structure. Pass by reference can also be used to return a value to the caller via an "output parameter", thus allowing a function to return more than one value. [Object-oriented programmers will detest the use of output parameters, citing bad programming practice (or poor style), but in C, it is not only desirable to use them to maintain efficiency, it is largely unavoidable. Object-oriented languages have highly efficient move constructors and move assignment operators, but C does not and copying large structures unnecessarily is detrimental to performance.]


Does Donne's argument about the harmlessness of his seduction pass muster What are the steps in this argument and where, if ever, does it break down?

C


What is difference between call by value and pass by value?

When you pass by value, the function's parameter is assigned the value of the argument that was passed. When you pass by reference, the function's reference parameter is assigned the address of the argument. In other words, pass by value copies the value, pass by reference passes the variable itself.Pass by reference is always the preferred method of passing arguments to functions when those arguments are complex objects. If the argument is a primitive data type then the cost in copying the value is minimal, but copying a complex object is expensive in terms of performance and memory consumption. If the function parameter is declare constant, you can be assured the object's immutable members will not be affected by the function. If it is non-constant and you do not wish your object to be altered, you can either copy the object and pass the copy, or pass the object by value if the function is overloaded to cater for this eventuality (good code will provide both options).