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
EsmaeelE
  • 2,331
  • 6
  • 22
  • 31
  • How did you run GCC? – user2357112 Aug 28 '17 at 21:56
  • `#include ` (and you might force using a C++compiler, by using the caps `C` ) – wildplasser Aug 28 '17 at 21:56
  • 11
    You should fix it by renaming it to a lowercase `.c`. Uppercase `.C` represents C++ source by convention. – Ry- Aug 28 '17 at 21:56
  • @Ryan thanks, i find out some Unix/Unix-like OS's use `.C` extension for cpp source code. [C++ code file extension?](https://stackoverflow.com/questions/1545080/c-code-file-extension-cc-vs-cpp/3223792#3223792) – EsmaeelE Aug 28 '17 at 22:04
  • File extension on the file has no bearing if it's a valid c file or not. – Fredrik Aug 28 '17 at 22:04
  • @Fredrik but my example show that gcc cant recognize my file as c code. you can test gcc by give it a no extension file as source code for example 'f' to see what i say – EsmaeelE Aug 28 '17 at 22:07
  • It works fine, you can use whatever extension you want. myfile.myextension – Fredrik Aug 28 '17 at 22:11
  • I _did_ know about `.cc`, `.cxx` and `.c++`, but not `.C`. Its kinda suprise to me. Cute. Thanks, @Ryan. Ubuntu really seems irrelevant here. Otherwise I don't see the reason to downvote. – rfx Aug 28 '17 at 22:13
  • @Fredrik gcc f.my /usr/bin/ld:f.my: file format not recognized; treating as linker script /usr/bin/ld:f.my:3: syntax error collect2: error: ld returned 1 exit status – EsmaeelE Aug 28 '17 at 22:14
  • @rfx link to answer said, that .C use for cpp source code expressed in [C++ Primer Plus](http://www.informit.com/store/c-plus-plus-primer-plus-9780321776402) – EsmaeelE Aug 28 '17 at 22:18
  • @Fredrik i also try it with cc compiler with no success – EsmaeelE Aug 28 '17 at 22:20
  • 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. – rfx Aug 28 '17 at 22:39
  • @rfx yes, i change topic from ubuntu to gcc. i dont know why they down vote it. i ask two clear question. and i provide two useful link about using `malloc()` – EsmaeelE Aug 28 '17 at 22:41
  • 6
    Give gcc the `-x c` option if it doesn't correctly guess that your program is C. – Mark Plotnick Aug 28 '17 at 22:47
  • @MarkPlotnick thanks alot. giving `-x c` to gcc force it to behave input file as .c source (valid c source code for gcc). and this solve my second question. – EsmaeelE Aug 28 '17 at 23:10
  • 1
    Upvoted also. I don't see why this is a bad question. – Andy J Aug 28 '17 at 23:33
  • Upvoted too. Well asked, sure to be a often-faced problem. – Ed. Aug 29 '17 at 00:45
  • Upvoted . Yes someone unknowingly can face this problem. – MCG Aug 29 '17 at 04:01

1 Answers1

4

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++.

Community
  • 1
  • 1
MCG
  • 1,011
  • 1
  • 10
  • 21
  • 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. – EsmaeelE Aug 29 '17 at 00:48
  • 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? – EsmaeelE Aug 29 '17 at 00:54