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);
}
}
Related
This question already has answers here:
Why am I getting a strange number in this code?
(5 answers)
Closed 2 years ago.
I have a simple program that is supposed to ask the user for 10 integers and print them out, but instead of printing the integer it prints a random 7 digit number that increases by 4 for every new int.
#include <stdio.h>
int main(int argc, char *argv[])
{
int number = 0;
int numbers[10];
for (int i = 0; i < 10; i++)
{
printf("Please enter 10 numbers: ");
scanf_s("%d", &number);
numbers[i] = &number;
printf("Your number was %d\n", &numbers[i]);
}
}
And the program ends up doing something like:
Please enter 10 numbers: 1
Your number was 5241560
Please enter 10 numbers: 2
Your number was 5241564
Please enter 10 numbers: 3
Your number was 5241568
And so on, with a different 7 digit number every time, but always increasing by 4.
The numbers that you might think of as random in this case are not random after all! They're actually addresses of the array elements. Why?
Well because in your printf you print out &numbers[i]. Putting the '&' sign means accessing the address of certain value, as opposed to accessing the value itself.
Therefore if you're interested in printing array elements as opposed to addresses of array elements then you should remove '&' form your printf statement.
Also numbers[i] = &number; means "assign address of variable number to ith element of array. You should remove unnecessary '&' signs if accessing addresses is not what you actually want.
Here are some good references on addresses in C:
Pointers in C,
5 Minute guide to C pointers
You use & unnecessarily. Replace
numbers[i] = &number;
with
numbers[i] = number;
and
printf("Your number was %d\n", &numbers[i]);
with
printf("Your number was %d\n", numbers[i]);
You also don't use int argc, char *argv[] parameters so replace
int main(int argc, char *argv[])
with
int main(void)
I was doing a programming question and one of the sample output is 64197148392731290. My code for that question is correct as it is giving me the right answers for other test cases (output for those test cases are in single digit).
I understand that there will be too many iterations for the test case which has 64197148392731290 as output. So what should I do to get correct answer for that test case too.
Here is the code :
#include<stdio.h>
#include<string.h>
int main() {
int test_case;long long int i, j, count, n, k, k1;
scanf("%d", &test_case);
while(test_case--) {
scanf("%lld%lld", &n, &k);
char a[n];
count=0;
k1=k;
scanf("%s", a);
while(k1--) {
strcat(a,a);
}
for(i=0;i<(n*k);i++) {
if(a[i]=='a') {
for(j=(i+1);j<(n*k);j++) {
if(a[j]=='b') {
count++;
}
}
}
}
printf("%lld\n", count);
}
return 0;
}
Sample Input and Output :
Input:
3
4 2
abcb
7 1
aayzbaa
12 80123123
abzbabzbazab
Output:
6
2
64197148392731290
My task is to count the number of subsequences "ab" (not necessarily consecutive) in the new string. The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains two integers N and K, denoting the length of the initial string S and the number of repetitions respectively.
The second line contains a string S. Its length is exactly N, and each of its characters is a lowercase English letter.
If you are trying to store input in "int" that wont work coz this number its out of range, change it to "long long int"
Well the previous answer was sure wrong. Thanks for the code.
Sorry don't have time for detailed study but preliminary analysis tells me that maybe the error is because you are trying to store a sting of length 2n in a[n]. It works for smaller values since when you declare
char a[n];
^
variable known at runtime
it actually allocates a large block so that any value of n within range is possible. For large values strcat(a,a) will probably fail.
Basically somewhere down the line the string becomes corrupt. Most probably that is because of strcat. I suggest remove strcat, do something else to a similar effect.
What I'm trying to achieve is this:
X+Y X - Y X/Y X*Y
a b c d
I have searched google and I didn't find exactly what I was looking for, tried multiple methods,it still shows messed up.
This is my code so far:
int main()
{
int x,y,a,b,c,d;
printf("Introdu X si Y");
scanf("%d%d",&x,&y);
a=x+y;
b=x*y;
c=x-y;
d=x/y;
printf("X+Y\t","X*Y\t","X-Y\t","X/y\t");
printf("3%d,3%d,3%d,3%d",a,b,c,d);
return 0;
}
EDIT:Removing the commas worked fine,and the 3's in the code, I thought you could allign to the left with 3 spaces using 3%d.Thank you.
First of all, there's no need for the commas of the first print().
Secondly, the format you used in the second printf() is "3%d". It means that the number 3 will be printed before any number, and this is unnecessary.
Look at this code as an option to fix your problem (added some alignments and newlines)
#include <stdio.h>
int main() {
int x,y,a,b,c,d;
printf("Introdu X si Y \n");
scanf("%d%d",&x,&y);
a=x+y;
b=x*y;
c=x-y;
d=x/y;
printf("X+Y\t X*Y\t X-Y\t X/y\t\n");
printf("%d\t,%d\t,%d\t,%d\t",a,b,c,d);
return 0;
}
Some possible output is:
Introdu X si Y
6 3
X+Y X*Y X-Y X/y
9 ,18 ,3 ,2
Note that the use of int variable for the division will always give you the result as integer. Hence, in your code: 1/2 = 0.
The problem is in your printf statements.
printf("X+Y X*Y X-Y X/y\n");
printf("%d %d %d %d", a, b, c, d);
I think you just want this.
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
This question already has answers here:
Set variable text column width in printf
(2 answers)
Closed 9 years ago.
I'm pretty sure there's no way to do this, but I was wondering, so here goes...
Say I get several int values from the user, and I know for a fact that the 1st
value is the largest.
Any way I can use the length of the first int value as the padding when printing
the rest of the numbers?
Browsing the forum I found that you can determine the length of an int (n) like
this:
l = (int) log10(n) + 1;
Using padding of 5 a print would look like:
printf("%5d", n);
So what I want to know is whether or not there's a way to use l instead of 5...
Thank you!
Do it like so:
int n = <some value>;
int i = (int) log10(n) + 1;
printf("%*d", i, n);
The * serves as a placeholder for the width being passed as an argument to printf().
You can use also snprintf() to get the number of characters printed for a value thus:
char buff[100];
int width = snprintf (buff, sizeof(buff), "%d", number1);
But, however you get the desired width, you can just use the * format modifier:
printf( "%*d %*d\n", width, number1, width, number2);
That modifier uses the next argument to specify a dynamic width rather than one fixed in the format string.