Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am completely new to C, and was writing a small very simple program. The problem is that the scanf() is taking wrong input:-
#include <stdio.h>
int main(int args, char*argv[])
{
int num1 = scanf("%d",&num1) ;
int num2 =scanf("%d", &num2) ;
printf("Num1 = %d\n", num1) ;
printf("Num2 = %d\n", num2) ;
return 0 ;
}
When I give 34 and 23 as input it is outputting:-
Num1 = 1
Num2 = 1
Why is it so?
Don't assign the return value of scanf to num1 and num2. The return value of scanf indicates if the scan is successful, not what you thought it was.
int num1, num2;
scanf("%d", &num1);
scanf("%d", &num2);
This is because scanf returns the number of characters it matches. scanf("%d",&num1) and scanf("%d",&num2) will return 1. You are assigning that number, i.e, 1 to num1 and num2.
Now do it as follows:
#include <stdio.h>
int main(int args, char*argv[])
{
int num1;
int num2;
scanf("%d",&num1) ;
scanf("%d", &num2) ;
printf("Num1 = %d\n", num1) ;
printf("Num2 = %d\n", num2) ;
return 0 ;
}
Suggested reading: comp.lang.c FAQ list ยท Question 3.8.
You're assigning the return value of scanf into your values. scanf returns how many characters it matched; you're already passing a reference to the variable you want scanf to store the result in.
Why are you assigning the return value of scanf into your num1 and num2?
My suggestion:
#include <stdio.h>
int main(int args, char*argv[])
{
int num1, num2;
scanf("%d",&num1);
scanf("%d", &num2);
printf("Num1 = %d\n", num1);
printf("Num2 = %d\n", num2);
return 0;
}
Related
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
The following is an attempt to print a number of fibonacci sequence numbers, determined by the user. Uses a user-define function, fibonacci(int a). It is printing the wrong output, not a recursive sequence, but a continually doubling sequence. How can the code be fixed so that it works correctly?
#include <stdio.h>
int fibonacci(int a);
void main()
{
int number, range;
printf("Enter the number of Fibonacci numbers: ");
scanf("%d", &range);
number = fibonacci(range);
printf("%d\n", number);
}
int fibonacci(int a)
{
int num1 = 1;
int num2 = 1;
int position;
if (a == 1)
{
printf("%d", num1);
}
if (a == 2)
{
printf("%d\n", num1);
printf("%d", num2);
}
if (a > 2)
{
for(position = 1; a >= position; position++)
{
printf("%d\n", num1);
num1 = num2;
num2 = num1 + num2;
}
}
}
This prints the following output for all numbers:
1
1
2
4
8
16
...
The desired output is the fibonnaci sequence (each number is the sum of the two previous ones):
1
1
2
3
5
8
13
...
The problem is that within the loop you first overwrite num1 by assigning num2 to it:
num1 = num2;
Then you make num2 be the sum of itself and num1, but you have just made num1 equal to num2, so this line is effectively just multiplying num2 by two (i.e., adding it to itself):
num2 = num1 + num2;
You need to preserve the old value and use that for the sum, e.g., by adding a third, temporary, variable.
(Apart from this, you also have various other problems, but all of them are such that they should produce compiler warnings. If they don't, enable all warnings and/or get a better compiler. Once you get warnings, don't ignore them but research the cause for each of them and fix all warnings before you even run your code.)
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 5 years ago.
Improve this question
So everything works in the program so far how the teacher wants it too. However, when I enter 0 for num2 it executes the printf like I want it too, but it also executes the printf at the end of the main. Is there anyway to stop it from also executing the one at end int main when I enter 0? I apologize if my code sucks. This is my first year(semester actually) coding and the teacher doesn't really teach us and I've been learning from the book
#include <stdio.h>
//Program with 3 functions 1) Divides 2) Multiplies 3) Calls the other functions
float multiply(float num1, float num2);
float divide(float num1, float num2);
void math_call(float num1,float num2);
int main(void){
float num1;
float num2;
//printf/scanf allows you to choose values for the functions
printf("Please enter 2 numbers:\n");
scanf("%f", &num1);
scanf("%f", &num2);
//printf below calls functions so it can print out your results
printf("Division = %f Multiplication = %f", divide(num1, num2), multiply(num1, num2));
return(0);
}
//Calls function 1 and 2
void math_call(float num1,float num2){
divide(num1,num2);
multiply(num1, num2);
}
//Divides the arguments and doesn't allow to divide by zero
float divide(float num1, float num2){
if(num2 != 0){
float divide;
divide = num1/num2;
return divide;
}
else{
printf("You can't divide by zero. So your answer for division is undefined.\n");
return(0);
}
}
//multiplies the arguments
float multiply(float num1, float num2){
if(num2 != 0){
float multiply;
multiply = num1*num2;
return multiply;
}
else{
printf("If you multiply by zero you will always get zero.\n");
return(0);}
}
This is the result
Please enter 2 numbers:
4
0
If you multiply by zero you will always get zero.
You can't divide by zero. So your answer for division is undefined.
Division = 0.000000 Multiplication = 0.000000
RUN SUCCESSFUL (total time: 1s)
Instead of this in your code
//printf below calls functions so it can print out your results
printf("Division = %f Multiplication = %f", divide(num1, num2), multiply(num1, num2));
Rewrite it as below
// call your functions here and fill variables with results
float divResult = divide(num1, num2);
float mulResult = multiply(num1, num2);
// check if num2 is not zero
if(num2 != 0)
{
// if it isn't it will print your message
printf("Division = %f Multiplication = %f", divResult, mulResult);
}
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 5 years ago.
Improve this question
Hi guys i have been check for my code many many times but i still confuse where is this error come from....
This error appear while build solution...
#include<stdio.h>
#include<conio.h>
int add(int a, int b);
int main()
{
int num1, num2, ans;
printf("Please Enter the two numbers :%d %d", num1, num2);
scanf_s("%d %d", &num1, &num2);
ans = add(num1, num2);
return 0;
}
int add(int a, int b)
{
int sum = a + b;
printf("\nSummition is = %d", sum);
}
Unexpected end-of-file found error
int main() {
int num1, num2, ans;
printf("Enter the two numbers : ");
scanf("%d %d", &num1, &num2);
ans= sum(num1, num2);
printf("\nAddition of two number is : ");
return (0);
}
int sum(int num1, int num2) {
int num3;
num3 = num1 + num2;
return (num3);
}
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 7 years ago.
Improve this question
I am writing an easy code that takes two numbers I enter and tells me which is the Max value and which is the Min value using 2 separate files for functions. here is my code so far. It scans the number and has the correct Max Min but when I go to print the values a crazy number prints.
#include<stdio.h>
int main(void)
{
double num2, max, min, num1;
printf("enter any space-separated pair of decimal numeric values on the same line: ");
scanf("%le%le", &num1, &num2);
max = ComputeMaximum(num1, num2);
min = ComputeMinimum(num1, num2);
printf("%le", max);
printf("%le", min);
return 0;
}
this is my main program, on seperate files in the same project I have:
double ComputeMinimum(double num1, double num2) {
double result;
result = (num1 < num2) ? num1 : num2;
return result;
}
And
double ComputeMaximum(double num1, double num2) {
double result;
result = (num1 > num2) ? num1 : num2;
return result;
}
I would like to have it print to screen:
ComputeMinimum(?, ?) returned ?
ComputeMaximum(?, ?) returned ? (question marks are the values.)
Code is missing prototypes.
That is all, format is OK, functions are OK.
#include<stdio.h>
// Add these to the same file as main()
// Or better yet, add to another file Compute.h and #include "Compute.h"
// here and in the the separate C file
double ComputeMinimum(double num1, double num2);
double ComputeMaximum(double num1, double num2);
int main(void)
...
// suggest adding check
if (2 != scanf("%le%le", &num1, &num2)) {
puts("Input error");
return -1;
}
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 3 years ago.
Improve this question
Write a program that prompts the user to enter in two long values. Implements a function called negative_count() that takes two arguments of data type long and returns an integer that is the number of arguments that were negative. The function main() then displays the result.
For example:
Enter two integers of data type "long": -1264364007 -2012334695
Number of negative number entered 2
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
long num1,num2;
void Negative_Count(long int,long int);
int main(void) {
printf("Enter two number: ");
scanf("%ld%ld",&num1,num2);
("you entered %ld%ld",&num1,&num2);
Negative_Count(num1,num2);
return (0);
}
void Negative_Count(long int num1,long int num2)
{
if (num1,num2<0)
printf("%ld%ld is negative.",&num1,&num2);
else if (num1,num2>0)
printf("%ld%ld is positive.",&num1,&num2);
else if (num1>0, num2<0)
printf("%ld is negative",&num2);
else if (num1<0, num2>0)
printf("%ld is negative",&num1);
}
Can I use a loop with two variables?
scanf("%ld%ld",&num1,num2);
should be
scanf("%ld%ld", &num1, &num2);
("you entered %ld%ld",&num1,&num2);
should be
printf("you entered %ld %ld", num1, num2);
if (num1,num2<0)
printf("%ld%ld is negative.",&num1,&num2);
else if (num1,num2>0)
printf("%ld%ld is positive.",&num1,&num2);
else if (num1>0, num2<0)
printf("%ld is negative",&num2);
else if (num1<0, num2>0)
printf("%ld is negative",&num1);
should be
if (num1 < 0 && num2 < 0)
printf("%ld and %ld are negative.", num1, num2);
else if (num1 > 0 && num2 > 0)
printf("%ld and %ld are positive.", num1, num2);
else if (num1 > 0 && num2 < 0)
printf("%ld is negative", num2);
else if (num1 < 0 && num2 > 0)
printf("%ld is negative", num1);
Use the and operator (&&): If both the operands are non-zero, then condition becomes true.
And note that printf (as opposed to scanf) doesn't need the address of the variable, just pass the value.
Your scanf and printf formatting had issues. Also the conditional check in the if statements were also wrong.
You can modify the code as below for proper working...
#include <stdio.h>
#include <stdlib.h>
void Negative_Count(long int,long int);
int main(void) {
long num1,num2;
printf("Enter two number: ");
scanf("%ld%ld",&num1,&num2);
printf("you entered %ld%ld \n",num1,num2);
Negative_Count(num1,num2);
return 0;
}
void Negative_Count(long int num1,long int num2)
{
if (num1<0 && num2<0) {
printf("%ld , %ld are negative.",num1,num2);
}
else if (num1>0 && num2>0) {
printf("%ld , %ld are positive.",num1,num2);
}
else if (num1>0 && num2<0){
printf("%ld is negative",num2);
}
else if (num1<0 && num2>0){
printf("%ld is negative",num1);
}
}