PF-Week 03
PF-Week 03
Fundamental
Week 03
Sobia Iftikhar
[email protected]
Class 07
Content
– A variable is nothing but a name given to a storage area that our programs can
manipulate. Each variable in C has a specific type, which determines the size
and layout of the variable's memory; the range of values that can be stored
within that memory; and the set of operations that can be applied to the variable.
type variable_list;
Rules for defining variables
local variable
global variable
static variable
automatic variable
external variable
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.
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.
Static variables
In programming, a static variable is the one allocated “statically,” which means its
lifetime is throughout the program run. It is declared with the ‘static’ keyword and
persists its value across the function calls.
#include <stdio.h>
void foo()
{
int a = 10; a = 15, sa = 15
static int sa = 10; a = 15, sa = 20
a += 5; a = 15, sa = 25
sa += 5; a = 15, sa = 30
printf("a = %d, sa = %d\n", a, sa); a = 15, sa = 35
} a = 15, sa = 40
int main() a = 15, sa = 45
{ a = 15, sa = 50
int i; a = 15, sa = 55
for (i = 0; i < 10; ++i) a = 15, sa = 60
foo();
}
Example
Data type in C
signed char 1 %c
unsigned char 1 %c
char
Keyword char is used for declaring character type variables. For example
Task
In C programming language, there are different varieties of data types, which are used to declare
variables before they are used as they are data storage for a particular variable to perform particular
tasks like int data types for integers, float for floating real numbers, etc.
In C, unsigned is also one data type in which is a variable type of int this data type can hold zero and
positive numbers.
There is also a signed int data type in which it is a variable type of int data type that can hold
negative, zero, and positive numbers. This unsigned int is data type cannot represent a negative
number.
Signed and Unsigned int
Example
ASCII TABLE
ASCII, abbreviation of American
Standard Code For Information
Interchange, a standard data-
transmission code that is used by
smaller and less-powerful computers
to represent both textual data (letters,
numbers, and punctuation marks)
Signed and unsigned char
A signed char is same as an ordinary char and has a range from -128 to +127; whereas,
an unsigned char has a range from 0 to 255.
Example
– %integervalue( sepcifer)
Escape sequences
– Compiler
•A compiler is a computer program (or a set of programs) that transforms source code
written in a programming language (the source language) into another computer
language (the target language).
•Typically, from high level source code to low level machine code or object code.
Loader
– After converting the source code into the object code, the linker performs the
linking task. It takes one or more object files generated by the compiler and
combines them together to generate an executable file.
Furthermore, it combines the object codes with libraries. For example, in a C program,
if there is sqrt() function to calculate the square root of a number, the linker links the
program with the math library. Finally, the CPU can read and understand the generated
executable file. Therefore, the CPU can execute that file to perform the task defined in
the program
Operators
– C Arithmetic Operators
– C Relational Operators
– C Logical Operators
– C Bitwise Operators
– Operators Precedence in C
– Math library functions
C Arithmetic Operators
Example
– Int a = 9, b= 4, c=
– c = a+b;
– printf("a+b = %d \n",c);
– c = a-b;
– printf("a-b = %d \n",c);
– c = a*b;
– printf("a*b = %d \n",c);
– c = a/b;
– printf("a/b = %d \n",c);
– c = a%b;
– printf("Remainder when a divided by b = %d \n",c);
C Relational Operators