+ 1

c program can i combine 2 different variables into 1 another variable?

for example int b; int c; int a=printf("%d%d",b,c); is it possible to do this for C programming?

5th Dec 2019, 7:07 AM
GG GG
GG GG - avatar
8 Answers
+ 5
here is a less confusing way char combine[100]; int a = 123; int b = 456; int c; sprintf(combine, "%d%d", a, b); sscanf(combine, "%d", &c); printf("%d", c); >>> 123456 try to learn about sscanf and sprintf, it'll come in handy for you a lot of times.
5th Dec 2019, 8:53 AM
Shen Bapiro
Shen Bapiro - avatar
+ 2
no, at least not like the way you mentioned in the question. tell us more specific that what kind of combining you want like:- int a=10,b= 50; so you want int c should store something combined value like c=1050; or like that so i think that is possible through some code.
5th Dec 2019, 7:18 AM
Pradeep Kumar Prajapati
Pradeep Kumar Prajapati - avatar
+ 2
yes!! this is possible. but remember you are dealing with integers so this process i think will do.. first: know the total size or we can say number of digits stored in "a" in this case a=10 so there are two digit value is stored is "a" second: c=a; //now c = 10 c=c*100; //now c =1000 //if there are three digit in a like a=100 so you need to do c=c*1000 c = c+b //now c=1000 +50 so we have c = 1050
5th Dec 2019, 7:31 AM
Pradeep Kumar Prajapati
Pradeep Kumar Prajapati - avatar
+ 2
the size of a can be also found using loop like- int size = 0; while(a != 0) { a=a/10; size++; } mess with all parts of the code this is the way to learn.🙌 and if you fail we will give you full code too.. learn from mistakes 👍👍 BEST OF LUCK..
5th Dec 2019, 7:37 AM
Pradeep Kumar Prajapati
Pradeep Kumar Prajapati - avatar
+ 2
hmmm.. well this look surely a better way 😂😂 ☝☝ here is the stupid code i was making... note: it will give wrong output if like a=11, b=022; then output c = 1122;😛😛 https://code.sololearn.com/ce70QxGJgp02/?ref=app
5th Dec 2019, 9:13 AM
Pradeep Kumar Prajapati
Pradeep Kumar Prajapati - avatar
+ 2
What is your definition of combine? Addition? Concatenation?
5th Dec 2019, 8:18 PM
Sonic
Sonic - avatar
0
well i want to combine like if a=10 b=50 , then c is the combination of a and b which is 1050 like you mention. is it really possible to do this? bcuz i found nothing about this. Thank you.
5th Dec 2019, 7:21 AM
GG GG
GG GG - avatar
0
alright. i will give it a try. Thank You Pradeep!
5th Dec 2019, 7:35 AM
GG GG
GG GG - avatar