Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

The GNU C Programming Tutorial - different type arg

Node:different type arg, Next:, Previous:...undeclared (first use in this function), Up:...undeclared (first use in this function)



different type arg

You might get this warning if you mismatch a parameter to printf and a conversion specifier. For example, the following code:

#include <stdio.h>

/* To shorten example, not using argp */
int main()
{
  int my_int = 5;
  printf ("%f", my_int);
  return 0;
}

produces the folliwing warning:

wrongtype2.c: In function `main':
wrongtype2.c:6: warning: double format, different type arg (arg 2)

The %f conversion specifier requires a floating-point argument, while my_int is an integer, so GCC complains.

Note: GCC is quite lenient about type mismatches and will usually coerce one type to another dynamically without complaining, for example when assigning a floating-point number to an integer. This extends to mismatched parameters and conversion specifiers -- although you may receive odd results from printf and so on, the causes of which may not be obvious. Therefore, in order to generate this warning, the -Wall option of GCC was used. This option causes GCC to be especially sensitive to errors, and to complain about problems it usually ignores. You will often find the -Wall option to be useful in finding tricky problems. Here is the actual command line used to compile this program:

gcc -Wall -o wrong wrongtype2.c

 
 
  Published under free license. Design by Interspire