Segmentation fault

I got the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  //Prova.cpp

#  inclue <stdio.h>

main(){
	char carattere;
	char num;
	int  n;
	printf("Inserire un carattere a scelta e premere invio: ");
	while (scanf("%c", &carattere != "\r")){
		printf("Il carattere inserito è %c\n", carattere);
		n++;
		printf("Inserire un carattere a scelta e premere invio: ");
	}
	num=n;
	printf("Il numero di caratteri inseriti è %c\n",num);
	return 0;	
}


The code has copied by a manual.
after compiled with g++ I start it, but this happens:

root@aw003:/home/Abe/Desktop# ./prova
Inserire un carattere a scelta e premere invio: j
Segmentation fault (core dumped)

Why?

Thanks a lot

Abe Wayer

Last edited on
scanf("%c", &carattere != "\r") ← this is plainly wrong
&carattere != "\r" will always evaluate to false, which will be converted to null pointer. Then scanf dereferences it and you got a crash.

Perharps you meant while (scanf("%c", &carattere) != "\r")?
But this is wrong too: scanf returns number of assigment made.

State what do you want to achieve and we might help you.
I translate the text in English so maybe you can help me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//Prova.cpp

#  inclue <stdio.h>

main(){
	char carattere;
	char num;
	int  n;
	printf("Enter a character of your choice and press enter: ");
	while (scanf("%c", &carattere != "\r")){
		printf("The character inserted is %c\n", carattere);
		n++;
		printf("Enter a character of your choice and press enter:  ");
	}
	num=n;
	printf("The number of characters insert is %c\n",num);
	return 0;	
}
Meaning of line 10 still not clear. I repeat my question: What do you want to achieve?
I imagine the author of the the program i've copied by the book is waiting for return.

The program has to ask a character end print the character after press return.
It asks again for a character and print the number of character pressed.
you probably want following:
1
2
3
4
5
6
7
8
9
scanf("%c", &carattere);
//Note single quotes (for denoting a character, not string literal)
//And \n which denotes endline instead of useless \r.
while(carattere != '\n') {
    printf("The character inserted is %c\n", carattere);
    ++n;
    printf("Enter a character of your choice and press enter:  ");
    scanf("%c", &carattere);
}


Also: it is #include , not #inclue ,
main() should return int
You probably wasnt to replace lines 15-16 by following:
printf("The number of characters insert is %D\n", n);
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#  include <stdio.h>

main(){
	char carattere;
	char num;
	int  n;
	printf("Enter a character of your choice and press enter: ");
	while(carattere != '\n') {
	printf("The character inserted is %c\n", carattere);
	++n;
	printf("Enter a character of your choice and press enter:  ");
	
	}
	printf("The number of characters insert is %D\n", n);
	return 0;	
}



Like this i got an infinite while cycle
Last edited on
You forgot scanf on your line 12:
scanf("%c", &carattere);
Topic archived. No new replies allowed.