Printing multiple variables in single printf() statement
For string use %s
instead of %c
and If your Id number is integer use %d
instead of %s
printf("Entered string is %s with length %d and
ID number is %s with length %d",string,i, number, j);
How can I print multiple variables using printf
Solution
printf '%s %d %s' "$infile" "$insize" "$indate" 2>&1 | tee -a "$logfile"
find "$infile" -printf ' %p %s %CY-%Cm-%Cd %CH:%CM:%.2TS \n' 2>&1 | tee -a "$logfile"
Result
ftp://ftp.ncbi.nlm.nih.gov/pub/README.ftp 2037 2015-08-12 15:47:26 ./README.ftp 2037 2019-08-24 20:32:53
man 1 printf
clear and simple explanation of argument handing.
GNU Manual Table of Output Conversions for format summary
Why is the printf specifiers, %s, printing multiple variables at once?
Whats happening is the a null terminating character is implicitly being added when you initialize a char[] like below:
char myStr[] ="Ignacio";
If you want to initialize a char[] with individual characters you must initialize it like the following because the null terminating character must be explicitly added:
char myStr[] ={'I', 'g', 'n', 'c', 'i','0','\0'})
.
Explanation for the Behaviour in Your Case:
Memory Layout:
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [A] [B] [C] [D] [E] [F] [10] [11]
'D' 'i' 'e' 'g' 'o' 'D' 'i' 'e' 'g' 'o' 'I' 'g' 'n' 'a' 'c' 'i' 'o' '\0'
After you initialize your strings, the characters are arranged in memory like above. In the C language, all valid strings must have a null terminating character at there end to specificity the end of the string. The reason why is printf
will just start at the address of the char* passed to it and will keep reading characters until a '\0' character is encountered. Because of this, if you pass printf
an address to an array of characters that does not contain a '\0', then printf
will keep marching past those characters into memory until a null terminating character is encountered. (This could result in a segmentation fault)
Explanation for Your Output: (Reference the memory layout above)
Name: DiegoIgnacio
printf
will start at index [5] and will print all characters until the '\0' character, thus "DiegoIgnacio" ([5] - [11]
) is the output.
Name: DiegoDiegoIgnacio
printf
will start at index [0] and will print all characters until the '\0' character, thus "DiegoDiegoIgnacio" ([0] - [11]
) is the output.
How to print variable multiple times with one printf statement in C
So the answer is...it is not possible to do what I wanted on the way I wanted. The best way is to use another for loop inside existing one.
Thank you all for your answers.
How to print multiple variable lines in Java
You can do it with 1 printf
:
System.out.printf("First Name: %s\nLast Name: %s",firstname, lastname);
(C) Displaying multiple variables on the printf line, having some trouble
Though you names these variables int1
etc, their type is double
! So either declare them as int
or if you choose to use double
:
Change
scanf("%d", &int1);
to
scanf("%lf", &int1);
and give them better names.
what is the correct syntax for awk's printf to insert multiple variables?
You need one format string, like %s
, for every variable. So, try:
printf "%s %s\n", a, a_counter
Or:
printf "pattern=%s and count=%s\n", a, a_counter
%s
converts any variable to a string. If the variable is a number, other formats, like %f
or %e
, give you more control over how the number is converted to a string. See man awk
for details.
Examples
$ awk -v a="genie" -v a_counter=3 'BEGIN{ printf "%s %s\n", a, a_counter }'
genie 3
$ awk -v a="genie" -v a_counter=3 'BEGIN{ printf "pattern=%s and count=%s\n", a, a_counter }'
pattern=genie and count=3
$ awk -v a="genie" -v a_counter=3 'BEGIN{ printf "pattern=%s and count=%7.2f\n", a, a_counter }'
pattern=genie and count= 3.00
$ awk -v a="genie" -v a_counter=3 'BEGIN{ printf "pattern=%s and count=%9.2e\n", a, a_counter }'
pattern=genie and count= 3.00e+00
How can I print multiple character with one printf?
You can not use printf
like that to print repetitive characters in Ansi C. I suggest you to use a loop like this -
#include <stdio.h>
int main()
{
int i;
for(i = 0; i < 10; i++) putchar('#');
return 0;
}
Or if you have absolutely no desire to use loops, you can do something like this-
#include <stdio.h>
int main()
{
char out[100];
memset(out, '#', 10);
out[10] = 0;
printf("%s", out);
return 0;
}
By the way, using printf
like this also works-
#include <stdio.h>
int main()
{
printf("%.*s", 10, "############################################");
return 0;
}
printing multiple values using pointer in printf
The C standard does not define in what order the parameters get evaluated. Try using *(ptr+1) instead of *ptr++, which also can be replaced with ptr[1].
Related Topics
Difference Between Cp and Mv Force Overwrite
How to Find Grid Points Nearest to Given Location Using Shell Script
Cannot Kill Redis-Server on Linux
How to Open Include File 'Io.Mac' Assembly
Mongodb Service Fails After Reboot
Redirecting Output of a C Program to Another C Program with a Bash Script Under Linux
Find Command Search Only Non Hidden Directories
Run Hydra (Mpiexec) Locally Gives Strange Ssh Error
Case Statement in a While Loop, Shell Scripting
Linux Script Telnet Head Request
Different Show Between "Cat" and "Vim"
Container Running in Privileged Mode