This question already has answers here:
'%d' expects argument of type 'int', but argument 2 has type 'long unsigned int' [-Wformat=] [duplicate]
(3 answers)
Closed 9 years ago.
I am getting a warning warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat]
I am writing a very basic program which gives the size of the data type but in the Linux environment I am getting this warning whereas in Visual Studio, the program works without any warning. The source code is as below :-
#include<stdio.h>
int main()
{
int a;
printf("\nThe Size Of Integer A Is = \t%d", sizeof(a));
return 0;
}
Answer will be appreciated and also can anyone tell me a proper way of solving such kind of warnings as I am new to this C and it's standard.
sizeof returns size_t type. Use %zu specifier to print the value of sizeof.
printf("\nThe Size Of Integer A Is = \t%zu", sizeof(a));
C11 6.5.3.4 The sizeof and _Alignof operators:
5 The value of the result of both operators is implementation-defined, and its type (an
unsigned integer type) is size_t, defined in <stddef.h> (and other headers).
NOTE: As loreb pointed out in his comment that when you will compile your code in Windows, then most probably you will get the warning like:
[Warning] unknown conversion type character 'z' in format [-Wformat]
[Warning] too many arguments for format [-Wformat-extra-args]
sizeof return size_t and not int
Use %zu in printf
In C99 the correct format for a size_t variable is %zu.
Use:
printf("\nThe Size Of Integer A Is = \t%zu", sizeof(a));
Related
This question already has answers here:
How to portably print a int64_t type in C
(7 answers)
Closed 4 months ago.
Consider the following snippet, compiled with gcc -Wall for two different architectures:
int64_t x;
printf("A: %ld\n", x);
printf("B: %lld\n", x);
When compiling for a 32 bit machine, the compiler complains about "A":
format '%ld' expects argument of type 'long int', but argument 2 has type 'int64_t' {aka 'long long int'}
When compiling for a 64 bit machine, the compiler complains about "B":
format '%lld' expects argument of type 'long long int', but argument 2 has type 'int64_t' {aka 'long int'}
The question: What is a sensible machine-independent way to printf() int64_t?
The header inttypes.h contains portable conversion specifiers for printf and scanf family of functions. (It also internally includes stdint.h.)
#include <inttypes.h>
int64_t x = ...;
printf("%"PRIi64, x);
See the C standard C17 7.8.1 for details.
This question already has answers here:
How should I print types like off_t and size_t?
(10 answers)
Closed 2 years ago.
I'm a student trying to learn C and C++, I've got a problem with the specifier %d I dont't understand the exception that is written in the console, It's writing
The format %d expects argument of type 'int', but argument 2 has type 'long long unsigned int' [-Wformat]
Here is the code :
#include<stdio.h>
#include<stdlib.h>
int main()
{
short int u=1;
int v=2;
long int w=3;
char x='x';
float y=4;
double z=5;
long double a=6;
long b=7;
printf("short int:%d\n",sizeof(u));
printf("int:%d octets\n",sizeof(v));
printf("long int:%d octets\n",sizeof(w));
printf("char:%d octets\n",sizeof(x));
printf("float:%d octets\n",sizeof(y));
printf("double:%d octets\n",sizeof(z));
printf("long double:%d octets\n",sizeof(a));
printf("long:%d octets\n",sizeof(b));
return 0;
}
The type of the value returned by the operator sizeof is the unsigned integer type size_t The conversion specifier d is used to output values of the type int. You have to use the conversion specifier zu. Otherwise you will get undefined behavior.
For example
printf("short int:%zu\n",sizeof(u));
From the C Standard (7.21.6.1 The fprintf function)
9 If a conversion specification is invalid, the behavior is
undefined.275) If any argument is not the correct type for the
corresponding conversion specification, the behavior is undefined.
The sizeof operator returns a size_t type. This is always unsigned and, on your platform, is a long long unsigned int; on other platforms, it may be just unsigned long or, indeed, some other (unsigned) integer type.
Use the %zu format specifier for arguments of this type; this will work whatever the actual size_t type definition happens to be.
This question already has answers here:
C: Which character should be used for ptrdiff_t in printf?
(3 answers)
Closed 3 years ago.
I am trying to subtract 2 pointers such that they such give the number of elements.I could compile the program and run it .But after compilation it is throwing error as
pointerarithmetic.c: In function ‘main’:
pointerarithmetic.c:9:8: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long int’ [-Wformat=]
printf("%d",(q-p));
The code:
#include<stdio.h>
int main(){
int a[5]={0,10,20,30,40};
int *p,*q;
p=&a[0];
q=&a[2];
printf("%d",*p);
printf("%d",*q);
printf("%d",(q-p));
return 0;
}
The expected output should be number of elements.
Subtraction of pointers return a type ptrdiff_t (defined in stddef.h), not an int.
Use %td to print the result.
This question already has an answer here:
Output data type of sizeof() operator
(1 answer)
Closed 4 years ago.
In this below code:
#include<stdio.h>
int main(void)
{
printf("%d",sizeof(int));
return 0;
}
When compiled on gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4 compiler it gives warning:
format ‘%d’ expects argument of type ‘int’, but argument 2 has type
‘long unsigned int’ [-Wformat=] printf("%d",sizeof(int));
Why I am getting this warning? Is it that return type of sizeof is 'long unsigned int' ?
When I replaced '%d' with '%ld' the warning went.
The sizeof operator is processed at compile time (and can be applied on both types and expressions). It gives some constant* of type size_t. On your system (and mine Debian/Linux/x86-64 also) sizeof(int) is (size_t)4. That size_t type is often typedef-ed in some type like unsigned long (but what integral type it actually is depends upon the implementation). You could code
printf("%d", (int)sizeof(int));
or (since printf understands the %zd or %zu control format string for size_t)
printf("%zu", sizeof(int));
For maximum portability, use %zu (not %ld) for printing size_t (because you might find systems or configurations on which size_t is unsigned int etc...).
Note *: sizeof is always constant, except for VLA
This question already has answers here:
How to print variable addresses in C?
(5 answers)
Closed 7 years ago.
i'm new learning C, and learning pointer right now,i study from this website, but this code give me an error : pointer.c: In function ‘main’:
pointer.c:6:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]
printf("Address: %d",&var); //Notice, the ampersand(&) before var.
i compile it with : gcc -o pointer pointer.c
/* Example to demonstrate use of reference operator in C programming. */
#include <stdio.h>
int main(){
int var = 5;
printf("Value: %d\n",var);
printf("Address: %d",&var); //Notice, the ampersand(&) before var.
return 0;
}
http://www.programiz.com/c-programming/c-pointers
You're trying to print the address of var as if it was a signed integer, which it isn't. It's a pointer, and might have a different size than an integer.
Passing values of mismatching types to printf() invokes undefined behavior, which is why the compiler is issuing a friendly warning.
You should use %p to format it, and cast the value to (void *) to comply with the C standard:
printf("Address: %p\n", (void *) &var);
Please use correct format specifiers for each data type. Addresses are specified by %p.
It's better if you use %p to print a pointer. From the specification:
p The argument shall be a pointer to void. The value of the pointer is
converted to a sequence of printing characters, in an
implementation-defined manner.
Don't forget the cast: printf("Address: %p\n",(void*)&var);