Thread: The function declared with const issues a warning

  1. #1
    programmer hacker DevZero's Avatar
    Join Date
    Jul 2017
    Posts
    42

    The function declared with const issues a warning

    My code:
    Code:
    #include <stdio.h>
    
    const void write()
    {
    	FILE *f;
    	char Text[]="Test\n";
        if ((f = fopen("text.txt","a")) == NULL)
            printf("Error\n");
        else
            fwrite(&Text, 5, 1, f);
        fclose(f);
    }
    
    int main()
    {
    	write();
    }
    When compiling, warnings are displayed:
    test.c:3:12: warning: function definition has qualified void return type
    const void write()
    ^
    test.c: In function ‘main’:
    test.c:16:2: warning: function with qualified void return type called
    write();
    ^
    I have read this in the K&R manual on page 84. Is the textbook wrong?

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,114
    There is a Standard Library function called "write". You are redefining it! Please rename yours.

    Even renaming it to my_write(), it should only write to an open file not open it as well.

    A function should do one thing only, and do it well. Open the file in main() or in a separate function.

  3. #3
    programmer hacker DevZero's Avatar
    Join Date
    Jul 2017
    Posts
    42
    I know the rules of unix but now I do not follow them as it is deprived of a phone instead of a computer.
    Code:
    #include <stdio.h>
    
    const void f_write()
    {
    FILE *f;
    	char Text[]="Test\n";
        if ((f = fopen("text.txt","a")) == NULL)
            printf("Error\n");
        else
            fwrite(Text, 5, 1, f);
    fclose(f);
    }
    
    int main()
    {
    	f_write();
    }
    This code still generates warnings:
    test.c:3:12: warning: function definition has qualified void return type
    const void f_write()
    ^
    test.c: In function ‘main’:
    test.c:16:2: warning: function with qualified void return type called
    f_write();
    ^
    Code:
    #include <stdio.h>
    
    const void f_write(FILE *f)
    {
    	char Text[]="Test\n";
        if ((f = fopen("text.txt","a")) == NULL)
            printf("Error\n");
        else
            fwrite(Text, 5, 1, f);
    }
    
    int main()
    {
    	FILE *f;
    	f_write(f);
    	fclose(f);
    }
    This program produces the same warnings but works fine too. Therefore, the problem in const
    The computer is just a dumb machine, people are foolish by the turbidity, but a person is better better as he is not a programmed object but an endless subject.

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,114
    Const here makes no sense at all. Const makes sense if you are actually returning data. Void means the function returns NO data! Please drop the const.

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,114
    In addition, const on return data really only makes sense if you are returning a pointer to const data.

  6. #6
    programmer hacker DevZero's Avatar
    Join Date
    Jul 2017
    Posts
    42
    Quote Originally Posted by rstanley View Post
    In addition, const on return data really only makes sense if you are returning a pointer to const data.
    Code:
    #include <stdio.h>
    
    static void print()
    {
    	printf("Ok!\n");
    }
    
    int main(int atgc, char *argv[])
    {
    	FILE *f;
    	char Text[]="Test\n";
        if ((f = fopen("text.txt","a")) == NULL)
           printf("Error\n");
       else
            fwrite(Text, 9, 1, f);
        fclose(f);
        print();
    }
    t turns out to confuse const with static! Now everything works and does not give out any warnings. Problem solved

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 31
    Last Post: 05-26-2016, 05:21 PM
  2. warning: 'struct xxxx' declared inside parameter list
    By Gil Carvalho in forum C Programming
    Replies: 4
    Last Post: 06-13-2012, 04:41 AM
  3. TICPP and const issues
    By TIMBERings in forum C++ Programming
    Replies: 9
    Last Post: 04-18-2010, 10:55 PM
  4. 'strcpy' was declared deprecated warning
    By rahulsk1947 in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2007, 09:11 AM
  5. Replies: 3
    Last Post: 10-12-2006, 06:58 AM

Tags for this Thread