Using the scanf() function

2019-03-04 04:18发布

I intend to modify each other letter of a particular string. But for the purposes of this program none of that occurs. So far I've grabbed a string from the user and stored it in userinput and intend to print it.

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

int main(void) {    
char userinput[256] ="";
printf("Enter somthing to change:\n");
scanf("%s", &userinput);
printf("%s\n", userinput);

int k = 2; // This is just here to do something every time k is even
int j = strlen(userinput);
for (int i = 0; i < j; i++) {

    if(k % 2 == 0) {
        printf("%s", userinput[i]);
        k++;
    }
    else {
        printf("%s", userinput[i]);
        k++;
    }
}

}

The strlen() function however does not work on the userinput. I figure this is because strlen() is supposed to take the address of the first char of a string and then iterate until reaching a null char but scanf doesn't actually create a null char. I couldn't figure out a way of adding the '\0' after the string without first knowing the length of the string.

How would I go about accessing the length of a stored character sequence if it's stored in an array?

标签: c scanf
4条回答
叼着烟拽天下
2楼-- · 2019-03-04 05:08

You're confused about types, as others have indicated. Using scanf and printf when you're confused about types is dangerous!

scanf("%s", &userinput);

The type of &userinput is char (*)[256], but scanf expects %s to correspond to a char *. Since the type expected and the type given differ and aren't required to have the same representation, the behaviour is undefined.

I figure this is because strlen is supposed to take the address of the first char of a string and then iterate until reaching a null char but scanf doesn't actually create a null char.

Wrong. scanf certainly does assign a null character, when you use the %s format specifier. That is, providing scanf doesn't encounter an error or EOF. On that note, you should probably check for errors from scanf:

if (scanf("%s", userinput) != 1) {
    /* Insert error handling here */
}

... as you should with all standard library functions in C.

k is pointless. Your loop already increments i at the same frequency as k.

strlen returns a size_t. Make sure you store return values in the right types. If a function returns size_t, then you store the return value in size_t. If a function returns int, then you store the return value in an int. Got it? Whatever the return type is, is whatever type you use to store the return type. Use the manual to find that out.

printf("%s", userinput[i]);

There's that type confusion again. userinput[i] is a char. %s tells printf to expect a char *. When the argument is invalid, the behaviour is undefined. That may cause your program to malfunction. Consider printf("%c", userinput[i]); or printf("%s", &userinput[i]);.

查看更多
Deceive 欺骗
3楼-- · 2019-03-04 05:09

Change this

scanf("%s", &userinput);

with

scanf("%s", userinput);

we have to use addresses for scanf:

  • If we will scan into an int a then we have to call scanf() with the address of a => &a
  • If we will scan into a double a then we have to call scanf() with the address of a => &a
  • But If we will scan data into with pointer (memory address) int *a; or char array char a[50]; then we have to call scanf() with the pointer or with the array without adding &

From the scanf() page

Depending on the format string, the function may expect a sequence of additional arguments, each containing a pointer to allocated storage where the interpretation of the extracted characters is stored with the appropriate type. There should be at least as many of these arguments as the number of values stored by the format specifiers. Additional arguments are ignored by the function. These arguments are expected to be pointers: to store the result of a scanf operation on a regular variable, its name should be preceded by the reference operator (&) (see example).

查看更多
Ridiculous、
4楼-- · 2019-03-04 05:15

This:

scanf("%s", &userinput);

should be:

scanf("%s", userinput);

The address of operator & is unrequired, and incorrect. Arrays decay to the address of their first element when passed to a function. scanf("%s") will append a null terminating character so it is unnecessary to explicitly insert one.

To prevent potential buffer overrun specify the maximum number of characters that scanf() should write to userinput. This should be one less than the size of userinput, leaving room for the terminating null character:

scanf("%255s", userinput);

The incorrect format specifier (which is undefined behaviour) is being used to print the characters of userinput: use %c not %s. This:

printf("%s", userinput[i]);

must be:

printf("%c", userinput[i]);
查看更多
该账号已被封号
5楼-- · 2019-03-04 05:19

Change

scanf("%s", &userinput);

to

scanf("%s", userinput);

The & operator is not required in the case of String capture. The scanf() automatically appends the null character('\0') to the end of the string so int j = strlen(userinput); should work.

If you still want to calculate the length without this function efficiently here is the link How to calculate the length of a string in C efficiently?

查看更多
登录 后发表回答