-6

I have a problem related with this code.

 #include <stdio.h>
 void main() 
 {
        char array[0];
        scanf("%s", array);
        printf("%s", array);
        return;
 } 

when I enter data in input-field it get store in array and output is obtained but it's size is 0.

and another thing happens is that when I enter suppose 'a' 12-times then somehow controls reaches to scanf() for taking input and again on entering 'a' 12-times control again reaches to scanf() and this goes on until I enter less than 12 character.

if on entering greater than 12 characters the program crashes.

Why this happens?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Aman Warudkar
  • 125
  • 1
  • 1
  • 12

1 Answers1

5

A statement like

 char array[0];

is invalid code as per ISO C. It's a pure constraints violation. The size has to be greater than 0.

Quoting C11, chapter §6.7.6.2 (emphasis mine)

In addition to optional type qualifiers and the keyword static, the [ and ] may delimit an expression or *. If they delimit an expression (which specifies the size of an array), the expression shall have an integer type. If the expression is a constant expression, it shall have a value greater than zero. [...]

That said,

  • void main() is not a conforming signature for hosted environment. It needs to be int main(void) at least to be standard compliant.

  • while using scanf() and family, always

    • limit the length of input by providing maximum field width, something like

      char input [10] = {0};
      scanf("%9s", input);
      
    • check the return value to ensure the success of the call.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 2
    @AmanWarudkar what is this? random experiment? Read the basics and know beforehand what you try to achieve. Just because you can type in anything, does not make it a valid code, anyways. – Sourav Ghosh Apr 24 '17 at 09:16
  • ok then on setting size of array to 1 the above picture goes on entering character a 13 times. – Aman Warudkar Apr 24 '17 at 09:19
  • @AmanWarudkar You need to provide sufficient buffer to hold the input you want to scan, e. g. `char array[32];`. Additionally, you *absolutely* should prevent scanf from writing beyound this buffer: `scanf("%31s", array);`. – Aconcagua Apr 24 '17 at 09:19
  • @AmanWarudkar read about [undefined behavior](https://en.wikipedia.org/wiki/Undefined_behavior), you have one in your code. – Sourav Ghosh Apr 24 '17 at 09:29
  • Ok thanks BROTHER now i understand. what's going on with this code. – Aman Warudkar Apr 24 '17 at 09:32
  • Please create an MCVE which only shows the remaining problems you have. I.e. incorporate answers and comments, then elaborate on the 12/13 character issue. – Yunnosch Apr 24 '17 at 09:36
  • @SouravGhosh: If only beginners would read the basics before asking ... (sidenote: OP did not change the dimension, but the length of the array in the (only) dimension). – too honest for this site Apr 24 '17 at 09:37
  • @AmanWarudkar and changing the array size to 1 in this case also invokes UB, as for a 1-element char array, %s will infer out of bound memory access leading to UB – Sourav Ghosh Apr 24 '17 at 09:42
  • Hey i have find a way to fix it by using "SEARCH SETS". scanf(%[^\n], &array); – Aman Warudkar Apr 24 '17 at 10:00
  • 1
    @AmanWarudkar that's even worse....don't do that. Learn C by careful study and practice, not by random experiment. – Sourav Ghosh Apr 24 '17 at 10:04