-1

I am a beginner in C and I encountered problems when dealing with scanf, hoping for an explanation behind this

this is my code

#include <stdio.h>
#include <math.h>
#include <string.h>

int fav_charac() {
    char *string = ""; 
    printf("whats your fav letter?: ");
    scanf("%s", string);                         // the problem
    printf("your fav letter is %s\n", string);
}

int main() {
    fav_charac();
    return 0;
}

and this is the program running

whats your fav letter?: a

C:\Users\DELL\source\repos\hello C\x64\Debug\hello C.exe (process 15052) exited with code -1073741819.
Press any key to close this window . . .

as you can see it exited right after an input is made, why?

1

3 Answers 3

4

In this declaration

char *string = "";

there is declared a pointer to an empty string literal that is represented in memory as one byte with the terminating zero value { '\0' }.

And in this call

scanf("%s", string); 

you are trying to change the string literal pointed to by the pointer string. Any attempt to change a string literal results in undefined behavior.

From the C Standard (6.4.5 String literals)

7 It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.

By this reason it is always better to declare pointers to string literals with the qualifier const.

const char *string = "";

By the way in C++ opposite to C string literals have types of constant character arrays. So for such a declaration in C++

char *string = "";

the C++ compiler will issue a message.

If you want to enter more than one character then declare a character array as for example

char string[10] = "";

and use the following call of scanf

scanf( "%9s", string );

If you want to enter only one character then declare an object of the type char as for example

char c = '\0';

and use the following call of scanf

scanf( " %c", &c );
1

The scanf will attempt to write to string, which is .rodata. That means, the Data in string can only be read from but not written to. Use char string[64]; or something similar. This will place the variable into .data. Also limit user input in scanf with scanf("%63s", string); so that scanf won't write out of bounds.

Full example:

char string[64];
scanf("%63s", string);
8
  • thank you so much for the explanation, I thought char *something is a good practice thanks for correcting me. the code worked too. so rodata is immutable and data is mutable, from what I learnt const also creates immutable data, does this mean *something functions same as const?
    – Electron X
    Apr 3, 2022 at 10:58
  • 1
    No its specific to strings. If you are initilizing a variable with a string-value, the string will be immutable. char * str = "some_string"; and char str2[] = "another_string"; will both be immutable. This is just how the compiler does things. The * means it is a pointer to a memory location. (arrays and pointers work pretty much the same that is why str[] and *str are basically interchangable). The important thing is the [length] which will cause the compiler to make the data mutable.
    – cediwelli
    Apr 3, 2022 at 11:02
  • 1
    I recommend you look a bit into pointers, they are confusing but very powerful. When you understand the inner functionality of pointers, you wont think in data-types anymore but rather in bytes and this is pretty fun.
    – cediwelli
    Apr 3, 2022 at 11:04
  • Off-by-one "char string[64]; ... scanf("%64s", string);" should be scanf("%63s", string);. Apr 3, 2022 at 11:14
  • @Cediwelli: Your 2nd example char str2[] = "another_string";, actually is mutable. It's initialized to another_string, but within its bounds can be altered. If you want str2 to be immutable, you've to write char const str2[] = … (or const char …).
    – datenwolf
    Apr 3, 2022 at 11:14
1

try this:


 char string[50];  // [ the max of letters you want in your string ] 
    printf("whats your fav letter?: ");
    scanf("%s", string); 
1
  • scanf("%s", string); lacks a width limit and is worse than gets(). Apr 3, 2022 at 11:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.