C Language Introduction New
C Language Introduction New
2. Multi-line Comment in C
The Multi-line comment in C starts with a forward slash and asterisk ( /* ) and ends with an asterisk
and forward slash ( */ ). Any text between /* and */ is treated as a comment and is ignored by the
compiler.
It can apply comments to multiple lines in the program.
Syntax of Multi-Line C Comment
/*Comment starts
continues
continues
.
.
Comment ends*/
-------------------------------------------------------------------------------------------------------------------------
https://www.geeksforgeeks.org/tokens-in-c/?ref=lbp
Output:
Addition of a and b is …13
1. Documentation Section
It consists a set of comment lines used to specify the name of program, the author and
other details etc.,
2. Comments
Comments are very helpful in identifying the program features and under lying logic
of the program. The lines begins with ‘/*’ and ending with ‘*/’are known as comment lines.
These are not executable, the compiler is ignored any thing between /* and */
3.Preprocessor Section
It is used to link system library files, for defining the macros and for defining conditional
inclusion.
Eg:
#define pi 3.24
#if def
#endif
6. Executable part
It contains atleast one valid statement .
The execution of the program begins with open brace ‘{‘ and ends with closing brace
‘}’.
Note:
All the statements in the program ends with semicolon except conditional and control
statements.
----------------------------------------------------------------------------------------------------------------
Tokens in C
A token in C can be defined as the smallest individual element of the C programming language that
is meaningful to the compiler. It is the basic component of a C program.
Types of Tokens in C
The tokens of C language can be classified into six types based on the functions they are used to
perform. The types of C tokens are as follows:
1.Identifiers
2.Keywords
3.Constants
4.Strings
5.Special Symbols
6.Operators
Identifiers
Identifiers are name given to various program elements such as variable, functions and array
etc.,
Rules for naming an Identifiers
Identifiers consist of letters and digits in any order.
The first character must be letter/character or may begin with underscore(_).
The underscore ‘_’ can also be used and considered as a letter.
An identifier can be any length but C compiler recognizes only the first 31 characters.
No space and special symbols are allowed between the identifier.
The identifier can not be a keyword.
KEYWORDS (32)
Reserved words are called Keywords, that have standard and predefined meaning.
Which cannot be changed and they are the basic building blocks for program statements.
All keywords must be written in lower case.
auto break case char const continue default do double enum else
extern float int signed unsigned struct for long short
switch void goto return static typedef volitile
if register sizeof union while
---------------------------------------------------------------------------------------------------------------------------
DATA TYPES
Data types is the type of data, that we are going to access within the program.
Each data type may have predefined memory requirement and storage representation
C supports 4 classes of Data types
C Data Types
All C compilers supports the five fundamental data types called int, char, float, double, and void.
The Primary data types are divided into the following.
INTEGER TYPE
(16 or 32 bits)
Signed Unsigned
FLOAT TYPE
(32 bits with 6 digits of precision)
void -is the null data type , generally specified with the function which has no arguments
---------------------------------------------------------------------------------------------------------------------------
Constants
The values of the variable name can not be changed during the execution of the program.
CONSTANTS
NUMERIC CHARACTER
1) Integer 1) Character
2) Real 2) String
1) Numeric constant
A) Interger Constant:
It is formed with sequence of digit.
Example:
Marks=98
Percentage=85
Rules for defining Integer Constant
It must have atleast one digit.
Decimal point is not allowed
It can be either positive or negative
No comma or blank spaces are allowed
The range limit will be -32768 to +32767
B) Real constants
A real constant formed with the sequence of numeric digits with decimal point.
It is used to represent quantities that varies continuously such as distance, heights, temperature
etc.,
Eample:
Distance=126.4
Height=3.9
Speed=3e11
2) Character Constant
A) Character Constant
The character constant contains a single character enclosed within a pair of single inverted comma
both pointing to the left.
The characters may be letters, numbers, special characters and blank space etc.,
Eg:
“HI”, “CSE”, “32.66” , “30”
---------------------------------------------------------------------------------------------------------------------------
Variables
Variable is an identifier that is used to represent some specified type of information within a
designated portion of the program.
Variable can take different values at different times during the execution. ( It is named as memory
location).
3.Length of the variable name can not exceed up to 8 characters long, some C compiler accepts 32
characters long.
4.No comma or blank space or special symbols are allowed with in a variable name.
Variable Declaration
It tells the compiler what the variable name and type of the data that the variable will hold.
Syntax:
data_type v1,v2,v3,……………..,vn;
where data_type is the type of the data
v1,v2,v3,….vn are the list of variables
Eg: int code
float price
char name[10]
Here code is the type of integer, price is the type of float and name[10] is defined as an
array of character.
identifier is the identifier refers to the new name given to the data type.
Eg:
Here marks is the type of int and this can be later used to declare variables.
b) Enumerated data type: The C language provides another user defined data type is
called enumerated data type.
Syntax:
Eg 1:
enum week {Sun, Mon, Tue, Wed, Thu, Fri, Sat};
Note: the compiler assigns integer values from 0 to all the enumeration constants
automatically.
Eg 2:
enum Level {
LOW = 5,
MEDIUM, // Now 6
HIGH // Now 7
};
Scope of variables
Scope of a variable implies the availability of variables within the program. Variables have
two types of scopes described below.
i) Local variables:
The variables which are defined inside a function block or inside a compound
statement of a function sub-program are called local variables.
Eg:
function( )
{
int i, j;
/* body of the function */
}
The integer variables i, j are defined inside the function block of function( ).. Hence I,
j are called local variables.
ii) Global / External variable:
The variables that are declared before the main( ) are called the global/external
variables.
Eg.
main( ) fun ( )
{ {
fun( ); sum= a + b;
} }
The integer variables a,b are global / external variables, as they are declared before the
main ( ). these variables are used later in the fun( ).
----------------------------------------------------------------------------------------------------------------
Storage Classes
In C language through declaration statements memory is allocated temporarily for all the
variables defined.
The size of the allocated memory varies with respect to the type of variable.
C language supports 4 types of storage classes and these are used to specify the scope of
different variables defined with in function blocks and programs.
1) auto (automatic)
2) static
3) extern
4) register
1) auto
After the execution all the automatic variables will get disposed.
Syntax:
2) static
The static variables are the variables for which the contents of the variables will be
retained through out the program.
These are permanent within the function in which they are declared.
Syntax:
Eg;
static int a, b;
3) extern:
The external variables are declared out of the main() function.
The availability of these variables are through out the program and that is both in
Syntax:
extern int a, b;
4) Register
Registers are special storage class which resides within a Central Processing Unit.
The actual arithmetic and logical operations are carried out within these register.
Syntax:
Eg;
register int a, b;
-> The above declaration specifies that the variables a, b will be integer variables
with storage class register.Hence the values of a,b will be stored within the registers of
computer CPU rather than memory.
Delimeters
These are the symbols, which have some syntactic( grammatical arrangement) meaning.
----------------------------------------------------------------------------------------------------------------
The data items that operators acts upon are called operands.
Some operators require two operands called binary operators, while other acts upon
only one operand called unary operator.
Eg:
a+b
Types of operators
1) Arithmetic operators
2) Relational operators
3) Logical operators
4) Assignment operators
6) Conditional operators
7) Bitwise operators
8) Special operators
1. Arithmetic Operators
‘C’ allows to carry out basic Arithmetic operations like addition, subtraction,
multiplication and division.
The following table shows the Arithmetic Operators and their meaning.
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo Division
All the above operators are called ‘Binary’ operators as they act upon two operands
at a time.
Eg:
main( )
c=a%b;
OUTPUT
i) Unary Arithmetic
Eg: +x, -y
It requires both operands whose values are integer values for arithmetic operation.
Expression Result
a+b 9
a-b 1
It requires both operands which are float type for arithmetic operation.
Expression Result
a+b 10.0
a-b 3.0
2. Relational Operators
Relational operators are used to compare two or more operands.
Operators Meaning
== is equal to
!= is not equal to
Syntax:
Suppose the ‘a’, ‘b’ and ‘c’ are integer variables, whose values are 1, 2, 3 respectively.
a<b True 1
(a+b)>=c True 1
(b+c)>(a+5) False 0
c!=3 False 0
b= =2 True 1
3. Logical Operators
Logical operators are used to combine the results of two or more conditions or
expressions.
Operator Meaning
|| Logical OR
! Logical NOT
&& - This operator returns true if both operands are non-zero (true).
Eg: (exp1)&&(exp2)
|| - This operator returns true if at least one of the operands is non-zero (true).
! - This operator reverses (inverts) the truth value of its operand. If the operand is non-zero
(true), it returns false, and vice versa.
Eg: !(exp1)
Eg:
4. Assignment Operator
Syntax:
x=10;
x=a+b;
x=y;
i) Compound Assignment
Some of the compound Assignment operators and their meaning are given in the table.
+= x += y x=x+y
-= x -= y x=x-y
*= x *= y x=x*y
/+ x /= y x=x/y
%= x %= y x=x%y
ii) Nested (or) Multiple Assignments
Syntax:
Var1=var2=var3=…..=varn=single variable or
expression;
Eg:
i = j = k = 1;
x = y = z = (i + j + k);
C has two way useful operators , these are increment ( ++ ) and decrement (-- )
operators.
The ‘ ++ ‘ adds one to the variables and ‘ -- ‘ subtracts one from the variable.
Operator Meaning
++ x Pre increment
Eg:
main( )
{
int a=10, b=10;
Output:
a ++ = 10
++b = 11
--a = 10
b-- = 11
Where
Conditional operator itself checks the condition and executes the statement depending
on the condition.
Syntax:
Eg:
main()
big = a>b ? a : b;
Output
Big is ……5
In the above case, it checks the condition ‘a>b’ if it is true, then the value of ‘a’ is stored in
‘big’ otherwise the value of ‘b’ is stored in ‘big’.
7. Bitwise Operator
Operator Meaning
| Bitwise OR
^ Bitwise XOR
<< Shift left
~ One’s complement
This operator is represented as ‘&’ and operates on two operands of integer type.
While operating upon these two operands they are compared on a bit by bit basis.
(Both the operands must be of same type).
& 0 1
0 0 0
1 0 1
Eg:
Bitwise OR ( | ):
This operator gives if either of the operand bit is ‘1’ then result is ‘1’ or both operands
are 1’s then also given ‘1’.
| 0 1
0 0 1
1 1 1
Eg: x=7 0000 0111
Here integer gives if either of the operand bit is high (1) then it gives high (1)result.
^ 0 1
0 0 1
1 1 0
One’s Complement
The 1's complement is used to represent the negative of a given binary number.
To find the 1's complement of a binary number by changing all the 0s to 1s and all the 1s
to 0s in the number.
Eg: x = 14 1110
Operators Meaning
Sizeof Sizeof operator (It is a unary operator that returns the length in
bytesof the specified variable.
Eg:
main()
int a;
printf(“%d”, sizeof(a));
Expressions
An expression represents data item such as variables, constants and are interconnected
with operators. An expression is evaluated using assignment operator.
Syntax:
Variable=expression;
Eg: x=a*b-c;
In the above statement the expression is evaluated first from left to right. After the
evaluation of the expression the final value is assigned to the variable from right to left.
Arithmetic Operators Precedence
Usually the Arithmetic Operators are evaluated from the left to right using the
precedence of operators when the expression is written the parameters.
Precedence Operators
High * / %
Low + -
The Arithmetic expression evaluation is carried out using the two phases from the left to right
through the expression.
First phase: The highest priority operators are evaluated in the expression.
Second Phase: the lowest priority operators are evaluated in the expression.
Eg:
result = x - y / 3 + z * 3 - 1
where x = 3, y = 9, z = 77
result = 3 - 9 / 3 + 77 * 3 - 1
First phase:
result = 3 - 3 + 77 * 3 - 1
result = 3 - 3 + 231 - 1
Second phase:
result = 0 + 231 - 1
result = 231 - 1
result = 230
However, during the evaluation of expression, the order of evaluation can be changed by
using the parenthesis in an expression.
Eg:
( 3 - 9 ) / 3 + 77 * ( 3 - 1 )
= - 6 / 3 + 77 * ( 3 - 1 )
= - 6 / 3 + 77 * 2
= - 2 + 154
= 152
2) Evaluate the arithmetic expression from left to right using the rules of precedence.
5) Evaluate the inner most sub expression if the parenthesis are nested.
The associative property in mathematics refers to the property of some binary operations
where rearranging the parentheses in an expression does not change the result 12345.
Specifically:
In addition: (a + (b + c) = (a + b) + c)
In multiplication: (a(bc) = (ab)c)