I've just started to learn C programming last week and I've learnt about some basics about it. So now I'm trying to make a program which can add up two numbers and show the result.
Here's my code:
#include <stdio.h>
int main (void)
{
int a;
int b;
int result;
printf("Insert a number:%d\n");
scanf ("%d",&a);
printf ("Insert the next number:%d\n");
scanf ("%d",&b);
result = a + b;
printf ("Result is:%d\n",result);
return 0;
}
It can be compiled and run but the following result is shown.
[1]http://i.stack.imgur.com/4Xjdv.png
Can someone please help me to get rid of that 4200612, which is output at the first printf statement? Thanks for your help and sorry for my bad english.
There is no need of %d in first two printf statement.
printf("Insert a number: ");
scanf ("%d",&a);
printf ("Insert the next number: ");
scanf ("%d",&b);
Since there is no corresponding argument. It will print some random value.
Try getting rid of the extra %d's in your printfs.
#include <stdio.h>
int main (void)
{
int a;
int b;
int result;
printf("Insert a number:\n");
scanf ("%d",&a);
printf ("Insert the next number:\n");
scanf ("%d",&b);
result = a + b;
printf ("Result is:%d\n",result);
return 0;
}
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;
}
I am a beginner in C and would like to know what's the problem about my code here :
#include "stdio.h"
int main(void)
{
int a;
printf("Please input an integer value: ");
scanf("%d", &a);
printf("You entered: %d\n", a);
return 0;
}
My problem is that I have to type a value before having any consol output, for example if I type 7, I get this console output : Please input an integer value: You entered: 7
I tried the exact same code in another computer and it worked pretty well, I guess it's a buffer problem ? but I have no idea how to fix it.. Any ideas please ?
As other already mentioned, to guarantee that that line is going to be printed at that point in your code you can flush the standard output like this,
#include "stdio.h"
int main(void)
{
int a;
printf("Please input an integer value: ");
fflush(stdout);
scanf("%d", &a);
printf("You entered: %d\n", a);
return 0;
}
you can read this for more details, Why does printf not flush after the call unless a newline is in the format string?
updated thanks to #Osiris comments
I am trying to make a simple calculator in c as i want to test my programming skills. I keep getting a error though.
#include <stdio.h>
int calc()
{
int *fnum;
int *snum;
printf("Enter your First Number: ");
scanf("%d", fnum);
printf("Enter your Second Number: ");
scanf("%d", snum);
int answer = *fnum + *snum;
printf("%d", answer);
return 0;
}
int main()
{
int *calcType;
printf("Type of Calculation: 1=A, 2=S, 3=M, 4=D: ");
scanf("%d", calcType);
if (*calcType == 1)
{
calc();
}
return 0;
}
But Then i get this error:
Segmentation fault (core dumped)
Please help, i have no idea what this means.
You called scanf to read an integer into the memory pointed to by calcType, but you never set calcType to point to a valid address.
Where the snum and fnum (int * pointers) point to? you have to declare a variable and pass its address (by reference operator &) to scanf.
The code should be something like this
int calc(){
int fnum;
int snum;
printf("Enter your First Number: ");
scanf("%d", &fnum);
printf("Enter your Second Number: ");
scanf("%d", &snum);
int answer = fnum + snum;
printf("%d", answer);
return 0;
}
Also same problem with calcType pointer.
You should be inputing ints, not int* (int pointers):
int fnum; /* This is now an int */
int snum; /* so is this */
printf("Enter your First Number: ");
scanf("%d", &fnum); /* Note that fnum's address is passed */
printf("Enter your Second Number: ");
scanf("%d", &snum); /* Same for snum */
int answer = fnum + snum;
printf("%d", answer);
return 0;
That is a generic error message. You are incorrectly using pointers for fnum and snum, and attempting to add their addresses in memory.
You missed
&
Without it the programm wont know where to storage the data
scanf("%d", &fnum);
I also dont understand why you put
*
so I'm currently running my C program in NetBeans IDE 8.1, but as soon as I tried using the scanf function, I began running into issues. I have MinGW download and have added C:\MinGW\bin; to my path environment variable. I looked up and found that I should be running external terminal to use scanf but I am receiving this error. Does anyone have any idea how to fix this. I'm pretty new to C and this IDE so simpler instructions would be appreciated.
Here is the code:
#include <stdio.h>
int main()
{
int int1, sum, int2;
printf("Enter\n");
scanf("%d", int1);
printf("Enter\n");
scanf("%d", int2);
sum = int1 + int2;
printf("sum is %d", sum);
return 0;
}
You need to pass an int *, not an int into scanf. This is because scanf must fill in each argument in the variable argument list. Your code should be
int main()
{
int a, b;
printf("Enter first number\n");
scanf("%d", &a);
printf("Enter second number\n");
scanf("%d", &b);
printf("sum is %d\n", a + b);
return 0;
}
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