Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Hi can anyone help me to correct this code, the result should be
/c=4.000000/
/d=4.0000 /
I know by putting the logic in single printf() i will get my result but i am not understanding that how to use two printf() and the varibles will be given by the second printf().
Here is my code:-
#include<stdio.h>
int main()
{
int a=19,b=4;
float c,d;
c=a/b;
d=a%b;
printf("/c=%12f/\nd=%");
printf("-12.4f/",c,d);
putchar(10);
return 0;
}
If I change my code to this, I will get the result,
#include<stdio.h>
int main()
{
int a=19,b=4;
float c,d;
c=a/b;
d=a%b;
/*
printf("/c=%12f/\nd=%");
printf("-12.4f/",c,d);
*/
printf("/c=%12f/\n/d=%-12.4f/",c,d);
putchar(10);
return 0;
}
but I want to use two printf() statements.
Thanks in advance.
You can not do this:
printf("/c=%12f/\nd=%");
printf("-12.4f/",c,d);
because you are lying to both printfs, in the first one you don't use the specifiers and in the second one you use specifiers that are not expected.
You can do this:
printf("/c=%12f/\nd=%"
"-12.4f/",c,d);
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
so I've written some codes to add two numbers in C, but I can't figure out how to add three instead without spilling the results into another variable or clobbering the caller's version of the variables?
int add(a,b,c)
int a, b, c;
{ int tempr;
for(;b--;++a); // danger
/*tempr = c+a;*/
tempr = a+c;
return (tempr);
}
Here's how, if you must use a function for some reason.
int add(int a,int b,int c) {
return a+b+c;
}
When you define a function in C, you have to define the type of the parameters as well.
Please note the following:
It is an error to define a variable in a function that has the same name as a function parameter
Although not an error, there's no need for parentheses when calling return (tempr);.
return tempr; is perfectly fine
if you use a for loop, ending it with ; immediately after the for statement will result in the following statement not being a part of the loop. This might not be what you had in mind.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am new to C. I was just interested in making a program which takes two integer input from the user and add it but the condition is that i have to use only one variable. I came up with this code:
#include <stdio.h>
int main()
{
int *a;
scanf("%d%d",a,(a+1));
printf("\nSum=%d",*a+*(a+1));
return 0;
}
scanf() function takes an valid address as an argument and i am passing the value in a(which is not initialised yet). So, how this code worked in Turbo C++?
You are trying to access an area that is not within the scope of the program. Luckily TCC gave it, but I believe if you go on experimenting, results will be undefined.
You can do something like this to solve your problem of adding using 1 variable.
int main()
{
int a;
scanf("%d",&a); //scan the first number
getchar();
a += getchar()-'0'; // get the second number (restricted to 0-9)
printf("%d",a);
return 0;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Before I start please keep in mind that I am still learning and need help with some stuff that may be easy to you but not to me. So here we go. I am having trouble using scanf in a custom function. It will not let me type anything. It just keeps running forever unless I stop it. How can I get scanf to work here:
#include <stdio.h>
#include <stdlib.h>
void function ();
int main (void)
{
char sel;
function ();
return 0;
}
void function ()
{
scanf("%c",&sel);
}
There is no problem with scanf. The problem is with your loop. l = 1 and l++ will always keep l >= 0 and hence it will keep on taking input infinite times.
Also, it will keep on overriding the value of var.a with every input
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I was trying to explain to my friend something about C coding and he asked me why his code (with "scanf") didn't work.
#include
int main() {
char x=scanf("%c",&x);
printf("%c\n",x);
return 0;
}
and this one yes
#include <stdio.h>
int main()
{
int k;
char x=getchar
printf("%c\n",x);
return 0;
}
When scanf completes, x contains the character that was read. However, that value is immediately overwritten when x is assigned the return value of scanf, which is the number of items successfully matched or EOF in the event of an error.
If you call scanf without assigning the return value to x you should get the expected result.
For example, this should work.
char x;
scanf("%c",&x);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Write a C program that reads lines containing floating point values of type double one per line from the standard input ( e.g. using scanf ), converts those values to integers and then prints those values as right justified integers in a 20-character wide field one per line on the standard output.
#include <stdio.h>
My biggest problem is I don't know where to start. Any tips and help would be appreciated.
The concept is to TYPECAST the float to an integer.
The loop here is for multiple values if you want.
This is the program. I hope this helps; it runs as you want.
#include <stdio.h>
int main()
{
float n;
int t;
//loop here
scanf("%f", &n);
t = (int)n;
printf("%20d", t);
// end loop here
return 0;
}