Question

I have written the following code in C programming (using Turbo c++).

#include<stdio.h>

struct test
{
   char fname[10];
   char age[2];
   char lname[10];
}s1[10];

int main()
{
   int i;
   for(i=0;i<3;i++)
   {
     printf("enter the first name : ");
     scanf("%s",s1[i].fname);
     printf("enter the last name : ");
     scanf("%s",s1[i].lname);
     printf("enter the age : ");
     scanf("%s",s1[i].age);
   }
   for(i=0;i<3;i++)
   {
     printf("\n %s %s age= %s",s1[i].fname,s1[i].lname,s1[i].age);
   }
getch();
return 0;
}

I have entered inputs as asked for. But the last name (lname) was found missing when the result printed.

Can you help me solve this unexpected behavior of structure. (Kindly neglect the value [10], age declared as string etc as I did it for my own purpose)

Kindly note: when I declared the age as ' int ' and used %d the lastname was displayed, but when all were declared character it is not working.

Was it helpful?

Solution

char age[2]; is way too small. If you input exactly 2 characters, scanf will write the termination string into lname[0] because in this specific case the memory between both arrays is contiguous.

Thus printf will see an empty array and the result will be the one you observe

OTHER TIPS

Change

char age[2];

to

char age[4];

Strings need additional space for the terminating null character.

Problem is, as others have pointed out, that you have a buffer overflow on scanf.

How to fix:

Solution is not to only increase buffer sizes. Solution is to prevent overflow altogether.

Increase buffer sizes:

char age[3+1]; /* 3 for digits, +1 for NUL char */

Specify maximum chars to read:

scanf("%3s", s1[i].age);

But since you read only strings, I would recommend using fgets instead:

fgets( s1[i].age, 4, stdin);

Do this for all fields. And even better: use constants for buffer sizes.

the new line character will be inserted as second input for scanf(); i.e: your input for s1 and s2 are: test1 test2 then s1= "test1" and s2="\n"

so in your case you should use gets() function instead of scanf(). and your string size will be number of characters you want to input plus 1 because every string will add additional terminating character('\0') at the end. i.e: if you want to input 5 character in char s1[SIZE]; then SIZE must be 6. follow these instructions. hope it will help for this issue to fix up. or you can run this:

#include<stdio.h>

struct test
{
    char fname[10];
    char age[3]; // for maximum 2 digits
    char lname[10];
} s1[10];

int main()
{
    int i;
    for(i=0; i<3; i++)
    {
        printf("enter the first name : ");
        gets(s1[i].fname);
        printf("enter the last name : ");
        gets(s1[i].lname);
        printf("enter the age : ");
        gets(s1[i].age);
    }
    for(i=0; i<3; i++)
    {
        printf("\n %s %s age= %s",s1[i].fname,s1[i].lname,s1[i].age);
    }
    getch();
    return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top