Possible errors in 'including * .c files' style C programming

I came across some codes as follows

//file.c  
#include <stdlib.h>

void print(void){

    printf("Hello world\n");
}

and

//file main.c  
#include <stdio.h>
#include "file.c"

int main(int argc, char *argv[]){

    print();

    return EXIT_SUCCESS;
}

Are there any flaws in this programming style? I can’t figure it out, although I feel that way because somewhere I read that splitting the implementation into a * .h and * .c file helps the compiler check for consistency. I do not understand what is meant by consistency.
I would be deeply grateful for some suggestions.

- thanks

+3
source share
11 answers

, . , XPM BMP char . , .

, (, *.inc, *.dat ..).

+2

.c. ( .h ) ( .c ), .h :

  • . , . .h ( .c ), .c , , .

  • . .h, .c . , , , . , , .

+9

, .c. .h . .c.

, , , :

gcc main.c file.c 

gcc main.c. , .

+9

, file.c , / , . / .

+6

, .

.

  • ( , ).
  • ( ).
  • .
  • , .
  • .
+2

.h . , :

//file.h
void print(void);

//file.c
void
print(void)
{
   printf("Hello world\n");
}
//file main.c  
#include <stdio.h>
#include "file.h"

int main(int argc, char *argv[]){

    print();

    return EXIT_SUCCESS;
}
0

C , .c . C , ++ , .

, , . , .c , . . , , .

0

, , C ( ):

  • , ,
  • () . ( , gcc .., , ).

:

  • , , ( , ...)
0

, , , ## , , C.

, , , , , - :

mytemplate.c:

MyTemplateFunction_ ## MYTYPE(MYTYPE x)
{
  // function code that works with all used TYPEs
}

main.c:

#define MYTYPE float
#include "mytemplate.c"
#undef MYTYPE

#define MYTYPE int
#include "mytemplate.c"
#undef MYTYPE

int main(int, char*)
{
  float f;
  int i;
  MyTemplateFunction_float(f);
  MyTemplateFunction_int(i);
  return 0;
}
0

:

file1.c

#define bottom arse

file2.c

int f()
{
    int arse = 4;
    bottom = 3;
    printf("%d", arse);
}

main.c

#include "file1.c"
#include "file2.c"

void main()
{
    f();
}

, . , - , .

I really got this error, I imported some lib code into a new project and couldn't worry about writing a Makefile, so I just generated all.cpp, which included all the sources from the library. And it did not work, as expected, due to macro pollution. Spent me to figure it out.

0
source

This is great for programs of this length.

0
source

Source: https://habr.com/ru/post/1710913/


All Articles