C programming compiler errors warnings debugging



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.

Subscribe to enable complete access to all , and .

This article will help you to gain required knowledge to successfully complete , which are required to complete and earn course completion certificates for software jobs.

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";
}

To Read Full Article

You need to subscribe & logged in

Subscribe
Share

 Articles

 C Programming

 Linux Software

 Embedded Systems

 Python Programming

 Search Code Snippets

 Popular C search examples

 Popular C search MCQs

 Online certificate courses

 Test your skills:online tests

 C Programming

 Python Programming

 Linux Software

 Quantitative Aptitude

 Embedded System Software