0

I accidentally assign .C [note to Uppercase C ] extension to my C source code in UBUNTU 16.04 LTS and open it with Gedit program to enter my codes.

gcc compiler not recognize it as a C programming source code and produce error when it try to compile.

And UBUNTU file manager show that as a cpp file.

Code

#include <stdlib.h>

int main(){

    int * c = malloc(sizeof(int));
    free(c); 
  
    return 0;   
}

gcc compile command, output:

$gcc test.C -o test
test.C:8:18: error: invalid conversion from ‘void*’ to ‘int*’ [-fpermissive]
  int * c = malloc( sizeof(int) );

As we know this is a C++ specific error, and i think gcc, behave that like a C++ file as said in this .

This is my system info

Linux ee 4.8.0-36-generic #36~16.04.1-Ubuntu SMP  i686 i686 i686 GNU/Linux

gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609

gedit - Version 3.18.3

I know we simply can cast the return value of malloc() to (int*) and this will obviously works.

But we know malloc() return type is void* see linuxdie malloc()

And it is not true to cast type of malloc() in C why not cast return of malloc()

Is only lowercase extension valid for C source code in UBUNTU, why they do this?

And how I can fix this and compile my source.C with gcc on my machine.


Edit

As MCG said in answers, we can force gcc to treat any given file as specific type which is tell to it with -x flag.

For example if we have a C valid source code with .f extension or even a source code without any extension on UBUNTU, by using this command it will be compiled correctly,

Compile C source code with any extension:

gcc -x c src.f -o src // with .f or any others
gcc -x c src -o src  //without extension
15
  • How did you run GCC? Commented Aug 28, 2017 at 21:56
  • #include <stdlib.h> (and you might force using a C++compiler, by using the caps C ) Commented Aug 28, 2017 at 21:56
  • 11
    You should fix it by renaming it to a lowercase .c. Uppercase .C represents C++ source by convention. Commented Aug 28, 2017 at 21:56
  • 2
    @EsmaeelE one can easily run into confusion with things of this kind, like extension .C for C++ sources. Especially while it doesn't look like widely used extension. Hence, methinks, the question itself ain't bad. It redirects one into right direction. Hence upvoted. However, gcc stays gcc even on gentoo. Ubuntu is not exception :) This is what I meant. Commented Aug 28, 2017 at 22:39
  • 6
    Give gcc the -x c option if it doesn't correctly guess that your program is C. Commented Aug 28, 2017 at 22:47

1 Answer 1

5

GCC recognizes .C (capital letter) extension as C++ file. You need to change extension of your file to .c (small letter). Also, you rightly mentioned and referenced that C++ requires a cast in case of malloc where as in c there is an implicit conversion from any object pointer type to void *.

See below explanation about file extensions (.C and .c) from GCC documentation. Please refer below GCC link for detail explanation for various file extensions.

file.C

C++ source code which must be preprocessed. Note that in .cxx, the last two letters must both be literally x. Likewise, .C refers to a literal capital C.

file.c

C source code which must be preprocessed.

https://gcc.gnu.org/onlinedocs/gcc-3.3/gcc/Overall-Options.html

Additionally, you can give the flag -x c to force GCC to treat the file as C, not C++.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks. please add some information from your link (You can specify the input language explicitly with the -x option) and also my comment to @MarkPlotnick, we can even compile a without extension or by any extension with gcc by give it -x c option.
and please require some info about this problem. As i say in comments section of post if we give to gcc a without extension file it produce f: file not recognized: File format not recognized collect2: error: ld returned 1 exit status. but in post question in *.C file case gcc actually behave it like a cpp source code. is there any flag in gcc compiler to use and show this misbehavior?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.