c programming error with binary operators - c

Im trying to write a calculator program. i have wrote the first part of it but i keep get the same error: invalid operands of types unsigned int*' andchar[80]' to binary `operator&'
Please help me
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
unsigned int num1, num2, num3;
char s[80];
int main (){
printf("type in an expression: ");
scanf(" %x %s %x\n", &num1 &s &num2);
if(strcmp ("add", s) == 0){
num3 = num1 + num2;
}
if(strcmp("subtract", s) == 0){
num3 = num2 - num1;
}
printf("the answer is: %x", num3);
}

try:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
unsigned int num1, num2, num3;
char s[80];
int main (){
printf("type in an expression: ");
scanf(" %x %s %x", &num1, s, &num2);
if(strcmp ("add", s) == 0){
num3 = num1 + num2;
}
if(strcmp("subtract", s) == 0){
num3 = num2 - num1;
}
printf("the answer is: %x\n", num3);
system("pause");
}
note: notice that I remove \n in the scanf..

As Yohanes mentions, you need to have the commas in between the arguments in the scanf, otherwise the compiler is trying to do: get the address of num1 (&num1) and logically AND it with the address of the array s (address is implied here because it is an array) and logically AND that with the value contained in num2.
I would suggest that you add an else between the two if statements since they are mutually exclusive.
Furthermore, you probably want to add a \n to the printf statement
printf("the answer is: %x\n", num3);
to flush the output.

Related

scanf to read multiple numbers

I am writing a c program to read two fractions and enter the sum of the fractions. I keep on getting 6487612 and 6487608 as the answer. please help thank you
#include <stdio.h>
int main ()
{
int num1, num2, denom1, denom2, result_num, result_den;
printf("enter first fraction\n");
scanf("%d/%d", &num1, &denom1);
printf("enter second fraction\n");
scanf("%d/%d", &num2, &denom2);
result_num = (denom2 * num1) + (denom1 * num2);
result_den = (denom1 * denom2);
printf("the result is %d/%d", &result_num, &result_den);
return 0;
}
just wanted sum of the two fractions

Pointer variable not being updated for C

#include <stdio.h>
void gcdFinder(int num1, int num2, int *result);
int main()
{
int n1, n2, result;
printf("Enter 2 numbers: \n");
scanf("%d %d", &n1, &n2);
gcdFinder(n1, n2, &result);
printf("rGcd2(): %d\n", result);
return 0;
}
void gcdFinder(int num1, int num2, int *result)
{
printf("Initial : %d %d \n",num1,num2);
*result=num1;
if(num2 ==0 ){
return;
}else{
gcdFinder(num2,(num1%num2),&result);
}
}
I am trying to find the GCD the 2 inputs and storing the result into the result variable. However my code does not seem to work as it only stores one time.
As the output suggest my final value for num1 should be 1 however it is not stored and it remains as 4.
In the function gcdFinder change
gcdFinder(num2,(num1%num2),&result);
to
gcdFinder(num2,(num1%num2), result);
^
No & as result is already a pointer here
Notice:
In main it's correct to use &result because result in main is an int

Why is my small program crashing?

I'm trying to make a tiny program where you input for example 1 + 2 and the output should be the sum of those two numbers. But it keeps crashing and or won't do anything. What's going on?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char *op;
char *first;
char *second;
printf("Enter operation\n");
scanf(" %s%s%s", &first, &op, &second);
int num1;
int num2;
int num3;
int add;
num1 = atoi(first);
num2 = atoi(op);
num3 = atoi(second);
add = num1 + num3;
printf("Sum = %i\n",add);
return 0;
}
atoi takes argument as const char * and not char . Your variables are of type char where as atoi converts string to int type.
Also you pass char * as argument to %d in scanf , that results in undefined behavour.
scanf(" %d%d%d", &first, &op, &second)
^^^^^^ expects int * not char *

Issue with swapping program, the program crashes

I am trying to make a program that scans 3 ints and then passes them through a function that sort them by this way- the biggest number will be at 'num3', the second will be at 'num2' and the lowest one will be at 'num1' but for some reason the program crashes when it gets to the sort function.
#include <stdio.h>
#include <stdlib.h>
void swap(int* a, int* b);
void changer(int* num1, int* num2, int* num3);
int main()
{
int num1 = 0;
int num2 = 0;
int num3 = 0;
printf("Please enter your value for 'num1': ");
scanf("%d", &num1);
getchar();
printf("Please enter your value for 'num2': ");
scanf("%d", &num2);
getchar();
printf("Please enter your value for 'num3': ");
scanf("%d", &num3);
printf("\nYour nums before- \n");
printf("num1 == %d\n", num1);
printf("num2 == %d\n", num2);
printf("num3 == %d\n", num3);
changer(&num1, &num2, &num3);
printf("\nYour nums after- \n");
printf("num1 == %d\n", num1);
printf("num2 == %d\n", num2);
printf("num3 == %d\n", num3);
system("PAUSE");
return 0;
}
void changer(int* num1, int* num2, int* num3)
{
if (*num1 > *num3)
{
swap(*num3, *num1);
}
else if (*num1 > *num2)
{
swap(*num1, *num2);
}
if (*num2 > *num3)
{
swap(*num3, *num2);
}
}
void swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
You should not dereference the pointers when you pass them to swap() since it takes int * and you would be passing int thus causing undefined behavior and in your case a crash. The compiler should warn about incompatible parameters.
Change every occurrence of
swap(*num1, *num2);
/* ^ ^ remove these */
with
swap(num1, num2);
swap(int*, int*) expects the integer pointer to be passed in, assume the value of num1 = 0 and num2 = 2 and when you pass these values to swap(), by dereferencing you are invoking like this swap(0, 2) or on 32 bit system
swap(0x00000000, 0x00000002)
Inside swap(int* a, int* b) you dereference the memory location 0x00000000 (not the memory pointed to by a) at the line,
int temp = *a;
and then you dereference 0x00000002 b at this line and trying to assign it to 0x00000000 (by dereferencing *a)
*a = *b;
You are generating
Memory access violation condition.
which is causing the crash. Summarizing, please do not ignore compiler warnings. Compiler raises warning on the line where you are making swap() calls.
'int *' differs in levels of indirection from 'int'
Both functions take integer pointers (i.e., memory addresses of integers) as arguments. So, the values of the arguments a, b, num1, num2, and num3 must ALWAYS be memory addresses of integers. In the function changer, values of the variables num1, num2, and num3 are ALREADY memory addresses. When you write *num1, value of *num1 is integer but not the memory address of an integer. So, you need to call the function swap as follows:
swap(num3, num1);
swap(num1, num2);
swap(num3, num2);

Segmentation Error in C programming

I am trying to compile a program in C, but I keep getting the following error:
Segmentation fault
Here is the code:
#include <stdio.h>
#define calculation1 main
void calculation1(int *num1, int *num2) {
int total;
printf("Program One: \n");
printf("=========== \n");
printf("Number One: ");
scanf("%d", &num1);
printf("Number Two: ");
scanf("%d", &num2);
total = *num1 + *num2;
printf("%d + %d = %d \n",&num1,&num2,total );
}
What am I doing wrong here? How can I fix this error?
What am I doing wrong here? How can I fix this error?
Problem 1
By using
#define calculation1 main
void calculation1(int *num1, int *num2) {
you are essentially using:
void main(int *num1, int *num2) {
This is wrong. main needs to be:
int main(void) {
or
int main(int argc, char** argv) {
Your program is subject to undefined behavior.
Problem 1
You are using
scanf("%d", &num1);
scanf("%d", &num2);
when num1 and num2 are of type int*. Thy need to be:
scanf("%d", num1);
scanf("%d", num2);
Problem 3
You are using
printf("%d + %d = %d \n",&num1,&num2,total );
Given the type of num1 and num2, that needs to be:
printf("%d + %d = %d \n", *num1, *num2, total );
Fix
Your program needs a bit of an overhaul. Try:
#include <stdio.h>
#define calculation1 main
int calculation1() {
int num1; // Not int*. If you use int*, you'll need to allocate memory
int num2;
int total;
printf("Program One: \n");
printf("=========== \n");
printf("Number One: ");
scanf("%d", &num1); // You need to use &num1 since num1 is of type int.
printf("Number Two: ");
scanf("%d", &num2);
total = num1 + num2;
printf("%d + %d = %d \n", num1, num2, total);
}
scanf("%d", num1);
scanf("%d", num2);
scanf needs an address and num1 and num2 already contain the address as you pass them as pointers in the function.
The other thing to change is:
printf("%d + %d = %d \n",*num1,*num2,total );
*num1 dereferences a pointer to provide the value
Despite errors pointed by #jayant-
#define calculation1 main
void calculation1(int *num1, int *num2) {
So , calculation1 will be replaced by main ,so in short this is your main function .
This is not a valid and should avoid it any case . I am confused how you make a call or take command line argument ? But this is definitely incorrect .
Simply,do this -
int main(void) or int main(int argc,char *argv[]) and declare num1 and num2 as int variables and then take input in it and perform desired operation.

Resources