Friday 23 November 2012

Introduction to C Programming

Programming with C Unit 1

Unit 1 Introduction to C Programming
Structure
1.0 Introduction
1.1 Features of C
Self Assessment Questions
1.2 Basic structure of C programs
Self Assessment Questions
1.3 A simple C program
Self Assessment Questions
1.4 More simple C programs
1.5 Summary
1.6 Terminal Questions
1.7 Answers to Self Assessment Questions
1.8 Answers to Terminal Questions
1.9 Exercises
1.0 Introduction
C is a generalpurpose,
structured programming language. Its instructions
consist of terms that resemble algebraic expressions, augmented by certain
English keywords such as if, else, for, do and while. C was the offspring of
the ‘Basic Combined Programming Language’ (BPCL) called B, developed
in the 1960’s at Cambridge University. B language was modified by Dennis
Ritchie and was implemented at Bell laboratories in 1972. The new
language was named C. Since it was developed along with the UNIX
operating system, it is strongly associated with UNIX. This operating system,
which was also developed at Bell laboratories, was coded almost entirely
in C.
Programming with C Unit 1
Sikkim Manipal University Page No. 2
Objectives
At the end of this unit, you will be able to:
· Understand the features of C programming language
· Understand the basic structure of a C program
· Write simple C programs
1.1 Features of C
C is characterized by the ability to write very concise source programs, due
in part to the large number of operators included within the language.
It has a relatively small instruction set, though actual implementations
include extensive library functions which enhance the basic instructions.
The language encourages users to write additional library functions of their
own. Thus, the features and capabilities of the language can easily be
extended by the user.
C compilers are commonly available for computers of all sizes. The
compilers are usually compact, and they generate object programs that are
small and highly efficient when compared with programs compiled from
other highlevel
languages.
Another important characteristic of C is that its programs are highly portable,
even more so than with other highlevel
languages. The reason for this is
that C relegates most computer dependent features to its library functions.
Thus, every version of C is accompanied by its own set of library functions,
which are written for the particular characteristics of the host computer.
Self Assessment Questions
i) State true or false
Using C language programmers can write their own library functions
ii) C is a ________ level programming language
Programming with C Unit 1
Sikkim Manipal University Page No. 3
1.2 Basic structure of C Programs
A C program can be viewed as a group of building blocks called functions. A
function is a subroutine that may include one or more statements designed
to perform a specific task. To write a C program we first create functions and
then put them together. A C program may contain one or more sections
shown in Fig. 1.1.
Fig. 1.1
The documentation section consists of a set of comment(remarks) lines
giving the name of the program, the author and other details which the
programmer would like to use later. Comments may appear anywhere within
a program, as long as they are placed within the delimiters /* and */ (e.g.,
/*this is a comment*/). Such comments are helpful in identifying the
Documentation section
Link section
Definition section
Global declaration section
main() function section
{
Declaration part
Executable part
}
Subprogram section
Function 1
Function 2
.
.
Function n
Programming with C Unit 1
Sikkim Manipal University Page No. 4
program’s principal features or in explaining the underlying logic of various
program features.
The link section provides instructions to the compiler to link functions from
the system library. The definition section defines all symbolic constants.
There are some variables that are used in more than one function. Such
variables are called global variables and are declared in the global
declaration section that is outside of all the functions.
Every C program must have one main function section. This section
contains two parts, declaration part and executable part. The declaration
part declares all the variables used in the executable part. There is at least
one statement in the executable part. These two parts must appear between
opening and closing braces({ and }). The program execution begins at the
opening brace and ends at the closing brace. The closing brace of the main
function section is the logical end of the program. All statements in the
declaration and executable parts end with a semicolon(;).
The subprogram section contains all the userdefined
functions that are
called in the main function. Userdefined
functions are generally placed
immediately after the main function, although they may appear in any order.
All sections, except the main function section may be absent when they are
not required.
Self Assessment Questions
i) The documentation section contains a set of __________ lines.
ii) State true or false
Every C program must have one main() function.
iii) What are global variables?
Programming with C Unit 1
Sikkim Manipal University Page No. 5
1.3 A simple C Program
#include <stdio.h>
main()
{
printf("Hello, world!\n");
return 0;
}
If you have a C compiler, the first thing to do is figure out how to type this
program in and compile it and run it and see where its output went.
The first line is practically boilerplate; it will appear in almost all programs we
write. It asks that some definitions having to do with the “Standard I/O
Library'' be included in our program; these definitions are needed if we are
to call the library function printf correctly.
The second line says that we are defining a function named main. Most of
the time, we can name our functions anything we want, but the function
name main is special: it is the function that will be “called'' first when our
program starts running. The empty pair of parentheses indicates that our
main function accepts no arguments, that is, there isn't any information
which needs to be passed in when the function is called.
The braces { and } surround a list of statements in C. Here, they surround
the list of statements making up the function main.
The line
printf("Hello, world!\n");
is the first statement in the program. It asks that the function printf be
called; printf is a library function which prints formatted output. The
parentheses surround printf 's argument list: the information which is
handed to it which it should act on. The semicolon at the end of the line
terminates the statement.
Programming with C Unit 1
Sikkim Manipal University Page No. 6
printf 's first (and, in this case, only) argument is the string which it should
print. The string, enclosed in double quotes (""), consists of the words “Hello,
world!'' followed by a special sequence: \n. In strings, any twocharacter
sequence beginning with the backslash \ represents a single special
character. The sequence \n represents the “`new line'' character, which
prints a carriage return or line feed or whatever it takes to end one line of
output and move down to the next. (This program only prints one line of
output, but it's still important to terminate it.)
The second line in the main function is
return 0;
In general, a function may return a value to its caller, and main is no
exception. When main returns (that is, reaches its end and stops
functioning), the program is at its end, and the return value from main tells
the operating system (or whatever invoked the program that main is the
main function of) whether it succeeded or not. By convention, a return value
of 0 indicates success.
Self Assessment Questions
i) The information that needs to be passed in when a function is called is
______
ii) State true or false
The main() function doesn’t return any value.
1.4 More simple C programs
Program 1.1 Area of a circle Here is an elementary C program that reads
in the radius of a circle, calculates the area and then writes the calculated
result.
#include <stdio.h> /* Library file access */
/* program to calculate the area of a circle */ /* Title (Comment) */
Programming with C Unit 1
Sikkim Manipal University Page No. 7
main() /* Function heading */
{
float radius, area; /*Variable declarations */
printf(“Radius=?”); /* Output statement(prompt) */
scanf(“%f”, &radius); /* Input statement */
area=3.14159*radius*radius; /* Assignment statement */
printf(“Area=%f”,area); /* Output statement */
}
Program 1.2 Print a few numbers Here is a program to illustrate a simple
loop
#include <stdio.h>
/* print a few numbers, to illustrate a simple loop */
main()
{
int i;
for(i = 0; i < 10; i = i + 1) /* Looping statement */
printf("i is %d\n", i);
return 0;
}
Program 1.3: Program to add two numbers
#include <stdio.h>
main()
{
int i,j,k; // Defining variables
i = 6; // Assign values
j = 8;
k = i + j;
printf("sum of two numbers is %d \n",k); // Printing results
}
Programming with C Unit 1
Sikkim Manipal University Page No. 8
1.5 Summary
C is a generalpurpose,
structured programming language. Its instructions
consist of terms that resemble algebraic expressions, augmented by certain
English keywords such as if, else, for, do and while. C is characterized by
the ability to write very concise source programs, due in part to the large
number of operators included within the language. Every C program
consists of one or more functions, one of which must be called main. The
program will always begin by executing the main function. Additional
function definitions may precede or follow main.
1.6 Terminal Questions
1. _____ enhance the basic instructions of C language
2. C was originally developed by _____
3. What are the major components of a C program?
4. What significance is attached to the function main?
5. What are arguments? Where do arguments appear within a C program?
1.7 Answers to Self Assessment Questions
1.1 i) True
ii) High
1.2 i) Comment
ii) True
iii) The variables that can be used in more than one functions
1.3 i) Arguments
ii) False
Programming with C Unit 1
Sikkim Manipal University Page No. 9
1.8 Answers to Terminal Questions
1. Library functions
2. Dennis Ritchie
3. Documentation section, Link section, Definition section, Global
declaration section, main() function section, Subprogram section
4. main is the function that will be “called'' first when our program starts
running.
5. The arguments are symbols that represent information being passed
between the function and other parts of the program. They appear in
the function heading.
1.9 Exercises
1. Explain the history of C language.
2. What are the advantages of C language?
3. Explain the basic structure of a C program with an example.
4. What are the different steps in executing a C program ?
5. Write a C program to convert Celsius to Fahrenheit and vice versa.

No comments:

Post a Comment