Confusion regarding a printf statement - c

So I was running this code
#include<stdio.h>
int add(int x, int y)
{
return printf("%*c%*c",x ,' ',y,' ');
}
int main()
{
printf("Sum = %d", add(3,4));
return 0;
}
And can't seem to understand how does the following statement works
return printf("%*c%*c",x ,' ',y,' ');
So I tried writing a simple code
int x=3;
printf("%*c",x);
and I got a weird special character (some spaces before it) as output
printf("%*c",x,' ');
I am getting no output. I have no idea what is happening? Please Help. Thank you.

This code
int x=3;
printf("%*c",x,'a');
makes use of the minimum character width that can be set for each input parameter to printf.
What the above does is print out the a character, but specifies that the minimum width will be x characters - so the output will be the a character preceded by 2 spaces.
The * specifier tells printf that the width of the part of the output string formed from this input parameter will be x characters minimum width where x must be passed as additional argument prior to the variable that is to be printed. The extra width (if required) is formed from blank spaces output before the variable to be printed. In this case the width is 3 and so the output string is (excluding the quotes which are just there to illustrate the spaces)
" a"
With your code here
printf("%*c",x);
you have passed the length value, but forgotten to actually pass the variable that you want printed.
So this code
return printf("%*c%*c",x ,' ',y,' ');
is basically saying to print the space character but with a minimum width of x characters (with your code x = 3), then print the space character with a minimum of y characters (in your code y = 4).
The result is that you are printing out 7 blank space characters. This length is the return value of printf (and hence your add function) which is confirmed by the output of
Sum = 7
from the printf inside main().
If you change your code to
return printf("%*c%*c",x ,'a',y,'b');
you would see the string (obviously excluding the quotes)
" a b"
printed which would make what is happening more clear.

Try to print that in the correct way. %d is for printing integer, %c is for printing char.
In your code:
int x=3;
printf("%*c",x);
The x is how much spaces the char will get, you write 3. But you didn't put the char you want to print, so it print garbage.
When you write:
printf("%*c",x,' ');
You printing space ' ' char inside 3 chars space.
Same thing when you do
printf("%*c",x, ' ',y,' '); - just 7 blank spaces. The result is correct because printf returns how many characters it writes.
Look here for extra printf formats.

Related

Right justifying with printf() with additional character

In a table I am making, I have to print an integer representing cost right justified so that it takes up six characters in total which is okay, but I also need to insert a '$' character before the number. Is there a way to do this without making a new string that is just the integer with the $ in front of it?
This will print [ $26]. Brackets are there just to show the leading space.
snprintf() will return the number of characters needed to print the formatted string.
%*c will print a character after first getting an argument for the width.
#include<stdio.h>
#include<stdlib.h>
int main ( ) {
int cost = 26;
printf ( "[%*c%d]\n", 6 - snprintf ( NULL, 0, "%d", cost), '$', cost);
return 0;
}
I'm assuming you're doing the normal printf call:
printf("%d", cost);
just add it in front?
printf("$%d", cost);
more help here: https://www.gnu.org/software/gawk/manual/html_node/Printf-Examples.html

Using atof() function in C with multiple input values

The goal of this program is to create a function which reads in a single string, user typed, command (ultimately for program to be used in conjunction with a robot) which consists of an unknown command word(stored and printed as command), and an unknown number of decimal parameters(the quantity is stored and printed as num, and the parameters are to be stored as float values in the array params). In the User input, the command and parameters will be separated by spaces. I believe my issue is with the atof function when I go to extract the decimal values from the string. What am I doing wrong? Thank you for the help!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void func(char *input, char *command, int *num, float *params);
int main()
{
char input[40]={};
char command[40]={};
int num;
float params[10];
printf("Please enter your command: ");
gets(input);
func(input,command,&num,params);
printf("\n\nInput: %s",input);
printf("\nCommand: %s",command);
printf("\n# of parameters: %d",num);
printf("\nParameters: %f\n\n",params);
return 0;
}
void func(char *input, char *command, int *num, float *params)
{
int i=0, k=0, j=0, l=0;
int n=0;
while(input[i]!=32)
{
command[i]=input[i];
i++;
}
for (k=0; k<40;k++)
{
if ((input[k]==32)&&(input[k-1]!=32))
{
n++;
}
}
*num=n;
while (j<n)
{
for (l=0;l<40;l++)
{
if((input[l-1]==32)&&(input[l]!=32))
{
params[j]=atof(input[l]);
j++;
}
}
}
}
A Sample Output Screen:
Please enter your command: Move 10 -10
Input: Move 10 -10
Command: Move
# of parameters: 2
Parameters: 0.000000
The Parameters output should, ideally, read "10 -10" for the output. Thanks!
Change atof(input[l]) to atof(input + l). input[l] is single char but you want to get substring from l position. See also strtod() function.
Other people have already remarked the problem in your code, but may I suggest that you have a look at strtod() instead?
While both atof() and strtod() discard spaces at the start for you (so you don't need to do it manually), strtod() will point you to the end of the number, so that you know where to continue:
while(j < MAX_PARAMS) // avoid a buffer overflow via this check
{
params[j] = strtod(ptr, &end); // `end` is where your number ends
if(ptr == end) // if end == ptr, input wasn't a number (say, if there are none left)
break;
// input was a number, so ...
ptr = end; // continue at end for next iteration
j++; // increment number of params
}
Do note that the above solution does not differentiate between invalid arguments (say, foo instead of 3.5) and missing ones (because we've hit the last argument). You can check for that by doing this: if(!str[strspn(str, " \t\v\r\n\f")]) --- this checks if we're at the end of string (but allowing trailing whitespace). See the second side-note for what it does.
SIDE-NOTES:
You can use ' ' instead of 32 to check for space; this has two advantages:
It is clearer to the reader (it's very clear that it's a whitespace, instead of "some magic number that happens to have meaning")
It works in non-ASCII encodings (and the standard allows other encodings, though ASCII is by far the most popular; one common encoding is EBCDIC)
For future reference, this trick can help you skip whitespace: ptr += strspn(ptr, " \t\v\r\n\f");. strspn returns the number of characters at the start of the string that match the set (in this case, one of " \t\v\r\n"). Check documentation for more info.
Example for strspn: strspn("abbcbaa", "ab"); returns 3 because you have aab (which match) before c (which doesn't).
you are trying to convert a char into a float,
params[j]=atof(input[l]);
you should get the entire word(substring) of the float.
Example, "12.01" a null terminated string with 5 characters and pass it to atof, atof("12.01") and it will return a double of 12.01.
so, you should first extract the string for each float parameter and pass it to atof
Avoid comparing character to ascii value, rather you could have use ' ' (space) directly.
Instead of using for loop with a fixed size, you can use strlen() or strnlen() to find the length of the input string.

How does calling printf() add numbers here?

I don't understand how this printf() call is working to add together two numbers. Does the %*c have something to do with it?
//function that returns the value of adding two number
int add(int x, int y)
{
NSLog(#"%*c",x, '\r');
NSLog(#"%*c",y, '\r');
return printf("%*c%*c", x, '\r', y, '\r'); // need to know detail view how its working
}
for calling
printf("Sum = %d", add(3, 4));
Output
Sum=7
When passed to printf (or similar functions), %*c means you're passing two parameters instead of one. The first parameter specifies a field width, and the second a character (or string, int, etc.) to write out in that width of field.
printf returns the total number of characters written to the output stream.
A number after % specifies the field width; so %5c will print 4 spaces, followed by the character argument, giving a total of 5 characters.
* specifies that the field width is provided as an argument to printf. So this will print one field of x characters, followed by another of y characters. Each field consists of spaces followed by a carriage-return ('\r'), so there shouldn't be any visible output. printf returns the total number of characters printed - in this case, x+y.
i agree with leeduhem, it is extremely clever,
printf() return the number of character it printed.
For the argument, I think it is more easy to understand with an example:(Also you can see the width trick in here:
#include <iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
int main() {
// your code goes here
int x = printf("%*c", 100, '\r');
printf("%d\n", x);
return 0;
}
x is whatever value you set to specific the width (in the eg, it's 100)
That means the width you specific is actually counted and return by the printf()
But I think this add() can only duel with positive numbers, if one of the number is <= 0, the method should fail, you may try add(-3,-4), in my own machine it still print 7...
Oh, this is clever.
return printf("%*c%*c", x, '\r', y, '\r');
On success, printf() will return how many character it printed, and "%*c", x, '\r' tell it to print x characters (x-1 spaces followed by one \r). Therefore, printf("%*c%*c", x, '\r', y, '\r') will return how many characters are printed, which is x+y.
See printf(3) for further details.
Note:
As pointed out by #shole, this int add(int x, int y) works only for both x and y are nonnegative integers. For example:
add(-1, 1) // gives 2 instead of 0
add(0, 1) // gives 2 instead of 1

Addition using printf in C [duplicate]

This question already has answers here:
Adding two numbers without using operators
(3 answers)
Closed 9 years ago.
on net: using printf to add two numbers(without using any operator) like following:
main()
{
printf("Summ = %d",add(10,20))
return 0;
}
int add(int x,int y)
{
return printf("%*d%*d",x,' ',y,' ');
}
Could anyone please explain, how this works:
return printf("%*d%*d",x,' ',y,' ');
Note: This fails when i call "sum" like following:
sum(1,1) or sum(3,-1)
There are two central concepts here:
printf() returns the number of characters printed.
The %*d format specifier causes printf() to read two integers from its arguments, and use the first to set the field width used to format the second (as a decimal number).
So in effect the values being added are used as field widths, and printf() then returns the sum.
I'm not sure about the actual d formatting of the space character, at the moment. That looks weird, I would have gone with an empty string instead:
static int sum(unsigned int a, unsigned int b)
{
return printf("%*s%*s", a, "", b, "");
}
int main(void)
{
unsigned int a = 13, b = 6;
int apb = sum(a, b);
printf("%d + %d = %d?\n", a, b, apb);
return 0;
}
The above works and properly computes the sum as 19.
printf returns the number of printed characters.
Here, it is used in the add function to generate a string only composed of 10 + 20 spaces, by using the format string.
So the printf in the add function will return 30.
Then, this result is simply printed with printf (his main purpose).
Note: it might be evident, but this printf usage has to be avoided. It's very dirty as it generates useless outputs. Imagine: add(10000,10000)...
First, printf returns the number of characters it prints.
Second, in the format specifier %*d, * means the minimum number of characters to be printed, but the width is not from the format string itself, but from the additional argument.
With all together, the job is done, but it won't work well on small numbers like 1 because of %d in the format specifier, a better solution could be :
("%*c%*c", a, ' ', b,' ');

my program prints some arbit ascii string

I wrote following program
int main ()
{
char a=0xf;
a=a+1;
printf("%c\n",a);
}
the output of above program is what I am not able to understand.It is giving me some character which I am not able to understand.Is it possible to find out ASCII code of the character that I am getting in my above program so that I understand what is it printing.
EDIT
Based on the replies I read I am adding further to my confusion
if I write a statement as following
char ch='Z';
then what would be stored in ch,
1) The character Z
2) ASCII value of Z
3) Z along with single inverted commas
4) Both (1) and (2)
ASCII value for 16(0x0f + 1 = 0x10) is DLE (data link escape) which is non-printable character.
Just Print as integer like this.
printf("%d\n",a);
The characters from 0 to 31 are non-printing characters (in your case, you've chosen 0xF, which is 15 in decimal). Many of the obscure ones were designed for teletypes and other ancient equipment. Try a character from 32 to 126 instead. See http://www.asciitable.com for details.
In response to your second question, the character stores the decimal value 90 (as characters are really 1-byte integers). 'Z' is just notation that Z is meant to be taken as a character and not a variable.
You can modify your program like that:
int main ()
{
char a=0xf;
a=a+1;
printf("Decimal:%u Hexa:%x Actual Char:|%c|\n",a,a,a);
}
Printf can use different formatting for a character.
Its printing the character 0x10 (16).
If you want the output, change your print to output the values (in this case, character, hex value, decimal value):
printf("%c - %x - %d\n", a, a, a);
#include<stdio.h>
int main ()
{
char a='z'; \\\ascii value of z is stored in a i.e 122
a=a+1; \\\a now becomes 123
printf("%c",a); \\\ 123 corresponds to character '{'
}

Resources