Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Operating System Lab Lab – 5

Fall - 2019 C Programming, Streams, Redirections, Pipes

C Programming in Linux:
In windows environment , We have seen different tools and IDEs like Visual
Studio (VC++), Code::blocks and DEV-C++, but in Linux we don't have such
tools, We have to use command line tools like GCC. We can easily compile and run
C programs using GCC Compiler.

GCC:
The GNU Compiler Collection (GCC) is a compiler system produced by the GNU
Project. Originally named the GNU C Compiler, GCC 1.0 was released in 1987. It
was extended to compile C++ in December of that year. later developed for
Objective C/C++, Java and Fortron.

you can check which version of gcc is installed by command:

if GCC is not installed, you can install it by following command

sudo apt install gcc

if above mentioned command doesn’t work then try

sudo apt install build-essential

after installation, Any file containing source code for C should be saved with the
extension .c For example test.c.

Consider the following example: Let "hello.c" (only save files with .c extension)

#include <stdio.h>
int main()
{
printf("Hello...\n");
return 0;
}

A standard way to compile this program is with the command

gcc hello.c -o hello

Page 1 of 6
Operating System Lab Lab – 5
Fall - 2019 C Programming, Streams, Redirections, Pipes

This command compiles hello.c into an executable program named "hello" that
you run by typing ./hello at the command line. It does nothing more than print
"Hello..." on the screen.

gcc has an option –o to specify the name of the output file. If the name of the
output file is not specified, gcc names the output file a.out by default.
Once you got the source program compiled successfully, its object file is ready to be
execute. To execute the object file type following command line on the prompt.

./hello

Note:
 Never type ./hello.c
 you need to have execute permission to run that file.

Exercise:
Write a C program that ask the user to enter a number and determine whether the
number entered is even or odd.

Java:
Similarly we can check whether java is installed by command: java -version

Date Command:

Command Description
date The date command displays the current date and time on the
screen. The system administrator sets the date users cannot
change them.

Example:

There are number of options in which date can be displayed. If you want to see
only date, you can do it like this:

Page 2 of 6
Operating System Lab Lab – 5
Fall - 2019 C Programming, Streams, Redirections, Pipes

Clear Command:

Command Description
clear Clears the screen.

Stream, Redirection & Pipes:

Streams:
In the computer field streams is an abstract idea. Informally, anything flow into the
computer in the form of input, and anything flow out of computer in form of output
are referred as streams. Formally, input flowing from input device like keyboard to
memory is referred as input stream, and output flowing from memory to output
device is referred as output stream. So streams are associated with flow of data.

As we know when a Linux command executes it may take input before execution
and gives some results after execution. Therefore, each command in Linux is
associated with streams. Stream associated with the execution of Linux commands
are
 Standard Input Stream (Stdin)
 Standard Output Stream (Stdout), and
 Standard error Stream (Stderr)

Normally, standard input flows from keyboard to the memory, and standard output
and standard error flow to the terminal. When a Linux command executes, in case
of errors, errors messages flow as output from computer to the terminal, is called
standard error stream.

I/O Redirections:

Redirection changes the assignments for standard input and standard output.

Page 3 of 6
Operating System Lab Lab – 5
Fall - 2019 C Programming, Streams, Redirections, Pipes

In normal cases standard input comes from keyboard, and standard output goes to
the screen. The term redirection is used when the standard input will not be
coming from the keyboard, but from some other scours like a file, and standard
output will not be going to screen, but to some other scours like file or to other
commands.
In the case of I/O redirection the shell should be informed. To tell the shell to
redirect input or output of any command, following characters are used.

Operator Description

> Redirects output of a command to a file or device (e.g.


printer). It overwrites the existing file.

>> It is similar to >, except if the target file already exists,


the new output is append to its end.

< Redirects input of command from a file or device.

<< The redirect operator << is used mostly in script files


(shell programs) to provide input to other
commands.

| Sends the output of one command to become the


input of another command.

Redirecting Output

Normally, the output is sent to screen, output can be sent to some other scours like
a file. The operator > is used with a command to redirect output.

The format is as follow:

command > filename

or

command >> filename

Example

As we know when the date command executes it sends its output to the screen. The
output from the date command, for example, can be sent to a file xyz.

date > xyz


Page 4 of 6
Operating System Lab Lab – 5
Fall - 2019 C Programming, Streams, Redirections, Pipes

Note:-
 If the already exists, then it will be over written over, and the contents of the
existing file are lost.
 If the specified filename does not exist, then the shell creates one to save the
output in it.

Redirecting Input

Normally, the input is taken from keyboard; input can be taken from other source
like file. The operator < is used for redirecting input.

The standard input may be received from a file rather then the keyboard. The
operator < reads standard input from file rather than keyboard. The format is as
follow:

command < filename


or
command << filename

Example

cat < datafile

The contents of datefile are read into the standard input by redirection operation.
Then the cat command reads the input and displays the contents.

Combining Redirection Operators

The redirection operations for both standard input and standard output can be
combined. For example, in this example, the standard input has been redirected to
receive its data from a file, and the standard output has been redirected to place its
data in a file.

cat < fileone > filetwo

Pipe Operator ( | )

In case of redirection the standard input and output are redirected to elsewhere
(terminal, printer or file) then the default. The pipe command sends output of one
Linux command to another Linux command. The pipe ( | ) operator (vertical bar)
is placed between the two commands forms a connection between them. The pipe
operation receives output from the command place before the pipe operator and
sends this output as an input command placed after the pipe operator. The general
format is as follows:

Page 5 of 6
Operating System Lab Lab – 5
Fall - 2019 C Programming, Streams, Redirections, Pipes

Command A | Command B

Example

The commands ls and lpr (printer) can be combined with the pipe. The list of
filenames output by the ls command is piped into lpr command.
$ ls | lpr

________________________ The End ________________________

Page 6 of 6

You might also like