I'm fairly new to C and I have a small function which reads an input of a simple math operation (+,-,*,/) and then calculates the result accordingly and returns -nan if the input is incorrect.
float simple_math(void) {
float a, b;
int char_c;
int ret_a;
ret_a = scanf("%f %c %f", &a, &char_c, &b);
float result;
if (char_c == '+')
result = a + b;
else if (char_c == '-')
result = a - b;
else if (char_c == '*')
result = a * b;
else if (char_c == '/')
result = a / b;
else
result = 0.0 / 0.0;
return result;
}
This code works just fine. However, if I change the order of the first two lines the return value is -nan.
int char_c;
float a, b; // this was originally the first line
int ret_a;
Why does the order of the variable declarations matter?
int char_c;
should be
char char_c;
%c is used to scan character and not int so your scanf will lead to undefined behavior.
The side effect of undefined behavior is sometimes things work as expected. So please get rid of undefined behavior it has nothing to do with the ordering of variable definitions.
This is your problem:
int char_c;
ret_a = scanf("%f %c %f", &a, &char_c, &b);
You tell scanf here to read a char, but pass him an int instead. That causes scanf to read only the size of a char (1byte) instead of the size of an int (larger than bytes), so in the end, whats contained in char_c is random and definitiveley non of the four allowed characters (x-*/).
Related
I have compiled this code and it works just fine up to value 4 then it starts returning character instead of integer. I am talking about first equation => x= num*2; Here when I enter num value as 5 the output returns a.
#include <stdio.h>
int main(void)
{
int num;
int x; This right here is an integer still it returns a character
char s[10] = "helloworld";
char f[10];
scanf("%d", &num); //
//printf("%d\n", num);
x = num * 2 ;
printf("%x\n", x);
scanf("%c", &f[10]);
if(s[10] = f[10]){
printf("helloworld");
}
}
please tell me if there is a mistake I am a newbie to coding.
As I see you are learning C language, and after reading your explanation, I feel that you want to print the integer value of variable x.
Kindly replace %x with %d in the print statement of variable x,
and you will be successfully able to print the value.
#include <stdio.h>
int main(void)
{
int num;
int x; // This right here is an integer still it returns a character
char s[10] = "helloworld";
char f[10];
scanf("%d", &num);
x = num * 2 ;
printf("%d\n", x); // %d for integer and %x for hexadecimal values
scanf("%c", &f[10]);
if(s[10] = f[10]){
printf("helloworld");
}
return 0;
}
Finally, do read more about format specifiers in scanf and
printf statements.
I'm working on my intro C course assignment and I'm tasked with the following...
Write code for a function that receives two parameters (a,and b) by value and has two more parameters (c and d) by reference. All parameters are double.
From main, use scanf to get two numbers, then call the function, and then display both returned values to the output in a printf statement.
The function works by assigning (a/b) to c and assigning (a*b) to d.
While my knowledge is basic, I believe I understand the gists
In main
//first and second double hold the scanf inputs
double first;
double second;
//unsure here - to reference c and d as parameters in the function, do I simply declare unfilled double variables here?
double *c;
double *d;
printf("Enter your first number\n");
scanf("%f\n", &first);
printf("Enter your second number\n");
scanf("%d\n", &second);
//call the function, first and second by value, &c / &d by reference - correct?
pointerIntro(first, second, &c, &d);
For the function...
float myFunction(double a, double b, double *c, double *d)
{
c = a/b;
d = a*b;
//printf statements
}
I apologize if the flow of this question is messy but its part of the process for me :P
So, for my formal questions
1. is it correct to initiate two double pointer variables (*c & *d) in main to be passed as reference in the function?
2. Am I right to call the function with the reference pointers by saying &c / &d?
3. Any other critiques of this questioning?
Variables 'c' and 'd' don't have to be pointers to pass them by reference. So you have two cases:
When you define 'c' and 'd' as pointers in main function you will pass them to function like this: pointerIntro(first, second, c, d); because they are already pointers and you don't need to send their reference, you just send them.
If you define 'c' and 'd' just as double variables double c, d; you will send them to the function by reference using '&' symbol like this: pointerIntro(first, second, &c, &d);.
Then in your function to actually set the values of 'c' and 'd' you will need to dereference the pointer to them like this: *c = a/b; *d = a*b;.
If you are not familiar you can check what dereferencing the pointer means here: What does "dereferencing" a pointer mean?
Code that should work:
#include <stdio.h>
void myFunction(double a, double b, double *c, double *d)
{
*c = a / b;
*d = a * b;
}
int main(void)
{
double a, b, c, d;
scanf("%lf", &a);
scanf("%lf", &b);
myFunction(a, b, &c, &d);
printf("%lf %lf", c, d);
}
You need to dereference c and d to assign them in myFunction():
*c = a/b;
*d = a*b;
Moreover from main, you need to create instances to which c and d refer to:
double c;
double d;
(you had created unitialised pointers with no instances).
Then in the the call:
pointerIntro(first, second, &c, &d);
The & address-of operator creates the reference arguments, referring to c and d in main().
It might be useful not to use the same symbol names c and d in main() and myFunction() to make it clearer what they are. For example:
void myFunction(double a, double b, double* cptr, double* dptr )
{
*cptr = a/b;
*dptr = a*b;
}
Note also, a function that returns nothing should be declared void.
A pointer is simply a normal variable that holds the address of something else as its value. In other words, a pointer points to the address where something else can be found. Where you normally think of a variable holding an immediate values, such as int a = 40;, a pointer (e.g. int *p = &a;) would simply hold the address where 40 is stored in memory.
If you need to access the value stored at the memory address pointed to by p, you dereference p using the unary '*' operator, e.g. int j = *p; will initialize j = 40).
If you want to obtain a variables address in memory, you use the unary '&' (address of) operator. If you need to pass a variable as a pointer, you simply provide the address of the variable as a parameter.
In your case that boils down to writing your function similar to:
void multdiv (double a, double b, double *c, double *d)
{
*c = a / b;
*d = a * b;
}
(note: you should ensure b != 0 before dividing a / b -- that is left to you)
In order to provide storage for the values you will take as input as well as the values you want to hold the results of the multiplication and division, you need four double values, e.g.
int main (void) {
double a, b, c, d; /* your doubles */
You then need to prompt the user for input and validate the user input by checking the return of the input function used, e.g.
fputs ("enter two double values: ", stdout); /* prompt */
fflush (stdout); /* flush stream (optional but recommended) */
if (scanf ("%lf %lf", &a, &b) != 2) { /* validate EVERY input */
fputs ("error: invalid double values.\n", stderr);
return 1;
}
(bonus: Why is the '&' used before a and b with scanf above? See first full paragraph under DESCRIPTION in man 3 scanf)
All that remains is to call your function to perform the calculations and output the results, e.g.:
multdiv (a, b, &c, &d); /* calculate c, d */
printf ("\nc = a / b => %.2f\nd = a * b => %.2f\n", c, d);
(note: as explained above the address of operator is used to pass the address of c and d as parameters)
Putting it altogether you could do:
#include <stdio.h>
void multdiv (double a, double b, double *c, double *d)
{
*c = a / b;
*d = a * b;
}
int main (void) {
double a, b, c, d; /* your doubles */
fputs ("enter two double values: ", stdout); /* prompt */
fflush (stdout); /* flush stream (optional but recommended) */
if (scanf ("%lf %lf", &a, &b) != 2) { /* validate EVERY input */
fputs ("error: invalid double values.\n", stderr);
return 1;
}
multdiv (a, b, &c, &d); /* calculate c, d */
printf ("\nc = a / b => %.2f\nd = a * b => %.2f\n", c, d);
return 0;
}
Example Use/Output
$ ./bin/multdivbyptrs
enter two double values: 4.0 2.0
c = a / b => 2.00
d = a * b => 8.00
Look things over and let me know if you have questions. Once you digest that a pointer is simply a normal variable that holds the address where something else is stored as its value -- things will start to fall into place. No magic..
It is perfectly valid. You can initialize and pass any number of pointer variables with their reference.
This is also valid..when you pass the variable address, you should store it into a pointers
you have to do some changes in your code,
You can assign directly a/b and a*b pointer variables *c & *d
Then you have to read double number with %lf format argument.
#include <stdio.h>
#include <string.h>
void myFunction(double a, double b, double *c, double *d)
{
*c = a/b; //change
*d = a*b; //change
printf("%lf %lf",*c,*d);
return;
//printf statements
}
int main()
{
//first and second double hold the scanf inputs
double first;
double second;
//unsure here - to reference c and d as parameters in the function, do I simply declare unfilled double variables here?
double *c;
double *d;
printf("Enter your first number\n");
scanf("%lf", &first); //change
printf("Enter your second number\n");
scanf("%lf", &second); //change
//call the function, first and second by value, &c / &d by reference - correct?
myFunction(first, second, &c,&d);
}
Here is the correct way to do it. The func() method
void func(double x,double y,double *z,double *w){
printf("pass by value = %lf, %lf",x,y);
printf("pass by reference = %lf, %lf",*z,*w);
}
The main() method
int main(void){
double first,second,val1,val2;
val1=3.1;
val2=6.3;
printf("Enter your first number\n");
scanf("%lf", &first);
printf("Enter your second number\n");
scanf("%lf", &second);
func(first,second,&val1,&val2);
}
I have written the following code
#include <stdio.h>
#include <stdlib.h>
int main()
{
// declaration of variables as strings
char astr;
char bstr;
char cstr;
/* Input three sides of a triangle */
printf("Enter first side of triangle: ");
scanf("%s",&astr);
printf("Enter second side of triangle: ");
scanf("%s",&bstr);
printf("Enter third side of triangle: ");
scanf("%s",&cstr);
// conversion of strings to float
float a = atof(&astr);
float b = atof(&bstr);
float c = atof(&cstr);
// checking if given triangle is valid
if((a + b > c) && (a + c > b) && (b + c > a))
{
// Checking for special cases of triangle
if(a==b && b==c)
{
/* If all sides are equal */
printf("Equilateral triangle.");
}
else if(a==b || a==c || b==c)
{
/* If any two sides are equal */
printf("Isosceles triangle.");
}
else
{
/* If none sides are equal */
printf("Scalene triangle.");
}
}
else
{
printf("Triangle is not valid. \n");
}
printf("%f \n", a);
printf("%f \n", b);
printf("%f \n" ,c);
return 0;
}
However when I run it, I get "Triangle is not valid" despite the fact that mathematically the triangle would be valid
When printing the Values stored in a b and c I discover that only the Value for c is stored correctly but a and b are always 0.000000
what have I done wrong?
Thanks in advance
instead of 3 wrong lines:
char astr;
scanf("%s",&astr);
float a = atof(&astr);
let me propose working lines:
char astr[20]; // should be enough
scanf("%19s",astr); // 19 protects from buffer overflow on astr
float a = atof(astr);
First, you cannot scan a string into a char: not enough room / undefined behaviour. Pass a char array instead (and in that case drop the &, it's already an address)
Second, pass the string to atof, not the pointer on the string.
Aside: atof doesn't check for errors, better use strtof which provides (optional) error checking.
int main () {
float val;
char str[10];
strcpy(str, "1234");
val = atof(str);
printf("%f", val);
return 0;
}
You can also use strtod() refer man page of atof()
I have a question in C where I need to insert coefficients of a quadratic equation into a function and return the number of solutions and result.
Write a program that accepts a series of 3 real numbers, which are the
coefficients of a quadratic equation, and the program will print out
some solutions to the equation and the solutions themselves.
Guidelines:
Functions must be worked with one of the functions that
returns the number of solutions as a returned value, and returns the
solutions themselves through output parameters.
3 numbers must be
received each time. The input will be from a file (will end in EOF)
In the meantime I built the function without reading from a file just to see that it works for me, I built the function that returns the number of solutions but I got entangled in how to return the result as output parameter
here is my code for now:
int main ()
{
double a, b, c, root1,root2,rootnum;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf",&a, &b, &c);
rootnum=(rootnumber(a,b,c);
printf("the number of roots for this equation is %d ",rootnum);
}
int rootnumber (double a,double b, double c)
{
formula=b*b - 4*a*c;
if (formula<0)
return 0;
if (formula==0)
return 1;
else
return 2;
}
In C, providing an "output parameter" usually amounts to providing an argument that is a pointer. The function dereferences that pointer and writes the result. For example;
int some_func(double x, double *y)
{
*y = 2*x;
return 1;
}
The caller must generally provide an address (e.g. of a variable) that will receive the result. For example;
int main()
{
double result;
if (some_func(2.0, &result) == 1)
printf("%lf\n", result);
else
printf("Uh oh!\n");
return 0;
}
I've deliberately provided an example that illustrates what an "output parameter" is, but has not relationship to the code you actually need to write. For your problem, you will need to provide two (i.e. a total of five arguments, three that you are providing already, and another two pointers that are used to return values to the caller).
Since this is a homework exercise, I won't explain WHAT values your function needs to return via output parameters. After all, that is part of the exercise, and the purpose is for you to learn by working that out.
Apart from a wayward parenthesis in the call and some other syntax errors, what you have so far looks fine. To print out the number of roots, you need to put a format specifier and an argument in your printf statement:
printf("the number of roots for this equation is %d\n", rootNum);
The %d is the format specifier for an int.
Here is your working code:
#include <stdio.h>
int rootnumber (double a,double b, double c)
{
double formula = (b*b) - (4*(a)*(c));
if (formula > 0) {
return 2;
}
else if (formula < 0) {
return 0;
}
else {
return 1;
}
}
int main (void)
{
double a, b, c;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf",&a, &b, &c);
printf("The number of roots for this equation is %d ", rootnumber(a,b,c));
return 0;
}
It just need some sanity checking, its working now:
#include<stdio.h>
int rootnumber(double a, double b, double c);
int main ()
{
double a, b, c, root1,root2;
int rootnum;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf",&a, &b, &c);
rootnum=rootnumber(a,b,c);
printf("the number of roots for this equation is %d", rootnum);
return 0;
}
int rootnumber(double a, double b, double c)
{
int formula= (b*b) - (4*a*c);
if (formula<0)
return 0;
if (formula==0)
return 1;
else
return 2;
}
int main()
{
double a = 1;
double b = 3;
int n = 128;
int answer = 0;
printf("select an option(1, 2) ");
scanf("%d", answer);
double y = calcIntegral (answer, a, b, n);
printf("%f \n", y);
system("pause");
return 0;
}
it gets to Scanf and then if accepts the answer but stalls completely and I have to force the task to end. What's going on? This is identical to other programs I've written, I think. I tried using %i as well, and using a char instead of a double for the variable "answer". It says it can't access the memory.
For scanf with modifier d, it matches an optionally signed decimal integer, and the next pointer must be a pointer to int. Says the standard. Also make sure always check scanf return value.
int ret = scanf("%d", &answer);
if (ret != 1) {
// failed to input the number
}
When using scanf(), the variable you read has to be a pointer . So your statement :
scanf("%d", answer);
should be :
scanf("%d", &answer);
as you have declared answer to be an int, so its memory address is a pointer to an int.
On the other hand, if you wanted to read a string and had declared :
char *str;
allocating some memory for it, then the statement would be :
scanf("%s", str);
as str is declared as a pointer to char.