putchar ( ) function


2 Οκτ 2013 (πριν από 4 χρόνια και 9 μήνες)

72 εμφανίσεις

Programming for Bioinformatics I (C Programming)

Explain the basic structure
of a C program with an example.

This is a basic introduction to good code style in the C Programming Language. It is designed
to provide
information on how to effectively use indentation, comments, and other elements
that will make your C code more readable. It is not a tutorial on actually programming in C.

As a beginning programmer, the point of creating structure in the programs' code mi
ght not
be clear, as the compiler doesn't care about the difference. However, as programs become
complex, chances are that writing the program has become a joint effort. (Or others might
want to see how it was accomplished.) Therefore, the code is no longe
r designed purely for a
compiler to read.

In the following sections, we will attempt to explain good programming practices that will in
turn make your programs clearer and more effective.

n C, programs are composed of statements. These statements are termi
nated with a semi
colon, and are collected in sections known as functions. By convention, a statement should be
kept on its own line, as shown in the example below:

#include <stdio.h>

int main(void)


printf("Hello, World!

return 0;


The following block of code is essentially the same: while it contains exactly the same code,
and will compile and execute with the same result, the removal of spacing causes an essential
difference making it harder to read:


int main(void) {printf("Hello, World!
n");return 0;}

The simple use of indents and line breaks can greatly improve the readability of the code;
without making any impact whatsoever on how the code performs. By having readable code,
it is much ea
sier to see where functions and procedures end, and which lines are part of which
loops and procedures.

This book is going to focus on the above piece of code, and how to improve it. Please note
that during the course of the tutorial, there will be many
(apparently) redundant pieces of
code added. These are only added to provide examples of techniques that we will be
explaining, without breaking the overall flow of code that the program achieves.

2. Explain relational and logical operators of C

The relational operators are

> >= < <=

They all have the same precedence. Just below them in precedence are the equality operators:

== !=

Relational operators have lower precedence than arithmetic operators, so an expression like

is taken as
i < (lim
, as would be expected.

More interesting are the logical operators

. Expressions connected by


evaluated left to right, and evaluation stops as soon as the truth or falsehood of the result is
known. Most C

programs rely on these properties. For example, here is a loop:

for (i=0; i < lim
1 && (c=getchar()) != '
n' && c != EOF; ++i)

s[i] = c;

Before reading a new character it is necessary to check that there is room to store it in the
, so t
he test
i < lim


be made first. Moreover, if this test fails, we must not go
on and read another character.

Similarly, it would be unfortunate if

were tested against


is called;
therefore the call and assignment must occur before

the character in

is tested.

The precedence of

is higher than that of
, and both are lower than relational and equality
operators, so expressions like

i < lim
1 && (c=getchar()) != '
n' && c != EOF

need no extra parentheses. But since the precedence of

is higher than assignment,
parentheses are needed in

(c=getchar()) != '

to achieve the desired result of assignment to

and then comparison with

By definition, the numeric value of a
relational or logical expression is 1 if the relation is
true, and 0 if the relation is false.

The unary negation operator

converts a non
zero operand into 0, and a zero operand in 1. A
common use of

is in constructions like

if (!valid)

rather tha

if (valid == 0)

It's hard to generalize about which form is better. Constructions like

read nicely (``if
not valid''), but more complicated ones can be hard to understand.

3. Explain branching statements supported by C

C provides two sytles

of flow control:



Branching is deciding what actions to take and looping is deciding how many times to take a
certain action.


Branching is so called because the program chooses to follow one branch or another.

if statement


is the most simple form of the branching statements.

It takes an expression in parenthesis and an statement or block of statements. if the expression
is true then the statement or block of statements gets executed otherwise these statements are


Expression will be assumed to be true if its evaulated values is non


statements take the following form:

Show Example

if (expression)





Block of statements;



if (expression)


Block of statements;




Block of statements;



if (expression)


Block of statements;


else if(expression)


Block of statements;




Block of statements;


? : Operator

The ? : operator is just like an if ... else statement except that because it is an operator you can
use it within expressions.

? :

is a ternary operator in that it takes three values, this is the only ternary op
erator C has.

? :

takes the following form:

Show Example

if condition is true ? then X return value : otherwise Y value;

switch statement:

The switch statement is much like

a nested if .. else statement. Its mostly a matter of
preference which you use, switch statement can be slightly more efficient and easier to read.

Show Example

expression )


case constant


[case constant


[case constant


[default : statements4;]


Using break keyword:

If a condition is met
in switch case then execution continues on into the next case clause also
if it is not explicitly specified that the execution should exit the switch statement. This is
achieved by using


Try out given example
Show Example

What is default condition:

If none of the listed conditions is met then default condition executed.

Try out given example
Show Example


Loops provide a way to repeat commands and control how many times they are repeated. C
provides a number of looping way.

while loop

The most basic loop in C is the while loop.A while statement is like a repeating if sta
Like an If statement, if the test condition is true: the statments get executed. The difference is
that after the statements have been executed, the test condition is checked again. If it is still
true the statements get executed again.This cycle r
epeats until the test condition evaluates to

4. Explain the following functions:
gets (
, puts


Getchar ( ) function

The getchar ( ) function returns a single character from a standard input device

(keyboard). It takes no parameter and the
returned value is the input character. The

general form of the getchar ( ) function is :

A = getchar ( ) ;

The variable A is of the type character. It inputs a character from the keyboard and

assign it to variable A.

putchar ( ) function

The putchar ( ) fu
nction takes one argument, which is the character to be sent to

output device. It also returns this character as a result. The general form of the

putchar ( ) function is :

putchar (ch) ;

where ch is a variable of type character.

Example 1

# include < iost
ream.h >

# include < stdio.h>

void main ( )

{ char ch;

ch = getchar ( ) ;

putchar (ch);


Functions :: 67

The above program takes a character from the keyboard and prints it on the screen.

gets ( ) function

The gets ( ) function gets a string terminated by

a newline character from the standard

input stream stdin. The gets ( ) replaces the newline by a null character (
0). It also

allows input string to contain white space characters (spaces, tabs), gets return

when it encounters a newline; everything upto
the newline is copied into A.

gets (A) ;

Example 2

# include < stdio.h >

# include < iostream.h >

void main ( )


char A [ 100 ];

cout << “Input a string”;

gets (A);

puts (A);


5. Define pointer? Discuss the advantages of using


is a secondary data type (also known as derived data type) in C. It is built from one
of the primary data types available in C language. Basically pointer contains memory address
of other variable or function as their value. As pointer deals with memory a
ddress, it can be
used to access and manipulate data stored in memory.

Benefits of using Pointer in C Program

Pointer is one of the most exciting features of C language and it has added power and
flexibility to the language. Pointer is in C language becaus
e it offers following benefits to the


Pointers can handle arrays and data table efficiently.


Pointers support dynamic memory management.


Pointer helps to return multiple values from a function through function argument.


Pointer increases progr
am execution speed.


Pointer is an efficient tool for manipulating structures, linked lists, queues stacks etc.

We can achieve these and much more benefits from pointer only if we can use it properly.

6. What is a Union? Explain with example

. Which are the decision making statements in C? Explain them

. Explain the following loop statements with

a) Do
while b) For