How to solve segmentation error in C program [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Guys I'm getting a segmentation error, exactly this error to be more specific:
Segmentation fault
Does anyone know how to solve this? my code is simple I don't know why the error, does anyone know how to solve it step by step?
I'm using this command to convert my c file into an executable gcc test.c -o test then I run my test file with./test, but then I get that error as I said above.
My code in c:
#include <stdio.h>
int main(){
int age;
int aget;
int total;
printf("Input your age: ");
scanf("%i", &idade);
printf("Input your age two: ");
scanf("%i", &age);
base = age + aget;
printf(total);
return 0;
}

printf needs a format string. Here we need to tell it %d for integer in decimal.
total = idade + age;
printf("%d\n", total);
Some random resource on how to use printf: https://man7.org/linux/man-pages/man3/printf.3.html

Variable base is undeclared.
Variable idade is undeclared.
It should be printf("%d",base) if your are using base.
#include <stdio.h>
int main(){
int age;
int aget;
int total;
printf("Input your age: ");
scanf("%i", &age);
printf("Input your age two: ");
scanf("%i", &aget);
total = age + aget;
printf("%d",total);
return 0;
}

Related

Why this code not working in code blocks( working in VS coe)? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 months ago.
Improve this question
#include<stdio.h>
int qt( int *x);
struct student
{
int id;
char name[10];
char b_g[10];
float cgpa;
char address[20];
};
int num,i;
int main()
{
printf("Input How Many students You Have: ");
scanf("%d",&num);
qt(&num);
}
int qt(int *x)
{
int v,n;
v=*x;
struct student ar[v];
printf("Enter Your ID, Name, Blood Group, CGPA, Adress:\n");
for(i=1;i<=v;i++)
{
scanf("%d %s %s %f %s",&ar[i].id,ar[i].name,ar[i].b_g,&ar[i].cgpa,ar[i].address);
}
printf("\nEnter Your ID:");
scanf("%d",&n);
for(i=1;i<=v;i++)
{
if(ar[i].id==n){
printf("Student ID:%d\n\nName: %s\nBlood Group: %s\nCGPA: %f \nAddress: %s\n",n,ar[i].name,ar[i].b_g,ar[i].cgpa,ar[i].address);
}
}
}
it taking many inputs in code blocks that i don't want to do,,but in same code is working fine in VS code.
I need to run it in code blocks and that should show proper output base on search id.
Firstly I would say that your qt function should probably actually return an integer at some point, seeing as you've declared the return data type without a return statement. Also, as someone mentioned above, it might be helpful going forwards to adopt the widely accepted standard of starting for loops at 0. If adding the return does not fix the issue, could you post the error message you are getting??

Bug in simple C source code? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I want to know if the format of user defined function i wrote i.e return(xxx) is correct or not .
Because when i compile my code, i have to enter the input 2 times.It might be a silly mistake because i just began learning C language
****MY CODE:****``
#include<stdio.h>
long cube(long x );
long input,answer;
int main (void )
{
printf("Enter a number:");
scanf("%ld ",&input);
answer = cube(input);
printf(" The cube of %ld is %ld",input ,answer);
return 0;
}
long cube(long x )
{
return (x*x*x);
}
****ANswer****
#include <stdio.h>
long cube(long x);
long input, answer;
int main( void )
{
printf("Enter an integer value: ");
scanf("%d", &input);
answer = cube(input);
printf("\nThe cube of %ld is %ld.\n", input, answer);
return 0;
}
long cube(long x)
{
long x_cubed;
x_cubed = x * x * x;
return x_cubed;
}
remove the space after '%ld' it will take one input.
according your code,
scanf("%ld '&input) ;
here compiler at first wants one input for '%ld' then it waits for blank space you used after '%ld'. remove it then it will go next step after one input.
you should use,
scanf("%ld%",&input);

Something is always wrong with this [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
#include <stdio.h>
struct Bank {
char name[500];
int mobile;
float balance;
};
int main() {
int i;
struct Bank a[100];
for (i = 0; i < 3; i++) {
printf("Enter the name of person %d: ", i + 1);
gets(a[i].name);
printf("Enter the mobile of person %d: ", i + 1);
scanf("%d", &a[i].mobile);
printf("Enter the mobile of person %d: ", i + 1);
scanf("%f", &a[i].balance);
}
for (i = 0; i < 3; i++) {
puts(a[i].name);
printf("His Balance is: %f", a[i].balance);
printf("His Mobile number is : %d \n\n", a[i].mobile);
}
return 0;
}
Try running this the user input requests don't come like I want them to, just run it and one would understand. What am I doing wrong?
The reason it's not working as expected is because there is a newline character left on the input buffer on the second and third iteration to gets(), presumably from the previous "enter" hit.
It works as expected (kind of, see below) if you change the gets() call to scanf("%s", a[i].name).
Please note that:
this code is badly vulnerable to buffer overflow on multiple lines (all scanf() and the gets() call) - see here, or here, or here, etc., bottom line: you should not use gets() at all, nor scanf() without a boundary
you have a typo in the third printf and it again asks for the mobile number

Program stopped working [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<windows.h>
int main(){
int iCount=1;
int min=999999;
int max=0;
int iSum;
int iAVG=0;
int iValue;
system("color 97");
for(iCount=1;iCount<36;iCount++){
system("cls");
SetConsoleTitle("chairs");
printf("\t\tCHAIR VALUES\n\n");
printf("\nPlease enter the value of chair#: %d.\n>>", iCount);
scanf("%d",iValue);
iSum+=iValue;
if(iValue<min){
min==iValue;
}
if(iValue>max){
max==iValue;
}
printf("\n\nThe minimum and maximum values entered are:\nminimum value>>%d\nmaximum
value>>%d", min, max);
getche();
}
(iAVG=iSum/iCount);
printf("\n\nThe average value of the entered chairs is: %d", iAVG);
getche();
system("cls");
printf("\t\t\nGOODBYE USER!");
}
I wrote this code, a c question. I compiled it within codeblocks, it was successfully compiled and executed. However, when i entered the first chair value, it says that"chairs.exe has stopped working. Im here trying to see what might have lead to this problem. Any can give me a helping hand?
You missed to add '&' in scanf
scanf("%d",iValue);
Should be
scanf("%d",&iValue); //<--- Notice '&'
And as pointed out by "Zev Eisenberg" in comments.
min = iValue //<-- Make sure you are using assignment operator here.

I am very new in c, and I need some help of arrays [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
So basically, I want user to put their family size and depending on their size, I want to ask their ages as many times as family member( so input depends on them).
Now, I want the age input as array listing. for example, if they are 2 of them , then I want to ask their age twice and save it as
scanf("%d",age[0]);
scanf(%d",age[1]);
Here I could think of c code:
#include <stdio.h>
int main(int n, int age[0]){
printf("enter your family number:");
scanf("%d", n);
}
for(i=1; i>=n; i++){
printf("Can I have your family's age one by one:");
scanf("%d",&age[i]);
}
#include <stdio.h>
int main(void){
int n;
printf("enter your family number:");
scanf("%d", &n);
int i, age[n];
for(i=0; i<n; ++i){
printf("Can I have your family's age one by one:");
scanf("%d", &age[i]);
}
return 0;
}

Resources