Thread: The C Programming Language Second Edition, global values.

  1. #1
    Registered User
    Join Date
    Jun 2018
    Posts
    10

    Post The C Programming Language Second Edition, global values.

    Hello.

    I'm currently using the book "The C programming Language" as my top tool to understand the programming patterns of the C language.

    In chapter 4 they were structuring functions and I've managed to split successfully some other programs declaring prototype functions in headers as well as defining them in their correspondent source code. However, by using the debugger "gdb 7.12" I've found the next odd behaviour.

    Code:
    #include "getch.h"
    
    #include <stdio.h>
    #include <ctype.h>
    
    
    #define SIZE 10
    
    
    int getint(int *pn){
        int character;
        int sign;
        int second_character;
    
        while (isspace(character = getch())) // call to getch()
            ;
    ...
    Code:
    #include <stdio.h>
    
    
    #define BUFSIZE 100
    
    
    char buf[BUFSIZE];
    
    // Supposedly, this global initialization should assign 0 to all bufp variables
    int bufp = 0;
    
    
    void ungetch(int character){
        if (bufp >= BUFSIZE)
            printf("Existen muchos caracteres\n");
        else
            buf[bufp++] = character;
    }
    
    
    // Entry point of getch() call
    int getch(void){
        // It never executed the statement int bufp = 0 according to dbg
        return (bufp > 0) ? buf[--bufp] : getchar();
    }
    As I have stated, I'm stuck in explaining why the compiler "gcc 6.3.0" does not execute the statement that assign bufp before returning the values in the getch function. Also, I have compiled using both c99 and c89 standards discarding any incompatibilities in that matter.

    Any help will be greatly appreciated.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    708
    I don't understand what you mean by "all bufp variables". There's only one. But it all works perfectly fine.
    Code:
    // main.c ///////////////////////////////////////////
    #include <stdio.h>
    #include <ctype.h>
    #include "getch.h"
    
    // A main to test it.
    int main(){
        int ch;
        while (isspace(ch = getch()))
            ;
        printf("[%c]\n", ch);
        ungetch(ch);
        printf("[%c]\n", getch());
        printf("[%c]\n", getch());
        return 0;
    }
    
    // getch.h ///////////////////////////////////////////
    #ifndef GETCH_H
    #define GETCH_H
    void ungetch(int ch);
    int getch(void);
    #endif
    
    // getch.c ///////////////////////////////////////////
    #include <stdio.h>
    // We would usually include the header here, too (but it works either way)
    #include "getch.h"
    
    #define BUFSIZE 100
    
    // These would normally be static (but it works either way)
    static char buf[BUFSIZE];
    static int bufp = 0;
    
    void ungetch(int character){
        if (bufp >= BUFSIZE)
            printf("Existen muchos caracteres\n");
        else
            buf[bufp++] = character;
    }
    
    int getch(void){
        return (bufp > 0) ? buf[--bufp] : getchar();
    }
    To compile and run it:
    Code:
    $ gcc -c main.c
    $ gcc -c getch.c
    $ gcc main.o getch.o
    $ ./a.out
        hello
    [h]
    [h]
    [e]
    
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,537
    > // It never executed the statement int bufp = 0 according to dbg
    Well no it won't.

    Global variables are initialised by the program loader, not the program itself.
    Data segment - Wikipedia
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jun 2018
    Posts
    10
    Thank you very much for you kind reply and I'm sorry for not making myself clear "by all bufp variables" I meant bufp variable ocurrences throughout the code. Anyhow, what troubles me is the value of bufp once the getch function enters in. In line 44, dbg show that bufp has random values instead of 0. Also, I use netbeans 8.2 IDE which generates an automatic makefile to compile my files, I don't know if that info may be useful.

    Also, knowing the existence of a program loader is very useful, I will study the internals of this language.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,537
    Mmm, it is as I would expect here.
    Code:
    $ gdb -q ./a.out
    Reading symbols from ./a.out...done.
    (gdb) b getch
    Breakpoint 1 at 0x4006f3: file getch.c, line 19.
    (gdb) run
    Starting program: ./a.out 
    
    Breakpoint 1, getch () at getch.c:19
    19	    return (bufp > 0) ? buf[--bufp] : getchar();
    (gdb) print bufp
    $1 = 0
    (gdb)
    > Also, I use netbeans 8.2 IDE which generates an automatic makefile to compile my files, I don't know if that info may be useful.
    You need to make sure it's passing the "-g" flag to the compiler, to ensure all debug information is present.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    680
    Actually, in this case, as in many programs, global variables are not recommended.

    Create the local variables in main(), then pass a pointer to the data to other functions, especially for passing arrays, or structs.

    In some programs, some minimal globals may be needed, but should be used with caution.

    A Google search on "Global variables are bad in C", will lead you to many sites explaining the issue.
    Last edited by rstanley; 07-19-2018 at 10:39 AM.

  7. #7
    Registered User
    Join Date
    Jun 2018
    Posts
    10
    Thank you very much, all your responses were very useful, I've got more insight in how the compiler works.
    Last edited by diego31416; 07-19-2018 at 07:53 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-08-2018, 12:03 PM
  2. Practical C Programming, Third Edition
    By jc99 in forum C Programming
    Replies: 10
    Last Post: 06-19-2009, 09:24 PM
  3. Who is doing programming as a hobby 2nd edition
    By SAMSAM in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 03-18-2003, 02:45 PM

Tags for this Thread