cprog
Search This Blog
Monday, June 11, 2012
Parts of a function
Parts of a function
A function basically consists of two parts: Function header and Body.
Header
The header consists of three parts: Function return type, Function name and Parameter list.
E.g. Return Type Function Name (Parameter List)
The function return type can be any of C’s data types: int, float, char, long etc. You can also define
a function that does not return a value by using ‘void’.
E.g. void function(…) Returns nothing
Note: function(void) Returns no argument
Arguments and Parameters
An argument is an actual value passed to the function by calling program. Parameter is an entry in a function header. It acts as the placeholder for an argument. Parameters of a function are fixed and do not change during program execution.
Body
Function body is the part that is enclosed in braces {…}. The body contains the statements that perform the actual work. When a function is called, execution begins at the start of the body and continues until a return statement or a closing brace is reached. The return statement not only terminates a function but also returns the value to the calling program if any. A function can contain multiple return statements. The first return is the only one that has any effect.
Friday, June 8, 2012
Functions
Functions
A function is a self-contained block of program that performs a coherent task of some kind. A function provides a convenient way to encapsulate some computation in a black box which can then be used without worrying about its innards.
- Every C program can be thought as a collection of functions.
- Any C program would contain at least one function
- If a C program contains only one function it must be main()
- Program execution always begins with main()
- There is no limit on the numbers of functions that might be present in a C program.
- main() will usually invoke other functions to perform its job, some coming from the same program and other from libraries of pre-written functions.
- Functions can appear in either order and in one source file or two
- Not all functions produce an integer value.
Structured Programming
Functions can be used in structured programming: a program discipline in which individual tasks are
performed by independent modules of program code. A specific program module can be a function
definition.
Top Down Design
Functions are really the only way to cope with the potential complexity of large programs. Structured
programming enables programmers to take the advantage of top down design methodology. In this case a
complex programming problem is broken up into a number of much simpler and manageable tasks in a
hierarchical way. Each task can be assigned a function in which program code and variables used are
isolated from the rest of the program.
Tuesday, June 5, 2012
File Inclusions , Conditional Compilation
File Inclusions
This directive Causes one file to be included in another.
Eg. #Include “file name”
This causes the whole content of the above file to be inserted into the source code at that point in a program. It is used in two occasions.
a) When there is a very large program, the code in divided into several files. This feature also
enables the modularity in a program.
b) In a situation where some functions or some macros are needed in our programs. In this case
those files can simply be included.
#include <filename.c> in a specified directory
#include ”filename.c”
#include <filename.h> .h says it is a header file and go to head of a c program.
Conditional Compilation
By inserting some preprocessor directives we can skip over part of a source code.
e.g. #ifdef macro name
Statement 1;
Statement 2;
#endif
The block code will be processed as usual if the macro name has been defined. but not otherwise. #ifdef
is used when you want to compile only a part of your program.
It is also used to improve the portability of a program. Suppose you want to install a program in two
different types of computers without modifying the source code, you can isolate the lines of codes that
should be different for each machine using #ifdef and #endif
e.g. #ifdef PENTIUM
code suitable for Pentium
#else
code suitable for MAC
#endif
code suitable for both
e.g # if linux
# include <getopt.h>
#endif
#ifndef can also be used in the same way but behaves opposite to #ifdef
Sunday, June 3, 2012
Macro Expansions
Macro Expansions
A macro consist of a define directive, a template and a macro expansion.
Eg: #define TOWN “Borella”
#define MAX 212
#define AND &&
During preprocessing the C preprocessor replaces the every occurrence of TOWN in the program
with “Borella”
e.g.
// Alphabetic character count
#include<stdio.h>
#include<conio.h>
#define isalpha(c) ((c>=’a’ && c<=’z’ ) || (c >=’A’ && c <= ‘Z’))
void main()
{
int ch, nc=0;
puts(“Enter a word or a sentence:”)
while (ch = getchar() != ‘\n’)
if (isalpha(ch)) nc++;
printf(“Your word or sentence contains %d alphabetic characters \n”, nc);
}
Friday, June 1, 2012
The ‘C’ Preprocessor
The ‘C’ Preprocessor
Before a C program is being executed, there are several prior steps involved. One of them is Preprocessing. The Preprocessor is the program that processes our source program before it is available to the compiler. The preprocessor offers several features known as preprocessor directives. They direct the compiler in various ways.
A directive starts with a # symbol and guides the compiler to behave in a special way. The directives are usually placed at the beginning of a program before the main() function, though it can be placed anywhere before a beginning of a function.
Preprocessing involves the directives
a) Macro expansion
b) File inclusions
c) Conditional compilation
Friday, May 25, 2012
Program Library
Program Library
In programming a collection of pre-compiled routines that a program can use is known as a Program Library.
These routines are stored in object format. They are known as library functions or routines. Libraries are especially useful for storing frequently used routines because you do not have to explicitly link them to every program that uses them. The linker automatically looks in libraries for routines that it does not find elsewhere.
Runtime Library
Runtime refers to the event that a program is being executed. The library routines (functions) that are bound to the program during execution are known as runtime library. A linker links the object code of a program to the runtime library in order to produce an executable code of a program.
Linker
Many programming languages allow you to write different pieces of code called modules separately. This
technique simplifies the programming task because you can break a large program into small more manageable tasks; also called divide and conquer. However, finally you need to put all these modules together. This is done by a program known as a linker.
A linker is also called a link editor (binder). So a linker combines all object modules into one executable
program.
A linker also does another job. It replaces the symbolic addresses you use with real addresses. Therefore
even if your program contains one module it has to be linked.
Saturday, May 12, 2012
Reading a character
Reading a character
getc()
getc() is a macro that reads one character from a stream.
getchar()
getchar() is a macro that gets a character from stdin. It returns the next character on the input stream stdin.
Eg:-1
/* Reading a character from the keyboard */
#include <stdio.h>
void main()
{
char ch;
printf("Enter a character:");
ch = getc(stdin);
printf("You have entered the character %c\n",ch);
}
Subscribe to:
Comments (Atom)