Declarations, Assignments & Expressions in C

Download Report

Transcript Declarations, Assignments & Expressions in C

Chapter 4
By:
Mr. Baha Hanene
LEARNING OUTCOMES
 This chapter will cover learning outcome no. 2 i.e.
 Use basic data-types and input / output in C programs.
(L02)
2
CONTENTS
 Declarations
 Data types
 Assignment Statements
 Expression Statements
 Format Specifiers
3
DECLARATIONS
The declaration means list all the variables (used for
inputs and outputs) for one program in start. In
declaration we have to tell the types of variables also. If
we have more than one types of variables in one
program then we have to mention them separately in
the start.
(Variable Declarations)
1. int num1, num2, num3;
2. float price1, price2;
3. char gender;
(Constant Declaration)
1. const int
week,per_year=52,
days_per_week=7;
The difference in variables and constants is, the value
of variable can be changed within a program but the
value of constant can’t be changed within a program.
4
AVAILABLE
DATA TYPESRange
IN C
Type1
Length
int
float
char
unsigned int
long int
unsigned long
16 bits
32 bits
8 bits
16 bits
32 bits
32 bits
-32768
1e-38
-128
0
to
to
to
to
32767
1e+38
127
65535
double
64 bits
1.7*(10**-308) to 1.7*(10**+308)
1.7*(E-308)
to 1.7*(E+308)
long double
80 bits
4.4*(10**-4932) to 1.1*(10**+4932)
-2147483648 to 2147483647
0 to 4294967295
5
ASSIGNMENT
 The way of storingSTATEMENTS
data in a variable i.e. storage
location inside computer memory.
A = 5;
E.g.
num1 = 15;
6
EXPRESSION STATEMENTS
 The basic assignment operator is the equal sign ( = ).
Sometimes we use double equal sign ( == ), but that is not
like assignment statement, but like comparison.
 The statement that contains more than one variable, values
or combination of variable & values on the right hand side
of an assignment statement is called expression statement
e.g.
 Salary = 11 +EXPRESSIO
33;
(Value Expression)
N
 Salary = allowance + bvar (Variable Expression)
EXPRESSIO
N
 Salary=salary * 12 + bvar; (Variable & Value Expression)
EXPRESSIO
N
7
DATA Data
TYPE
SPECIFIERS
TypeFORMAT
Format
Specifiers
int
float
char
double
long int
%i , %d
%f
%c
%d
%li
These format specifiers we use in Input / Output
statements.
8
OUTPUT STATEMENT
printf(“”);
 printf(“Welcome at KIC”);





To display simple
text
printf(“Welcome \n at KIC”); Start new line “\n”
printf(“Welcome \t at KIC”); Give space b/w text “\t”
printf(“Result= %i ”, res);
Displays variable
value
printf(“%i %i”, rate1, rate2); Display multiple
variable
printf(“%6i%6i%6i”, rate1, rate2, total);
9
FORMAT SPECIFIERS FIELD WIDTH
 Field: the place where a value is displayed
 Field width: the number of characters a value occupies
including any leading spaces
9800
10040
printf(“%6i%6i%6i”,
rate1,240
rate2,
total);
Field Width = 6
10
SAMPLE PROGRAM
#include <stdio.h>
void main()
{
int field_one, field_two;
field_one = 1234;
field_two = field_one - 6757;
printf(“%i%i\n", field_one, field_two);
printf("%6i%6i\n", field_one, field_two);
printf("%4i%4i\n", field_one, field_two);
}
11
SAMPLE PROGRAM OUTPUT
The results shown to the monitor screen should be on
three lines without any kind of comments:
1234-5523
1234 -5523
1234-5523
12
FLOATING POINT
 Use the keyword float to declare float variables
 Use the float data type when you know the variable
will hold values that has decimal point.
 The range of float numbers is from
1e-38 to 1e38
 To display float numbers use %f
printf(“%9.3f”,
12345.123);
12345.123
13
FLOATING POINT
 Show the output obtained from:
 printf(“%9.3f%2.2f\n”, 41.57, 79.341);
41.57079.34
 printf(“%7.4f%10.2f\n”, 325.7, 324.125);
325.7000 324.13
14
DOUBLE
NUMBERS
 The keyword
double is used to define double
numbers
 Double values have the range 1E-308 to 1E+308
CHARACHTERS
 Characters are letters and symbols
 When you need to store letters, use character
variable.
 Use the keyword char to declare character
variables.
 Character variables can store only one letter e.g.
A or B or C etc.
15