Need to Create Macro Named “CountBitsM” which provides number of bits

CountBitsM syntax: This macro has one parameter and produces a value of type int. There is no prototype (since macros are never prototyped).

Parameters: objectOrType – any expression with an object data type (24, temp, printf("Hello"), etc.), or the literal name of any object data type (int, float, double, etc.)

Synopsis: Determines the number of bits of storage used for the data type of objectOrType on any machine on which it is run. This is an extremely trivial macro.

Return: the number of bits of storage used for the data type of objectOrType.

CountBitsM must:

not assume a char/byte contains 8 or any other specific number of bits;
not call any function;
not use any external variables;
not perform any right-shifts;
not display anything.
not use any variables;
use a macro from header file limits.h

How can I create Macro Function in header file named "CountBitsM"?
Last edited on
Is there a question?
Seems to be trivially
sizeof(m) * CHAR_BIT

Need to create Macro function in header file which I can call from main.
I am struggling to create that function!
Seriously?

 
#define CountBitsM(m) (sizeof(m) * CHAR_BIT) 
Giving me warning:

warning: type of 'm' defaults to 'int' [-Wimplicit-int]
int CountBitsM(m)

This is my driver for this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#include <stdlib.h>

#include "C2A2E1_CountBitsM.h"

#define ARRAY_ELEMENTS 25

int CountIntBitsF(void);

int main(void)
{
    //lint -e550 -e754
    char cArray[ARRAY_ELEMENTS];
    double dArray[ARRAY_ELEMENTS];
    long double xyz;
    typedef struct test {long double x; float y; struct test *p;} TEST;

    // Test the function version.
    printf("Implementation-dependent bit widths:\n\n");
    printf("   From CountIntBitsF:\n");
    printf("      Type 'int'         -> %d\n\n", CountIntBitsF());

    // Test the macro version.
    printf("   From CountBitsM:\n");
    printf("      Type 'char'        -> %d\n", CountBitsM(char));
    printf("      Type 'short'       -> %d\n", CountBitsM(short));
    printf("      Type 'int'         -> %d\n", CountBitsM(int));
    printf("      Type 'long'        -> %d\n", CountBitsM(long));
    printf("      Type 'float'       -> %d\n", CountBitsM(float));
    printf("      Type 'double'      -> %d\n", CountBitsM(double));
    printf("      Type 'long double' -> %d\n", CountBitsM(long double));
    printf("\n");
    //lint -e719
    printf("      printf return      -> %d\n", CountBitsM(printf("")));
    //lint +e719
    printf("      'A'                -> %d\n", CountBitsM('A'));
    printf("      2000000+8000000    -> %d\n", CountBitsM(2000000 + 8000000));
    printf("      xyz                -> %d\n", CountBitsM(xyz));
    printf("      cArray             -> %d\n", CountBitsM(cArray));
    printf("      dArray             -> %d\n", CountBitsM(dArray));
    printf("      TEST               -> %d\n", CountBitsM(TEST));
    printf("\n");

    return EXIT_SUCCESS;
}
Do you know the difference between a function and a macro?

As the assignment says: There is no prototype (since macros are never prototyped).

use a macro from header file limits.h

You haven't done this.

You're printing the result of CountBitsM() as if it's an int (%d). You should make it an unsigned long (%ld). On a 64-bit system, an object's size can exceed the max value of int. Bugs like this are the reason that you could use cout instead of printf().

Macros are different from functions. They are not "called" the way a function is. Instead, the text of the macro is expanded and inserted where you put it.
#define CountBitsM(x) (sizeof(x) * CHAR_BIT)

A macro is nothing more than a "copy-and-replace" program that you would see in places like Microsoft Word. It literally just replaces all occurrences of the macro with the body, wherever you use it. Like "find-and-replace".
Topic archived. No new replies allowed.