Unexpected behavior of printf() in C [duplicate] - c

This question already has answers here:
what does the - operator do with char *?
(4 answers)
Closed 9 years ago.
#include <stdio.h>
int main()
{
short int a = 5;
printf("%d" + 1, a);
return 0;
}
The code prints the alphabet enclosed in quotes in printf irrespective of the value and type of variable a. If any other number is added except 1 nothing gets printed.
Why is it so?

Not sure, I would expect it to print just d, of course. That's what happened when I tested it.
If you add more than 1 (or 2) all bets are off and you're getting undefined behavior for passing a random pointer instead of a valid formatting string.

On compiling the above code, you should get a warning like:
[Warning] too many arguments for format [-Wformat-extra-args]
Now remove the printfs argument a.
printf("%d" + 1);
This will print d.
100 101
% d
^
|
Here is the starting address of the string.
%d is a string and its starting address is 100. "%d" + 1 will give you the address 101.

Why you want to do this?
if you want you can do like
do like
printf("%d", a+1);

Try this and you'll understand what unwind is trying to make you understand
#include <stdio.h>
int main()
{
short int a = 5,b = 4;
printf("%d %d" + 4, a,b);
return 0;
}
OUTPUT: d
Since it takes the 4th character inside the double quotes in printf() statement..
If number is 3
OUTPUT: 5
If number is 2
OUTPUT: 5
If number is 1
OUTPUT: d

Related

printf statement modification in C [duplicate]

This question already has answers here:
Can we use only one variable for many format descriptors in printf in C
(3 answers)
Closed 1 year ago.
Lets take an example: (in C)
consider we have inputted a,b; (say a=1,b=2)
expected output:
1 + i2
1 - i2
1 X i2
so i do
printf("%d + i%d \n %d - i%d \n %d X i%d ",a,b,a,b,a,b);
Here Iam using a,b 3 times in printf statement ,the same variable is being defined 3 times
This is a small example so only 3 times we are using a,b but in big problems this thing might become a tedious job.
so is their a better way or alternative way to do this ?
You can split your printf function into few printfs , as it will reduce the work of matching the number of format specifiers and the variables.
By the way , I think you could use a loop structure to reduce that further.
Maybe use this: (the character array a contains +,- and X. The for loop is to display the characters contained in a)
#include<stdio.h>
int main(int argc, char *argv[]){
char a[]="+-X";
int n1=1;
int n2=2;
for(int i=0;i<3;i++){
printf("%d %c i%d\n",n1,a[i],n2);
}
}

Passing ("text"+1) argument to Strlen function in C. Why is Output 3?

I am having problem understanding why is the output to this argument to strlen function 3.
I tried changing values from 1 to 2,3. I tried changing "text" to "textt".
The pattern i observed is that the output is like this
number of characters in string - numerical value
for e.g.if I use 2 instead of 1. The answer is 4 - 2 = 2
for +1 it is giving 4 - 1 = 3.
I am getting confused with this. Please help me understand.
Thanks
#include<stdio.h>
#include<string.h>
int main(void)
{
char str[] = "Text";
printf("%ld %ld \n", strlen(str+4), strlen("text"+1));
return 0;
}
The syntax "string_literal" + N will result in a pointer N bytes up from the start of the literal. "test"+1 points to the string "est", which has length 3.

Explain the return value of printf [duplicate]

This question already has answers here:
How do I explain the output of this simple C code?
(4 answers)
Return value of printf() function in C
(13 answers)
Closed 6 years ago.
int main (void){
const int y = 99;
printf("%d\n", printf("y = %d", y));
}
This program prints "y = 996", I would like help in understanding.
Firstly,
printf("y = %d", y) prints out y = 99
So the rest of the final expression is:
printf("%d\n", "y = 99");
But this should be invalid as %d expects a number, not a charArray?
The printf function returns the number of bytes printed. That value is what is being passed to the second call.
The inside call prints y = 99 which is 6 characters. So the outer call receives 6 as the second parameter:
printf("%d\n", 6);
So the output is y = 996.
Let me take a simple example to clear your doubt:
sample :
void main(void)
{
int b=printf("hellow");
printf("%d",b);
}
output:
6
printf("message") is a predefined method which returns "no. of characters present in the message" as the above example.
you code:
int main (void){
const int y = 99;
printf("%d\n", printf("y = %d", y));
}
note: After printing the message printf() always returns int value which is no of characters because return type of printf() is int type.
step 1: Inner printf() function prints 99 first.
step 2: After execution it returns 6 which is no. of characters in the message.
message is "y = %d"
step 3: 6 is hold by outside printf() function as :-
printf("%d",6);
So, it gives output combinely :
996
While printf will print out a string to stdout, what it returns is an integer that represents the number of characters it printed out.
The nested call to printf, printf("y = %d", y), prints out 99 and returns 6 because the string "y = 99" is 6 characters long, which makes the outer call to printf, which takes the return value of the nested call to printf as an argument, produce the output 6, giving you the final output of 996.

Unable to understand the output of C progaram [duplicate]

This question already has answers here:
sequence points in c
(4 answers)
Closed 9 years ago.
My code is :
#include<stdio.h>
int main()
{
char c='8';
int d=8;
printf("%d %d %d",d,d+=c>='0'&&c<='9',c++);
return(0);
}
The output of this question is : 9 9 56.
I'm unable to understand this.
Please somebody help me and please explain me the output.
Thanks.
You are observing undefined behaviour. d is passed as an argument twice, and once with side effects. If done in sequence your code should be equivalent to
char c='8';
int d=8;
printf("%d",d);
d+= (c>='0)' && (c<='9');
printf(" %d", d);
printf(" %d",c);
c++;
But since it is undefined in what order the arguments are computed when passing them with side effects to a function you are seeing here:
char c='8';
int d=8;
d+= (c>='0)' && (c<='9');
printf("%d",d);
printf(" %d", d);
printf(" %d",c);
c++;
Basically printf reads the argument from right to left so the first print would be of
" printf("%d %d %d",d,d+=c>='0'&&c<='9',c++); "
- 1st output = c++ -> which is nothing but ascii value of '8' = 56
and then c is incremented by 1 .
- next is the d -> it boils down to d = d + if(c>='0'&&c<='9') ...
so d = d+1 so 9 here
- next d again so it is again Hope it is clear !
Ok, the first thing you have to know is, a char is stored in the memory as Ascii Table. So in the memory the char c will have a integer value of '8'. From the table we know that the integer value of '8' is 56. According to the table we'll get:
'8': 56
'0': 48
'9': 57
So let's get started.
c++: It is a statement which adds c by one and return the value of current c.
Example:
int a,c;
c=1;
a=c++;
printf("a=%d,c=%d",a,c);
The result of this code is "a=1,c=2"
so %d of c++ is still 56.
d+=c>'0'&&c<='9': According to C's priority this statement will be like:
d+=(c>'0'&&c<='9')
So let's start with c>'0'&&c<='9' first. It is a condition statement. Is c's Ascii value great or equal to 0's Ascii value AND less or equal to 9's Ascii value?( Notice c's Ascii is 56 or 57 now because the evaluation order of printf is undefined. So it will be 56 if this statement is evaluated before c++ or 57 if after c++. But both way, c<='9' is true ) YES. So the statement is true. In C the TRUE is 1.
So d+=c>'0'&&c<='9' will be d+=1 which means d=d+1. So %d of d is 9.
So the result is "9 9 56"

Explain the following code snippet?

int main()
{
int a, b, c;
a = 10;
b = 20;
c = printf("%d", a) + ++b;
printf("\n%d", c);
}
The output of the above program is 23 it seems but i dont know how it is obtained. Can anyone have an idea about it?
printf has a return value, which is the total number of characters it prints.
The statement printf("%d",a) will print 10, which means the return value of printf here is 2.
The rest is easy:
c=printf("%d",a)+ ++b;
c will have a value of 2 + 20 + 1, which is 23.
Here the output will be two different integers,for two different printf statements . For the first printf statement the code prints 10, then when this printf statement participate in some assignment statement , it is treated as the number of characters it is printing i.e. 2 here. Then it is added to ++b i.e. 21 (PRE-INCREMENTED) . So the output is 23(2 + 21) .
The whole output looks like this :
10
23
printf returns the number of characters printed as an integer. So as you are printing 10 it will return 2.
So now
c=printf("%d",a)+ ++b;
will become
c=2+ ++b;
since b with a value of 20 is pre-incremented this will become
c=2+21
Therefore c=23

Resources