-2

What is the difference in the following? ( C )

char x[100] ;
char y[] = ""; // no space between the double quotations
char z[] = " "; // space between the double quotations

if the user entered an input for example "test" in the array y , does it's size changes to 5 ?

char y[] ="";
gets(y); // user entered "test"

and if user entered an input larger than 100 in the array x , does it's size changes ?

char x[100] ;
gets(x); // user entered an input larger than 100

and why this code works : ( if user entered "test" it will print "test" )

#include<stdio.h>
#include<string.h>
int main(){
    char name[] = " " ; // space between the double quotations 
    gets(name);
    for(int i=0 ; i< strlen(name) ; i++) {
        printf("%c",name[i]);
    }
    return 0 ;
}

and this one doesn't ? ( this one prints strange symbols ) ( if user entered "test" it will print "t" and a smiley symbol )

#include<stdio.h>
#include<string.h>
int main(){
    char name[] = "" ; // no space between the double quotations 
    gets(name);
    for(int i=0 ; i< strlen(name) ; i++) {
        printf("%c",name[i]);
    }
    return 0 ;
}

and this one makes me crazy , it worked without a loop , even with no space between double quotations

#include<stdio.h>
#include<string.h>
int main(){
    char name[] = "" ; // no space between the double quotations
    gets(name);
    printf("%c",name[0]);
    printf("%c",name[1]);
    printf("%c",name[2]);
    printf("%c",name[3]);
    return 0 ;
}

and this one works using ( puts ) even with no space between double quotations :

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

int main(){
    char name[] = "" ;
    gets(name);
    puts(name);
    return 0 ;
}
Saidar
  • 17
  • 1
  • 1
  • 9

2 Answers2

0

No, the array is not resized.

The reason why it works, is because the behavior is undefined and one of the things that can happen is that it works.

When you do that you write beyond the bounds of the array, depending on what data is at the location of the illegal write different things would happen.

Never use gets() because it does not prevent buffer overflowing which is what your program is doing, you should use the fgets() function which takes the size of the array as a parameter to prevent writing more than that size bytes into the array

fgets(array, sizeOfTheArray, stdin);

would prevent the problem and the code would not "work" as you believe it does.

Also, the string in c wouldn't have it's size stored anywhere, so calling strlen() like you do, is bad, this

for(int i=0 ; i< strlen(name) ; i++)
               /*  ^ do not do this */

leads to bad performance, you can store the value like

size_t length = strlen(name);
for(int i = 0 ; i < length ; i++)

or, use the fact that c strings are sequences of non-nul bytes followed by a nul byte, like this

for(int i = 0 ; name[i] != '\0' ; i++)
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • thanks a lot for helping :) , the undefined behavior info was very useful ... and i liked the idea of this loop for(int i = 0 ; name[i] != '\0' ; i++) – Saidar Apr 07 '15 at 12:33
0

if the user entered an input for example "test" in the array y , does it's size changes to 5 ?

No. The size of an array is constant.(1 in case of y)

and if user entered an input larger than 100 in the array x , does it's size changes ?

No. The size of an array is constant.(100 in case of x)

In both the cases, the code triggers Undefined Behavior(which means that anything can happen) as extra characters of the input are written in invalid locations. This is why the gets() function is dangerous too! The reason being that gets() does not prevent buffer overflows.

and why this code works : ( if user entered "test" it will print "test" )

Anything can happen if the input is more than one character long. Again, because of Undefined Behavior

it worked without a loop , even with no space between double quotations

UB

and this one works using ( puts ) even with no space between double quotations :

UB

Community
  • 1
  • 1
Spikatrix
  • 20,225
  • 7
  • 37
  • 83