Getting Segmentation Fault in C in a simple program [closed] - c

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 4 years ago.
Improve this question
I wrote a program which test the age of person , if the age is above 18 it further asks the name and gender of the person and depending on the gender it prints if output.
But after I enter the age I am getting Segmentation Fault error.
I am using debian and I am a beginner in C language.
#include <stdio.h>
#include <stdlib.h>
int main(){
char name[20];
int age;
char gender;
printf("How old are you ? \n");
scanf(" %d",age);
if(age >= 18){
printf("What your name ? \n");
scanf(" %s",&name);
printf("What's your gender ? (m/f) \n");
scanf(" %c",&gender);
if(gender == 'm'){
printf("Welcome Mr. %s \n",name);
}else{
printf("Welcome Ms/Mrs %s \n",name);
}
}else{
printf("Nothing to see here! \n");
}
return 0;
}

You missed & in the line:
scanf(" %d",age);

Related

what is error : incompatible type for argument of fprintf? [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 last year.
Improve this question
So I'm trying to write a code for university students. The algorithm is that they enter their name and choose the subject they want to know the grade of. Then the program should read the file and show them the grade. Here is the code :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main()
{
float math_grade, biology_grade, art_grade;
char math[5];
char Biology[10];
char name[20];
char subject[20];
FILE *info;
printf("Enter name : \n");
scanf("%s", name);
info = fopen("c:\\New folder\\info.txt", "r");
fscanf(info, "%s", name);
printf("Choose subject : \n", subject);
scanf("%s", subject);
if (subject == math)
{
fscanf(info, "%f", math_grade);
fprintf("Grade = %f", math_grade);
else if (subject == Biology)
fscanf(info, "%f", biology_grade);
fprintf("Grade = %f", biology_grade);
else
fscanf(info,"%f", art_grade);
fprintf("Grade = %f", art_grade);
}
}
'''
The problem is that i encounter the error : incompatible type for argument 2 of fprintf.
Anyone knows what am I doing wrong and how should i fix this? p.s : ignore the other bugs. I didn't debug the code yet.
fprintf("Grade = %f", math_grade);
The fprintf function expects a FILE * as its first argument. This is why you're getting an error. If you want to output to the console, use printf instead.

How do i fix my output in my C program using format specifiers? the program is running but the output is not as expected [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 5 years ago.
Improve this question
#include <stdio.h>
void main() {
int age;
char name;
clrscr();
printf("NAME: ");
scanf("%s", &name);
printf("AGE: ");
scanf("%i", &age);
printf("\n\n\t\t Hi, %s ! You are %i years old."name, age);
getch();
}
I'm having trouble with my output im expecting: Hi, Marvin! You are 16 years old. but the output is always like this: Hi, e error Abnormal program termination! You are 15 years old. The format specifier for name is not working but the age is working what should I do?
First thing I doubt that your code compiles, since in printf() you missed ',' :
printf("\n\n\t\t Hi, %s ! You are %i years old.", name,age);
Second, change following:
char name;
clrscr();
printf("NAME: ");
scanf("%s",&name);
to
char name[100];
clrscr();
printf("NAME: ");
scanf("%s",name);

Program crashes when prompting user for input [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
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.
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.
Improve this question
I made a simple little program and it crashes before it gets to the part of the if statement.
#include <stdio.h>
int main()
{
char name[100];
printf("What is your name?");
scanf("%s", name);
printf("Hello, %s\n",name);
printf("You are trapped in a tunnel. Go through the right doors to get out\n");
int choice;
printf("Choose door 1 or door 2");
scanf("%d", choice);
if (choice == 1){
printf("This is the correct door");
}
else if (choice == 2){
printf("This is the wrong door");
}
else{
printf("Press 1 or 2");
}
return 0;
}
My program runs fine with no errors, it just crashes...
scanf("%d", &choice);
This is how it was meant to be. choice is a standalone int, so you need to pass its address to scanf to get correct results. What you do now is passing a value that is stored in choice to scanf, while it expects an address.
The C library function int scanf(const char *format, ...) reads
formatted input from stdin.
So it should be like this:
int choice;
printf("Choose door 1 or door 2");
scanf("%d", &choice); //& means the address-of
if (choice == 1){
printf("This is the correct door");
}
else if (choice == 2){
printf("This is the wrong door");
}
else{
printf("Press 1 or 2");
}
You miss & in scanf statement that why it doesn't working..
It should be
scanf("%d",&choice);

compare single char in C [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 8 years ago.
Improve this question
my code here
char temp;
AGAIN:
printf("Enter char: "); scanf("%c", &temp);
if (temp != 'A') goto AGAIN;
why did it go back to AGAIN even though my input is 'A' ?
If I put this block of code to main() function, it works correctly.
But if I do the same thing in the subrountine, it failed.
This is because of the \n character read by the sacnf is next call.
Try this
scanf(" %c", &temp);
^ A space before %c can eat up any number of white-spaces
But for me, its work!. Are you given any other label as AGAIN in your program?
#include<stdio.h>
//./a.out
int main()
{
char temp;
calfunc(temp);
}
char calfunc(char temp)
{
printf("Inside Function");
AGAIN:
printf("Enter char: "); scanf("%c", &temp);
if (temp != 'A')
goto AGAIN;
else
printf("Temp: %c\n",temp);
printf("Final: %c\n",temp);
return temp;
}
Result:
If type 'A', Temp:A Final:A
If type any other even 'a', Enter char: Enter char:. Then cursor places.

What are the bugs in my code? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to code a simple program in C asking your age and then display the message:
"You are X years old !"
I can't figure out the bugs in the following code:
#define <cini.h>
#define MY STRING "You are"
int main() {
int age;
printf("How old are you?\n");
if(scanf ("%d", age) != 1) {
printf ("Error message\n");
exit(1);
}
printf ("%s years old!\n", MY STRING, age);
return 0;
}
Thanks for help and happy Xmas !
you missed printing age:
printf ("You are %d years old!\n" age);
and scan value need &
if(scanf ("%d", &age) != 1)
you defined pre-processor with space, space are not valid here:
#define MY_STRING "You are"
You missed & before scanf argument.
if(scanf ("%d", &age) != 1)
^
|
Place & before age
Also change your macro MY STRING to MY_STRING. And also you missed a %d specifier in your last printf
Use :
printf ("%s %d years old!\n", MY_STRING, age);

Resources