1

Input -

COMETQ
HVNGAT

Here's the code -

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

#define MAXLEN 6

main(void)
{
    char comet[MAXLEN], group[MAXLEN];
    unsigned long int result[2] = { 1,1 };
    short int i, j;

    scanf("%s",comet);
    scanf("%s",group);
    printf("\nComet's Name: %s\nGroup's Name: %s",comet,group);
    printf("\nComet's No.: %ld\nGroup's No.: %ld",result[0],result[1]);

    i = j = 0;
    while(comet[i]!='\0' && i<MAXLEN){
        result[0] *= (comet[i] - 'A' + 1);
        i++;
    }
    while(group[j]!='\0' && j<MAXLEN){
        result[1] *= (comet[j] - 'A' + 1);
        j++;
    }

    printf("\nComet's No.: %ld\nGroup's No.: %ld",result[0],result[1]);
    printf("\nComet's No. Mod 47: %ld\nGroup's No. Mod 47: %ld",result[0]%47,result[1]%47);

    if(result[0]%47 == result[1]%47)
        printf("\nGO");
    else
        printf("\nSTAY");

    exit(0);
}

Now, as far as I know, scanf() reads a string till a whitespace is detected. But here, the output is-

Comet's Name: COMETQHVNGAT
Group's Name: HVNGAT
Comet's No.: 1
Group's No.: 1
Comet's No.: -534663680
Group's No.: 994500
Comet's No. Mod 47: 43
Group's No. Mod 47: 27
STAY

But, shouldn't it be like this?

comet = "COMETQ" & Group = "HVNGAT"

I don't understand why isn't this happening?

In addition, when the size of comet is 6 bytes, how can it store - COMETQHVNGAT?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
PalashV
  • 759
  • 3
  • 8
  • 25
  • there are a few problems with the code, especially the scanf calls: the code should always check the returned code from scanf to assure the operation was successful. the format string should (almost) always start with a leading ' ' (space) so white space is skipped before any input. The input is 6 chars per line, the buffers are only 6 characters each. the printf is trying to print a string using '%s', however, the 'string' is missing a terminating nul char, so the first printf keeps going until a nul char is encountered, This is undefined behaviour and can lead to a seg fault event – user3629249 Dec 16 '14 at 11:29
  • Suggest: make buffers (at least) 7 characters long and pre-set the buffer contents to all '\0' before calling scanf. Note: scanf does NOT insert a nul char at the end of a input array of chars. – user3629249 Dec 16 '14 at 11:32

3 Answers3

2

If you want to use a char array as a string, you need to null-terminate it.

In your code, the array length is specified as 6, whereas, your inputs are itself 6 bytes, leaving no room to store the terminating null character. That's why, when the comet and group are passed to printf(), they're producing weird output.

A string is null-terminated, and based on this principle, the memory access in your case will go beyond the allocated memory size of comet and group, producing undefined behaviour .

Safer alternative:

Use fgets() to read the input, limited by the size of buffer, get rid of the last \n character, and you're all good-to-go. Check the man page for details.

Also, you need to change the format specifier %ld to %lu for unsigned long int. %ld is used for signed long int.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
1

Its because of the buffer size #define MAXLEN 6 - its causing Undefined Behavior- there is no space for the terminating character \0.

COMETQ\0
0123457

define MAXLEN as 7.

Sadique
  • 22,572
  • 7
  • 65
  • 91
  • But how can it store 'COMETQHVNGAT' then? – PalashV Dec 16 '14 at 07:23
  • 1
    @paver - its not storing it at all. The output is random - its called [Undefined_behavior](http://en.wikipedia.org/wiki/Undefined_behavior) – Sadique Dec 16 '14 at 07:23
  • Can it be the case that `group` is allocated memory just after `comet`? – PalashV Dec 16 '14 at 07:25
  • @paver - Its called Undefined Behavior - which means anything can happen on different platforms. Don't try to reason it out with `how or why`- read this http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points – Sadique Dec 16 '14 at 07:26
  • @paver - your particular compiler on your particular system happens to have allocated `comet` and `group` contiguously. C doesn't check bounds on arrays, so you can quite happily fly off the end of the array and keep going, and `printf` is doing just that, looking for a `\0`. You should not rely on other compilers and other systems, or even your compiler in a different piece of code, allocating the variables contiguously. al-Acme is right about needing to define MAXLEN big enough to accommodate the nul. – Tony Dec 16 '14 at 07:30
  • @Tony - Ok. I'll keep that in mind. I'm using ideone.com btw. – PalashV Dec 16 '14 at 07:36
1

Array is declared memory will be allocated in a continuous manner. So in first string you are not able to give the null character. So this is the reason it is printing fully.

If you give the input less than the maxlen it give the output correctly. Make sure that maxlen is high value because user can give the name in n no.of characters.

Check:

If you print the address of starting character of the both array you can get the difference that is 6 bytes.

Karthikeyan.R.S
  • 3,991
  • 1
  • 19
  • 31