Wednesday, July 19, 2017

Fundamentals of C
·         Character Set used in C
·         Use of Comments
·         Identifiers, Keywords and Tokens.
·         Data Types in C
·         Constants and Variables
·         Type of Specifier
·         Statements – Simple and Compound Statements


Character Set
The set of characters that are used to form words, numbers and expression in C is called C character set.
Following character sets are supported by C as:
a)  All decimal digits from 0 to 9.
b)  Letters or alphabets
               i.     Uppercase letter: from A to Z (ASCII value 65 to 90)
              ii.     Lowercase letter: from a to z (ASCII value 97 to 122)
c)   Special Symbols or Characters: - C language contains the following special character in association with the letters and digits.

Symbol


Meaning

 ~ 
Tilde
 ! 
Exclamation mark
 # 
Number sign( hash)
 $ 
Dollar sign
Percent sign
 ^ 
Caret
 & 
Ampersand
  * 
Asterisk
(  
Lest parenthesis
 ) 
Right parenthesis
Underscore
 + 
Plus sign
 | 
Vertical bar
  \ 
Backslash
 `
Apostrophe
 - 
Minus sign
 = 
Equal to sign
  { 
Left brace
  } 
Right brace
 [ 
Left bracket
Right bracket
 :
Colon
 " 
Quotation mark
 ; 
Semicolon
 < 
Opening angle bracket
 > 
Closing angle bracket
  ? 
Question mark
 , 
Comma
 . 
Period
  / 
Slash

d)  White spaces
             i.     Blank space.
            ii.     Horizontal tab
            iii.     Vertical tab.
            iv.     Carriage return.
            v.     New line or line feed.
            vi.     Form feed.

Use of Comments
A comment is a programmer-readable annotation in the source code of a computer program. Generally, Comments are used to provide the description about the Logic written in program. Comments are not display on output screen.
Comments provide clarity to the C source code allowing others to better understand what the code was intended to accomplish and greatly helping in debugging the code. It is generally ignored by compiler and interpreter. It can be written anywhere in the program as you needed.

A comment can be written in two forms:
a.   Single-line comment: it begins with the double forward slash (//).
For example:
// WAP to add any two numbers.
b.   Multi-line comment: it starts with a forward slash & asterisk (/*) and ends with a asterisk & forward slash (*/).
For example:
/* WAP to find area and perimeter of a circle.
Where radius of a circle is 7            */    
Look in turbo compiler:
comment in c

Nested Comments are not possible, that means comments within comments.
Example
void main()
{
/*
/*      comments   */
*/
}

Comments are not case sensitive.
Example
void main()
{

/*  MAIN Function BODY   */

}

Definition of identifiers

Identifiers are sequences of characters used for giving name to variables, functions, arrays, pointers, constants, user define data type and preprocessor macros. 

 

The rules for writing identifiers

a.   An Identifier can only have alphanumeric characters ( a-z , A-Z , 0-9 ) and underscore( _ ).
b.   The first character of an identifier can only contain alphabet ( a-z , A-Z ) or underscore ( _ ).
c.   Identifiers are also case sensitive in C. For example roll and Roll are two different identifier in C.
d.   Keywords are not allowed to be used as Identifiers.
e.   No special characters, such as semicolon, period, whitespaces, slash or comma are permitted to be used in or as Identifier.
f.   White space is not allowed between two words or letter.
g.   Only first 31 characters are significant.

For example:
Valid: A1    roll   e_name           kic_1   etc.
Invalid: 1A  int    35x         kic-1  x.1   etc.

Keywords
Keyword is a predefined or reserved word in C library with a fixed meaning and used to perform an internal operation. These meaning cannot be changed. Thus, the keywords cannot be used as variable names because that would try to change the existing meaning of the keyword, which is not allowed.  C Language supports 32 keywords.
Every Keyword exists in lower case latter like auto, break, case, const, continue, int etc.

32 Keywords in C Language

auto
double
int
struct
break
else
long
switch
case
enum
register
typedef
char
extern
return
union
const
float
short
unsigned
continue
for
signed
void
default
goto
sizeof
volatile
Do
if
static
while


C Token
      A token is source-program text that the compiler does not break down into component elements.
@  C tokens are the basic buildings blocks in C language which are constructed together to write a C program.
@  Each and every smallest individual unit in a C program is known as C tokens.

C tokens are of six types. They are,
1.   Keywords                     (eg: int, while),
2.   Identifiers                     (eg: main, total),
3.   Constants                     (eg: 10, 20),
4.   Strings                               (eg: “total”, “hello”),
5.   Special symbols                (eg: (), {}),
6.   Operators                     (eg: +, /,-,*)

C TOKENS EXAMPLE PROGRAM:

int main()
{
   int x, y, total;
   x = 10, y = 20;
   total = x + y;
   printf ("Total = %d \n", total);
}
Where,
·         main – identifier
·         {,}, (,) – delimiter
·         int – keyword
·         x, y, total – identifier
·         main, {, }, (, ), int, x, y, total – tokens

Data types
Data type is a keyword used to identify type of data. Data types are used for storing the input of the program into the main memory (RAM) of the computer by allocating sufficient amount of memory space in the main memory of the computer.
Simply, a data type specifies the type of data that a variable can store such as integer, floating, character etc.
PTR
@  C data types are defined as the data storage format that a variable can store a data to perform a specific operation.
@  Data types are used to define a variable before to use in a program.
@  Size of variable, constant and array are determined by data types.

ANSI C language supports 3 different types of data types
1)  Primary (or fundamental) data types
2)  Derived data types later.
3)  User-defined data types.
datatype.png 1.jpeg
Primary data types
These are the data types whose variable can hold maximum one value at a time, in C language it can be achieve by int, float, double, char.
Primary data types in c
Integer type
Integers are used to store whole numbers. Size and range of Integer type on 16-bit machine.

Type
Size(bytes)
Conversion character
Range
example
int or signed int
2
%d
-32,768 to 32767
int a;
signed int a;
unsigned int
2
%u
0 to 65535
unsigned int a;
unsigned b;
short int or signed short int
2
%d or %i
-128 to 127
short int b;
short c;
Unsigned int
2
%u
0 to 65535
unsigned short int a;
unsigned short b;
long int or signed long int
4
%ld
-2,147,483,648 to 2,147,483,647
signed long int a;
long int a;
long a;
unsigned long int
4
%ul
0 to 4,294,967,295
unsigned long int a;
unsigned long a;

Floating type
Floating types are used to store real or fractional numbers (values).

Type
Size(bytes)
Conversion character
Range
Example
Float
4
%f
3.4E-38 to 3.4E+38
float a;
float x, y;
double
8
%lf
1.7E-308 to 1.7E+308
double a;
long double
10
%Lf
3.4E-4932 to 1.1E+4932
long double a;


Character type
Character types are used to store characters value.

Type
Size(bytes)
Range
Conversion character
Example
char or signed char
1
-128 to 127
%c
char x;
unsigned char
1
0 to 255
%c
char y;

Void Data Type
Void is an empty data type that has no associated value. This is usually used to specify the type of functions.

Derived Data Type
Data types that are derived from fundamental data types are called derived data types (Array, Structures, Unions and Pointers). There are some data types which are derived from the existing primary data types.
For example:
struct kic{
int x,y;
float m;
char c;
}
Where, struct is a derived data types ‘structure’.

User defined data types
User defined data types are those data types which are defined by the user/programmer.
The typedef statement is used to give new name to an existing data type. It allows users to define new data types that are equivalent to existing data types. It takes the general form-
Typedef existing data type user define data type name;
For example:
Typedef int integer;
Where, integer is same as int.
Int x; or integer x;
Constant
A constant is an identifier whose value cannot be changed at the execution time of program. Constants are also known as literals.
In general constant can be used to represent as fixed values in a C program. For Example:     'A', 1234, 123.5, "Kantipur".
Constants are classified into following types.
1)  Numeric constant
a)  Integer constant
                       i.     Decimal
                      ii.     Octal
                     iii.     Hexadecimal
b)  Real constant
                       i.     Fractional form
                      ii.     Exponential form
2)  Character constant
a)  Single character
b)  String constant

Integer Constants
Integer constants are whole numbers without any fractional part or exponential part.
Point to remember
@  An integer constant must have at least one digit.
@  It must not have a decimal point.
@  It can either be positive or negative.
@  No commas or blanks are allowed within an integer constant.
@  If no sign precedes an integer constant, it is assumed to be positive.
@  The allowable range for integer constants is -32768 to 32767.

    An integer constant can be a decimal, octal, or hexadecimal. A prefix specifies the base of number system.

Prefix
Description
Base
Example
0x or 0X
Hexadecimal Number: - it consists of any combination of digits from the set 0 to 9 and A to F, with a leading ox 0x or 0X.
16
0x5C, 0x22
0
Octal Number: it consists of any combination of digits from the set 0 to 7, with a leading 0.
8
012C, 0243
Nothing
Decimal Number: -it consists of any combination of digits from the set 0 to 9, with no leading.
10
25, 100

Floating Point Constants
A floating point constant is a numeric constant that has either a fractional form or an exponent form (it has two parts as mantissa and exponent).
For example:
-2.0
0.0000234
-0.22E-5
Note: E-5 = 10-5
Where, the digit before ‘e’ is called mantissa and after is called exponent.
Point to remember
@ A real constant must have at least one digit
@ It must have a decimal point
@ It could be either positive or negative
@ If no sign precedes an integer constant, it is assumed to be positive.
@ No commas or blanks are allowed within a real constant.

Character constants
A character constant is a constant which uses single quotation around characters.
For example: 'a', 'l', 'm', 'F'
String Constants
string constant is a set of characters enclosed between a pair of double quotes. A string literal is actually stored as a character array.
For Example
" “: Null String.
"A”: String literal having single characters.
"ymk123@gmail.com”: String literal with multiple characters.
"Manish Yadav\n”: String with spaces and escape characters.

Point to remember
@ A character constant is a single alphabet, a single digit or a single special symbol enclosed within single quotes.
@ The maximum length of a character constant is 1 character.
@ String constants are enclosed within double quotes.

Variable
Variable is an identifier which holds data or another one variable. It is an identifier whose value can be changed at the execution time of program. It is used to identify input data in a program.
variable is a name of memory location. It is used to store data. Its value can be changed and it can be reused many times.
Rules to declare a Variable
@ Every variable name should start with alphabets or underscore (_).
@ No spaces are allowed in variable declaration.
@ Variable name must be up to 8 characters.
@ Variable name can consist of alphabets, digits and special symbols like underscore _.
@ Blank or spaces are not allowed in variable name.
@ Keywords are not allowed as variable names.
@ Variable name is case sensitive.
@ Variable type can be char, int, float, double or void.

Declaration of variable
Declaration of variables must be done before they are used in the program. Declaration does two things.
@ It tells the compiler what the variable name is.
@ It specifies what type of data the variable will hold.
Syntax:
Data type variable name;
For example:           
int a, b;
float r;
char ch;




Labels:

0 Comments:

Post a Comment

thank you

Subscribe to Post Comments [Atom]

<< Home