learn c programming from basic

C-Fundamentals Introduction

session 1:
 C Language was originally developed at Bell Labs by Ken Thompson and Dennis Ritchie in the mid of 1970‟s. It is one of the most popular procedural, general purpose, and high-level programming language used in all operating systems of today. Although C was designed for writing system software, it is also widely used for developing application software. C is one of the most popular programming languages of all time and there are very few computer architectures for which a C compiler does not exist. C has greatly influenced many other popular programming languages, most notably C++, which began as an extension to C. It supports the programmer with a rich set of built-in functions and operators. C supports different types of statements like sequential, selection, looping etc. Procedural programming concept is well supported in C, this helps in dividing the programs into function modules or code blocks.


Basic Structure

 A C-program has the following basic structure:
 documentation Section
 link Section
 definition Section
 global declaration Section
 main() 

}
 Sub-program Section function1() 
{
 }
 function2() 
{
}

The documentation section consists of a set of comment lines giving the name of the program, the author and other details such as a short description of the purpose of the program etc. The link section provides instructions to the compiler to link functions from the system library. The definition section defines all the symbolic constants. The variables can be declared inside the main function or before the main function. Declaring the variables before the main function makes the variables accessible to all the functions in a C language program, such variables are called Global Variables. Declaring the variables within main function or other functions makes the usage of the variables confined to the function only and not accessible outside. These variables are called Local Variables. Every C program must have one main function. It contains different executable statements between the opening and closing braces. The sub-program section contains all the user-defined functions that are called in other and main function. User-defined functions are generally placed immediately after the main function although they may appear in any order.

Example: 

#include<stdio.h>
#include<conio.h>
#define PI 3.1415
float r,a,c;
float area();
float circumference();
void main()
{
clrscr();
Chapter 1 Page 3
C-Fundamentals
printf("Radius=");
scanf("%f",&r);
a=area();
c=circumference();
printf("Area=%f",a);
printf("\n Circumference=%f",c);
getch();
}
float area()
{
return PI*r*r;
}
float circumference()
{
return 2*PI*r;

}




Some Simple C Programs

1. Program to display the message “Hello World”
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("Hello World");
getch();
}
2. Program to add two whole numbers
#include<stdio.h>
#include<conio.h>
void main()
Chapter 1 Page 4
C-Fundamentals
{
clrscr();
int a,b,c;
printf("a=");
scanf("%d",&a);
printf("b=");
scanf("%d",&b);
c=a+b;
printf("Sum=%d",c);
getch();
}

float area()
{
return PI*r*r;
}
float circumference()
{
return 2*PI*r;
}

The C Character Set

C uses the uppercase letters A to Z, the lowercase letters a to z, the digits 0 to 9, and certain
special characters as building blocks to form basic program elements. The special characters are
given below.




Identifiers

Identifiers are the names given to various program elements such as variables, functions, arrays,
and other user defined items. Identifiers can be a combination of letters, digits and underscore, in
any order, except that the first character must not be a digit.
In an identifier, upper- and lowercase are treated differently. For example, the identifier
count is not equivalent to Count or COUNT. There is no restriction on the length of an
identifier. However, only the first 31 characters are generally significant. For example, if your C
compiler recognizes only the first 3 characters, the identifiers pay and payment are same. Some
valid and invalid identifiers are given below.



Keywords

There are certain reserved words, called keywords that have standard, predefined meanings in C.
these keywords can be used only for their intended purpose; they cannot be used as programmer defined
identifiers. The ANSI (American National Standards Institute) C defines the following
32 keywords.

 Data Types

C supports several different types of data, each of which may be represented differently within
the computer?s memory. A data type defines the amount of memory that will be used during
program execution, valid range of values it can represent, and the operations that may be
performed on it. C is strongly typed language so every variable in C must be declared of specific
type explicitly. Data types can be either predefined or derived.
The C language supports four basic data types, each of which are represented differently
within the computer memory. The basic types for a particular compiler are listed below:


 The basic types can be augmented by the use of data type qualifiers short, long, signed, and unsigned. The int type can be qualified by signed, short, long and unsigned. The char type can be modified by unsigned and signed. You can also apply long to double.

Note: Memory requirements for each data type may vary from one C compiler to another.


1 comment:

Powered by Blogger.