I am getting runtime error (SIGSEGV) in a c program [closed] - c

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 8 years ago.
Improve this question
Why I am getting runtime error (SIGSEGV) on the following code:
#include<stdio.h>
#include<string.h>
int main()
{
int t_line,count[10000],i;
scanf("%d",&t_line);
for(i=1;i<=t_line;i++)
{
fflush(stdin);
gets(t);
count[i]=(int)t[0]+(int)t[1]+(int)t[2];
}
for(i=1;i<=t_line;i++)
printf("%d\n",count[i]);
return 0;
}
I have also tried to solve this problem by initialized all the elements of array.

I am wondering how the code compiled, without declaration of the variable t. But, still the only missing elemnt was: char t[your choice of size];. Apart from that
#include<stdio.h>
//#include<string.h> No need of this header,a s you are not using any string functions
int main()
{
int t_line,count[10000],i;
char t[64];//you need to declare the variable before using it
scanf("%d",&t_line);
//Its safer if you check this
if(t_line >= 10000)//if you use 0 and < t_line in for loop below then change the condition to: if(t_line > 10000)
{
printf("ERROR: Limit exceeded. Not enough memory.\n");
return 1;//or you could use exit(1); and #include <stdlib.h>
}
for(i=1;i<=t_line;i++)//suggested: for(i=0;i<t_line;i++)
{
//fflush(stdin);
//gets(t);
char *rc = fgets(t, sizeof(t), stdin);
if(rc != NULL)
{ t[strlen(t) - 1] = '\0';\\because fgets gets the \n into the string too. This line makes fgets similar to gets, improving safety from overflow.
}
else
{
// handle fgets failed error
}
count[i]=(int)t[0]+(int)t[1]+(int)t[2];
}
for(i=1;i<=t_line;i++)//suggested: for(i=0;i<t_line;i++)
printf("%d\n",count[i]);
return 0;
}
Find the solution and suggested changes inline as code comments.
In C, its better to use indexes starting from 0, unless there is a specific requirement to use other values.

Related

C program compiles but console screen gives a black screen [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
I'm new in C coding and I just wrote a C program, but when I tried to run the code, it gave a black screen. What do I need to change in my code to fix this problem?
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int islemler(){
int x,y,z,c,s0,s1,s2,s3;
printf("Merhaba, matematik islemlerine baslamak icinn lutfen ilk sayiyi giriniz...\n"); /*wants to first number*/
scanf("%d", &x);
printf("Simdi ise ikinci sayiyi giriniz...\n"); /*wants to second number*/
scanf("%d", &y);
printf("Son olarak 3. sayiyi giriniz...\n"); /*wants to thirth number*/
scanf("%d", &z);
printf("Yapacaginiz islemi secmek icin lutfen bir sayi seciniz...\n 1-----Toplama\n 2-----Cikarma\n 3-----Carpma\n 4-----Bolme"); /*Prompts the user to select an action. 1- addition 2- subtraction 3- multiplication 4- division*/
scanf("%d", &c);
s0= x+y+z;
s1= x-y-z;
s2= x*y*z;
s3= x/y/z;
if (c==1){
printf("Sonucunuz %d", s0); /*give answers*/
}
if (c==2){
printf("Sonucunuz %d", s1);
}
if (c==3){
printf("Sonucunuz %d", s2);
}
if (c==4){
printf("Sonucunuz %d", s3);
}
}
int main(){
int islemler();
return 0;
}
int main(){
int islemler();
return 0;
}
You basically declare islemler and then return. It does nothing. I think you wanted to call islemler and not declare it. Remove the int from that. Make it islemler():
int main() {
islemler();
return 0;
}
In your code, you're declaring it. Which is basically like letting the compiler know that the function exists somewhere and not to worry. And it also helps to catch incorrect calls to functions. They are only required in certain situations when the compiler has no way to know that a function exist. If it occurs later on or if it is in a different compilation unit, it's a good practice to declare the prototype like you have done. It does not call the function. And is also completely unnecessary here.
Also, the islemler function should return an int but doesn't return anything. You should get a warning about this. Do not ignore warnings.

How do I resolve this int overflow? [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 4 years ago.
Improve this question
int frequency(string note)
{
int i;
float f;
int n=0;
float octave= note[strlen(note)-1];
if(strlen(note)==3)
{
if(note[1]=='#')
{
n+=1;
}
else if(note[1]=='b')
{
n-=1;
}
}
if(note[0]=='B')
{
n+=2;
}
else if(note[0]=='C')
{
n-=9;
}
else if(note[0]=='D')
{
n-=7;
}
else if(note[0]=='E')
{
n-=5;
}
else if(note[0]=='F')
{
n-=4;
}
else if(note[0]=='G')
{
n-=2;
}
n+=(octave-4.0)*12.0;
float p= n/12.0;
f=(int)(round(pow(2.0,p)*440.0));
return f;
}
So basically whenever I run this code I get an error stating "runtime error: value 7.3641e+16 is outside the range of representable values of type 'int'"
Then the value returned is just-2147483648. I've looked it up online and haven't found an answer that helps me with my code. Also this was made in the cs50 IDE so there are a bunch of commands and things that are imported. My program compiles properly and it can run so how do I fix this?
Frequencies of notes of the chromatic scale are not integers in the first place.
If you are passed a two character string like Cb, you get a stupidly huge octive with a frequency way too high.

Vigenere cipher in cs50 Segfau [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
I am not experienced coder.Please dont judge for bad styling.I've run into an issue with the vingenere cipher
It keeps giving me segfau
#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>**
int main(int argc,string argv[])
{
while (argc!=2)
{
printf("Incorrect input\n");
return 1;
}
printf("plaintext:");
string plaintext=get_string();
string num=argv[1];
int keylength=strlen(num);
for (int j=0, n=strlen(plaintext); j<n; j++)
{
char letter=plaintext[j];
if (isalpha(letter))
{
This is the reason of Segmantation Fault error.
If i don't check if num is upper or lower case,
code runs normally.
if (isupper(letter))
{
if (isupper(num))
{
printf ("%c",((((letter-65)+(num[j%keylength]-65))%26)+65));
}
else
{
printf ("%c",((((letter-65)+(num[j%keylength]-97))%26)+65));
}
}
else
{
if (isupper(num))
{
printf ("%c",((((letter-97)+(num[j%keylength]-65))%26)+97));
}
else
{
printf ("%c",((((letter-97)+(num[j%keylength]-97))%26)+97));
}
}
}
else
{
printf ("%c",letter);
}
}
printf("\n");
return 0;
}
Most likely, you meant
if (isupper(num[j % keylength))
Passing a string (really a char *) to isupper() is an undefined operation, and may produce weird effects depending on how isupper() is implemented. The GNU implementation ---
#define __ctype_lookup(__c) ((__ctype_ptr__+sizeof(""[__c]))[(int)(__c)])
#define isupper(__c) ((__ctype_lookup(__c)&(_U|_L))==_U)
--- uses a lookup table with all sorts of fun pointer math and casting under the hood, so it likely allows a char * to be passed without the compiler complaining, but it's definitely not going to produce good results. Change your if statement, and it should work better.

How to build a function like ask users enter yes or no in C console program [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 6 years ago.
Improve this question
I try to make a C console program ask user enter Y (means yes) or N (means no) by using if and while,the user only can enter Y or N,otherwise the program will tell the user input error,no matter they input what the characters, how many strings.The fuction just like terminal ask user “Are you sure?Input Y or N".
But my program will give me more than one error feedback when i test.I relly want it just give me one feedback.Hope you can help me perfect my progrem.
Here are my code:
#include <stdio.h>
int main(void)
{
char yon = '\0';
do
{
if (yon != '\0')
printf("\nSorry,Please try again.\n");
printf("\nEnter Y/N?:");
}
while ((yon = getchar()) != 'y'&&yon != 'Y'&&yon != 'n'&&yon != 'N');
return 0;
}
My English is not good,If you do not know what i mean,Please tell me to edit.
Thanks.
I just now edit my code,it runs very nice without bugs.
Here are my code:
#include <stdio.h>
int main(void)
{
char yon = '\0';
do
{
fflush(stdin);
if (yon != '\0')
printf("\nSorry,Please try again.\n");
printf("\nEnter Y/N?:");
scanf("%1s",&yon);
}
while ( yon!= 'y'&&yon != 'Y'&&yon != 'n'&&yon != 'N');
return 0;
}
Thanks.

Compiler doesn't show any errors or warnings but the program doesn't work [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 7 years ago.
Improve this question
I tried to build and run the following program, but it breaks down executing. I thought maybe I made a mistake but 0 errors and 0 warnings were shown.
After researching such behavior on stackoverflow, I mostly saw some misplaced semicolons or forgotten address-operators, which I do not see in this source code or am I overlooking something?
Could some C or GCC Guru tell me what is wrong and why?
Operating system is Windows 7, and compiler had enabled:
-pedantic -w -Wextra -Wall -ansi
Here is the source code:
#include <stdio.h>
#include <string.h>
char *split(char * wort, char c)
{
int i = 0;
while (wort[i] != c && wort[i] != '\0') {
++i;
}
if (wort[i] == c) {
wort[i] = '\0';
return &wort[i+1];
} else {
return NULL;
}
}
int main()
{
char *in = "Some text here";
char *rest;
rest = split(in,' ');
if (rest == NULL) {
printf("\nString could not be devided!");
return 1;
}
printf("\nErster Teil: ");
puts(in);
printf("\nRest: ");
puts(rest);
return 0;
}
The expected behavior is that the string "Some text here" is split at its first space ' ' and the expected output would be:
Erster Teil: Some
Rest: text here
You are modifying a string literal, that's undefined behavior. Change this
char* in = "Some text here";
to
char in[] = "Some text here";
This makes in an array and initializes it with "Some text here". You should use const to prevent accidentally having this bug when you define a pointer to a string literal.

Resources