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

4.Constants, Variables

Uploaded by

Durga Prasanna
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

4.Constants, Variables

Uploaded by

Durga Prasanna
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

SNS COLLEGE OF

Kurumbapalayam (Po), Coimbatore – 641 107


TECHNOLOGY
An Autonomous Institution
Accredited by NBA – AICTE and Accredited by NAAC – UGC with A ‘ ’ Grade
Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai

COURSE NAME : 23CST101–PROBLEMSOLVING&CPROGRAMMING

I YEAR / I SEMESTER

Unit 2-C PROGRAMMING BASICS

Topic : Constants And Variables

05/12/2024 1
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.
• Let's see the syntax to declare a variable:

type variable_list;
• The example of declaring the variable is given below:

• int a;

• float b;

• char c;
PROBLEM SOLVINGAND C PROGRAMMING/CONSTANTS
05/12/2024 AND VARIABLES / DURGALAKSHMI B/AIML/SNSCT 2
Variables in C

• Here, a, b, c are variables. The int, float, char are


the data types. .

• We can also provide values while declaring the variables as given below:

• int a=10,b=20;//declaring 2 variable of integer


type

• float f=20.8;

• char c='A';

PROBLEM SOLVINGAND C PROGRAMMING/CONSTANTS


05/12/2024 AND VARIABLES / DURGALAKSHMI B/AIML/SNSCT 3
Variables in C
Rules for defining variables

• A variable can have


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.

PROBLEM SOLVINGAND C PROGRAMMING/CONSTANTS


05/12/2024 AND VARIABLES / DURGALAKSHMI B/AIML/SNSCT 4
Variables in C

Local Variable
.
• A variable that is declared inside the function or block is
called a local variable.

• It must be declared at the start of the block.


void main(){
int x=10;//local variable
}

PROBLEM SOLVINGAND C PROGRAMMING/CONSTANTS


05/12/2024 AND VARIABLES / DURGALAKSHMI B/AIML/SNSCT 5
Variables in C

Global Variable

• A variable that is. declared outside the function or


block is called a global variable.

• Any function can change the value of the global variable. It


is available to all the functions.
int value=20;//global variable
void main(){
int x=10;//local variable
}

PROBLEM SOLVINGAND C PROGRAMMING/CONSTANTS


05/12/2024 AND VARIABLES / DURGALAKSHMI B/AIML/SNSCT 6
Constants in C
A constant is a value or variable that can't be changed in the
program, for example: 10, 20, 'a', 3.4, "c programming" etc.
There are two simple ways in C to define constants
Using #define preprocessor.
.
Syntax:
#define identifier value Example

#define LENGTH 10

#define WIDTH 5
Using const keyword.

Syntax:
const type var
const int LENGTH = 10; const int
WIDTH = 5;
PROBLEM SOLVINGAND C PROGRAMMING/CONSTANTS
05/12/2024 AND VARIABLES / DURGALAKSHMI B/AIML/SNSCT 7
THANK YOU

04/12/2024
05/12/2024 8

You might also like