Questions tagged [conversion-specifier]

120 questions
65
votes
7 answers

Reading in double values with scanf in c

I try to read-in 2 values using scanf() in C, but the values the system writes into memory are not equal to my entered values. Here is the code: double a,b; printf("--------\n"); //seperate lines scanf("%ld",&a); printf("--------\n");…
user1880009
  • 663
  • 1
  • 5
  • 5
5
votes
3 answers

What is exactly an "invalid conversion specification"?

As per C11, chapter §7.21.6.1, P9 If a conversion specification is invalid, the behavior is undefined.282) If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined. Till time, my…
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
4
votes
2 answers

How to separate format specifiers from characters with printf

Another way to phrase this question might be "How to end format specifiers in printf" If I want to print microseconds in this format 100us using the following code... long microseconds = 100L; printf("%lus", microseconds); only prints 100s because…
4
votes
5 answers

How does printing 577 with %c output "A"?

#include int main() { int i = 577; printf("%c",i); return 0; } After compiling, its giving output "A". Can anyone explain how i'm getting this?
4
votes
2 answers

What is the difference between the %PRId and %d format characters?

Consider the following code: #include #include void main(void) { int32_t a = 44; fprintf(stdout, "%d\n", a); fprintf(stdout, "%"PRId32"\n", a); } When is it better to use %d and when would it be better to use…
user7659542
4
votes
2 answers

sscanf(s, "%u", &v) matching signed integers

After Cppcheck was complaining about "%u" as the wrong format specifier to scan into an int variable, I changed the format into "%d", but when having a second look on it before committing the change, I thought that the intention could be to prevent…
Wolf
  • 9,679
  • 7
  • 62
  • 108
3
votes
2 answers

Are %f and %lf interchangeable when using printf?

In an article on the w3resource.com, I've seen the following code: double x, y; pr1 = sqrt(pr1); x = (-b + pr1)/(2*a); y = (-b - pr1)/(2*a); printf("Root1 = %.5lf\n", x); printf("Root1 = %.5lf\n", y); The article says to take three floating-point…
Anik Sen
  • 31
  • 3
3
votes
2 answers

Why is int to float conversion failing in printf?

When using a int to float implicit conversion, it fails with printf() #include int main(int argc, char **argv) { float s = 10.0; printf("%f %f %f %f\n", s, 0, s, 0); return 0; } when compiled with gcc -g scale.c -o scale it…
3
votes
2 answers

Is the default value of malloc with the size of a single char P?

char *ptr = malloc(sizeof(char)); printf("\nValue of ptr: %d\n",ptr); printf("\nValue of address of ptr: %x\n",&ptr); printf("\nValue of pointed by ptr: %c\n",*ptr); printf("\nValue of address pointed by ptr: %x\n",&(*ptr)); *ptr='u'; …
NeuronB
  • 53
  • 6
3
votes
3 answers

Why doesn't the '+' sign work with printf for unsigned values?

I just wrote and ran following program. This just gave an unexpected output without + sign printed to U...MAX. #include #include #include int main() { // ... printf("LLONG_MIN: %+lli\n", LLONG_MIN); …
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
3
votes
3 answers

sscanf check doesn't work when given more input than required

I have an if statement that is supposed to check if the user entered exactly 4 arguments separated by a var like the following: param1,param2,param3,param4 . Problem is that it doesn't return an error if the user gives more than 4 inputs. It only…
MM1
  • 912
  • 1
  • 10
  • 28
3
votes
1 answer

String format in C %*s

I saw somewhere a portion of code in C char name[51]; int group = 0; scanf("%*s %50s %*s %d", name, &group); printf("%s / %d\n", name, group); If we introduce "Name:Smith Group:7" it waits for us to introduce another values. It's strange. What…
user11193267
2
votes
3 answers

Difference between & address and pointer address- Hexadecimal and pointer type data

I am using the following piece of code as a learning exercise for pointers: int a=8; int *ptr=&a; printf("\nThe & address of a is: %x\n",&a); printf("\nThe value of the pointer ptr is: %p \n",ptr); I am doing this to identify the address values…
NeuronB
  • 53
  • 6
2
votes
3 answers

Printf performing implicit casting

In the below code snippet, why do we get an output of 600? Two questions will help me understand the behavior. I specified the type for variable b to be uint8_t, why doesn't the math operation restrict to the same type? I thought PRIu8 is the right…
niil87
  • 47
  • 5
2
votes
5 answers

Getting an integer from a string

I have some code that reads a string of the form name 1 in a while loop, and then I have to extract the integer value to print yes or no. But when I tried to do it with sscanf, it gives the destination integer the value only after the loop…
1
2 3 4 5 6 7 8