Umang
Umang

Reputation: 139

Unusual behaviour while printing strings in C

I am trying to input two strings in the same line with just a single space between the two and then print the same. But I do not get the desired output.

Here's the code for your reference:

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

typedef struct m
{
    char name[50];
    char phone[8];
}arr;

int main() {
arr a[100000];
int i, n, f;
char s[100];
char na[50];
char ph[8];
scanf("%d",&n);
for(i = 0; i < n; i++)
{
    scanf("%s",a[i].name);
    scanf("%s",a[i].phone);
}
for(i = 0; i < n; i++)
{
   printf("%s=%s\n",a[i].name,a[i].phone);
}
return 0;
}

Input:

3
sam 99912222
tom 11122222
harry 12299933

The output I get is:

sam=99912222tom
tom=11122222harry
harry=12299933

Upvotes: 0

Views: 82

Answers (2)

user3629249
user3629249

Reputation: 16540

to be able to use a value without having to hardcode that value into the scanf() the value must be stringified

The following proposed code

  1. cleanly compiles, except warns about not checking returnd value from calls to scanf()
  2. demonstrates the desired technique.

Note: it is a very poor programming technique to include header files those contents are not used.

Note: for flexibility it is best to separate the definition of a struct from a typedef for that struct.

Note: the OPs code contained several 'magic' numbers. Those 'magic' numbers have been eliminated by using #define to give each of them a meaningful name.

Strongly suggest using the variable length array feature of C, then eliminate:

#define MAX_A 100000

and modify:

arr a[ MAX_A ];

to be:

arr a[ n ];

and move that statement to after the call to scanf() that inputs 'n'

and now, the proposed code:

#include <stdio.h>
//#include <string.h>
//#include <math.h>
//#include <stdlib.h>

#define MAX_NAME 49
#define MAX_PHONE 7

//#define MAX_A 100000

struct m
{
    char name[ MAX_NAME + 1 ];
    char phone[ MAX_PHONE + 1 ];
};
typedef struct m arr;

// #define MAX_S 100
// #define MAX_NA 50
// #define MAX_PH 8

// following used to 'stringify' a value
#define STR2(x) #x
#define STR(X) STR2(X)


int main( void ) 
{
    //arr a[ MAX_A ];
    int i;
    int n;
    // int f;
    // char s[ MAX_S];
    // char na[ MAX_NA ];
    // char ph[ MAX_PH ];

    scanf("%d",&n);
    arr a[ n ];

    for(i = 0; i < n; i++)
    {
        scanf("%" STR(MAX_NAME)  "s", a[i].name);
        scanf("%" STR(MAX_PHONE) "s", a[i].phone);
    }
    for(i = 0; i < n; i++)
    {
       printf("%s=%s\n",a[i].name,a[i].phone);
    }
    return 0;
}

Upvotes: 0

Rohan Bari
Rohan Bari

Reputation: 7726

When you were declaring char phone[8], the debugger tells (no null-terminator):

Debugger shows clarified results of error.

But after changing, it places a null terminator which solves your problem. There was just a lack of null-terminator in your program. After changing the line:

Debugger after fixing the code.


Your problem will get solved after editing:

typedef struct m
{
    char name[50];
    char phone[8];
} arr;

Into

typedef struct m
{
    char name[50];
    char phone[9]; // increased by 1.
} arr;

Then you'll get something like:

3 // --- INPUT
sam 99912222
tom 11122222
harry 12299933
sam=99912222 // --- OUTPUT
tom=11122222
harry=12299933

Upvotes: 2

Related Questions