#warning directive in C

Do not miss this exclusive book on Binary Tree Problems. Get it now for free.

Reading time: 20 minutes | Coding time: 5 minutes

#warning is a preprocessor directive in C which is used to raise compilation warnings for various situations. It is different from #error directive in that #warning will not cancel the compilation process while #error will cancel it.

#warning directive is used to declare situations which the user of the software may not consider or miss during execution. For example, if the performance of a software is less for a particular factor, it can be raised as a warning so that it clarifies the situation for the end user.

Syntax:

#warning warning_message

Example of using warning in compilation:

In this C code example, we are raising a warning that in an integer, there is a certain limit on the largest number that can be stored.

#include <stdio.h>

int make_data_stronger(int data)
{
    return data * 2;
}

int main() {
    int data = 1;
    data = make_data_stronger(data);
    #warning int datatype does not hold more than 2^31-1
    printf("%d", data);
	return 0;
}

Compile it using the command:

gcc opengenus.c

When you will compile it, you will get the following message:

prog.c: In function 'main':
prog.c:11:6: warning: #warning int datatype does not hold more than 2^31-1 [-Wcpp]
     #warning int datatype does not hold more than 2^31-1
      ^

It will generate the executable (a.out) which we can run as follows:

2

Example of using warning with directive conditions

In this C code example, we will display the warning only for a certain condition. For this example, we have placed the warning directive but the warning will not be displayed as the condition is not satisfied.

#include <stdio.h>
#include <limits.h>

int make_data_stronger(int data)
{
    return data * 2;
}

#define data 5

int main() {
    #if (data >= INT_MAX / 2)
        #warning int datatype does not hold more than 2^31-1
    #endif
    int new_data = make_data_stronger(data);
    printf("%d", new_data);
	return 0;
}

There will be no compilation warnings.

10

If we set data to a higher value, we will get back the compilation warning as follows:

#include <stdio.h>
#include <limits.h>

int make_data_stronger(int data)
{
    return data * 2;
}

#define data INT_MAX-5

int main() {
    #if (data >= INT_MAX / 2)
        #warning int datatype does not hold more than 2^31-1
    #endif
    int new_data = make_data_stronger(data);
    printf("%d", new_data);
	return 0;
}

It will give a compilation warning:

prog.c: In function 'main':
prog.c:13:10: warning: #warning int datatype does not hold more than 2^31-1 [-Wcpp]
         #warning int datatype does not hold more than 2^31-1
          ^

Output:

-12

Note that in the if condition check, we cannot use a function or a variable as it happens during compilation in which only macros will have an impact.

Example of warning in a real software

Following is a real life compilation warning taken while building TensorFlow code:

WARNING: /home/opengenus/.cache/bazel/_bazel/9ac980dce88b76f8f56d1e11/external/grpc/BUILD:1992:1:

in srcs attribute of cc_library rule @grpc//:grpc_nanopb: please do not import 
'@grpc//third_party/nanopb:pb_common.c' directly. You should either move the file to 
this package or depend on an appropriate rule there. Since this rule was created by
the macro 'grpc_generate_one_off_targets', the error might have been caused by the
macro implementation in 
/home/opengenus/bazel/_bazel/9ac980dce88b76f8f56d1e11/external/grpc/
bazel/grpc_build_system.bzl:172:12

WARNING: /home/opengenus/bazel/_bazel/88b72af6886e6f8f51e11/external/grpc/BUILD:1992:1: 
in srcs attribute of cc_library rule @grpc//:grpc_nanopb: please do not import 
'@grpc//third_party/nanopb:pb_decode.c' directly. You should either move the file to
this package or depend on an appropriate rule there. Since this rule was created by
the macro 'grpc_generate_one_off_targets', the error might have been caused by the 
macro implementation in 
/home/opengenus/bazel/_bazel/88b72af6886e6f8f51e11/external/grpc/bazel
/grpc_build_system.bzl:172:12

Similarly, the warning directive is widely used in C software to display warning during the compilation process. You may use it as well.

Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.