Segmentation fault(core dumped) in C after running [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 3 months ago.
Improve this question
I am a beginner to C language and I wrote this code
#include <stdio.h>
int main() {
int day = 35;
if (day>31)
printf('Please enter a valid date');
else if (day%7==1)
printf('Monday');
else if (day%7==2)
printf('Tuesday');
else if (day%7==3)
printf('Wednesday');
else if (day%7==4)
printf('Thursday');
else if (day%7==5)
printf('Friday');
else if (day%7==6)
printf('Saturday');
else
printf('Sunday');
return 0;
}
I ran this code on Replit and it is showing Segmentation error(core dumped).
enter image description here
Plz help me to solve this problem and explain about my mistake
I tried the above code on replit and I expected to get my result as-Please enter a valid date
But I got segmentation error(core dumped)

'' are character literals; "" are string literals. You cannot have a multi-length character literal. Here is working code:
int day = 35;
if (day > 31)
printf("Please enter a valid date");
else if (day % 7 == 1)
printf("Monday");
else if (day % 7 == 2)
printf("Tuesday");
else if (day % 7 == 3)
printf("Wednesday");
else if (day % 7 == 4)
printf("Thursday");
else if (day % 7 == 5)
printf("Friday");
else if (day % 7 == 6)
printf("Saturday");
else
printf("Sunday");

Related

C program: if statement within switch statement [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 4 years ago.
Improve this question
I am trying to create a program that will switch between "modes". For example here are little snippet of the code:
int main()
{
int mode,input;
mode = 1;
for(;;)
{
scanf("%d", &input);
switch(input)
case 1:
if(mode = 1)
{
//statements go here;
mode = 2;
}
else
{
//statements go here;
mode = 1;
}
break;
}
}
So what I'm trying to do is get the program to switch between mode 1 and mode 2 by the input of the 1 button. However each time I press the number 1 key, it will only print the statements of mode 1 but won't switch to mode 2 and print out the statements for mode 2 if i press the number 1 button a second time. Is there something fundamentally wrong with my code?
*restriction: I must use the switch statements in the program.
To test the value of a variable you need double equals:
if(mode == 1)

Why does the computer ignore my program? [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
#include <stdio.h>
#include <conio.h>
void positive(int input, int t);
void negative(int input, int t);
void main(){
int input,t;
clrscr();
textcolor(YELLOW);
cprintf("ENTER YOUR NUMBER: ");
scanf("%i",&input);
cprintf("\nNUMBER IN WORD(S): ");
if (input < -9999 && input > 9999) // <------ THIS GUY
printf("INVALID ENTRY, PROGRAM TERMINATED"); // <-------- AND ALSO THIS THING
if(input == 0)
printf("Zero");
if(input < 0 && input >-10000){
negative(input,t);
}
if(input > 0 && input <10000){
positive(input,t);
}
getch();
}
This is the main function of my code, it has other functions but whenever a user types an integer either less that -9999 and greater that 9999 the print command is not appearing on the screen but the program is up and running it just ignores the code.
EXAMPLE:
Enter a Number: 123151 Your number in Word:
no answer.
What to fix here?
You need to replace following 'if' condition
if (input < -9999 && input > 9999)
with
if (input < -9999 || input > 9999)

Basic C program does not display anything [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 7 years ago.
Improve this question
What does my program not output anything. I have tried other similar versions of solutions for project euler problem 1. I don't need the answer I would just like to know why their is no output. After I compile with gcc and execute the file it seems like is freezes with no output. I have to ctrl-z to kill the program.
#include <stdio.h>
/* Project Euler Problem 1 */
int main()
{
int sum = 0;
int i = 0;
while (i <= 1000);
{
if (i % 3 == 0 || i % 5 == 0);
{
sum += i;
}
i++;
}
printf("%d\n", sum);
return 0;
}
You are closing the while without doing any instruction by putting a semi-colon:
while (i <= 1000);
You should drop the semicolon.
The same for the if instruction:
if (i % 3 == 0 || i % 5 == 0);

what is the fault with c here? [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
Please excuse me for asking this, because I know the code I'm gonna give you is wrong. Being a newbie I am not able to find the fault. Please help me correct the question and give a solution as well. Again I'm sorry to bother with this simple problem. Tomorrow is my exm in C so i'm kinda desperate. :(
Q: What will be the output of the program?
First let me show you how I find the code first:
#include<stdio.h>
int funct l(int n){
if (n>3)
return int funct(n-3)));
}
main() {
int n= 10;
printf("%d", funct l (n));
}
Then I thought i'd correct it. Then I cleaned up the code as far as i can. Then the code came to this:
#include<stdio.h>
int funct(int n){
if (n>3){
return funct(n-3);
}
}
main() {
int n= 10;
printf("%d", funct(n));
}
still it doesn't give proper answer (though I don't know what it'll show). It is either 1 or 2 and process returned 1 (0*1) is showing at the last line.
Please help me out!
Your funct function doesn't always return a value. This means that it could return anything. Try this:
int funct(int n) {
if (n > 3)
return funct(n - 3);
return n;
}
Here is the call stack when n = 10
funct(n = 10)
funct(n = 7)
funct(n = 4)
funct(n = 1)
return 1
return 1
return 1
return 1
Here is the call stack when n = 11
funct(n = 11)
funct(n = 8)
funct(n = 5)
funct(n = 2)
return 2
return 2
return 2
return 2

C code having trouble displaying the right time with a minute added to it [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 9 years ago.
Improve this question
this is the ques i was suppose to code...ive done the code but its not displaying the final time properly...any help would be usefull
Write a program that prompts a user to enter the hour, minute, and either AM
or PM. The program will add 1 minute to the time that has been entered, and
display the new time. This program need only to ask for the time once.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int mint, hour;
char tod;
printf("Enter the hour: ");
scanf("%d", &hour);
printf("Enter the minute: ");
scanf("%d", &mint);
printf("Enter AM or PM: ");
getchar;
scanf("%s", &tod);
if(mint ==59){
mint = 0;
}
if (hour == 12){
hour = 1;
if (tod='A'){
tod = 'P';
}
else
tod = 'A';
}
else
mint = mint + 1;
/* if (hour < 11){
if (mint < 59){
mint = mint + 1;
}
else
hour = hour + 1;
mint = 0;
}
if (hour == 11 && mint == 59){
hour = 12;
mint = 0;
if (tod[0]='A'){
tod[0]='P';
}
else
tod[0]='A';
}
if (hour == 12 && mint == 59){
hour = 1;
mint = 0;
}*/
printf("The new time is %d:%0d %c", hour, mint, tod);
}
When you compile, get into the good habit of adding the compiler flag -Wall to turn on all warnings, which will quickly show you the problem spots:
$ gcc -Wall test42.c
test42.c:23:19: warning: using the result of an assignment as a condition without parentheses [-Wparentheses]
if (tod='A'){
~~~^~~~
test42.c:23:19: note: place parentheses around the assignment to silence this warning
if (tod='A'){
^
( )
test42.c:23:19: note: use '==' to turn this assignment into an equality comparison
if (tod='A'){
^
==
test42.c:15:8: warning: expression result unused [-Wunused-value]
getchar;
^~~~~~~
2 warnings generated.
Take a look at the second note to the first warning.

Resources