Segmentation Fault (Core Dump) C [duplicate] - c

This question already has answers here:
What will happen if '&' is not put in a 'scanf' statement?
(6 answers)
Closed 5 years ago.
I've started to study C this week, I'm totally new in programming in C, and when I tryed to do this exercise this error in the console keep showing up.
#include <stdio.h>
#include <stdlib.h>
float calc(float *sall, float *salb)
{
float hraula, insspc;
int naula;
printf("Digite o valor da hora-aula e o numero de aulas dadas:");
scanf("%f%i", hraula, naula);
printf("Digite a porcentagem do inss retirada do salário:");
scanf("%f",insspc);
*salb = hraula * naula;
*sall = *salb * ((100 - insspc) / 100);
return 0;
}
int main()
{
float salbt, sallq;
calc(&sallq, &salbt);
printf("O salário bruto é: %f R$, liquido: %f R$", salbt, sallq);
return 0;
}
Well hope someone can help me, thanks!

scanf(" %f%i", hraula, naula);
scanf(" %f",insspc);
It should be as scanf requires the pointers to variables:
scanf(" %f%i", &hraula, &naula);
scanf(" %f",&insspc);

Pass a pointer to the receiving variables like this:
scanf("%f%i", &hraula, &naula);
Similarly
scanf("%f", &insspc);
Reference: man 3 scanf
It is also good practice to check the return value of scanf to ensure that you have collected the correct number of values. Something like this:
if (scanf("%f%i", &hraula, &naula) != 2) {
fprintf(stderr, "Failed to read hraula and naula\n");
return -1;
}
and then check the return value of calc():
if (calc(&sallq, &salbt) == 0)
printf("O salário bruto é: %f R$, liquido: %f R$", salbt, sallq);

Related

calculation program in C [duplicate]

This question already has answers here:
(Why) is using an uninitialized variable undefined behavior?
(7 answers)
Closed 2 years ago.
#include <stdio.h>
int main()
{
int a, b, c;
c = a * b;
printf("a는?: ");
scanf("%d", &a);
printf("b는?: ");
scanf("%d", &b);
printf("%d * %d = %d ", a, b, c);
return 0;
}
i made a calculation program . but the result is wrong.
if i put 5,10 in a,b
c should be 50(510)
but the result is 0
i used ab instead of c . then it was solved. but i want to use c variation.
what is the problem?
should i use double instead of int? please tell me the reason why the problem evoked
You're assigning c = a * b at a point when a and b doesn't have proper values. You should do this calculation after assigning values to a and b, otherwise result will be some garbage value.
Right way to do it:
#include <stdio.h>
int main()
{
int a, b, c;
printf("a는?: ");
scanf("%d", &a);
printf("b는?: ");
scanf("%d", &b);
c = a * b;
printf("%d * %d = %d ", a, b, c);
return 0;
}

Factorial with while loop in C [duplicate]

This question already has answers here:
When should I use ampersand with scanf()
(3 answers)
Closed 3 years ago.
Hello friendly community,
i´m really new in coding and also here registred on stackoverflow, so excuse me when I ask such simple questions. I lack the understanding to understand why this code does not work, because even after compiling no error message appears.
My Code below shows my try to code a while loop, calculating the factioral and printing the total:
#include <stdio.h>
/* Factorial 5! = 5*4*3*2*1 */
main ()
{
int Wert;
int fak = 1;
scanf ("%d", Wert);
while ( Wert > fak) {
fak = fak * Wert;
Wert = Wert - 1;
printf ("%d", fak);
}
}
It should calculate the factorial after input of a number and print the total. Maybe this can´t work but i don´t understand the why.
Thank you for your time.
Firstly, scanf expects the address of Wert, not its value, and also update your while loop to compare with 1. Here's the fixed version:
#include <stdio.h>
int main(void)
{
int Wert;
int fak = 1;
scanf ("%d", &Wert);
while ( Wert > 1 ) {
fak = fak * Wert;
Wert = Wert - 1;
}
printf ("%d", fak);
}
Input:
5
Output:
120
But since factorials easily overflow integers, it may be a better idea to use double instead:
#include <stdio.h>
double fact(double d)
{
if (d < 1.0)
return 1.0;
return d * fact(d - 1.0);
}
int main(void)
{
double d = 0;
if (scanf("%lf", &d) != 1) {
perror("Failed to read stdin");
return -1;
}
printf("%lf! = %lf", d, fact(d));
return 0;
}
Input:
100
Output:
100.000000! = 93326215443944102000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000
Just keep in mind that floating point math is broken.

Why do I get a segmentation error here in my code? [duplicate]

This question already has answers here:
Why does scanf() need & operator (address-of) in some cases, and not others? [duplicate]
(5 answers)
Ampersand(&) and scanf in C?
(5 answers)
errors and warning message when using pointer
(3 answers)
Closed 5 years ago.
It's an extremely simple code, when I run it, it lets me enter the int, but when I enter it, the program crashes. I tried putting a printf right after the scanf to check if the program reads the int correctly, it seems it doesn't. I can't output the integer that I input.
#include <stdio.h>
#include <math.h>
void coolfunction(int x, int *y)
{
*y = exp(x);
}
int main()
{
int n;
double result = 0;
printf("Enter an integer: ");
scanf("%d", n);
coolfunction(n, &result);
printf("\n e^%d = %lf \n", n, result);
return 0;
}
Here's a version using a double parameter:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void coolfunction(int x, double *y)
{
*y = exp(x);
}
int main()
{
int n = 0, scanned = 0;
double result = 0;
printf("Enter an integer: ");
scanned = scanf("%d", &n);
if (scanned < 1) {
fprintf(stderr, "value entered not a valid integer\n");
exit(EXIT_FAILURE);
}
coolfunction(n, &result);
printf("\n e^%d = %g \n", n, result);
return 0;
}
You also missed the & in your scanf call: the function needs to know the address of your variable, not its value.

C program Do while doesnt work [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
A have a problem with my program. I dont know what im doing wrong but loop do while doesnt work. At and program should ask "If you want to run this program again, press T.Other key should close this program.
#include "stdafx.h"
#include <stdlib.h>
#include <conio.h>
#include <time.h>
int main()
{
int a;
int b;
int c;
int f;
int g;
int h;
int d = 0;
char e;
srand(time(0));
do {
printf("How many numbers do you want to show: ");
scanf_s("%i", &a);
printf("od: ");
scanf_s("%i", &b);
printf("do: ");
scanf_s("%i", &c);
h = c + 1;
f = b - h;
for (d; d < a; d++) {
printf("%i ", b + rand() % f);
}
printf("\n");
printf("Restart program? T- Yes");
scanf_s("%s", &e);
} while (e == 't');
_getch();
return 0;
}
Program works fine, but when i press T at the end. it will close. Im using Visual Studio 2015
Now my code is below:
do {
printf("How many numbers do u want: ");
scanf_s("%i", &a);
printf("od: ");
scanf_s("%i", &b);
printf("do: ");
scanf_s("%i", &c);
h = c + 1;
f = b - h;
//printf("%i %i %i\n", h, f);
for (d; d < a; d++) {
printf("%i ", b + rand() % f);
}
printf("\n");
printf("Restart? T- yes");
scanf_s("%c", &e);
} while (e == 't' || e == 'T');
_getch();
return 0;
}
But it still doesnt work. I cant enter any letter. When i press any key a windows is going to close
As others have pointed, this is because you're trying to get a "%s" and, therefore, you can't compare it to a character. Use "%c" instead, or use strcmp function to compare 2 char arrays.
By the way, be aware that scanf_s is a Microsoft only function. Not sure if visual studio is forcing you to use it, but the common usage of scanf wouldn't hurt, check it out:
scanf("%c", &e);
Do you press 'T'? because 'T' and 't' are not the same character. check the ascii table
Your program will continue while you press a 't' so with a 'T' or anything else it will stop.
ps: You may want to use "read(0, &e, 1);" or "scanf_s("%c", &e);" instead of "scanf_s("%s", &e);" (last line of do while), because you have a char and if you enter a string like "Mr T. says yes" it will overwrite your memory which is never good.As a reminder char is 1 byte in memory, with '&' you can access its place in memory, so it acts like a string but if you write more than 1 character it will just write in the following memory which isn't attributted to your program. it will probably do nothing but it can overwrite another of your variable or worse overwrite another program in your computer (hopefully not something important like a window's program or else!)
char and string basics

Trying to run a program in C

Can someone help me to run this program? I tried this:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void) {
double Cateto1;
double Cateto2;
double hipotenusa;
printf("dame el primer cateto: ");
scanf("%1f", Cateto1);
fflush(stdout);
printf("dame el segundo cateto: ");
scanf("%1f", &Cateto2);
fflush(stdout);
hipotenusa = sqrt ((Cateto1*Cateto1)+(Cateto2*Cateto2));
printf("hipotenusa= %2f",hipotenusa);
system("pause");
}
I can build it but I can't run it... it gives me:
RUN FAILED (exit value -1.073.741.790, total time: 17s)
scanf("%lf", Cateto1);
↑ ↑
| You are missing a '&' character here
The width specifier for doubles is l, not 1
The first argument to scanf must be "%lf" (as the letter L) to specify that the corresponding output variable is a pointer to double instead of a float. '1' (One) has no meaning for scanf.
The second argument to scanf here is expected to be a pointer to double, and you're giving it a double instead.
I suppose it is a simple typo since you got it right the second time.
Here is the mistake:
scanf("%1f", Cateto1);
Change it to:
scanf("%1f", &Cateto1);
There are a couple of errors:
The syntax of the scanf expression was wrong: "%1f" should be "%lf"
You need to pass the address of Cateto1 (&Cateto1) to scanf
You don't need the fflush
You don't need the system call
Here's the updated code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void) {
double Cateto1;
double Cateto2;
double hipotenusa;
printf("dame el primer cateto: ");
scanf("%lf", &Cateto1);
printf("dame el segundo cateto: ");
scanf("%lf", &Cateto2);
hipotenusa = sqrt ((Cateto1*Cateto1)+(Cateto2*Cateto2));
printf("hipotenusa= %2f\n",hipotenusa);
}
There is an Error in your code. Instead of
scanf("%1f", Cateto1);
you should write:
scanf("%1f", &Cateto1);
Simple Mistake
scanf("%1f", &Cateto1); // '&' was missing in all scanf statements
#include <stdio.h>
#include <math.h>
int main(void)
{
double Cateto1;
double Cateto2;
double hipotenusa;
printf("dame el primer cateto: ");
scanf("%lf", &Cateto1);
//fflush(stdout);
printf("dame el segundo cateto: ");
scanf("%lf", &Cateto2);
//fflush(stdout);
hipotenusa = sqrt ((Cateto1*Cateto1)+(Cateto2*Cateto2));
printf("hipotenusa= %2f\n",hipotenusa);
//system("pause");
return 0;
}

Resources