0% found this document useful (0 votes)
34 views54 pages

8 Unnamed 01 08 2023

The document discusses different types of variables in C programming including local, global, static, automatic and external variables. It also discusses data types, keywords, escape sequences and integer representations in C.

Uploaded by

Dhruv Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views54 pages

8 Unnamed 01 08 2023

The document discusses different types of variables in C programming including local, global, static, automatic and external variables. It also discusses data types, keywords, escape sequences and integer representations in C.

Uploaded by

Dhruv Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 54

C programming

Dr. Poongundran Selvaprabhu, B.E., M.S.,


Ph.D.,
Assistant Professor (Senior),
Department of Communication Engineering,
School of Electronics Engineering (SENSE),
Vellore Institute of Technology (VIT),
Vellore, Tamil Nadu, India.
Programming

2 Dr. Poongundran Selvaprabhu 19/02/2024


C Data Types
19/02/2024 Dr. Poongundran Selvaprabhu 4
Rules for defining variables
• A variable can have alphabets, digits, and
underscore.
• A variable name can start with the alphabet,
and underscore only. It can't start with a digit.
• No whitespace is allowed within the variable
name.
• A variable name must not be any reserved
word or keyword, e.g. int, float, etc.
Variable
Valid variable names:

• int a;
• int _ab;
• int a30;

Invalid variable names:

• int 2;
• int a b;
• int long;
Variables in C
A variable is a name of the memory location. It is used to
store data. Its value can be changed, and it can be reused
many times.
It is a way to represent memory location through symbol
so that it can be easily identified.
int a=10,b=20;
float f=20.8;
char c='A';

7 Dr. Poongundran Selvaprabhu 19/02/2024


Variable (vs) Constant
Types of Variables in C
type variable_list;
There are many types of variables in c:

10 Dr. Poongundran Selvaprabhu 19/02/2024


Local Variable
A variable that is declared inside the function or
block is called a local variable.
They can be used only by statements that are inside
that function or block of code.
Local variables are not known to functions outside
their own.
It must be declared at the start of the block.
You must have to initialize the local variable before
it is used.

11 Dr. Poongundran Selvaprabhu 19/02/2024


Local Variables
#include <stdio.h>
int main () {
/* local variable declaration */
int a, b, c;
/* actual initialization */
a;
b;
c = a + b;
printf ("value of a = %d, b = %d and c = %d\
n", a, b, c);
return 0; }
12 Dr. Poongundran Selvaprabhu 19/02/2024
Global Variable
A variable that is declared outside the function or block
is called a global variable.
Global variables are defined outside a function, usually
on top of the program.
Global variables hold their values throughout the
lifetime of your program and they can be accessed inside
any of the functions defined for the program.
A global variable can be accessed by any function. That
is, a global variable is available for use throughout your
entire program after its declaration.
It must be declared at the start of the block and any
function can change the value of the global variable.
13 Dr. Poongundran Selvaprabhu 19/02/2024
Global Variable
#include <stdio.h>
/* global variable declaration */
int g;
int main ()
{ /* local variable declaration */
int a, b;
/* actual initialization */
a = 10; b = 20;
g = a + b;
printf ("value of a = %d, b = %d and g = %d\n", a, b, g);
return 0; }
14 Dr. Poongundran Selvaprabhu 19/02/2024
Global Variable
#include <stdio.h>
/* global variable declaration */
int g = 20;
int main ()
{
printf ("value of g = %d\n", g);
return 0; }
15 Dr. Poongundran Selvaprabhu 19/02/2024
#include <stdio.h> Global Variable
/* global variable declaration */
int g = 20;
int main ()
{
/* local variable declaration */
int g = 10;
printf ("value of g = %d\n", g);
return 0; }
16 Dr. Poongundran Selvaprabhu 19/02/2024
Static Variable
A variable that is declared with the static
keyword is called static variable.
It retains its value between multiple function
calls.
Static variables have a property of stabilizing
their value even after they are out of their
scope.
 Static variables preserve their previous value
in their previous scope and are not initialized
again in the new scope.
17 Dr. Poongundran Selvaprabhu 19/02/2024
Static variables
#include<stdio.h>
int fun()
{ static int count = 0;
count++;
return count;
}
int main()
{
printf("%d ", fun());
printf("%d ", fun());
return 0; }
18 Dr. Poongundran Selvaprabhu 19/02/2024
Static variables
#include<stdio.h>
int fun()
{ int count = 0;
count++;
return count;
}
int main()
{
printf("%d ", fun());
printf("%d ", fun());
return 0; }
19 Dr. Poongundran Selvaprabhu 19/02/2024
Automatic (auto) variables
The variables which are declared inside a block are
known as automatic or local variables.
 These variables allocates memory automatically upon
entry to that block and free the occupied memory upon
exit from that block.
These variables have local scope to that block only that
means these can be accessed in which variable declared.
Keyword 'auto' may be used to declare automatic
variable but we can declare these variable without using
'auto' keywords.

20 Dr. Poongundran Selvaprabhu 19/02/2024


Automatic Variable
All variables in C that are declared inside the block, are
automatic variables by default. We can explicitly declare
an automatic variable using auto keyword.
void main(){
Char x[];//local variable (also automatic)
auto int y=20;//automatic variable
}
these variables allocates memory automatically upon
entry to that block and free the occupied memory upon
exit from that block.

21 Dr. Poongundran Selvaprabhu 19/02/2024


Automatic Variable
#include <stdio.h>
int main( )
{
auto int j = 1; "visibility level"
{ for auto variables in each block code
auto int j= 2; which are independently to each other:
{
auto int j = 3;
printf ( " %d ", j);
}
printf ( "\t %d ",j);
}
printf( "%d\n", j); return 0;}
22 Dr. Poongundran Selvaprabhu 19/02/2024
External Variable
 External variables are also known as global variables. These
variables are defined outside the function.
 These variables are available globally throughout the function
execution.
 The value of global variables can be modified by the functions.
“extern” keyword is used to declare and define the external
variables.

 Scope − They are not bound by any function. They are


everywhere in the program i.e. global.
 Default value-Default initialized value of global variables are
Zero.
 Lifetime − Till the end of the execution of the program.
23 Dr. Poongundran Selvaprabhu 19/02/2024
Important points about extern
• External variables can be declared number of times but
defined only once.
• “extern” keyword is used to extend the visibility of function
or variable.
• By default the functions are visible throughout the program,
there is no need to declare or define extern functions. It just
increase the redundancy.
• Variables with “extern” keyword are only declared not
defined.
• Initialization of extern variable is considered as the
definition of the extern variable.
24 Dr. Poongundran Selvaprabhu 19/02/2024
External Variable
#include <stdio.h>
extern int x=32;
int b=8;
int main()
{
auto int a=28;
extern int b;
printf("Value of auto variable : %d\n", a);
printf("Value of extern variables x & b : %d,%d\n",x,b);
x=15;
printf("The value of modified extern variable x : %d\n",x);
return 0;
}
25 Dr. Poongundran Selvaprabhu 19/02/2024
Basic Data Types

26 Dr. Poongundran Selvaprabhu 19/02/2024


Keywords

27 Dr. Poongundran Selvaprabhu 19/02/2024


KEYWORD In Program

28 Dr. Poongundran Selvaprabhu 19/02/2024


Keywords

29 Dr. Poongundran Selvaprabhu 19/02/2024


List of Constants in C

30 Dr. Poongundran Selvaprabhu 19/02/2024


List of Constants in C

31 Dr. Poongundran Selvaprabhu 19/02/2024


Escape Sequences
Escape Sequences Character
\b Backspace
\n Newline
\r Return
\t Horizontal tab
\v Vertical tab
\\ Backslash
\' Single quotation mark
\" Double quotation mark
\? Question mark
\0 Null character
32 Dr. Poongundran Selvaprabhu 19/02/2024
Integers- {decimal, octal, hexadecimal} Binary
An integer is a numeric literal(associated with numbers)
without any fractional or exponential part. There are three
types of integer literals in C programming:
decimal (base 10)
octal (base 8)
hexadecimal (base 16)
Binary(base 2)
For example:
Decimal: 0, -9, 22 etc
Octal: 021, 077, 033 etc
Hexadecimal: 0x7f, 0x2a, 0x521 etc

33
Binary: 0,1Selvaprabhu
Dr. Poongundran 19/02/2024
#include<stdio.h>
void convert(int, int);
int main() Part1
{ int num;
printf("Enter a positive decimal number : ");
scanf("%d", &num);
printf("\nBinary number :: ");
convert(num, 2);
printf("\n");
printf("\nOctal number :: ");
convert(num, 8);
printf("\n");
printf("\nHexadecimal number :: ");
convert(num, 16);
printf("\n");
return 0;
34}/*EndDr. Poongundran Selvaprabhu 19/02/2024
of main()*/
void convert (int num, int base)
{
Part 2
int rem = num%base;

if(num==0)
return;
convert(num/base, base);

if(rem < 10)


printf("%d", rem);
else
printf("%c", rem-10+'A' );
}/*End of convert()*/
35 Dr. Poongundran Selvaprabhu 19/02/2024
Floating-point Literals
 A floating-point literal is a numeric literal that has either a
fractional form or an exponent form. For example:

#include<stdio.h>
int main(){
float X1, X2, X3;
X1=-2.0;
X2=0.0000234 ;
X3=-0.22E-5 ; [Note: Note: E-5 = 10-5 ]
return 0;
}

36 Dr. Poongundran Selvaprabhu 19/02/2024


C Arithmetic Operators
An arithmetic operator performs mathematical
operations such as
Addition,
Subtraction,
Multiplication,
Division etc on numerical values
(constants and variables).

37 Dr. Poongundran Selvaprabhu 19/02/2024


Arithmetic Operators
// Working of arithmetic operators
#include <stdio.h>
int main()
{ int a = 9,b = 4, c; Output:
c = a+b;
a+b = 13
printf("a+b = %d \n",c);
a-b = 5
c = a-b;
a*b = 36
printf("a-b = %d \n",c);
c = a*b;
a/b = 2
printf("a*b = %d \n",c); Remainder when a divided by b=1
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0; }
38 Dr. Poongundran Selvaprabhu 19/02/2024
C Assignment Operators
Operator Example Same as
= a=b a=b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b

39 Dr. Poongundran Selvaprabhu 19/02/2024


#include <stdio.h>
int main()
{ int a=5, c;
c = a; // c is 5
printf("c = %d\n", c);
Output
c += a; // c is 10
printf("c = %d\n", c); c=5
c -= a; // c is 5 c = 10
printf("c = %d\n", c); c=5
c *= a; // c is 25 c = 25
printf("c = %d\n", c);
c=5
c /= a; // c is 5
c=0
printf("c = %d\n", c);
c %= a; // c = 0
printf("c = %d\n", c);
return 0; }
40 Dr. Poongundran Selvaprabhu 19/02/2024
C Relational Operators
Operator Meaning of Operator Example

5 == 3 is evaluated to
== Equal to
0

> Greater than 5 > 3 is evaluated to 1

< Less than 5 < 3 is evaluated to 0

5 != 3 is evaluated to
!= Not equal to
1

Greater than or 5 >= 3 is evaluated to


>=
equal to 1

5 <= 3 is evaluated to
<= Less than or equal to
41 Dr. Poongundran Selvaprabhu 0 19/02/2024
// Working of relational operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;
printf("%d == %d is %d \n", a, b, a == b);
printf("%d == %d is %d \n", a, c, a == c);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d > %d is %d \n", a, c, a > c);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d < %d is %d \n", a, c, a < c);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d != %d is %d \n", a, c, a != c);
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d >= %d is %d \n", a, c, a >= c);
printf("%d <= %d is %d \n", a, b, a <= b);
printf("%d <= %d is %d \n", a, c, a <= c);
return 0; }
42 Dr. Poongundran Selvaprabhu 19/02/2024
Output
5 == 5 is 1
5 == 10 is 0
5>5 is 0
5 > 10 is 0
5<5 is 0
5 < 10 is 1
5 != 5 is 0
5 != 10 is 1
5 >= 5 is 1
5 >= 10 is 0
5 <= 5 is 1
5 <= 10 is 1
43 Dr. Poongundran Selvaprabhu 19/02/2024
C Logical Operators
Operator Meaning Example
If c = 5 and d = 2
Logical AND. True
then, expression
&& only if all operands
((c==5) && (d>5))
are true
equals to 0.

If c = 5 and d = 2
Logical OR. True
|| then, expression
only if either one
((c==5) || (d>5))
operand is true
equals to 1.

Logical NOT. True If c = 5 then,


! only if the operand expression !(c==5)
is 0 equals to 0.
44 Dr. Poongundran Selvaprabhu 19/02/2024
45 Dr. Poongundran Selvaprabhu 19/02/2024
// Working of logical operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;
result = (a == b) && (c > b);
printf("(a == b) && (c > b) is %d \n", result);
result = (a == b) && (c < b);
printf("(a == b) && (c < b) is %d \n", result);
result = (a == b) || (c < b);
printf("(a == b) || (c < b) is %d \n", result);
result = (a != b) || (c < b);
printf("(a != b) || (c < b) is %d \n", result);
result = !(a != b);
printf("!(a != b) is %d \n", result);
result = !(a == b);
printf("!(a == b) is %d \n", result);
return 0; }
Dr. Poongundran Selvaprabhu
46 19/02/2024
Output
(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0

47 Dr. Poongundran Selvaprabhu 19/02/2024


C Bitwise Operators
Operators Meaning of operators
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
<< Shift left
>> Shift right

Bitwise operators are used in C programming to perform


bit-level operations.
48 Dr. Poongundran Selvaprabhu 19/02/2024
Bitwise AND
#include <stdio.h>
int main()
{
int a = 12, b = 25;
printf("Output = %d", a&b);
return 0;
}

49 Dr. Poongundran Selvaprabhu 19/02/2024


Bitwise OR
#include <stdio.h>
int main()
{
int a = 12, b = 25;
printf("Output = %d", a|b);
return 0;
}

50 Dr. Poongundran Selvaprabhu 19/02/2024


Bitwise XOR (exclusive OR) operator ^
#include <stdio.h>
int main()
{
int a = 12, b = 25;
printf("Output = %d", a^b);
return 0;
}

51 Dr. Poongundran Selvaprabhu 19/02/2024


Bitwise complement operator ~
Bitwise compliment operator is an unary operator
(works on only one operand). It changes 1 to 0 and 0
to 1. It is denoted by ~.

52 Dr. Poongundran Selvaprabhu 19/02/2024


C const keyword
The const keyword is used to define constant in C
programming.
const float PI=3.14;

#include<stdio.h>
int main(){
const float PI=3.14;
printf("The value of PI is: %f",PI);
return 0;
}
output: The value of PI is: 3.140000
53 Dr. Poongundran Selvaprabhu 19/02/2024
Try this
#include<stdio.h>
int main(){
const float PI=3.14;
PI=4.5;
printf("The value of PI is: %f",PI);
return 0;
}
Output: Compile Time Error: Cannot modify a const object

54 Dr. Poongundran Selvaprabhu 19/02/2024

You might also like