0% found this document useful (0 votes)
10 views

Oops

The document discusses the different elements or tokens that make up the C programming language including comments, variables, data types, constants, keywords, operators and expressions. It provides details on each of these elements including their syntax and examples to illustrate their usage.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Oops

The document discusses the different elements or tokens that make up the C programming language including comments, variables, data types, constants, keywords, operators and expressions. It provides details on each of these elements including their syntax and examples to illustrate their usage.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

Elements of C

1
Elements (Tokens) of C
 Token: Every individual unit involved in the program.

/*Find the number of Tokens*/


void main()
{
printf("i = %d”, i);
}
Elements (Tokens) of C
 Comment
 Variables / Identifiers
 Data types
 Reserved words / keywords
 Constants
 Operators
 Expressions
1. COMMENT
I. Comment
 Used for documenting code i.e., non executable code.
 provide information about lines of code.
 Future reference for the user.
 Single line comment - represented by double slash \\.
 Mutli line comment - represented by slash asterisk \* ... *\
 Example:
#include<stdio.h> Output:
// Main program
Hello C
void main(){
/*printing information
Multi-Line Comment*/
printf("Hello C");
}
2. VARIABLE
II. Variable
 Container storing data i.e., integer, float, character,…….
 Represent memory location through symbols or names.
 It can be changed or reused many times.
 NOTE: Before using variables, it is necessary to declare
them.
 Syntax:
Datatype VariableName;
 Example:
int x; float b; char str;
Naming rules of variables
 It can have alphabets, digits, and underscore.
 It start with the alphabet, and underscore only.
 It can't start with a digit / number.
 No whitespace is allowed within the variable name.
 It must not be any reserved word or keyword, e.g. int, struct, float, etc.
 Length is unlimited but 1st 31 characters are significant.
Necessity of declaring variable:
 Inform compiler about variable name and data type of the variable.
 Then the compiler allocate memory space for that variable.
 Example:
int a; // declaration
 NOTE: size of integer is 2 bytes so the compiler allocated 2 bytes for the
variable a.
Local Variables
 Local Variable: A variable that is declared inside the function
or block is called a Local variable.
 Example
void function()
{
int x=10; //Local variable
}
 You must have to initialize the local variable before it is used.
Global Variable
 Global variable: A variable that is declared outside the
function or block is called a Global variables. Any function
can change the value of the global variables. It is available to
all functions.
 It must be declared at the start of the block.
Example
int value=20; //Global Variable
void function()
{
int x=10; //Local variable
}
Static Variable
 A variable that is declared with the static keyword is called
static variable.
 It retains its value between multiple function calls.
Example
void function()
{
int x=10; //Local variable
static int y=10; // Static variable
x=x+1;
y=y+1;
printf(“%d, %d”, x,y);
}
If you call this function many times, the local variable will print the
same value for each function call, e.g, 11,11,11 and so on. But the
static variable will print the incremented value in each function
call, e.g. 11, 12, 13 and so on.
Quiz?
 Find valid and invalid identifiers:
 45sample;
 Sample_new;
 Sample new;
 float;
 floatsample;
 wel-come;
 _sample;
3. DATA TYPES
III. Data types
 Behaviour or type of the variable.
Data types in C
Types Data Types

Basic (Primary) data type int, float, double, char

Derived data type array, pointer, structure, union

Enumeration data type enum

Void data type Empty value

Boolean type True or False


(a) Basic data type
 int: It refers to positive and negative whole numbers
(without decimal), such as 10, 12, 65, 3400, etc.
 char: It refers to all the ASCII character sets within single
quotes such as „a‟,„A‟, etc.
 float: Refers to all the real number values or decimal points,
such as 3.14, 10
 double: Used when the range exceeds the numeric values
that do not come under either floating-point or integer data
type. .09, 5.34, etc.
Storage size & range
Example
OUTPUT:
//BasicDataType.c
#include <stdio.h> The integer value is: 5
void main()
The character value is: b
{
int i = 5; The float value is: 7.2357
printf("The integer value is: %d \n", i);
The double value is: 71.2357455
char c = 'b';
printf("The character value is: %c \n", c);
float f = 7.2357;
printf("The float value is: %f \n", f);
double d = 71.2357455;
printf("The double value is: %lf \n", d);
}
Data Type Modifiers In C
 Modify the meaning of fundamental data types. To adjust the
memory allocated for a variable, modifiers are prefixed with
fundamental data types.
 long
 short
 signed
 Unsigned
(b) Derived data types
 Derived from primitive data types.
 These are primary data types that are grouped together.
 We can group many elements of similar data types. For
example group of integer elements as declared as an integer
array.
 These data types are defined by the user.
 The following are the derived data types in C:
 Array
 Pointers
 Structure
 Union
(c) Enumerated data type
 User-defined data types that consist of integer values.
 Used to define variables that can only assign certain discrete
integer values in the program.
 Keyword: „enum‟
 Syntax:
enum flag {const1, const2, const3………};
Example
//EnumType.c
#include<stdio.h>
enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};
int main()
{ OUTPUT:
4
enum week day;
day = Fri;
printf("%d",day);
return 0;
}
(d) Void
 It is an empty data type that represents that no value is
available.
 It is used for functions.
 When we declare a function as void, it doesn‟t have to return
anything.
4. CONSTANT
IV. Constant
 Value that can not be changed in the program.
 Example:

Integer constant 23,5674,123,…………….


Floating point constant 23.55,67.22,…………….
Character constant „a‟, „v‟, „d‟,…………
String constant “asdf ”, “ghdf ”, ……..

 Two ways to define constant:


 int a = 34; char s = „g‟; float w = 1.22;
 # define PI 3.14 // PI = 3.14
5. KEYWORDS
V. Keywords
 There are 32 reserved words (keywords).

 NOTE: We can not use keywords as variable name.


6. OPERATORS
VI. Operators
 Symbol that tells the compiler to perform operations on the
operands.
Symbol Operator Type
++, -- Increment/decrement Unary Operator
+, -, *, /, % Arithmetic
<, <=,>,>=,==,!= Relational
&&, ||, ! Logical Binary Operator
&, |, <<, >>, ~,^ Bitwise
=, +=, -=, *=, /=, %= Assignment
?: Conditional Ternary
(a) Increment / Decrement
Example
//UnaryOp.c
#include<stdio.h>
OUTPUT:
#include<conio.h>
y=8 x=8
void main ( ) b=5 a=6
{
int x = 7, y,a=5,b;
y=++x; /*pre increment*/
printf(“y=%d \t x = %d\n”, y,x);
b=a++; /*post increment*/
printf(“b=%d \t a = %d\n”, b,a);
}
Example
#include<stdio.h>
void main()
{
int a=20;
printf("PostIncrement= %d\n",a++);
printf("Prencrement= %d\n",++a);
printf("PreDecrement= %d\n", --a);
printf("PostDecrement= %d\n", a--);
printf("Preincrement= %d\n", ++a);
}
(b) Arithmetic Operators
Example
// ArithmeticPgm.c
# include <stdio.h>
OUTPUT:
void main()
{ Addition: 50
int a=30,b=20; Subtraction: 10
Multiplication: 600
printf("Addition:%d\n", a+b); Division: 1
printf("Subtraction:%d\n", a-b); MOD: 10
printf("Multiplication:%d\n", a*b);
printf("Division:%d\n", a/b);
printf("MOD:%d\n", a%b);
}
(b) Assignment Operator
(b) Assignment Operator
Operator Description Example
= Assigns values from right side operands to x=y
left side operand
+= Adds the right operand to the left operand C += A is
and assign the result to the left operand equivalent to
C=C+A
-= Subtracts the right operand from the left C-=A is
operand and assigns the result to the left equivalent to
operand. C=C-A
*= Multiplies the right operand with the left C*=A is
operand and assigns the result to the left equivalent to
operand C=C*A
(b) Assignment Operator
Operator Description Example

/= Divides the left operand with the C /= A is equivalent to


right operand and assigns the result C=C/A
to the left operand

%= Takes modulus using two operands C %= A is equivalent


and assigns the result to the left to C = C % A
operand.
<<= Left shift AND assignment operator. C <<= 2 is same as C =
C << 2

>>= Right shift AND assignment operator. C >>= 2 is same as C =


C >> 2
(b) Assignment Operator
Operator Description Example

&= Bitwise AND assignment C &= 2 is same as C = C


operator. &2
^= Bitwise exclusive XOR and C ^= 2 is same as C = C
assignment operator. ^2
|= Bitwise inclusive OR and C |= 2 is same as C = C
assignment operator. |2
Example Program
//AssignOp.c
#include <stdio.h>
void main()
{ OUTPUT:
S=6
int s=4;
s+=2;
printf("S=%d",s);
}
(C)Comparison Operator(Relational Operator)

Operator Description Example


== Checks if the values of two operands are x==y
equal or not. If yes, then the condition
becomes true.

!= Checks if the values of two operands are x!=y


equal or not. If the values are not equal, then
the condition becomes true.
> Checks if the value of left operand is greater x>y
than the value of right operand. If yes, then
the condition becomes true.
Comparison Operator(Relational Operator)
OPERATOR DESCRIPTION EXAMPL
E
< Checks if the value of left operand is less than x<y
the value of right operand. If yes, then the
condition becomes true.

>= Checks if the value of left operand is greater x>=y


than or equal to the value of right operand. If
yes, then the condition becomes true.

<= Checks if the value of left operand is less than x<=y


or equal to the value of right operand. If yes,
then the condition becomes true.

NOTE: output of the relational operation is boolean value (0 or1).


Example Program
//RelationOp.c
#include <stdio.h>
#include <conio.h> OUTPUT:
void main() a==b: 0
a>b: 0
{ a<b: 1
int a=3,b=45;
printf("a==b: %d\n", a==b);
printf("a>b: %d\n", a>b);
printf("a<b:%d\n", a<b);
}
(d) Logical Operators :Combine two or
more relational expressions and produces boolean
type results.
Example Program
// LogicalOp.c
OUTPUT:
#include <stdio.h> a>b && a<b: 0
a>b || a<b: 1
void main() ! ( a>b && a<b )): 1
{
int a=3,b=45;
printf(“a>b && a<b: %d\n", a>b && a<b);
printf(" a>b || a<b : %d\n", a>b || a<b);
printf(“! ( a>b && a<b ) %d\n", ! ( a>b && a<b ));
}
(e) Bitwise Operator
Left and right shift
 Left shift << operator

 NOTE: shift a number by n position to left, the output will


be number * (2n).
 Right shift >> operator

 NOTE: shift a number by n times to right, the output will


be number / (2n) .
Example
 Let us assume x = 10 , y =12

x&y 0000 1010 & 0000 1100 = 0000 1000


(10) & (12) = (8)
x|y 0000 1010 & 0000 1100 = 0000 1110
(10) | (12) = (14)
x^y 0000 1010 ^ 0000 1100 = 0000 0110
(10) ^ (12) = (6)
~x NOTE: ~x= - (x+1) ~(10) = - (10+1) = -11
x<<1 0000 1010 << 1 = 0001 1000
(10)<<1 = (20)
x>>1 0000 1010 >> 1 = 0000 0110
(10)>>1 = (5)
Example
// BitwiseOp.c
#include<stdio.h>
void main ( )
{
int x = 10, y = 6;
printf ( "x & y = %d\n", x & y );
printf ( "x | y = %d\n", x | y );
printf ( “~11 = %d\n", ~11 );
printf ( "x ^ y = %d\n", x ^ y );
printf ( "y << 1 = %d\n", y << 1 );
printf ( "y >> 1 = %d\n", y >> 1 );
}
(f) Conditional operator (?:)
 Decision-making statements which depends upon the output
of the condition.
 Syntax: (condition) ? True statement : False statement;
 Example: Minimum = (2<1) ? 2 : 1
(g) Example - conditional & sizeof
operator
//CondOpSizeOp.c OUTPUT:
void main()
Minimum =1
{ size of myInt: 4
int myInt; size of myFloat: 4
float myFloat; size of myDouble: 8
size of myChar: 1
double myDouble;
char myChar;
int Minimum = (2<1) ? 2 : 1 ;
printf(“Minimum =%d”, Minimum );
printf(“size of myInt: %d\n", sizeof(myInt));
printf(“size of myFloat:%d\n", sizeof(myFloat));
printf(“size of myDouble:%d\n", sizeof(myDouble));
printf(“size of myChar:%d\n", sizeof(myChar));
}

You might also like