0

there is a part about UNIX system functions in R&K C programming guide.

there are system calls like read(), write(), open() or etc ,while we can use scanf(), printf(), fopen() or other c library functions to do the same work.

so what do we need C library functions instead of them?

why do we need to know and use UNIX system functions in C programming while we have that functions in C library?

arianpress
  • 456
  • 1
  • 6
  • 16
  • 1
    Sentences start with a capital letter. Also I have a counter question. Why do you want to know less stuff? That sounds like a flawed way of thinking. – DeiDei May 07 '17 at 12:34

3 Answers3

4

The functions from the standard library provide your program an operating system independent interface to have the system perform tasks for you. So they are not UNIX specific, although they first appeared with many UNIX implementations because that was the de-facto operating system in those days (not counting IBM360, Honeywell GCOS, and other mainframe/mini OS-es).

alk
  • 69,737
  • 10
  • 105
  • 255
Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • This answers why library functions may be preferred, but how does this answer OP's question, "why do we need to know and use UNIX system functions in C programming?" – ad absurdum May 07 '17 at 13:09
  • @DavidBowling For one reason, there are things you can't do in strictly-conforming C code - like determine the size of a file. – Andrew Henle May 07 '17 at 14:53
2

Unix has system calls like write() that are used by library function like printf() for formatted and more comfortable usage. printf() is using write() in a certain fashion, and sometimes we would like to use it in another way, that's why it would be helpful to also be familiar with those read(), write() andopen(). That said, most of the time, printf() will be the more suitable choice for writing ,instead of write().

Also, read this

Community
  • 1
  • 1
CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
  • UNIX is written by C language.how is it possible that printf() uses write() function? – arianpress May 07 '17 at 13:02
  • @arianpress `how is it possible that printf() uses write() function?` By implementing the parts of the OS that support `printf()` without using the `printf()` function. I'll admit that is a very short summary, but it's close enough to help understand how it's done. – Andrew Henle May 08 '17 at 09:43
0

Functions read, write, ... are more primitive and provide direct reading/writing to OS. fread, fwrite, ... implement a more convenient buffered interface to those primitive functions. However, the convenience costs something. For example asynchronous input/output is not available for those functions. Also there is no equivalent to select. If you need those functionalities, you don't have the choice.

Marian
  • 7,402
  • 2
  • 22
  • 34