Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C
#include <stdio.h>

int main()
{
    int number1;
    float number2;

    printf("Enter Integer:");
    scanf("%d",&number1);

    printf("Enter Float:");
    scanf("\n%f",&number2);

    printf("The Resulting Integer Is: %d",&number1);
    printf("\nThe Resulting Float Is: %f",&number2);

    return 0;
}


What I have tried:

Please what is wrong with this code?
Posted
Updated 14-Dec-23 13:45pm
v2

C++
printf("The Resulting Integer Is: %d",&number1);
printf("\nThe Resulting Float Is: %f",&number2);
The problem is in these two lines of code. The function printf expects its arguments to be passed by value and you are passing their addresses. To correct this you can remove the ampersands so it looks like this :
C++
printf("The Resulting Integer Is: %d\n", number1);
printf("The Resulting Float Is: %f\n", number2);
Note that I moved the new line character. I prefer to always follow a line of text with the new line character so that subsequent lines start at column zero. This is a habit I got into a long, long time ago.

One other note - I prefer to use the function fgets[^] to obtain input from a console. It includes the new line character in the string and has less quirky behavior than scanf[^]. Then one can call atoi[^] and atof[^] to get their respective numerical values. For your purposes, it does not matter that the new line character is included because the conversion functions ignore it.
 
Share this answer
 
v3
Comments
KarstenK 15-Dec-23 3:18am    
Your habit is some "clean code" with leaving the app in the state to do the next job and NOT cleanup up some mess at first.
When you print them out using & gives the address of number1 and number 2, you don't want the address you want the value.
C++
#include <stdio.h>

int main()
{
	int number1;
	float number2;

	printf("Enter Integer:");
	scanf("%d", &number1);

	printf("Enter Float:");
	scanf("\n%f", &number2);

	printf("The Resulting Integer Is: %d", number1);
	printf("\nThe Resulting Float Is: %f", number2);

	return 0;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900