A function in printf 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 4 years ago.
Improve this question
How can I call a function that has printf in it - in printf BUT without it printing the value entered twice?
in the code written below is how it is now, it prints the inserted value.
can I just use the function without printing the value again?
can I use printf(roll(num))
#include<stdio.h>
int roll(int);
int main()
{
int num;
printf("%d",roll(num));
return 0;
}
int roll(int a)
{
printf("Enter a number between 1-6:\n");
scanf("%d",&a);
while(a<1 || a>6)
{
printf("Wrong input! please enter a number between 1-6:\n");
scanf("%d",&a);
}
return a;
}

Just call the function without printf.
roll(num);

Related

How to copy all chars(include '\0') from one array to another without using strcpy function? [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 3 years ago.
Improve this question
copy all characters from one character array to another without using strcpy function.
char s1[80],s2[80];
int i;
printf("input s2");
scanf("%s",&s2);
for(i=0;i<=strlen(s2);i++)
s1[i]=s2[i];
printf("s1:%s",s1);
Instead of scanf, using gets function for getting input with spaces.
#include<stdio.h>
int main()
{
char s1[80],s2[80];
int i;
printf("input s2");
// scanf("%c",&s2);
gets(s2);
printf("%d",strlen(s2));
for(i=0;i<strlen(s2);i++)
{
s1[i]=s2[i];
}
printf("s1:%s",s1);
}

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

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

Is it possible for an if statement to say if something is not entered? [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'm doing a project and I'm just curious to know if it is possible to have a line that says "if something is not entered" and a prompt statement would be followed.
For example,
if(id_ == NULL){printf("John Doe is absent.")}.
Just a curious question because I want to explore C programming a bit more.
You can do this with scanf (or similar functions: fscanf, sscanf...).
Assuming id_ is an int:
if(scanf("%d",&id_)!=1){
printf("John Doe is absent.");
}
These functions return the number of input items successfully matched and assigned.
see top voted answer here for more info.
#include <stdio.h>
void input_id(int **id){
int num;
printf("input id:");
if(scanf("%d", &num)==1)
**id = num;
else
*id = NULL;
}
int main(void){
int id;
int *id_ = &id;
input_id(&id_);
if(id_ == NULL){
printf("John Doe is absent.\n");
} else {
printf("id : %d\n", id);
}
return 0;
}

Program stopped working [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 8 years ago.
Improve this question
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<windows.h>
int main(){
int iCount=1;
int min=999999;
int max=0;
int iSum;
int iAVG=0;
int iValue;
system("color 97");
for(iCount=1;iCount<36;iCount++){
system("cls");
SetConsoleTitle("chairs");
printf("\t\tCHAIR VALUES\n\n");
printf("\nPlease enter the value of chair#: %d.\n>>", iCount);
scanf("%d",iValue);
iSum+=iValue;
if(iValue<min){
min==iValue;
}
if(iValue>max){
max==iValue;
}
printf("\n\nThe minimum and maximum values entered are:\nminimum value>>%d\nmaximum
value>>%d", min, max);
getche();
}
(iAVG=iSum/iCount);
printf("\n\nThe average value of the entered chairs is: %d", iAVG);
getche();
system("cls");
printf("\t\t\nGOODBYE USER!");
}
I wrote this code, a c question. I compiled it within codeblocks, it was successfully compiled and executed. However, when i entered the first chair value, it says that"chairs.exe has stopped working. Im here trying to see what might have lead to this problem. Any can give me a helping hand?
You missed to add '&' in scanf
scanf("%d",iValue);
Should be
scanf("%d",&iValue); //<--- Notice '&'
And as pointed out by "Zev Eisenberg" in comments.
min = iValue //<-- Make sure you are using assignment operator here.

Resources