GCC Conflicting Types in C
In high level languages such as C and C++, “Conflicting Types” are the compilation errors which occur when there is a mismatch of datatypes, which means there are two different datatypes defined for a single variable. This error happens when the declaration of a variable in one part of the code/program doesn’t match the declaration of the same variable in another part of the program.GCC, which is GNU Compiler Collection, is a compiler for the programming languages such as C and C ++.
When you are compiling a C or C ++ program, and you face a conflicting type error on the console, then it is a datatype mismatch error in your written code/program. In order to solve this problem in your code, you have to retrace all the declaration of variables in your code to find out which datatype declaration is causing the error. One variable should be defined with only one data type throughout the whole code/program.
Let us take an example if we have a sum function which takes two integer variables as input parameters. Now, we at some part of the program want to pass some string argument to this function, and then this will cause a Conflicting type error in the code/program. In order to fix this problem, you have to make sure that the type of argument with which you are defining the function should match the data type of input parameters which you are passing to the function.
Let’s understand this whole concept of conflicting errors in GCC with the help of some examples. See the following example:
#include <stdio.h> // defined the standard input output library int main() { int y = 10; //Declaring and defining a variable x with datatype integer printf("The value of y is %s\n", y); //Now trying to print the integer variable //with a string specifier return 0; }
In the above example, we have declared and defined a variable named “y”, which is of datatype integer. Now we are printing the integer variable on the console using the “printf” function but with a format specifier “%s”, which is a format specifier for printing strings in C programming language. Hence, this is a datatype conflict error in C programming, because we are trying to print an integer variable using a format specifier “%s”. We should replace the “%s” format specifier with any integer type format specifier.
Hence, the following code/program should be used to remove the conflicting error.
Example 1:
#include <stdio.h> // defined the standard input output library int main() { int y = 10; //Declaring and defining a variable x with datatype integer printf("The value of y is %d\n", y); //Now trying to print the integer variable // %s format specifier is replaced with a %d integer format specifier return 0; }
Output:
The value of y is 10
Hence,the datatype of a variable should not be inconsistent throughout the code/program.
See another example:
#include <stdio.h> // defined the standard input output library int add_numbers(int a, int b) { // function which sums 2 numbers given as input //parameters to the function return a + b; // functions returns the sum of variable a and b } int main() { int x = 5; // defining integer variable x int y = 10; // defining integer variable y float z = 3.14; // defining variable z as a float type variable printf("The sum of %d and %d is %d\n", x, y, add_numbers(x, y)); printf("The value of z is %d\n", z); return 0; }
So in this program, we have two conflicting type errors. First is in the second “printf” statement, we are trying to print a floating point value using the %d format specifier, which should be used for printing integer values. Hence, this will result in an error because the datatype of the variable z is float, not int.
Second is the add_numbers function because we have specified the return type as an integer value, but we are returning a sum of two integers, which may exceed the maximum value of an integer?
Hence, the following code/program should be this to remove the conflicting errors.
Example 2:
#include <stdio.h> // defined the standard input output library long long add_numbers(int a, int b) { // function which sums 2 numbers given as input //parameters to the function return (long long)a + b; // functions returns sum of a and b } int main() { int x = 5; // defining integer variable x int y = 10; // defining integer variable y float z = 3.14; // defining variable z as a float type variable printf("The sum of %d and %d is %d\n", x, y, add_numbers(x, y)); printf("The value of z is %f\n", z); return 0; }
The output of this program is:
Output:
The sum of 5 and 10 is 15 The value of z is 3.140000
Hence to avoid conflicting errors, we should focus on maintaining the consistency of data types of variables throughout the code/program.