A Simple C Program
Every C program must have one special function
main (). This is the point where execution begins when the program is running.
We will see later that this does not have to be the first statement in the
program, but it must exist as the entry point. The group of statements
defining the main () enclosed in a pair of braces ({}) are executed
sequentially. Each expression statement must end with a semicolon. The closing
brace of the main function signals the end of the program. The main function can
be located anywhere in the program but the general practice is to place it as
the first function.
Here is an elementary C program.
main ()
{
}
There
is no way to simplify this program, or to leave anything out. Unluckily, the
program does not do anything. Following the "main" program name is a couple of
parentheses, which are an indication to the compiler that this is a function.
The 2 curly brackets { }, properly called braces, are used to specify the limits
of the program itself. The actual program statements go between the 2 braces and
in this case, there are no statements because the program does absolutely
nothing. You will be able to compile and run this program, but since it has no
executable statements, it does nothing. Keep in mind however, that it is a legal
C program.
main ( ) function should return zero or one.
Turbo C accepts both int and void main ( ) and Turbo C coders use both int and
void main ( ) in their programs. But in my belief, void main ( ) is not a
standard usage. The reason is, whenever a program gets executed it returns an
integer to the OS. If it returns 'zero', the program is executed successfully.
Otherwise it means the program has been ended with error. So main ( ) shouldn't
be declared as void.d as void.
main( ) should be declared as
int main( )
{
……………..
……………..
return 0 ;
}
.For a much more interesting program,
load the program
int main ()
{
printf (“Welcome to C language”);
return 0;
}
and display it on your monitor. It is
same as the previous program except that it has one executable statement between
the braces.
The executable statement is another
function. Again, we won't care about what a function is, but only how to use
this one. In order to output text to the monitor, it's placed within the
function parentheses and bounded by quotes. The end result is that whatever is
included between the quotes will be showed on the monitor when the program is
run.
Notice the semi-colon; at the end of the line.
C uses a semi-colon as a statement terminator, so semi-colon is required as a
signal to the compiler that this line is complete. This program is also
executable, so you'll be able to compile and run it to see if it does what you
think it should. With some compilers, you may get an error message while
compiling, indicating that the function printf () should have a prototype.
#include<stdio.h>
#include<conio.h>
int main ()
{
printf (“Welcome to C language”);
return 0;
}
}
Here you'll be able to see #include at the
beginning of the program. It is a pre-processor directive. It's not a part of
our program; it's an instruction to the compiler to make it do something. It
says the C compiler to include the contents of a file, in this case the system
file stdio.h. This is the name of the standard library definition file for all
Standard Input Output. Your program will almost certainly want to send stuff to
the screen and read things from the keyboard. stdio.h is the name of the file in
which the functions that we want to use are defined. A function is simply a
group of related statements that we can use later. Here the function we used
is printf . To use printf correctly C needs to know what it looks like, i.e.
what things it can work on and what value it returns. The actual code which
performs the printf will be tied in later by the linker. Note that without the
definition of what printf looks like the compiler makes a guess when it sees the
use of it. This can lead to the call failing when the program runs, a common
cause of programs crashing.
The <> characters around the name tell C
to look in the system area for the file stdio.h. If I had given the name
"abc.h" instead it would tell the compiler to look in the current directory.
This means that I can arrange libraries of my own routines and use them in my
programs.
Imagine you run above program and then change
it and run it again you may find that the previous output is still stucked there
itself, at this time clrscr(); would clear the previous screen.
One more thing to remember while using clrscr() is that it should be called only after the variable declaration, like
int p,q,r;
clrscr()
One more thing to remember while using clrscr() is that it should be called only after the variable declaration, like
int p,q,r;
clrscr()
Here is an example of minimal C program that displays the string Welcome to C language (this
famous example included in all languages moreover been done originally in C in 1978
from the creators of the language, Brian Kernighan and Dennis Ritchie)
Example
#include<stdio.h>
#include<conio.h>
int main ()
#include<conio.h>
{
clrscr();
printf (“Welcome to C language”);
return 0;
}
When you execute above program you won’t
see ‘Welcome to C language’ on the console because the screen will just
flash and go away .If you want to see the line you can use getch() function
just below the printf() statement. Actually it waits until a key is pressed.
Example:
Identifiers
Identifiers are the names that are given to
various program
elements such as variables, symbolic constants and
functions.Variable or function identifier that is called a symbolic
constant name.
Identifier can be freely named, the following restrictions.- Alphanumeric characters ( a ~ z , A~Z , 0~9 ) and half underscore ( _ ) can only be used.
- The first character of the first contain letters ( a ~ z , A~Z ) or half underscore ( _ ) can only be used.
- Case is distinguishable. That is, word and WORD is recognized as a separate identifier.
- Reserved words are not allowed. However, part of an identifier reserved words can be included.
-
Comma and blank spaces cannot be included within the constants.
-
Constants can be preceded by a – or + sign, if desired. If either sign
does not precede the constant it is assumed to be positive.
-
The value of a constant cannot exceed specified minimum and maximum
bounds. For each type of constant, these bound vary from one C compiler to
another.
Variables
Variables are means for location in memory used by a program to store data. The size of that block depends upon the range over which the variable is allowed to vary.
For example, on personal computer the size of an integer variable is two bytes, and that of a long integer is four bytes.
A variable region is temporarily remember a number or string value, such as covered by the program. To identify the variables, you have a name unique to every single variable. This is called a variable name. Before using a variable, use variables to what is called a variable declaration that you have to reveal the names and data types that can be stored in the variable variable.
The format for declaring a variable in C.
[Storage-class] type data variable name [= initial value];
Storage class and the initial value can be omitted.
The same data type and storage class variable can be declared, separated by commas.
[Storage-class] type data variable name [= initial value] variable [= initial value] variable [= initial value];
In C the size of a variable type such as an integer need not be the same on all types of machines. When we declare a variable we inform the compiler of two things, the name of the variable and the type of the variable. For example, we declare a variable of type character with the name i by writing:
char i;
On seeing the "char" part of this statement the compiler sets aside one bytes of memory to hold the value of the character. It also sets up a symbol table. In that table it adds the symbol i and the relative address in memory where those one byte was set aside. Thus, later if we write: i = 'x'; we expect that,at run time when this statement is executed, the value 'x' will be placed in that memory location reserved for the storage of the value of i.
Following are the rules for naming the variables:
-
All variables must be declared before they can appear in executable
statement.
-
A declaration consists of a
data type followed by one or more variable names separated by commas.
Example: int a,b,c;
-
Variables can be distributed among declarations in any fashion. The above
declaration can be written as
int a;
int b,c;
-
Integer type variables can be declared to be short integer for smaller
integer quantities or long integer for larger integer quantities.
Example:
short int a,b,c;
long int a,b,c;
-
An integer variable can also be declared to be un signed by writing
unsigned int.
Example: unsigned int; -
Here are the rules you need to know:
1. Identifier name must be a sequence of letter and digits, and must
begin with a letter.
2. The underscore character (‘_’) is considered as letter.
3. Names shouldn't be a keyword (such as int , float, if ,break, for
etc)
4. Both upper-case letter and lower-case letter characters are
allowed. However, they're not interchangeable.
5. No identifier may be keyword.
6. No special characters, such as semicolon,period,blank space,
slash or comma are permitted
Examples of legal and illegal identifiers follow, first some legal
identifiers:
float _number;
float a;
int this_is_a_very_detailed_name_for_an_identifier;
The following are illegal (it's your job to recognize why):
float :e;
float for;
float 9PI;
float .3.14;
float 7g;
Example :
Keywords
Keywords are standard identifiers that have standard
predefined meaning in C. Keywords are all lowercase, since uppercase and
lowercase characters are not equivalent it's possible to utilize an uppercase
keyword as an identifier but it's not a good programming practice.
Points to remember
1. Keywords can be used only for their intended purpose.
2. Keywords can't be used as programmer defined identifier. 3. The keywords can't be used as names for variables.
The standard keywords are given below:
Data Types
C offers a standard, minimal set of basic data types.
Sometimes these are called "primitive" types. A lot of complex data structures
can be developed from these basic data types. The C language defines 4
fundamental data types:
character integer floating-point and double floating-point This data types are declared using the keywords char,int,float and double respectively. Typical memory requirements of the basic data types are given below
The size and range of these data types may vary among
processor types and compilers. Data type qualifiers modify the behavior of
variable type to which they are applied. Data type qualifiers can be classified
into two types.two types.
1. size qualifiers 2. sign qualifiers
Size qualifiers:
Size qualifiers alter the size of the basic data types.
There are two size qualifiers that can be applied to integer: short and long.
Sign qualifiers:The minimum size of short int is 16 bit. The size of int must be greater than or equal to that of a short int. The size of long int must be greater than or equal to a short int. The minimum size of a long int is 32 bits. The keywords signed and unsigned are the two sign qualifiers that specify whether a variable can hold both –ve and +ve numbers, or only +ve numbers. These qualifiers can be applied to the data types int and char only. Example: unsigned int I; The following table shows all valid data type combinations supported by C, along with their minimal ranges and typical memory size. Constants
The term constant means that it does not change
during the
execution of program.In the language C, constant and is the data
with a constant value that does not change in the program. For example,
in the program "100" "3.14" "'A'" "" Hello "" and the like, if you
write data directly, and constant. Moreover, also called a literal
constant. Constant expression is an expression consisting only of
constants. There are four basic types of constants in C. They are:
1.Integer
constants
2.Floating-point constants 3.Character constants 4.String constants Integer and floating-point constants represent numbers. They are often referred to as numeric-type constants. The following rule applies to all numeric type constants: Expressions
An expression is a sequence of operators and operands that specifies computation
of a value. An expression may consist of single entity or some combination of
such entities interconnected by one or more operators. All expression represents
a logical connection that's either true or false. Thus logical type expression
actually represents numerical quantities.
In C every expression evaluates to a value i.e., every expression results in
some value of a certain type that can be assigned to a variable. Some examples
of expressions are shown in the table given below.
A+b
3.14*r*r
a*a+2*a*b+b*bExample: Statements
The statements of a C program control the flow of program
execution.In C language several kinds of statements are available. They are
if statement
switch statement goto statement for statement while statement Symbolic constant in c LanguageA symbolic constant is name that substitute for a sequence of character that cannot be changed. The character may represent a numeric constant, a character constant, or a string. When the program is compiled, each occurrence of a symbolic constant is replaced by its corresponding character sequence. They are usually defined at the beginning of the program. The symbolic constants may then appear later in the program in place of the numeric constants, character constants, etc., that the symbolic constants represent.For example, a C program consists of the following symbolic constant definitions.#define PI 3.141593 #define TRUE 1 #define FALSE 0 #define PI 3.141593 defines a symbolic constant PI whose value is 3.141593. When the program is preprocessed, all occurrences of the symbolic constant PI are replaced with the replacement text 3.141593. Note that the preprocessor statements begin with a #symbol, and are not end with a semicolon. By convention, preprocessor constants are written in UPPERCASE. Example: 1#include<stdio.h> #include<conio.h> #define TRUE 1 #define PI 3.141593 void main() { float a; float b; float c; float d=PI; clrscr(); if(TRUE) { a=100; b=a*10; c=b-a; } printf("\na=%f\nb=%f\nc=%f\nPI=%f",a,b,c,d); getch(); } Example: 2OperatorsAn operator in general, is a symbol that operates on a certain data type. C language is very rich in operators. The commonly used operators include
do-while statement break statement continue statement expression statement compound statement return statement null statement |
No comments:
Post a Comment