PL1 Lecture 3 Variables
PL1 Lecture 3 Variables
List of topics,
Identifiers
Variables
Data Types
Types of variables
Identifiers
Variables
Data Types
• Every variable must have a type.
Data Types
Type Syntax
Declarations
• Variables must be declared before they are used.
Declarations
Types of variable
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.
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
Directives
• Before a C program is compiled, it is first edited by a preprocessor.
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.
Functions
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");