PL1 Lecture 3 Variables (1)

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 24

Identifiers and Variables

Course Title: Programming language 1

Dept. of Computer Science


Faculty of Science and Technology

Lecturer No: 03 Week No: 3 Semester: Spring 23-24


Lecturer: Shaikat Das Joy; skdas@aiub.edu
Lecture Outline

 List of topics,
 Identifiers
 Variables
 Data Types
 Types of variables
Identifiers

Each program elements written by the programmer is given a name


called identifiers, e.g., names for variable, function and array…
Identifiers

 First character should be an #include<stdio.h>


alphabet or underscore. int main(){
// legal identifier names for variables
 Succeeding characters might be int ab_1 = 10;
int ab_2 = 12;
digits or letter. char _c = 'c';
char _name [30] =
 Punctuation and special
"My name is Rajit.";
characters aren’t allowed except
//illigal names
underscore. float ?abc = 5;
char 1na = 'd';
 Identifiers should not be
keywords. return 0;
}
Keywords

Keywords are pre-defined words in a C compiler.

Each keyword is meant to perform a specific function in a C


program.
Since keywords are referred names for compiler, they can’t
be used as variable name.
Keywords

In C there are 32 keywords defined which are listed bellow:

case enum auto double


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

Follow the link to get example program on


each keyword: http://fresh2refresh.com/c-programming/c-tokens-identifiers-keywords/
7

Variables

• Most programs need to a way to store data temporarily during


program execution.
• These storage locations are called variables.

• C variable is a named location in a memory.

• This memory is used to hold value of a certain type.

• The value of a variable can be changed.

• E.g., int, float, char, double.


8

Data Types
• Every variable must have a type.

• C has a wide variety of types, including int and float.

• A variable of type int (short for integer) can store a whole


number such as 0, 1, 392, or –2553.
• The largest int value is typically 2,147,483,647.
9

Data Types

• A variable of type float (short for floating-point) can store


much larger numbers than an int variable.
• Also, a float variable can store numbers with digits after the
decimal point, like 379.125.
• Drawbacks of float variables:
• Slower arithmetic
• Approximate nature of float values
10

DECLARING & INITIALIZING C VARIABLE

Type Syntax

Variable declaration data_type variable_name;


int x, y, z; char flat, ch;

data_type variable_name = value;


Variable initialization
int x = 50, y = 30; char flag = ‘x’, ch=’l’;
11

Declarations
• Variables must be declared before they are used.

• Variables can be declared one at a time:


 int height;
 float profit;
• Alternatively, several can be declared at the same time:
 int height, length, width, volume;
 float profit, loss;
12

Declarations

 When main contains declarations, these must precede


statements:
int main()
{
declarations
statements
}
13

Types of variable

Three types of variables:


• Local variable
• Global variable
• Environment variable
14

Local variable

#include <stdio.h>
• These variables are
declared within the int main(){
function and can’t be
/*variables declared inside a function
accessed outside the are called local variables*/
function.
int x = 10, y = 20;
• The scope of local variables
int sum = x + y;
will be within the function
only. printf("sum of %d and %d is: %d \n", x,y, sum );
return 0;
}
15

Global variable

• This variable is defined outside the main function. So that, this variable
is visible to main function and all other sub functions.

• The scope of global variables will be throughout the program. These


variables can be accessed from anywhere in the program.
16

Global variable

#include <stdio.h>
/*variables declared outside a fucntion are called global variables*/
int x = 10, y = 20;

int main(){
int sum = x + y;
printf("sum of %d and %d is: %d \n", x,y, sum );
return 0;
}
17

The General Form of a Simple Program


• C uses {} in much the same way that some other languages use
words like begin and end.
• Even the simplest C programs rely on three key language
features:
 Directives
 Functions
 Statements
18

Directives
• Before a C program is compiled, it is first edited by a preprocessor.

• Commands intended for the preprocessor are called directives.

 Example:

 #include <stdio.h>
 <stdio.h> is a header containing information about C’s
standard I/O library.
19

Directives
• Directives always begin with a # character.

• By default, directives are one line long; there’s no semicolon or


other special marker at the end.
20

Functions

• A function is a series of statements that have been grouped


together and given a name.
• Library functions are provided as part of the C implementation.

• A function that computes a value uses a return statement to


specify what value it “returns”:
 return x + 1;
21

The main Function


• The main function is mandatory.

• main is special: it gets called automatically when the program is


executed.
• main returns a status code; the value 0 indicates normal
program termination.
• If there’s no return statement at the end of the main function,
many compilers will produce a warning message.
22

Statements
• A statement is a command to be executed when the program
runs.
• first.c uses only two kinds of statements. One is the
return statement; the other is the print statement.
• Asking a function to perform its assigned task is known as calling
the function.
• first.c calls printf to display a string:
 printf(“Welcome to CSE102.\n");
23

Printing Strings
• When the printf function displays a string literal—characters
enclosed in double quotation marks—it doesn’t show the
quotation marks.
• To make printf advance one line, include \n (the new-line
character) in the string to be printed.
24

Printing Strings
• The statement
 printf(“Hello World\n");

could be replaced by two calls of printf:


 printf(“Hello ");
 printf(“\nWorld\n");

• The new-line character can appear more than once in a string


literal:
 printf(“Hello\nWOrld\n");

You might also like