-3

I have done my school assignment, and I try to fix it by the tutor's comment. However, from my tutor latest email, he points out that "It is luck if it works without making this change. With local variables, you have no idea what is in them by default." I just have no idea what the default is in my code

This is my tutor's comment: 1. The question said only local variables should be used.

  1. The read function was supposed to read in as well as do validation of the length of the input string. Lines 14 and 15 should be inside the read function, not in main.

  2. Because you used text as a global variable, you have been able to write your functions without the need for the string parameters s, st and str. The whole point of this assignment was to test your ability to program with string parameters. While your functions appear to have string parameters, they might as well not be there with text global and only text in the code inside the functions. The read function should be written in terms of s, not text. The count function should be written in terms of st, not text. The justify function should be have str wherever you have text. You have not shown that you understand how string parameters are used.

#include<stdio.h>
#include<string.h>
void read(), justify(char *str, int g);
int count(char *st);
int main(){
    char text[100];
    int gaps, i;
    gaps = 0;
    for(i=0; i<3; i++) {
        read(text);
        gaps = count(text);
        justify(text, gaps);
       printf("\n");
    }
}
void read(char *s){
    int length;
    printf("Enter a line of text: ");
    gets(s);
    length = strlen(s);
    while(length!=0){
        if(length<46){
        printf("123456789012345678901234567890123456789012345\n");
            length = 0;
        } else {
            printf("Enter a line of text: ");
          gets(s);
            length = strlen(s);
        }
    }
}
int count(char *st){
    int num, i, num2;
    num = 0;
    num2 = strlen(st);
    for(i=0; i<num2; i++){
        if(st[i]==' '){
            num++;
        }
    }
    return num;
}
void justify(char *str, int g){
    int i, j, num, m, n, temp;
    temp = strlen(str);
    num = 45 - temp;
    m = num / g;
    n = num % g;
    for(i=0; i<temp; i++){
        printf("%c", str[i]);
        if(str[i]==' '){
            for(j=0; j<m; j++){
                printf(" ");
            }
            if(n!=0){
                printf(" ");
                n--;
            }
        }
    }
    printf("\n");
}

I would like to learn how to improve the code and make it work without luck. Cheers!

Wu Yuekai
  • 3
  • 1
  • 4
    The value of uninitialized local variables is unspecified (which is what your tutor meant by saying you don't know their value). – sepp2k May 23 '19 at 12:44
  • In C, local variables initially get the values of what the underlying storage happens to contain. Most modern compilers will issue a warning if you refer to a local without having explicitly initialized it in code. – 500 - Internal Server Error May 23 '19 at 12:44
  • 4
    The comments you posted seem to relate to a previous version of your code which used global variables, and have nothing to do with the code you posted here. – interjay May 23 '19 at 12:44
  • The tutor says "by default" but there is no default. Space is reserved, and that is all – Weather Vane May 23 '19 at 12:47
  • 1
    @500-InternalServerError: In simplistic C implementations, or possibly with optimization turned off, a local variable may take the value sitting in storage. But, when you write “In C,” the context is the C standard, which defines the behavior in terms of an abstract machine. In that model, an uninitialized local variable has an unspecified value; it does not take the value of storage. When that model is implemented by an optimizing compiler, the result may be the variable takes the value of a register or appears to have different values at different times, or the program crashes, or more. – Eric Postpischil May 23 '19 at 13:45
  • 1
    Duplicate: [(Why) is using an uninitialized variable undefined behavior?](https://stackoverflow.com/questions/11962457/why-is-using-an-uninitialized-variable-undefined-behavior). – Lundin May 23 '19 at 14:06
  • I don't see the use of an uninitialized variable, but i see the abominable _gets_ and if the input do not contains a space you try to divide by 0 – bruno May 23 '19 at 16:29

1 Answers1

1

Regarding the title, “What is in local variables by default?”

Nothing. Per C 2018 6.7.9 10, “If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate.” (What you think of as a “local variable” is an object with automatic storage duration.) Per 3.19.2, an indeterminate value is either an unspecified value or a trap representation. Per 3.19.3, the C standard imposes no requirements on which value is chosen in any instance.

Consider:

int x;
printf("%d", x);
printf("%d", x);

In these two instances, the printf may print different numbers. The value of x is unspecified in any instance. The program is broken.

Furthermore, there is a rule in 6.3.2.1 2 that amounts to: If you use an uninitialized local variable and its address has never been taken, the behavior of the program is not defined by the C standard. This means, not only can the variable appear to have different values at different times, but that the C standard does not define the behavior of the program at all. The printf statement will not necessarily print. The program could crash. The program could call entirely different routines from what you expect. The C standard imposes no requirements.

Solution: Initialize your local variables by writing = SomeInitialValue when declaring them, as in int x = 0;, or by assigning them values before using them.

1. The question said only local variables should be used.

Solution: Use only local variables. Remove global variables from your program. If a routine needs data from a caller, pass it with a parameter.

2. The read function was supposed to read… Lines 14 and 15…

Lines 14 and 15 in the code in your question do not appear to correspond to lines that should be in the read function. You appear to have put code in the question that differs from what the tutor was referring to.

Solution: Put exactly the code the tutor reviewed in the question. Follow the tutor’s instructions.

3. Because you used text as a global variable…

See both 1. and 2. above. As with 2., the code does not match this comment; you appear to have shown different code from what the tutor reviewed. Show the exact code in the question, and do not use global variables.

Community
  • 1
  • 1
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • I am circumspect of such an effort, was it necessary? However UV for the time you used to answer :-) – bruno May 23 '19 at 16:34