C arguments from the C primer plus 6e should effect print [duplicate] - c

This question already has answers here:
What can happen if printf is called with a wrong format string?
(5 answers)
Closed 4 years ago.
C written in Emacs
// print1.c --displays some properties of printf()
#include <stdio.h>
int main(void)
{
int ten = 10;
int two = 2;
printf("Doing it right: ");
printf("%d minus %d is %d\n", ten, 2, ten - two);
printf("Doing it wrong: ");
printf("%d minus %d is %d\n", ten); // forgot 2 arguments
return 0;
}
Welcome to the Emacs shell
Output:
~ $ ./a.exe
Doing it right: 10 minus 2 is 8
Doing it wrong: 10 minus 2 is 8

It should do nothing of the sort.
The behaviour of your program is undefined.
One manifestation of undefined behaviour is the compiler figuring out what you really wanted to do. Don't ever rely on that though.

Related

inner workings of scanf in c [duplicate]

This question already has answers here:
What is the effect of trailing white space in a scanf() format string?
(4 answers)
Using "\n" in scanf() in C [duplicate]
(2 answers)
Closed 2 years ago.
firstly i am a noob in c, and i have seen this issue various times in c, and it happens most of the time
here is the code
#include <stdio.h>
int main ()
{
int n,a=5, A[n], i;
printf("value of n is\n");
scanf("%d\n", &n);
for(i=0; i<n; i++)
{
printf("value of A[%d] is %d\n", i, a++);
}
return 0;
}
so when it comes to output, something strange happens, the program asks for two input values
here see this
/tmp/bi8gJ4fc18.o
value of n is
10
7
value of A[0] is 5
value of A[1] is 6
value of A[2] is 7
value of A[3] is 8
value of A[4] is 9
value of A[5] is 10
value of A[6] is 11
value of A[7] is 12
value of A[8] is 13
value of A[9] is 14
so why does scanf require two inputs or when using scanf two inputs are to be provided, and it only considers the 1st input, what is happening here
also one more question, here in the stack-over-flow why I have to press enter two times in the body so that the next line actually goes to the next line in the output here
also, one more plz comment on this and have mercy on me, what is the proper way to search a question in sttack-over-flow, I mean i am never able to find the answer to my questions but when i ask a question others easily find some years old answer to it

C #define strange outputs [duplicate]

This question already has answers here:
The need for parentheses in macros in C [duplicate]
(8 answers)
Closed 5 years ago.
#include <stdio.h>
#define test(x) x*x
#define test2(x) x
int main(void) {
int x=5;
printf("%d %d %d %d %d %d",test2(x), test2(x-2), test(x), test(x-2),4*(test(x-3)),4*test(x-3));
return 0;
}
This gives the output as :
5 3 25 -7 -52 2
Well can understand for first 3 but why -7 when test(x-2) and for last 2…
After compiler preprocessing step your printf becomes
printf("%d %d %d %d %d %d",x, x-2, x*x, x-2*x-2,4*(x-3*x-3),4*x-3*x-3);
x-2*x-2 is evaluated as:
x-2*x-2 -> x-(2*x)-2
5-10-2
= -7
ie * takes precedence over -
Your this expression will expand something like given at the second code snippet: Below code is indented just to easily show you the code, I know it is syntactically wrong. :)
printf("%d %d %d %d %d %d",test2(x),
test2(x-2),
test(x),
test(x-2),
4*(test(x-3)),
4*test(x-3));
After expansion:
printf("%d %d %d %d %d %d",x,
x-2,
x*x,
x-2*x-2,
4*(x-3*x-3),
4*x-3*x-3);
Macros doesn't replace expression with brackets enabled, rather it does the literal replacement.
If you do the maths, you will get the answer which you had already shown on the top.

Format specifier in c [duplicate]

This question already has answers here:
What does "%.*s" mean in printf?
(4 answers)
Closed 6 years ago.
Why the following program work ? & what is difference b/w %d vs %*d ?
#include<stdio.h>
int main()
{
int n=5;
printf("n=%*d\n", n, n);
return 0;
}
What does width meant here by ?
The first n belongs to the * in %*d, so for your example it is %5d. %5d means to print an integer of width 5. Example: 234 is printed as __234 (2 spaces)

Trying to understand this printf formatting output [duplicate]

This question already has answers here:
Why does printf print wrong values?
(7 answers)
Closed 7 years ago.
I don't understand why this code produces the below output:
#include <stdio.h>
#define LOWER 0
#define UPPER 300
#define STEP 20
float c_to_f(float c);
int main(void) {
for (int c = LOWER; c <= UPPER; c += STEP) {
printf("%3.0f %6.1f\n", c, c_to_f(c));
}
return 0;
}
float c_to_f(float c) {
return c * (9.0/5.0) + 32;
}
with output:
0 5144477247317086170901765440027035767837163293591161256351693248184965237877467107389389528872273154691913581744607058050215827488351921876414407003384176234234181468372580859505320314312544948225387164490993094256968227227818959640206687395851530141696.0
0 5144477248223936133773425621301895611375254354002848388186656757998856300277376171732587490599187934010444965285645525890922748188992798901374640654313592657351174354641522290319226294263523595109393871854132336451448805097328901373303486131449817464832.0
0 5144477248704033172940775129035644940307184915397270987393402145547386862724386852855456999748731052473196874219136479453649941501096792620471234940099754293118994117960373636044235342472865231695044481634617817613232640440010635232001791344413616635904.0
0 5144477249104114038913566385480436047750460383225956486732356635171162331430229087124514924040016984525490131663712274089255935927850120719718396844921555656258843920726083090815076215980649928849753323118355718581385836558912080114250379021883449278464.0
0 5144477249344162558497241139347310712216425663923167786335729328945427612653734427685949678614788543756866086130457750870619532583902117579266693987814636474142753802385508763677580740085320747142578628008598459162277754230252947043599531628365348864000.0
0 5144477249584211078080915893214185376682390944620379085939102022719692893877239768247384433189560102988242040597203227651983129239954114438814991130707717292026663684044934436540085264189991565435403932898841199743169671901593813972948684234847248449536.0
I do understand that c should be a float, just not the output. Any help would be greatly appreciated.
The problem here, as I see it is
printf("%3.0f %6.1f\n", c, c_to_f(c));
c is an int and you're trying to print it's value using %f, which is undefined behavior.
Using an inappropriate type of argument for a format specifier is undefined behavior.
When using printf and floats you have the right token %f. But your using it wrong. To printf out 2 decimal places you would use %.2f, 3 would be %.3f and so on. I think the console is bugging out because you are using %3.0f and %6.1f

gcc compiler printf execution order [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 9 years ago.
For the testing code as follow:
#include <stdio.h>
int addTen(int x, int b[])
{
b[2] = x + b[2];
return b[2];
}
void main(void)
{
int a[3] = {4,5,6};
int i = 2;
printf("%i %i %i \n", a[i], addTen(10,a), a[i]);
}
Why is the output is 16, 16, 6? I know that even if the compiler processes the order from right to left like a[i] <- addTen(10,a) <-a[i]. After calling addTen(10,a), a[i] is already 16 (not 6). So why the output is not 16, 16,16? THANKS!
It's undefined behavior, you should read about sequence points. You're modifying a and simultaneously reading it in a same expression.
In addition the order of evaluating is not defined.
There's no order defined for evaluating arguments. The compiler is free to evaluate arguments in any order, and will normally choose the most convenient order. So, you can't define any expected output.

Resources