how to use scanf in a custom function in C [closed] - c

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

Related

Trying to understand the error in my code [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I'm creating a code where someone enters the amount of people eating a cut from and from there I can figure out how many pieces I can have in one pizza.I'm having trouble and don't know how to fix my error.
#include <stdio.h>
int Cuts(int n)
{
int max = n*2;
return m;
}
int main()
{
int m;
m = Cuts();
printf("P1:%d\n" , m);
}
Your CutYourPizza function is written to require one integer argument (called n), but when you invoked that function on the line max = CutYourPizza(); you did not supply any argument.
For example, if you wanted to supply the number 10 as an argument, then you could have written max = CutYourPizza(10); with the argument 10 inside the parentheses.

How to break Printf() in multiple Printf() [closed]

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);

How pointer variable works, without initialising it in the code in TurboC++? [closed]

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;
}

Is there any chance to store many variables without array? [closed]

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 wanna get some numbers from keyboard. But how to store that number without array[] ? Have i chance to do that ? I don't know exact how many numbers come from keyboard. If i had permission of array, its simple. But array is not allowed.
In your situation, I'd still go with arrays, but if you insist on using pointers, this code below will help you. Regardless of whether you need arrays or pointers, you still need to define an upper limit on how many elements can be stored in memory.
It is now up to you to modify the code to make it efficient and pretty to your assignment needs.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
int numelements=10;
int curelement=0;
int* data=calloc(1,numelements*sizeof(int));
int* p=data;
int* res=data;
while (curelement < numelements){
scanf("%d",p);
if (*p==0){break;} //exit if number entered is zero.
p++;
curelement++;
}
//print results
while(*res != 0){
printf("%d ",*res);
res++;
}
free(data);
return 0;
}

Differences between scanf and getchar in C [closed]

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);

Resources