Thread: Would like some help

  1. #16
    zach
    Guest
    Quote Originally Posted by jimblumberg View Post
    By the way I would recommend Pelles C over DEV-C as it is probably more up to date with the current standards.

    Also if the libraries you're talking about are things like conio, then you would probably be better off without it in the first place.
    ~~~~~~~~~~~~~~~~
    DEV-C has an awful editor, but specifies errors during compile time. I like the editor of Pelle C, but the IDE was erroring out when DEV-C wasn't; neither did it specify errors. Which IDE would be your top choice?

  2. #17
    Registered User catacombs's Avatar
    Join Date
    May 2019
    Location
    /home/
    Posts
    72
    In C, you need to declare the prototype of the function (or define it altogether) before you use it.
    Salem is right about prototyping functions, OP:

    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);
    }
    But keep in mind you can also simply do this.

    Code:
    void my_function(char* name)
    {
        printf("%s\n", name);
    }
    
    int main()
    {
        char* name = "Hello";
        my_function(name);
    }

  3. #18
    Registered User
    Join Date
    May 2010
    Posts
    4,447
    I like the editor of Pelle C, but the IDE was erroring out when DEV-C wasn't; neither did it specify errors.
    That's probably because Pelles C is more up to date and has higher "default" error diagnostics.

    Which IDE would be your top choice?
    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.

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

  4. #19
    zach
    Guest
    [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?)

  5. #20
    Registered User
    Join Date
    May 2010
    Posts
    4,447
    So in other words, C, that doesn't have strings, does have strings after all?
    So who ever said C doesn't have strings? A general purpose language that doesn't have strings would really be very limited.

    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.

    How would you describe char* (A lone wolf in the world of char?)
    Depending on content "char*" could describe a pointer to a single character or a pointer to an array of char.

    When talking about function parameters the following are all equivalent: char* a, char a[], char[1000].

    Re your example of a function / stressing that functions need prototypes.
    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).

  6. #21
    Registered User
    Join Date
    May 2019
    Posts
    209
    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.

  7. #22
    zach
    Guest
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdio.h>
    int main()
    {
        char* monkey = "what are you doing?";
        puts(monkey);
        getchar();
        return 0;
    }
    [Warning] deprecated conversion from string constant to 'char*'

    Oh oh.

  8. #23
    zach
    Guest
    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.

  9. #24
    Registered User
    Join Date
    May 2019
    Posts
    209
    [Warning] deprecated conversion from string constant to 'char*'
    Well, as declared, a char * monkey represents a non constant string.

    const char * monkey = "whatever";

    would declare that "whatever" is const, and can't be changed.

  10. #25
    Registered User
    Join Date
    May 2010
    Posts
    4,447
    [Warning] deprecated conversion from string constant to 'char*'

    Oh oh.
    Yes, you're trying to assign a constant char array (a string literal) to a non-const char pointer.

    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.
    It was someone else
    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.

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

Popular pages Recent additions subscribe to a feed

Tags for this Thread