Monday 15 April 2013

arrays - fgets and strlen in c -


  contains # lt; Stdio.h & gt; #to & lt include, stdlib.h & gt; #to & lt include, string.h & gt; Main () {four letters [5] = {'\ 0'}; Printf ("initial length = pass% ld \ n", strlen (str)); Input (STR); Printf ("received string =% s \ n", str); Printf ("After receiving the input length (function calling) =% ld \ n", size (str) ()); } Input (four * buffer) {puts ("Enter something:"); Printf ("= = ld \ n", initial length after strlen (buffer)); If (fgets (buffer, size (buffer), stdin) == faucet) return -1; Else {printf ("After the input received the length (function is called) =% ld \ n", strollon (buffer)); Return 0; }}   

Output 1
  = Enter the initial length = some before entering: = 0 after receiving the initial length input after halo Length (function is called) = 6 received string = Halo input after receiving length = 5   

output 2
  = before passing Initial length = 0 Write something: after the initial length after receiving input = 0 = length after receiving the input = 5   

Why is it printing different lengths of input?

  1. Output 1 & amp; 2 Why is the initial length 6 when I only allocated the space for 5 characters?
  2. How much is the length of the string before going into both output 1 and output 2?
  3. In output 2, when I have allocated only less space, then after the "input received length (function called) = 7"?
    "post-text" itemprop = "text">

    1) Output 1 & amp; 2 Why is the initial length 6 when I only allocate space for 5 characters?

    Your first strlen (str) call is not really defined as you have declared four letters [5] but in this Nothing applied. So who knows the content is the fact that strlen (str) returns 6 only means that because there are 6 non-zero characters in the memory, <0.01 Before confronting the first 0. / P>

    2) Before passing and why is the length of the string different after going into both output 2 and output 2?

    The memory content length of 6 for random, you loaded into some string buffer and zero ended it. So the length has changed somewhat in reality

    3) In the output 2 "Length after the input (called function) = 7" when I allocate less space only?

    Because you actually override your assigned space with a long string (length 7). You were lucky that the program did not crash in that situation.

    When you declare buffer in C, such as:

      four letters [5];   

    It all tells the compiler that you are reserving 5 bytes of space to do something and it gets you to use that location and only the place will be Str . It is not necessary that anything to start in buffer does not start until you put anything there, and it does not prevent you from writing more than what you declare.

    Note that str [5] is not large enough to catch "Hello" because the c in the wire is zero-eliminated. So you need a character buffer of size N + 1 to hold a string of size N. When you remove overflow buffers in C, your results will be irregular and unpredictable.

    No comments:

    Post a Comment