C programming compiler errors warnings debugging


Advertisements

Share

Article background

    When you are developing any c programming project or application you will come across compiler errors and warnings. It is part of every programmer to get few of these errors or warnings during program compilation and resolve them before executing the program.
    This article is one of the best in explaining c programming compiler errors or warnings by example and ways to resolve them. If you are in the middle of a c programming assignment, this article will be useful to resolve all the errors and warnings you come across in your program.

Article contents

C compiler errors, warnings


error: expected ‘;’ before ‘}’ token

Why this error?
Program having error => expected ‘;’ before ‘}’ token

#include <stdio.h>
int main(){
    printf("Hello world\n")
}
    This program will produce the following compilation error.
$ gcc prog.c
prog.c: In function ‘main’:
prog.c:4:1: error: expected ‘;’ before ‘}’ token
 }
 ^
$
Error resolution?
    This is a c programming syntax error that occurs when you missed a semi column at the end of a statement in C. Make sure to put ; at the end of each C statement. Before coming to the exact error compiler gives another line prog.c: In function ‘main’:. This means error is in program file prog.c and within that file in main function. As we missed the semi column in line 3, next line says problem is in file prog.c and line 4. The program after error correction is shown below.

#include <stdio.h>
int main(){
    printf("Hello world\n");
}

warning: implicit declaration of function ‘addInt’

Why this warning?
Program having warning => implicit declaration of function ‘addInt’

#include <stdio.h>
int main(){
    int a1 = 20,a2 = 30;
    printf("a1 + a2 = %d\n",addInt(a1,a2));
}
int addInt(int a,int b) {
    return a + b;
}        
The output of the above program is shown below.
$ gcc prog.c
prog.c: In function ‘main’:
prog.c:4:29: warning: implicit declaration of function ‘addInt’ [-Wimplicit-function-declaration]
     printf("a1 + a2 = %d\n",addInt(a1,a2));
                             ^~~~~~
$
Warning resolution?
        This is a c programming warning you get if forgot to declare the prototype of a function you are using in your program. In this particular case it complains about the function addInt whose prototype is not included in the program. Include the following addInt function prototype and recompile the program. Program will compile without this warning. The program after warning correction is shown below.

#include <stdio.h>
int addInt(int ,int );
int main(){
    int a1 = 20,a2 = 30;
    printf("a1 + a2 = %d\n",addInt(a1,a2));
}
int addInt(int a,int b) {
    return a + b;
}        

error: conflicting types for function

Why this error?
Program having error => conflicting types for ‘addInt’

#include <stdio.h>
int addInt(int ,int );
int main(){
  int a1 = 20,a2 = 30;
  printf("a1 + a2 = %d\n",addInt(a1,a2));
}
void addInt(int a,int b) {
  return a + b;
}
The output of the above program is shown below.
$ gcc prog.c
prog.c:7:6: error: conflicting types for ‘addInt’
 void addInt(int a,int b) {
      ^~~~~~
prog.c:2:5: note: previous declaration of ‘addInt’ was here
 int addInt(int ,int );
     ^~~~~~
prog.c: In function ‘addInt’:
prog.c:8:12: warning: ‘return’ with a value, in function returning void
   return a + b;
          ~~^~~
prog.c:7:6: note: declared here
 void addInt(int a,int b) {
      ^~~~~~
$
Error resolution?
    This error occurs when a function prototype and its definition does not match. You need to make sure that the return data type of the function and the data types of the arguments passed to the function in both function prototype and function definition matches. In our particular program here compiler finds that in addInt function the return datatype in the function prototype is int where as in the function definition return data type is void. Change the return data type in addInt function definition to int.
The program after correcting this error is given below.

#include <stdio.h>
int addInt(int ,int );
int main(){
  int a1 = 20,a2 = 30;
  printf("a1 + a2 = %d",addInt(a1,a2));
}
int addInt(int a,int b) {
  return a + b;
}

error: ‘variable’ undeclared (first use in this function)

Why this error?
Program having error => ‘sum’ undeclared (first use in this function)

#include <stdio.h>
int addInt(int ,int );
int main(){
    int a1 = 20,a2 = 30;
    printf("a1 + a2 = %d",addInt(a1,a2));
}
int addInt(int a,int b) {
    sum = a + b;
    return sum;
}
    The above program gives the following compilation output.

Error resolution?
    This error occurs when a function prototype and its definition does not match. You need to make sure that the return data type of the function and the data types of the arguments passed to the function in both function prototype and function definition matches. In our particular program here compiler finds that in addInt function the return datatype in the function prototype is int where as in the function definition return data type is void. Change the return data type in addInt function definition to int.
The program after correcting this error is given below.

#include <stdio.h>
int addInt(int ,int );
int main(){
    int a1 = 20,a2 = 30;
    printf("a1 + a2 = %d",addInt(a1,a2));
}
int addInt(int a,int b) {
    int sum;
    sum = a + b;
    return sum;
}

error: undefined reference to 'addInt'

Why this error?
Program having error => undefined reference to 'addInt'

#include <stdio.h>
int addInt(int ,int );
int main(){
    int a1 = 20,a2 = 30;
    printf("a1 + a2 = %d",addInt(a1,a2));
}
int addIntt(int a,int b) {
    sum = a + b;
    return sum;
}
    The above program gives the following compilation output.
$ gcc prog.c
/tmp/ccyKLrMd.o: In function `main':
prog.c:(.text+0x24): undefined reference to `addInt'
collect2: error: ld returned 1 exit status
$
Error resolution?
    This error occurs when a name addInt is found in the program which is not recognized by compiler. Check that the function name as in addInt function definition matches with the addInt function call and addInt function prototype.
The program after correcting this error is given below.

#include <stdio.h>
int addInt(int ,int );
int main(){
    int a1 = 20,a2 = 30;
    printf("a1 + a2 = %d",addInt(a1,a2));
}
int addInt(int a,int b) {
    int sum;
    sum = a + b;
    return sum;
}

warning: character constant too long for its type

Why this warning?
Program having warning => character constant too long for its type

#include <stdio.h>
int main(){
    char *str = 'string';
}
    The above program gives the following compilation output.
$ gcc prog.c
prog.c: In function ‘main’:
prog.c:3:17: warning: character constant too long for its type
     char *str = 'string';
                 ^~~~~~~~
prog.c:3:17: warning: initialization makes pointer from integer without a cast  -Wint-conversion]
$
Warning resolution?
    This warning occurs if any declared string is not surrounded by double quotation marks.
The program after correcting this warning is given below.

#include <stdio.h>
int main(){
    char *str = "string";
}

warning: passing argument n of ‘function’ makes pointer from integer without a cast

Why this warning?
Program having warning => passing argument 2 of ‘intPtr’ makes pointer from integer without a cast

#include <stdio.h>
void intPtr(int *, int *); 
int main(){
    int a = 20, b = 30;
    intPtr(&a, b);
}
void intPtr(int *a, int *b) {
    printf("%d\n", (*a) + (*b));
}
    The above program gives the following compilation output.
$ gcc prog.c
prog.c: In function ‘main’:
prog.c:5:16: warning: passing argument 2 of ‘intPtr’ makes pointer from integer without a cast [-Wint-conversion]
     intPtr(&a, b);
                ^
prog.c:2:6: note: expected ‘int *’ but argument is of type ‘int’
 void intPtr(int *, int *);
      ^~~~~~
$
Warning resolution?
    This warning occurs because argument 2 is expected to be a pointer, but we have passed an integer instead. A type cast explicitly changes the type of data from one type to another, but you haven't used one to modify the data type. This warning message is received whenever there is a type mismatch between the data you pass to a function and the type of data that the function expects.
The program after correcting this warning is given below.

#include <stdio.h>
void intPtr(int *, int *); 
int main(){
    int a = 20, b = 30;
    intPtr(&a, &b);
}
void intPtr(int *a, int *b) {
    printf("%d\n", (*a) + (*b));
}

warning: assignment makes pointer from integer without a cast

Why this warning?
Program having warning => assignment makes pointer from integer without a cast

#include <stdio.h>
int *ptr;
int a[5]={1,2,3,4,5};
int x;
void main()
{
    ptr = a[4];
    x = *ptr;
    printf("%d",x);
}
    The above program gives the following compilation output.
pi@raspberrypi:~/errors $ gcc prog.c
prog.c: In function ‘main’:
prog.c:7:9: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
     ptr = a[4];
         ^
pi@raspberrypi:~/errors $
    This is a c programming warning you get if you assign pointer wrongly. In this particular case ptr is a pointer to int. But a[4] is an integer. We have assigned ptr which is a pointer(an address) to an integer which is not correct. We should assign ptr to an address of int variable.

Warning resolution?
    Assign the pointer ptr to an address of integer. Program will compile without this warning.

ptr = &(a[4]);
The program after correcting this warning is given below.

#include <stdio.h>
int *ptr;
int a[5]={1,2,3,4,5};
int x;
void main()
{
    ptr = &a[4];
    x = *ptr;
    printf("%d",x);
}

error: void value not ignored as it ought to be

Why this error?
Program having error => void value not ignored as it ought to be

#include <stdio.h>
void addInt(int a, int b);
int main() {
  int result;
  result = addInt(10,20);
  printf("%d\n", result);
  return 0;
}
void addInt(int a, int b) {
    int sum;
    sum = a + b;
    printf("sum:%d \n", sum);
}
    The above program gives the following compilation output.
$ gcc prog.c
prog.c: In function ‘main’:
prog.c:5:10: error: void value not ignored as it ought to be
   result = addInt(10,20);
          ^
$ 
    This is a c programming error you get if you call a function and try to get it's output in another variable, but the function is declared as void. In this program addInt function is declared as void whereas in main function we are trying to collect the result of this function call in result variable.

Error resolution?
     The program after correcting this error is given below.

#include <stdio.h>
void addInt(int a, int b);
int main() {
  addInt(10,20);
  return 0;
}
void addInt(int a, int b) {
    int sum;
    sum = a + b;
    printf("sum:%d \n", sum);
}

 | 
Share

Articles


C Programming

More Articles Categories

Technical articles

Prepare for software jobs


Test your skills