Some simple c programs
Program to display the message “Hello World”
#include<stdio.h>
#include<conio.h>
void main()
{
printf(“Hello
World”);
getch();
}
Program to add two whole numbers
#include<stdio.h>
#include<conio.h>
void main( )
{
int a,b,c;
printf("Enter 1st number");
scanf("%d",&a);
printf("Enter 2nd number");
scanf("%d",&b);
c = a + b;
printf("Sum is %d", c);
getch( );
}
#include<conio.h>
void main( )
{
int a,b,c;
printf("Enter 1st number");
scanf("%d",&a);
printf("Enter 2nd number");
scanf("%d",&b);
c = a + b;
printf("Sum is %d", c);
getch( );
}
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:
Tokens
The basic elements recognized by the compiler are the token.
A token is a group of characters that logically belong together. A token is a
group of characters that the compiler does not break down into component elements.
A token is understood as a unit. In fact, a programmer can write a program by
using tokens. For example, identifiers, keywords, literals, operators, and
punctuators are tokens.
Delimiters
Delimiters are those special kinds of symbols which are used
in program to define the scope of anything. Delimiter is a character that
identifies the beginning or the end of a character string and is not the part
of the character string.
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
lower are treated differently.
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.
·
auto
·
default
·
float
·
register
·
struct
·
volatile
·
while
·
switch
·
return
·
for
·
do
·
break
·
case
·
double
·
goto
·
short
·
typedef
·
char
·
else
·
if
·
signed
·
union
·
const
·
enum
·
int
·
sizeof
·
unsigned
·
continue
·
extern
·
long
·
static
·
void
Data Types
C supports several 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 explicity. Data types can be either predefined or
derived.
The C languge supports four basic
data types, each of which are represented differently within the computer
memory.
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.
Variables
A variable is an identifier that
is used to represent some specified type of information in a program. It is a
named location in memory. It is used to hold a value that can be modified by a
program. It has a type associated with it.
Syntax:
data-type var1, var2, var3,… ;
eg: int count;
int
m,n;
char
letter;
When a variable is declared, its initial value is undefined.
Before using a variable, its value should be initialized to a known value. To
initialize a variable, the declaration must consist of a data type, followed by
a variable name, and equal sign (=) and a literal constant of the appropriate
type.
For eg: char ch=’a’;
int
sum=0;
Constants:
A constant is a data storage location used by a program.
Unlike a variable, the value stored in a constant cant be changed during
program execution. There are two types of constants in C: literal constants
(numeric, character, and string constants) and symbolic constants.
Literal Constants
1. Numeric Constants
Integer and floating point constants represent numbers. They
are often referred to collectively as numeric-type constants. Integer constants
are written without decimal point and floating point constants are written with
a decimal point and/or exponent. The following rules apply to all numeric type
constants:
·
Commas and blank spaces cannot be included
within the constant.
·
The constant can be preceded by a minus (-) sign
if desired. The minus sign is an operator that changes the sign of a positive
constant.
·
The value of a constant cannot exceed specified
minimum and maximum bounds. For each type of constant, these bounds will vary
from one C compiler to another.
2. Character Constants
Character constant is a single character, enclosed in
apostrophes. Character constants have integer values that are determined by the
computer’s particular character set. Most computers make use of the ASCII
character set. In ASCII, each individual character is numerically encoded with
its own unique 7-bit combination. For example:
Constant
|
Value
|
‘A’
|
65
|
‘3’
|
51
|
‘?’
|
63
|
Escape Sequences
Certain non printing characters like newline, as well as
backslash(\), apostrophe(‘), and certain other characters can be expressed in
terms of escape sequences. Escape sequences begin with a backward slash and are
followed by one or more special characters. An escape sequence is regarded as a
single character and is therefore valid as a character constant. It is also
called backslash character constant. The following are the commonly used escape
sequences.
3. String Constants
String constants consist of any
number of consecutive characters (including none), enclosed in (double)
quotation marks. A character constant (eg.’A’) and the corresponding
single-character string constant (“A”) are not equivalent. For example,
“Kathmandu, Nepal” “$34”
Symbolic Constants
Constant that is represented by
a name(symbol) in your program is called symbolic constant. The value
represented by this constant cannot be changed during program execution.
Whenever you need the constant’s value in your program, you use its name as you
would use a variable name. Thus, a symbolic constant allows a name to appear in
place of a numeric constant, a character constant or a string constant. When a
program is compiled, each occurrence of a symbolic constant is replaced by its
corresponding character sequence. C has two methods for defining a symbolic
constant: the #define directive and the const keyword. For example,
#define PI 3.14159
…
…
area= PI*(radius)*(radius);
OR
const double PI= 3.14159;
...
…
area= PI * (radius)*(radius);
Writing Comments
Comments are sentences that are not treated as a part of the
program. These are helpful in identifying the program’s principle feature or in
explaining the underlying logic of various program features. In C there are two
ways of putting comments on the program codes.
1.
Single line comments
It begins with // and end at the
end of the line. For example,
//This program adds two whole
numbers
2.
Multi line comments
It is enclosed in /*……………..*/
For example,
/*This program
adds
two whole numbers*/
Operators
An operator is a symbol that is used to perform
some action on one, two or three operands. Operators that perform on one
operand are called unary operators, that perform on two operands are called
binary operators, and that perform on three operands are called ternary
operators.
Operators and their
corresponding associativity are given in the table below where high precedence
operators appear before low precedence operators and operators within the same
group have same precedence. The associativity indicates the order of execution
of operators of equal precedence in an expression. When an expression contains
multiple operators, the precedence of the operators controls the order in which
the individual operators, the precedence of the operators controls the order in
which the individual operators are evaluated. For example, the expression x + y
* z is evaluated as x + (y * z) because the * operator has higher precedence
than the binary + operator. When an expression contains more than one operand
with the same precedence, the associativity of the operators controls the order
in which the operations are performed. Some operators are left-associative and
some are right-associative as shown in the table below. For example, x-y+z is
evaluated as (x – y) + z. However, the expression x = y = z is evaluated as x =
(y = z). Precedence and Associativity can be controlled using parenthesis. For example,
x + y * z first multiplies y by z and then adds the result to x, but (x + y) *z
first adds x and y and then multiplies the result by z.
Operator
|
Description
|
Associativity
|
()
|
Parenthesis (function call and
grouping sub expression)
|
Left-to-right
|
[]
|
Brackets (array subscript)
|
|
.
|
Member selection via variable
|
|
->
|
Member selection via pointer
|
|
++ --
|
Post-fix increment/decrement
|
No comments:
Post a Comment