#include <stdio.h>
int main(void)
{
int a,b,c;
printf("Enter values of a,b,c:");
scanf("%d %d %d",a,b,c);
printf("\nDescending order of the numbers entered:");
/*Test for Biggest Number*/
if((a>b)&&(a>c))
printf("%d",a);
else if((b>a)&&(b>c))
printf("%d",b);
else if((c>a)&&(c>b))
printf("%d",c);
/*Test for Second Biggest Number*/
if((a>b&&a<c)||(a<b&&a>c))
printf("%d",a);
else if((b>a&&b<c)||(b<a&&b>c))
printf("%d",b);
else if((c>a&&c<b)||(c<a&&c>b))
printf("%d",c);
/*Test for Smallest Number*/
if((a<b)&&(a<c))
printf("%d",a);
else if((b<a)&&(b<c))
printf("%d",b);
else if((c<a)&&(c<b))
printf("%d",c);
return 0;
}
this is a c program in which 3 numbers are entered and the program prints the in descending order. i compiled the program and the ran the program.after entering the three numbers the program would just crash. is there something wrong with my code or do i have to add something?
That's because you are not passing the address of your variables to scanf. Change
scanf("%d %d %d",a,b,c)
to
scanf("%d %d %d",&a,&b,&c)
Try to use:-
scanf("%d %d %d",&a,&b,&c)
instead of
scanf("%d %d %d",a,b,c)
as & refers to the address of your variables.
In C the parameters are passed by value so you need to pass the address (or pointer). When you pass the address (or pointer) then scanf knows where it has to put the value.
Related
#include<stdio.h>
int main(){
int num1=0;
int num2=0;
int sum=0;
printf("enter 2 numbers\n");
scanf("%d %d",&num1,&num2);
sum=num1+num2;
printf("%d",&sum);
return 0;
}
This is what i am trying but 23+23 is coming out to be 6422292 in this way.I cant find the error. Please help.
Do NOT put an "address of" operator (&) on this line:
printf("%d",&sum);
It should be
printf("%d", sum);
Hey actually the error is in the printf() function
The & is your telling to print the value stored in the sum variable
Make the following changes to your code
printf("%d", sum);
Hope you got fixed the error
#include<stdio.h>
int main() {
int a , b;
printf ("enter a\n");
scanf ("%d", &a);
printf ("enter b\n");
scanf ("%d", &b);
int sum = a + b;
printf ("the sum is : %d", sum);
return 0;
}
Hi I am studying C language by myself.
My question is, is there something I missed which need to know when working with scanf() that takes char value?
To practice do...while loop, I wrote some code like below but it did not work as I expected.
#include <stdio.h>
int main()
{
char y_or_n;
int x =1;
int y;
do
{
printf("ENTER A NUMBER\n");
scanf("%d", &y);
printf("THE NUMBER WILL BE ADDED TO x WHICH IS %d\n", x);
x = x+y;
printf("x TURNED INTO %d\n", x);
printf("KEEP DOING THIS?(y/n)\n");
scanf(" %s", &y_or_n);
printf("x is %d\n", x);
}
while(y_or_n =='y');
printf("GOOD BYE\n");
return 0;
}
For the first loop, it worked as I expected.
For example, when I entered 7, x turned into 8. But after scanf() was executed, value of x was changed into 0.
So from second loop, value of x changed temporarily into value of y and changed again into 0.
I guessed that there is something wrong with scanf() function and modified the code slightly: changed type of y_or_n into integer so that scanf() takes integer value.
The modified code is like below
include <stdio.h>
int main()
{
int y_or_n;
int x =1;
int y;
do
{
printf("ENTER A NUMBER\n");
scanf("%d", &y);
printf("THE NUMBER WILL BE ADDED TO x WHICH IS %d\n", x);
x = x+y;
printf("x TURNED INTO %d\n", x);
printf("KEEP DOING THIS?(y/n)\n");
scanf(" %d", &y_or_n);
printf("x is %d\n", x);
}
while(y_or_n ==1);
printf("GOOD BYE\n");
return 0;
}
This time the code worked as I expected.
Value of x was not changed into 0 even after an execution of scanf() and every time I entered a number that number was added to x.
If my question is not clear, please let me know.
Thank you for reading.
%s is for reading null-terminated strings, so passing pointer to one-byte buffer for that is bad.
It seems the variable x is placed just after the variable y_or_n on the memory and writing of terminating null-character by scanf() is setting value of x to 0.
To read one character, use %c instead.
char y_or_n;
/* ... */
scanf(" %c", &y_or_n);
Actually, I'm learning C langage and I have written a programm
Enter the value of 2 numbers as shown below.
#include<stdio.h>
int main()
{
int a, b, c;
printf("Enter two numbers to add\n");
scanf("%d%d", &a, &b);
printf("Sum of the numbers = %d\n", c);
return 0;
}
But if I enter an alphabet I'm getting some 1522222 numbers. Instead of
this I want it throws an error as invalid input if I type alphabet ie a,b,c.
How could I do that?
You can check the return value of scanf. If it is successful, it should return 2, since you're reading two values. If you get anything else, you know that the input is incorrect. Try this:
if (scanf("%d%d", &a, &b) != 2)
printf("Invalid input type!\n");
else
printf("Sum of the numbers = %d\n", a+b);
On a different note, you don't initialize c anywhere, so printing it is undefined behavior. You don't even need c for this, you can just print a+b instead.
So here is my program. It is suposed to write out square of some intiger.
#include <stdio.h>
int main (){
int a;
printf("Type an intiger.");
scanf("%i", &a);
printf("Square of that intiger is %i", a*a);
return 0;
}
When i run a program in Eclipse it first requires me to input a number.I put in 5. And then as output it gives me
Type an intiger.Square of that intiger is 25.
It should first print "Type an intiger" and then the rest. But it just combines two printf commands. What is the problem?
You need a newline character - printf("Type an intiger.\n");
In computing, a newline, also known as a line break or end-of-line
(EOL) marker, or simply break, is a special character or sequence of
characters signifying the end of a line of text.
Also format specifier for integer is %d
scanf("%d", &a);
printf("Square of that intiger is %d", a*a);
If you want it on separate lines you can always add '\n' to the string to get a new line.
#include <stdio.h>
int main (){
int a;
printf("Type an intiger.\n");
scanf("%i", &a);
printf("Square of that intiger is %i", a*a);
return 0;
}
There is 2 problem in it. First, if you input the integer, it should be %d. Example :
scanf("%d", &a);
The second, after the input, you should print \n. So, it will be like this printf("\n");. Take a look at my code :
#include <stdio.h>
int main (){
int a;
printf("Type an intiger.");
scanf("%d", &a);
printf("\nSquare of that intiger is %d", a*a);
return 0;
}
In code::blocks it compiles fine anyway put a \n at the end of the first printf and change %i with %d
After execution, result was very strange:
#include <stdio.h>
int main(){
int a,b;
printf("enter two numbers :");
scanf("%d%d",&a,&b);
if(a>b){
printf("maximum number is %d",&a);
}
else{
printf("maximum number is %d",&b);
}
return 0;
}
After enter two numbers in console result was:
maximum number is 2686696
2686696 very strange for me if I enter two numbers such as 5, 3 must shows me 5 but shows 2686696 !!!
Can anyone guide me?
You are trying to print the address of int not its value. Do this:
if(a>b){
printf("maximum number is %d",a);
}
else{
printf("maximum number is %d",b);
}
& operator returns the address of a or b.
Remove & from printf to print the value, now you're printing the address. Should be:
printf("maximum number is %d",a);
The & in printf prints the address of the variables instead of the value.
To print a value use :
if(a>b){
printf("maximum number is %d",a);
}
else{
printf("maximum number is %d",b);
}
Remove & operator from the argument of printf if you are interested in printing the numbers a and b else change the format specifier %d to %p if you are trying to print the address of a and b.
printf("maximum number is %p", (void *)&a);