printf in Eclipse - c

I have to work with eclipse in C. I wrote a simple program, but I have a problem with a printf command which doesn't work properly. Any idea?
Here is the code :
#include <stdio.h>
void change(double *x, double *y)
{
double help = *x;
*x = *y;
*y = help;
return;
}
int main()
{
double x=0, y=0;
printf("please give a value to a \n ");
scanf("%f",&x);
printf("please give a value to b \n");
scanf("%f",&y);
printf("x=%.2f\t y=%.2f\n",x,y);
printf("will give \n");
change(&x,&y);
printf("x=%.2f\t y=%.2f\n",x,y);
return 0;
}
So the problem is that I dont't get this first printf.

All your values are double for which you have to use %lf. Buut you are using %f which invokes undefined behaviour.
Change %f to %lf in your scanfs and prints.

Related

getDouble function in C

I have created a custom function to prompt the user with whatever you want and they need to input a double. The problem is, if I were to enter a number like 1.22 it would print '1' so it doesn't print anything behind the decimal, no rounding
int main(void) {
double f = getDouble("Enter a double: ");
printf("%d\n", f);
}
double getDouble(char* y){
double x;
printf("%s", y);
scanf("%d", &x);
return x;
}
In your code, you are using printf("%d\n", var); to print a double and you are also using scanf("%d", &x); to get a double. You should use "%lf" instead as the format specifier of in both printf in scanf. Your code obviously doesn't work because scanf is looking for an integer input and printf is trying to print and integer instead of a double.
int main(void) {
double f = getDouble("Enter a double: ");
printf("%lf\n", f);
}
double getDouble(char* y){
double x;
printf("%s", y);
scanf("%lf", &x);
return x;
}
This should work. You can find format specifiers for every basic type: https://en.wikipedia.org/wiki/C_data_types#Main_types
Use the correct format specifier for the correct type otherwise, the function will try to do things assuming the wrong datatype which will lead to problems.
For reading double you need to use "%lf", "%d" is for integer.
double getDouble(const char* y) {
double x;
printf("%s", y);
scanf_s("%lf", &x);
return x;
}
int main()
{
double f = getDouble("Enter a double: ");
printf("%lf\n", f);
return 0;
}
In scanf/printf use %lf instead of %d
#include <stdio.h>
double getDouble(char *y) {
double x;
printf("%s", y);
scanf("%lf", &x);
return x;
}
int main(void) {
double f = getDouble("Enter a double: ");
printf("%lf\n", f);
}
As others have already pointed out, you are simply using the wrong specifier in scanf. They can be confusing without practice.
Here's a reference.

Squared Equation calculator not working in C

Hello guys I started learning C few weeks ago and I am trying to make my first useful program, I am trying to create a squared equation calculator but no matter what I type in the input it always outputs "no solution2" can anyone take a look and help me ?
examples for input :
0 0 0=
1 4 1=
#include <stdio.h>
#include <math.h>
int main()
{
printf("enter a\n");
double a; scanf("%1f", &a);
printf("\nenter b\n");
double b; scanf("%1f", &b);
printf("\nenter c\n");
double c; scanf("%1f", &c);
if (a==0)
{
if (b==0)
{
if (c==0)
printf("x can be every number\n");
else
printf("no solution1\n");
}
else
{
printf("x equals %.2f\n", ((-1)*c) / b);
}
}
else
{
double delta = (b*b)-(4*(a*c));
if (delta>0)
{
double sqrtDlt = sqrt(delta);
printf("x1 = %4.2f\n", (((-1)*b) + sqrtDlt) / 2 * a);
printf("x2 = %4.2f\n", (((-1)*b) - sqrtDlt) / 2 * a);
}
else
printf("no solution2\n");
}
return 0;
}
For double use specifier %l(ell)f not %1(one)f in scanf.
Note that this is one place that printf format strings differ substantially from scanf (and fscanf, etc.) format strings. For output, you're passing a value, which will be promoted from float to double when passed as a variadic parameter. For input you're passing a pointer, which is not promoted, so you have to tell scanf whether you want to read a float or a double, so for scanf, %f means you want to read a float and %lf means you want to read a double.

My code produces -nan on changing data type from float to double

int main(void) {
float a;
scanf("%f", &a);
double c = sqrt(a);
printf("%f", c);
return 0;
}
A float variable results in correct output.
However, on changing the data type to double result is NaN.
You should use the format specifier %lf for taking in a double.
#include<stdio.h>
#include<math.h>
int main(void) {
double a;
scanf("%lf", &a);
double c = sqrt(a);
printf("%lf", c); //printf("%f", c) also works perfectly.
return 0;
}
Since you are possibly using just %f, you get NaN.

Unable to find any valid shell. Required for execution in an external terminal

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;
}

Why does my program keep returning the input prompt?

This code compiles perfectly, but when I run it, on the second ´scanf´ it will always return the prompt, like its expecting an infinite amount of values. I'm using Clang on Linux. I really need your help as I have an exam this Friday.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
int fatorial(int n){
int f=1, t;
for(t=n;t>1;t--){
f=f*t;
}
return f;
}
float sen(float x, float tol){
float res=0, aux=0;
int n=0;
for(n=1;res-aux!=tol||aux-res!=tol; n++){
res=aux;
res=res+(pow(-1,n+1))*((pow(x,2*n-1))/fatorial(2*n-1));
}
return res;
}
int main(){
float yo, tol, res;
printf("What's the value of x? ");
scanf(" %f", &yo);
printf("What's the tolerance? ");
scanf(" %f", &tol);
res=sen(yo, tol);
printf("The sin of %.2f is %f.\n", yo, res);
return 0;
}
Based on your linked screenshot, your program is not asking for more input. It's calculating an answer. In Linux, you can still type things into the terminal while a program is running, which is what you are doing.
You can use top or other CPU monitor to see that your process is using 100% CPU. The problem is that your algorithm in sen() is running an infinite loop and never reaching its target tolerance value.
try this
float sen(float x, float tol){
float res=0, aux=tol+1;
int n;
for(n=0;fabsf(res-aux)>tol; n++){
aux=res;
res=res+pow(-1, n)*pow(x, 2*n+1)/fatorial(2*n+1);
}
return res;
}

Resources