Why is my small program crashing? - c

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 *

Related

Getting calculation into struct field type of char

I have declared following struct
typedef struct STRUCT{
char calculation[30];
}STRUCT;
I have a function:
int add_number(int num1, STRUCT *pointer) {
int integer;
int sum;
printf("\nGive an integer to be added: ");
scanf("%d", &integer);
sum = num1 + integer;
printf("%d+%d=%d\n", num1, integer, sum);
scanf("%s", pointer->calculation); // here I would want to get the %d+%d=%d stored into pointer->calculation
num1 = sum;
return num1;
}
I would want to store this:
"%d+%d=%d\n", num1, integer, sum
into this:
pointer->calculation
(So for example, if num1 = 1 and integer = 2 I would want to have 3 = 1 + 2 stored into pointer->calculation)
How could it be done? I don't get it.
You can use e.g. snprintf to print a formatted string into a buffer (similar to what printf does but instead of directly to stdout).
For example:
#include <stdio.h>
int main()
{
int num1 = 1;
int integer = 2;
int sum = num1 + integer;
char buf[30];
snprintf(buf, 29, "%d+%d=%d\n", num1, integer, sum);
printf(buf);
}
Output:
1+2=3
The size passed to snprintf is 29 instead of 30, because we need to keep one element for the zero termination of the string.
Note: you wrote that you would like the buffer to contain 3 = 1 + 2 which does not match your string format "%d+%d=%d\n", so I assume you meant you'd like 1+2=3.

C Extract number from infix string

I am trying to extract operators and operands from a string/char array, but unable to do so.
I've tried:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char str[] = "222+333";
char *endPtr;
long int x = strtol(str,&endPtr,10);
printf("Number is %d \n" , x);
printf("Operator is %s \n" , *endPtr);
long int y = strtol(endPtr,&endPtr,10);
printf("Number is %d\n" , y);
return 0;}
Not sure how can I get the operator after using the strtol function.
Also are there any ways to do this without using libraries?
You almost had it, the issue with your code is the printing of the operator. %s is trying to print a null terminated string and requires a memory address. You are passing a character + which is probably ascii on your system so turns into the memory address 43
I've changed the printf statement to print a char vs a null terminated string and it works as you expect it too
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char str[] = "222+333";
char *endPtr;
long int x = strtol(str, &endPtr, 10);
printf("Number is %d \n", x);
printf("Operator is %c \n", *endPtr);
long int y = strtol(endPtr, &endPtr, 10);
printf("Number is %d\n", y);
return 0;
}
To answer the comment question, endPtr is not pointing at the 3 it is pointing at the + which is valid for strtol to read ie a positive number, when you change the sign to divide or multiply it fails (should work with minus)
What you need to do is advance endPtr by 1 char past the operator that it is point at as in the code below
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char str[] = "222/333";
char *endPtr;
char theOperator= ' ';
long int x = strtol(str, &endPtr, 10);
printf("Number is %d \n", x);
theOperator= *endPtr;
++endPtr;
printf("Operator is %c \n", theOperator);
long int y = strtol(endPtr, &endPtr, 10);
printf("Number is %d\n", y);
return 0;
}

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.

c programming error with binary operators

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.

Weird results using simple math operations

i have the following struct:
typedef struct number
{
int x;
int y;
int z;
unsigned long int final;
}number;
my code is the following:
number* numbers;
numbers= (number*)malloc(sizeof(number));
scanf("%d %d %d", &numbers->x, &numbers->y, &numbers->z);
printf("input: %d,%d,%d\n",numbers->x, &numbers->y, &numbers->z);
numbers->final=(numbers->x)*4000 + (numbers->y)*50 + (numbers->z);
printf("final: %d",numbers->final);
but the output is wrong. for example here is a run:
12 12 12
input: 12,12,12
final: -28640
i cant figure out the problem. the highest number that number->final can get is 90,000 (i make sure of it as i gives the input)... i seems like there is overlap? please help.
Your problem is the pointer. I am assuming you initialised the struct as follows.
numbers *numbers;
However if you use it in the main where you declare it don't use a pointer. There are also a few errors in your printf call, you are printing the memory address of y and z instead of the value like you did for the x value.
Use something like this.
#include <stdio.h>
typedef struct number
{
int x;
int y;
int z;
unsigned int final;
} number;
int main()
{
number numbers;
scanf("%d %d %d", &numbers.x, &numbers.y, &numbers.z);
printf("input: %d,%d,%d\n",numbers.x, numbers.y, numbers.z);
numbers.final=(numbers.x)*4000 + (numbers.y)*50 + (numbers.z);
printf("final: %d\n",numbers.final);
return 0;
}
Right and if you used malloc it looks like this.
#include <stdio.h>
#include <stdlib.h>
typedef struct number
{
int x;
int y;
int z;
unsigned int final;
} number;
int main()
{
number *numbers = malloc(1 * sizeof(number));
scanf("%d %d %d", &numbers->x, &numbers->y, &numbers->z);
printf("input: %d,%d,%d\n",numbers->x, numbers->y, numbers->z);
numbers->final=(numbers->x)*4000 + (numbers->y)*50 + (numbers->z);
printf("final: %d\n",numbers->final);
free(numbers);
return 0;
}
Running example here
The reason for your wrong answer is because you have kept the datatype as int which has max value of 32767 change it to unsigned long int as your ans calculates to 2400612

Resources