Showing posts with label Turbo c. Show all posts
Showing posts with label Turbo c. Show all posts

11 May 2012

Coding Functions in 'C'

In order to create and use a function in 'C', the programmer must provide three things which have matching descriptions:

  1. A function prototype - describes the structure of the function to the compiler.
  2. A function call - passes program control to the function.
  3. A function header - begins the program code of the function.
Function Prototype

A prototype shows the name of the function

Use of Functions

Complex problems can usually be broken down into a series of less complex problems, which may themselves be broken down, and so on.
Program design techniques (and programming languages) can accommodate this program approach, to allow the programmer to think about a problem in manageable chunks.

Consider the following pseudocode:
Begin
    Display "Triangle Area Calculator"
    Calculate Area
    Display "Program Complete"
End
The program displays a heading, does a calculation, then displays an end message. The process is quite clear, but the actual calculation process is

08 May 2012

String Functions in 'C' (#include<stdio.h>)

The 'C' language provides a number of functions which help when handling strings.

strcat(string1, string2);

This function concatenates (adds to the end of) string2 to string1.
char string1[30] = "First String", string2[15] = "Second String";
strcat(string1, string2);
printf("%s", string1); // will print First StringSecond String
strlen(string);

Use of Strings in 'C'

A string is defined in 'C' as an array (list) of characters.

char str[10];   defines a string variable of 10 characters.

When data is placed in a string variable, a null character is placed at the end of the data. It is possible to look for this null when processing the string, so that processing

05 May 2012

Loops with variable repetitions

'For' loops are controlled by a built-in counter and are used where the number of repetitions is known before the loops is executed. Some problems involve repetition based on a condition whose occurrence is unpredictable. For these problems,a pre-test loop is used with an appropriate condition.

04 May 2012

For Loops

Sometimes we need to write a program where a block of code needs to be repeated. Rather than write a code over and over, we use one of the Repetition Structures available in programming languages.

The simplest of these structures is the For Loop.

This loop is used when a set of instructions is to be repeated a predetermined number of times.

02 May 2012

The Selection Structure

Selection is used to allow a program to choose between two processes, depending on a condition.
The commonest form of selection uses the 'IF' keyword.

scanf and gets

scanf

scanf is used to allow a program to get data from the keyboard.

int input;
printf("Please enter an integer");
scanf("%d%*c", &input);

These instructions will display a prompt to the operator and then stop the program until the operator keys in an integer and presses <Enter>. The integer entered will be stored in the variable named "input".

29 April 2012

The "printf"

printf is used to display data on the screen. Its simplest form is:
printf("Hello World");
This statement tells the program to display the data in the inverted commas (known as the format string) on the screen.

Data Types in C

All programming languages use different ways to store different types of data. 'C' is a strongly typed language. this means the programmer must know what type a particular piece of data is, and create a variable suitable to hold it.

The "Hello World" program written in C

/**********************************************
            This is a simple program
  The program displays a message on the screen
**********************************************/
#include

void main()
{
    printf("Hello World");
    scanf("%*c");
}