Can somebody tell me why I have this runtime error? - c

I have created a program on Pelles C, however, when I run it, it is skipping straight to the end of the function simply saying "press any key to continue"
#include <stdio.h>
int main()
{
char letter;
int num1, num2;
printf("Enter any one keyboard character ");
scanf("%c", &letter);
printf("Enter 2 integers seperated by a space ");
scanf("%d %d", &num1, &num2);
printf("Numbers inputted were %d and %d \n" num1, num2);
printf("letter input %c", letter);
printf(" Stored at: %p \n", &letter);
return 0;
}
Can anybody tell me why this is happening ?

printf("Numbers inputted were %d and %d \n" num1, num2);
^
You missed a , before num1 in above printf statement.
printf("Numbers inputted were %d and %d \n",num1, num2);

Related

Can anyone help me with my C code in this Visual Studio scanf error C6031 [duplicate]

This question already has answers here:
How to fix "return value ignored: 'scanf'" code C6031 in visual studio
(3 answers)
Closed 2 years ago.
#include <stdio.h>
int main(void)
{
int num1, num2, num3;
printf("Enter First Number");
scanf("%d", &num1);
printf("Enter First Number");
scanf("%d", &num2);
printf("Enter First Number");
scanf("%d", &num3);
printf("The numbers in reverse order : %d %d d% ", num1, num2, num3);
return 0;
}
#include <stdio.h>
int main(void)
{
int num1, num2, num3;
printf("Enter First Number");
scanf("%d", &num1);
printf("Enter second Number");
scanf("%d", &num2);
printf("Enter third Number");
scanf("%d", &num3);
printf("The numbers in reverse order : %d %d %d ", num3, num2, num1 );
return 0;
}

Why the output is coming like this?

When I tried to do division of 6/3 the output comes like this 2 / -1431650288. What's wrong in code?
My program in c is like this:
#include <stdio.h>
int main(){
char Operator;
int num1, num2;
printf("Enter the operator in which you want to perform calculation(+, -, *, /)\n");
scanf(" %c", &Operator);
if (Operator == '/'){
printf("Enter two numbers: ");
scanf(" %d %d", &num1, &num2);
if (num2==0){
printf("\a Denominator must be greater than 0.\n");
}
else{
printf(" %d / %d", num1/num2);
}
}
else{
printf("Enter two integer numbers: ");
scanf(" %d %d", &num1, &num2);
if(Operator =='+'){
printf(" %d + %d = %d", num1, num2, num1+num2);
}
else if(Operator == '-'){
printf(" %d - %d = %d", num1, num2, num1-num2);
}
else if(Operator == '*'){
printf(" %d * %d = %d", num1, num2, num1*num2);
}
else{
printf("\t \a Invalid Operator.\n");
}
}
}
This line:
printf(" %d / %d", num1/num2);
The first '%d' is the result of num1/num2 and that's enough. The second %d and the '/' character should not be here. Change it to:
printf(" %d ", num1/num2);
Additionaly, for your purpose, the switch case structure is more suitable for code readability (and better optimization too I think)

Why scanf after the if block do not work it just displays "press 1 to enter again:" and do not take input again

scanf statement after if block is not working can somebody help me please
#include<stdio.h>
main()
{
int input,itemno,input2;
int name1,price1,name2,price2,name3,price3;
printf("enter input:\n");
scanf("%d", &input);
if(input==1)
{
printf("enter number of items:\n");
scanf("%d",&itemno);
if(itemno<=3)
{
printf("enter name and age:\n");
scanf("%d %d\n %d %d\n %d %d\n",&name1,&price1,&name2,&price2,&name3,&price3);
}
else
printf("you can only enter 3 students");
}
printf("press 1 to enter again: \n");
scanf("%d",&input2);
if(input2==1)
{
printf("hey");
}
}
scanf("%d %d\n %d %d\n %d %d\n",&name1,&price1,&name2,&price2,&name3,&price3);
Don't use escape characters in scanf function call.
just delete the last \n in your scanf
the result is :
result

C: scanf and printf error during second prompt input [duplicate]

This question already has answers here:
Problems with character input using scanf()
(4 answers)
Closed 7 years ago.
The first half of this code runs with no issues. I can enter in the second set of char float int char input but not sure why my code displays zeros in display prompt.
/*Prompt the user and accept the following 4 types of values from a single
input line: char int char float */
char ch1, ch2;
int num1;
float num2;
printf("Enter char int char float: ");
scanf("%c %d %c %f",&ch1, &num1, &ch2, &num2) ;
printf("You entered: '%c' %d '%c' %.3f\n", ch1, num1, ch2, num2);
/*Prompt the user and accept the following types of values from a
single input line: char float int char */
char ch3, ch4;
float num3;
int num4;
printf("Enter char float int char: ");
scanf("%c %f %d %c",&ch3, &num3, &num4, &ch4);
printf("You entered: '%c' %.3f %d '%c'\n", ch3, num3, num4, ch4);
The space is the problem.
The first enter ENTER you stroke is considered as input for the second scan. Use a space like the following:
scanf("[SPACEHERE]%c %f %d %c",&ch3, &num3, &num4, &ch4);
Here for more details
You can use getchar(). getchar() takes an unknown char form stdin .
use after first prinf.
printf("You entered: '%c' %d '%c' %.3f\n", ch1, num1, ch2, num2);
getchar();//add this

Switch small program does not let me see result

I am learning the switch statement of C. This is my small program and it runs and does the calculation but doesn't let me see the result of the operation. The black window shows up so that I input the numbers and the operator and then for a fraction of a second shows the result and disappears. Any help is appreciated.
#include <stdio.h>
int main(int argc, char *argv[])
{
int num1, num2, ans=0;
char ch, name;
printf("Enter a value: ");
scanf("%d", &num1);
printf("Enter a second value: ");
scanf("%d", &num2);
printf("Input * To multiply\
+ To add\
- To subtract: ");
scanf(" %c", &ch);
switch(ch)
{
case'*':
ans=num1 * num2;
printf("%d times %i equals: %i",num1,num2,ans);
break;
case'+':
ans=num1+num2;
printf("%i plus %i equals: %d",num1,num2,ans);
break;
case'-':
ans=num1-num2;
printf("%d minus %d equals: %d",num1,num2,ans);
break;
default:
printf("Range numbers");
}
getchar();
return ch;
}
Probably due to output buffering. Add newlines (\n) last in your formatting strings.
As a newbie, you should end all your printf format string with an escaped newline \n, i.e. printf("%i plus %i equals %d\n", num1, num2, ans); (or you should call fflush(stdout); just after the end of the switch before the getch and before all your scanf).

Resources