Thread: K&R chapter 8

  1. #1
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272

    K&R chapter 8

    Which OS do i need to successfully run programs from chapter 8 in K&R?

    I saw they included "syscalls.h" in their first program, allowing them usage of read/write/open/close functions from the system.

    Since im using Windows, do i have to install a UNIX OS? If so which one? And even then, do i need to install a special compiler to be able to use the mentioned system functions?

    Code:
    while((n = read(0, buf, BUFSIZE)) > 0)
            write(1, buf, n);
    My compiler actually recognizes these functions - a bubble pops up with the arguments i have to write in when i write read (...) in the code window. But i dont know in which header they are. Im using Dev-C++.
    Last edited by Tool; 02-21-2010 at 05:03 PM.

  2. #2
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Well it is definitely available in Linux (Ubuntu 9.10):
    Code:
    jeff@jeff-gate:~$ find /usr/include/ -name "*.h" | grep syscall
    /usr/include/asm/vsyscall.h
    /usr/include/syscall.h
    /usr/include/linux/nfsd/syscall.h
    /usr/include/bits/syscall.h
    /usr/include/sys/syscall.h
    jeff@jeff-gate:~$
    However they might also be available in cygwin via GCC...

    YMMV

    Jeff
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  3. #3
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Do i need to include some special header in a UNIX C Compiler to use those system functions? If so which one?

    Which compiler should i download on KUBUNTU OS? Thats the one im planning to use.

    cygwin
    Can you name the header? Havent found anything like this...
    Last edited by Tool; 02-22-2010 at 11:30 AM.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Tool View Post
    Which compiler should i download on KUBUNTU OS? Thats the one im planning to use.
    As in you are going to install Kubuntu? The standard compiler there is gcc and it is part of a basic install, I think, otherwise:

    apt-cache search gcc

    will give you a list, probably the package is just called "gcc.x86_64" or something. Ubuntu also has a graphical package manager, you can use that and search for "gcc".

    But first just type "gcc" at the command-line, if it says "gcc: no input files" then it is already installed.
    Last edited by MK27; 02-22-2010 at 11:40 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    And do i have to include a special header in the standard compiler in order to use read/write and other system functions?

    Btw, can i do it all from a VirtualBox Machine? I mean execute the programs.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Tool View Post
    And do i have to include a special header in the standard compiler in order to use read/write and other system functions?
    You just have to use #include <syscall.h>

    Btw, can i do it all from a VirtualBox Machine? I mean execute the programs.
    Dunno!
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    How to run the C compiler in Ubuntu 9.10?

    Im runing the terminal but i have no idea which command to write to run the compiler for C.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Your source file is called whatever.c:

    gcc -Wall whatever.c

    "-Wall" is enable all warnings.

    The executable will be by default "a.out". If you want to call it something else use:

    gcc -Wall -o something_else whatever.c

    ps. dos "dir" == unix "ls".

    For info:
    man ls
    man gcc
    man man
    Last edited by MK27; 02-22-2010 at 02:04 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Takes deep breath.

    OK Tool first I think the header you are looking for is unistd.h...see this (write is part of the POSIX standard):
    write

    Next, I thought you were on Windows so I suggested Cygwin which lets you compile unix-like programs for Windows...its not a header, more like a mini-environment on Windows providing many UNIX-like tools. However if you are running KUbuntu in a VirtualBox session..
    1. Compiling will work just fine in VirtualBox, albeit like any virtualized environment, slower that if it ran on bare metal.
    2. To run the compiler on any Linux, like MK said just run gcc (or g++ for C++). BUT if you are coming from a Windows back-ground and are used to the hand-holding, spoon-fed compilers there then what you are probably expecting is an IDE a la Visual Studio or Code::Blocks. Actually you can install CodeBlocks on Ubuntu (open a terminal and run sudo apt-get install codeblocks) or you can run KDevelop (sudo apt-get install kdevelop) which are both GUI front-ends for gcc/g++.
    3. In any event, keep in mind that gcc is the *compiler* not the IDE front-end. You can run gcc from the terminal but like most free tools it demands that you know what you are doing
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  10. #10
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    In MinGW and Visual Studio those functions are in io.h, albeit with an underscore prefix.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In need of guidance
    By ajdspud in forum C++ Programming
    Replies: 7
    Last Post: 06-01-2016, 02:23 AM
  2. please help with binary tree, urgent.
    By slickestting in forum C Programming
    Replies: 2
    Last Post: 07-22-2007, 07:55 PM
  3. Standish Chapter 7 (C/Unix)
    By cchallenged in forum C Programming
    Replies: 16
    Last Post: 09-18-2006, 11:44 PM
  4. K&R question
    By caduardo21 in forum C Programming
    Replies: 1
    Last Post: 08-18-2005, 07:14 PM
  5. newbie | k&r book
    By xion in forum C Programming
    Replies: 4
    Last Post: 07-17-2003, 03:10 AM