0

I think the reason it keeps crashing is related to my scan statement, but I have tried multiple different was.

#include <stdio.h>
int main()
{
  char str[100], i;

  printf("Enter a string: ");
  scanf_s("%s", &str);

  for (i = 0; str[i] != '\0'; ++i);

  printf("Length of string: %d", i);
  return 0;
}
Jens
  • 69,818
  • 15
  • 125
  • 179
Crowe
  • 21
  • 4
  • what is `scanf_s()`? It's not in the man pages. – SIGSTACKFAULT Nov 22 '16 at 18:21
  • 1
    `scanf_s("%s", &str);` should be `scanf_s("%s", str);`. i.e. drop the `&`. – P.P Nov 22 '16 at 18:21
  • 2
    @Blacksilver: It is part of the standard. Albeit optional. Man-pages are not authoritative. – too honest for this site Nov 22 '16 at 18:22
  • @Blacksilver scanf_s is a safer form of scan and if i drop the & the program still crashes. – Crowe Nov 22 '16 at 18:24
  • Read the documentation of functions you use! Your code invokes undefined behaviour. Said that: use the normal `scanf` and provide the max. field-width. – too honest for this site Nov 22 '16 at 18:24
  • if i use scan in visual studio the program will not run. I have to use scanf_s @Olaf – Crowe Nov 22 '16 at 18:27
  • Thank you @user3121023 that fixed it. – Crowe Nov 22 '16 at 18:28
  • @Crowe: You will get a warning, which is against the standard. You can silence this (read the help! Better use a C standard compliant compiler, MSVC has even more severe issues. Anyway, call functions with the correct arguments! A good compiler should already warn about the wrong call. To repeat: read the documentation. – too honest for this site Nov 22 '16 at 18:39
  • @Olaf `scanf_s` requires a size argument for each `%c` and `%s` format, to follow each usual address argument. IMO that makes it even more tricky than the standard `scanf` function, and so not at all "safer". – Weather Vane Nov 22 '16 at 18:41
  • @WeatherVane: It also requires one for the `[` conversion specifier. And that's why I recommended to use a compliant compiler, as `scanf` has not been removed from the standard. `scanf_s` has advantages for RLE encoded character sets like UTF16 and UTF8, though. – too honest for this site Nov 22 '16 at 18:49
  • 1
    @Olaf did I say otherwise? I would prefer to read an input with `fgets` and then the next processing choices are wider - `sscanf` or `strtok` or examing the input *ad hoc*. – Weather Vane Nov 22 '16 at 18:56

3 Answers3

0

There are safer and more robust ways to do what you are trying to do, but i assume this is a learning experience so try this:

#include <stdio.h>

# define MAX_CHARS   100

int main( void )
{
   char str[MAX_CHARS];
   int i;    /* i should be an integer */


   printf("Enter a string (less than %d characters) : ", MAX_CHARS );

   /* scanf( "%s", str );  */   /* do not use &str, str is already a pointer */

   fgets(str, MAX_CHARS - 1, stdin );

   for ( i = 0; (i < MAX_CHARS) && ( str[i] != '\0' ); ++i ) ;

   printf("Length of string: %d\n", i);

   return 0;
}

using scanf("%s", str); will only pick up the characters prior to a space, so if your string is hello world then the contents of str will only be hello.

and if using scanf then if you enter more than MAX_CHARS, or in your case 100 characters, then you will have an overflow which is bad. Using fgets is safer, but will include the \n character in str.

normally you hit the enter key to signal that is the end of input, however you can hit control-d to signal end of file, and this will work with fgets and the contents of str will not have the \n character at the end. On my system I have to hit ctrl-d twice.

ron
  • 967
  • 6
  • 23
0

There is another way of solving this problem use gets(str); instead of scanf_s("%s", &str);

and use strlen(str) instead of loop; But you need to add a header file for it which is <string.h>

#include <stdio.h>
#include<string.h>
int main()
{
  char str[100];
  int  i;

  printf("Enter a string: ");
  gets(str);

  i=strlen(str);

  printf("Length of string: %d", i);
  return 0;
}
  • Noooooooooo... Don't use gets. the man page literally starts with `Never use this function; see BUGS`. It's only use is codegolf. – SIGSTACKFAULT Nov 24 '16 at 14:24
-1

1) scanf_s() is not a thing. Wait, it is? Oh, It's a visual studio thing. Okay.
2) you passed &str instead of str (this is why you crashed)

int main(){
    char str[100];
    int i;

    printf("Enter a string: ");
    scanf("%s", str);

    for (i = 0; str[i] != '\0'; ++i){};

    printf("Length of string: %d\n", i);
    return 0;
}
SIGSTACKFAULT
  • 919
  • 1
  • 12
  • 31
  • 2
    Hang on - [`scanf_s` *is* a thing](http://stackoverflow.com/questions/21434735/difference-between-scanf-and-scanf-s). – Jongware Nov 22 '16 at 18:27
  • 3
    I really don't appreciate how condescending you are. scanf_s is a thing, I do the the & to read in the string. I'm using visual studio. A Microsoft based IDE which uses scanf_s as a safer form of scan. – Crowe Nov 22 '16 at 18:33
  • I didn't know it was a thing until *just now* when @RadLexus told me. Sorry. – SIGSTACKFAULT Nov 22 '16 at 18:33
  • 1
    @Blacksilver - So, I feel, you don't qualify to write this answer. Please delete your answer! – Am_I_Helpful Nov 22 '16 at 18:36
  • It works now. all i did was scanf_s("%s", &str, 100) – Crowe Nov 22 '16 at 18:37
  • 1
    Read the standard! While man-pages are nice to use, they don't define what is standard and what not. – too honest for this site Nov 22 '16 at 18:41
  • @Am_I_Helpful: You are free to downvote such answers, but please don't press writers to delete something (maybe you could have phrased this a bit more friendly). And yes, I agree this is not a good answer, especially as it now includes a new question. – too honest for this site Nov 22 '16 at 18:44
  • I've edited my question to try to be less condescending. Sorry! – SIGSTACKFAULT Nov 24 '16 at 14:28