-Wall warning

I just started using -Wall to give me warnings. The following code compiles fine without -Wall but the warning I am getting is:

fileprint.c:7:2: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
if (fp = fopen ("test.txt", "w")){

I have tried to solve this in several ways but have been unsuccessful. So what is the solution?

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>

int main(){
FILE *fp;
	if (fp = fopen ("test.txt", "w")){
	fprintf (fp, "Hello World!\n");
	printf ("File successfully written\n");
	fclose(fp);
					 }
	else
	printf ("Error opening file\n");
return 0;
	  }
Last edited on
maybe showing us what your typing would help.
It should look somethign like this
$ g++ -Wall hello.cc -o hello
Sorry, I am using c, so

gcc -Wall -o fileprint fileprint.c
The warning message tells you how to fix the issue, add parentheses around the assignment. I myself would also add the rest of the comparison as well.

if ( (fp = fopen ("test.txt", "w")) != NULL){
closed account (E0p9LyTq)
if ((fp = fopen ("test.txt", "w"))) makes the -Wall warning go away.
Thank you very much for your help. Got it!
jlb,

I understood that I needed parenthesis somewhere in there, I just didn't understand where!
Hi,

You should be compiling with -Wall -Wextra -pedantic

Always compile with the highest warnings levels. Warnings are a good thing, they tell one about where a program might be going wrong :+)

Good Luck !!

Edit:

This might seem pedantic, but one has to be really careful with C.

Line 7 says that writing to the file was successful, but you have no idea whether that is true or not. All the printf and scanf functions return a value, so you should make use of that to see that it worked.

Consider using snprintf to print to a string first, if that works, use fprintf to print the string you just made, but still check to see it worked. A switch is handy to process the return values.

http://en.cppreference.com/w/c/io/fprintf


Maybe all this is too advanced right now, but at least you have an idea :+)
Last edited on
Got it!

Thanks for the advice, TheIdeasMan! I love to learn! I will look into it :-)
> You should be compiling with -Wall -Wextra -pedantic

Favour -Wall -Wextra -pedantic-errors, particularly while learning C++.
Non-standard (and obviously non-portable) linuxisms ought to be errors and not just warnings.
Use an up-to-date C++ compiler (currently C++11 or C++14) with a set of options that do not accept extensions. - CppCoreGuidelines


1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <cstdlib>

int main()
{
    int n ;
    std::cin >> n ;
    
    char array[n] ;
    std::cout << sizeof(array) << '\n' ; 
}

pedantic
main.cpp: In function 'int main()':
main.cpp:9:17: warning: ISO C++ forbids variable length array 'array' [-Wvla]
     char array[n] ;
                 ^
ok
100

pedantic-errors
main.cpp: In function 'int main()':
main.cpp:9:17: error: ISO C++ forbids variable length array 'array' [-Wvla]
     char array[n] ;
                 ^
this is not standard C++

http://coliru.stacked-crooked.com/a/2f2a03ca3b91996d
@JLBorges

The OP is doing C, but what you are saying still holds true :+) I should have said "at least", and that work isn't finished until there are no warnings or errors. Maybe -Werror as well to enforce that idea?

I also should have said something about which standard to compile against (the OP didn't specify), the default for gcc is currently (v5.3) gnu11 but at least it's not C89 :+). So the OP should be using -std=c11

So the whole command would now be:

gcc -std=c11 -Wall -Wextra -pedantic-errors -o fileprint fileprint.c

This was very helpful. Thank you both TheIdeasMan and JLBorges.
Topic archived. No new replies allowed.