Scanf in boolean [duplicate] - c

In the following example, I'm trying to scan the value of boolean type of variable. When I compile in GCC, I get following warning,
warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘_Bool *’ [-Wformat=]
scanf("%d",&b);
code:
#include <stdio.h>
#include <stdbool.h>
int main()
{
bool b;
scanf("%d",&b);
printf("%d\n",b);
}
My question is, Is there any format specifier of bool in C?

There is no format specifier for the bool type in C.
For printf, you can rely on the implicit promotion to int, and use %d as the specified formatter.
For scanf, you ought to read it into an int, and convert appropriately. Again, using %d.

There is no specifier in C for bool.
You should typecast it in printf() to avoid warnings if you want.
But there is no dedicated format specifier for represent bool type.
Try below to avoid scanf() warning:
scanf("%d",(int*)&b)

Related

Want to remove warning without change data-type to char

I want to remove warning without change data-type to char.
#include<stdio.h>
#include<stdlib.h>
main()
{
unsigned char ch;
printf("Hello This is Problematic\n");
scanf("%d",&ch);
printf("1\n");
}
This generates the warning
test.c:7:2: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘unsigned char *’ [-Wformat=] scanf("%d",&ch);
Actually,
scanf("%d",&ch);
leaves your program with undefined behavior as the supplied argument is not the correct type for the conversion specifier. You need to write
scanf("%hhu",&ch);
Quoting C11, chapter §7.21.6.2
hh
Specifies that a following d, i, o, u, x, X, or n conversion specifier applies
to an argument with type pointer to signed char or unsigned char.

(1)In function ‘main’:(2) warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat] [duplicate]

This question already has answers here:
Correct format specifier to print pointer or address?
(5 answers)
Closed 6 years ago.
What is wrong with my code?
#include<stdio.h>
int main(void){
int a;
int *p;
p=&a;
printf("%d\n",p);
}
%d format specifier in printf() expects an int argument. In your code you're passing a int *. You need to either
Dereference the pointer to get an int type, if you want to print the value stored in the memory location pointed by the pointer.
use %p format specifier and cast the argument to void *, if you want to print the pointer.
It invokes undefined behavior to pass a wrong type of argument to any format specifier.

How printf works in case of type mismatch with type specifier?

int main()
{
printf("%c", "\n");
return 0;
}
Here according to type specifier a character is required. But we are passing it const char *. I was hoping that it would give me a warning message in code blocks GNU GCC compiler but it is not giving me any warning and printing the $ character.
why it is not giving any type of warning?
You need to enable that warning.
Compiling your code as test.cpp and adding #include <stdio.h> for printf with the correct warning flag under gcc 4.7.2:
$ gcc -c -Wformat test.cpp
test.cpp: In function 'int main()':
test.cpp:5:18: warning: format '%c' expects argument of type 'int', but argument 2 has type 'const char*' [-Wformat]
With printf() if there is a type mismatch then it leads to undefined behavior.
The format specifier should match with the type which you want to print.
Further the number of arguments should match with the number of format specifiers violating which will also leads to undefined behavior.
Just add -Wall while compiling your code you will get the below error:
warning: format %c expects type int, but argument 2 has type char *
You could see that the code also works with %d, %x, %u format specifiers.
Why it works without any warnings ?
Because you don't have warnings enabled in your CodeBlocks.
Go to settings -> compiler and check
Enable All Common Compiler Warnings [-Wall]
And now you get:
In function 'int main()':
warning: format '%c' expects argument of type 'int', but argument 2 has type 'const char*' [-Wformat=]|
Why it even works ?
With %c, $ is the output in CodeBlocks, X is the output in Visual Studio . So, that sounds like undefined behavior.
Wrong format specifiers in scanf (or) printf
Anyways if you want the first char this way only you could do this:
#include <stdio.h>
int main()
{
printf("%c", *"Hello\n"); // Not asked in Question but still :)
return 0;
}
It prints H by dereferencing the const pointer.

Getting Warning When Using SizeOf [duplicate]

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

snprintf giving different output

Why is snprintf giving different value in second case. Is it because any integer limit. Can you please explain how snprintf works and why the reason for that negative value
#include <stdio.h>
#include <string.h>
main()
{
char buff[256];
snprintf(buff,256,"%s %d"," value",1879056981);
printf("%s",buff);
}
output:
value 1879056981
#include <stdio.h>
#include <string.h>
main()
{
char buff[256];
snprintf(buff,256,"%s %d"," value",2415927893);
printf("%s",buff);
}
output:
value -1879039403
It's because the integer 2415927893 can't represented by any integer type on your system and you have signed overflow in your program.
The exact type of integer literal depends on how big the number is. C11 defines that an integer literal can be of int or long int or long long int, depending on which one fits first in that order.
6.4.4.1 Integer constants
The type of an integer constant is the first of the corresponding list
in which its value can be represented.
Turn on the compiler warnings.
On my system, gcc warns about it when I compile youre code with:
gcc -std=c11 -Wall -pedantic t.c
t.c:4:1: warning: return type defaults to ‘int’ [enabled by default]
t.c: In function ‘main’:
t.c:9:4: warning: format ‘%d’ expects argument of type ‘int’, but argument 5 has type ‘long long int’ [-Wformat]
t.c:9:4: warning: format ‘%d’ expects argument of type ‘int’, but argument 5 has type ‘long long int’ [-Wformat]
The literal 2415927893 is interpreded as an int. As it is larger than INT_MAX on your machine, you get an overflow.
To avoid this, you may interpret it as an unsigned int:
snprintf(buff,256,"%s %u"," value",2415927893U);
Or as long long:
snprintf(buff,256,"%s %lld"," value",2415927893ll);

Resources