Salem is right about prototyping functions, OP:In C, you need to declare the prototype of the function (or define it altogether) before you use it.
But keep in mind you can also simply do this.Code:#include <stdio.h> void my_function(char*); int main() { char* name = "Hello"; my_function(name); } void my_function(char* name) { printf("%s\n", name); }
Code:void my_function(char* name) { printf("%s\n", name); } int main() { char* name = "Hello"; my_function(name); }
That's probably because Pelles C is more up to date and has higher "default" error diagnostics.I like the editor of Pelle C, but the IDE was erroring out when DEV-C wasn't; neither did it specify errors.
Truthfully I don't do Windows so neither of the above would be my choice, but of the two Pelles C. And since this is C I can't even recommend Visual Studio since it doesn't support modern C.Which IDE would be your top choice?
Of course if you're really adventurous you may want to consider Clang (part 1 and part 2 but make sure you follow the install instructions carefully (Don't ask me if it doesn't work, remember I don't do Windows).
[QUOTE=jimblumberg;1288747]That's probably because Pelles C is more up to date and has higher "default" error diagnostics.
Re your example of a function / stressing that functions need prototypes. (I never queried that.) in your example you use char*. That really itches my brain. Am I right in assuming that what in some language could be string x = "whatever", in C could be char* x = "whatever". So in other words, C, that doesn't have strings, does have strings after all? How would you describe char* (A lone wolf in the world of char?)
So who ever said C doesn't have strings? A general purpose language that doesn't have strings would really be very limited.So in other words, C, that doesn't have strings, does have strings after all?
In C a string is an array of char terminated by the end of string character '\0'. Every string must have the end of string character, otherwise it is just an array of char and if you try to use any of the strXXX() functions you will invoke undefined behaviour.
Depending on content "char*" could describe a pointer to a single character or a pointer to an array of char.How would you describe char* (A lone wolf in the world of char?)
When talking about function parameters the following are all equivalent: char* a, char a[], char[1000].
I never stated anything about needing function prototypes or creating the function before use that was already covered by others earlier in the topic. I was talking about you having a function without any parameters ie: myFunction(). In C having no parameters in the parameter list doesn't mean that the function has no parameters, it means it can have any number of parameters. If you want a function to have no parameters you need to state that by using void: myFunction(void).Re your example of a function / stressing that functions need prototypes.
I just wanted to point out that in Windows, Visual Studio 2017 and 2019 easily install and support CLang "out of the box" - very easy setup and installation (basically just check the box, it does everything else), giving VS2017 and VS2019 Clang 8.0, with full features of the IDE.
[Warning] deprecated conversion from string constant to 'char*'Code:#include <stdio.h> #include <string.h> #include <stdio.h> int main() { char* monkey = "what are you doing?"; puts(monkey); getchar(); return 0; }
Oh oh.
OK, (re: prototypes / re: I never stated anything about needing . . .) Apologies!! It was someone else who came up with the remark about prototyping and the char* example.
Well, as declared, a char * monkey represents a non constant string.[Warning] deprecated conversion from string constant to 'char*'
const char * monkey = "whatever";
would declare that "whatever" is const, and can't be changed.
Yes, you're trying to assign a constant char array (a string literal) to a non-const char pointer.[Warning] deprecated conversion from string constant to 'char*'
Oh oh.
It should be like the following:
Code:const char* monkey = "what are you doing?"; // Note you can't change the string because it is a string literal.Yes, but make sure you head the advice. The compiler needs to know about the function before you try to use it, this means that you need to either create a prototype (a declaration) prior to using the function, or define the function prior to use. You also need to implement (a definition) of the function before you try to link the program. Remember creating a program from C code is broken into several steps, the Preprocessing step, the Compiling, then the linking.It was someone else
Also C before C99 would allow default function arguments and return values, this is no longer allowed (you must implicitly state the return type and the type of each parameter in modern C).