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

PF-Week 03

programming fundamentals

Uploaded by

lachman das
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)
10 views

PF-Week 03

programming fundamentals

Uploaded by

lachman das
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/ 52

Programming

Fundamental

Week 03

Sobia Iftikhar
[email protected]
Class 07
Content

– - More on input and output functions


– Variables
– Data Types
– Format Specifiers
– Constant
– Keywords
– Escape Sequences
Variables

– 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

– 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 Declaration and
initialization
Types of Variables in C

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.

static data_type variable_name;


Static variable

#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

1. int main() 1. int main()


What if I put static in line
2. { 2. {
no. 10 before “int” ?
3. printf("%d",func()); 3. printf("%d",func());
4. printf("\n%d",func()); 4. printf("\n%d",func());
5. 5.
6. return 0; 6. return 0;
7. } 7. }
8. int func() 8. int func()
9. { 9. {
10. int count=0; 10. static int count=0;
11. count++; 11. count++;
12. return count; 12. return count;
13. } 13. }
Class 08
Data types

Data type in C

Basic Derived Enumeration void


There are the following data
types in C language.

Types Data Types


Basic Data Type int, char, float, double
Derived Data Type array, pointer, structure, union
Enumeration Data Type enum
Void Data Type void
DATATYPES IN C
Basic Data types
Type Size (bytes) Format Specifiers
int at least 2, usually 4 %d, %i
char 1 %c
float 4 %f
double 8 %lf
short int 2 usually %hd
unsigned int at least 2, usually 4 %u
long int at least 4, usually 8 %ld, %li
long long int at least 8 %lld, %lli
unsigned long int at least 4 %lu

unsigned long long int at least 8 %llu

signed char 1 %c
unsigned char 1 %c

long double at least 10, usually 12 or 16 %Lf


Basic Data types-ranges

Type Storage size Value range

char 1 byte -128 to 127 or 0 to 255


unsigned char 1 byte 0 to 255

signed char 1 byte -128 to 127

int 2 or 4 bytes -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647

unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295

short 2 bytes -32,768 to 32,767


unsigned short 2 bytes 0 to 65,535

long 8 bytes or -9223372036854775808 to 9223372036854775807


(4bytes for 32
bit OS)

unsigned long 8 bytes 0 to 18446744073709551615


int
1. Integers are whole numbers that can have both zero, positive and negative values
but no decimal values. For example, 0, -5, 1.
2. We can use int for declaring an integer variable.

float and double


float and double are used to hold real numbers.
In C, floating-point numbers can also be represented in exponential. For example,

char
Keyword char is used for declaring character type variables. For example
Task

Example #1: C Output


Example #2: C Integer Output
Example #3: C Integer Input/Output
Example #4: C Floats Input/Output
Example #5: C Character I/O
Example #6: I/O of Floats and Integers
Float vs double
Signed vs unsigned

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

Signed char [-128 --- + 127]

unSigned char [0 --- + 255]


Format Specifiers

SPECIFIER USED FOR


%c a single character
%s a string
%hi short (signed)
%hu short (unsigned)
%Lf long double
%n prints nothing
%d a decimal integer (assumes base
10)
%i a decimal integer (detects the base
automatically)
%o an octal (base 8) integer
%x a hexadecimal (base 16) integer
%p an address (or pointer)
%f a floating point number for floats
%u int unsigned decimal
%e a floating point number in scientific
notation
%E a floating point number in scientific
notation
%% the % symbol
How to print string
Format Specifiers

– %integervalue( sepcifer)
Escape sequences

Escape Sequence & Description


\t
Inserts a tab in the text.
\b
Inserts a backspace in the text.
\n
Inserts a newline in the text.
\r
Inserts a carriage return in the text.
\f
Inserts a form feed in the text.
\’
Inserts a single quote character in the text.
\”
Inserts a double quote character in the text.
\\
Inserts a backslash character in the text.
\?
Inserts a question mark in the text.
\a
Play beep or Alarm
Defining Constants:

In C/C++ program we can define constants in two ways as


shown below:
1. Using #define preprocessor directive
#define identifierName value

2. Using a const keyword


Why constant?

– An advantage of using constants as well as variables may be a small increase in


speed when your program runs on a computer.
– but it also has the advantage that it makes your programs clearer and easier to
understand.
– If you declare a constant you are telling the people who read your program that this
value never changes within the program and so whenever it is accessed it will be the
same.
– The mathematical value π (pi) is a constant value and if you were to use π in your
programs you wouldn’t want to have to type out 3.14159 (or more decimal places)
every time you needed it, nor would you ever want to give it any other value.
Constant Variables in C++

– If you make any variable as constant, using const


keyword, you cannot change its value. Also, the constant
variables must be initialized while they are declared.
Class 09
Linking, compiler, Loader

– 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

– A loader locates programs in memory and prepares them for execution. It is an


important component when starting a program. It includes tasks such as reading
the content of the executable file and placing the file in memory.
Linking

– 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

– A relational operator checks the relationship between two operands. If the


relation is true, it returns
– 1; if the relation is false, it returns value 0.
Example
Logical Operators

An expression containing logical operator returns either 0 or 1 depending upon whether


expression
results true or false. Logical operators are commonly used in decision making in C
programming.
Example
Size of() operator
Math Library

printf( "%.2f", sqrt( 900.0 ) );


Thankyou

You might also like