Upload
cecilia-carroll
View
212
Download
0
Embed Size (px)
Citation preview
Declarations, Assignments & Expressions in C
By:
Mr. Baha Hanene
Chapter 4
2
LEARNING OUTCOMESThis chapter will cover learning outcome no.
2 i.e.
Use basic data-types and input / output in C programs. (L02)
3
CONTENTSDeclarationsData typesAssignment StatementsExpression StatementsFormat Specifiers
4
DECLARATIONSThe 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.
5
AVAILABLE DATA TYPES IN CType1 Length Range
int 16 bits -32768 to 32767
float 32 bits 1e-38 to 1e+38
char 8 bits -128 to 127
unsigned int 16 bits 0 to 65535
long int 32 bits -2147483648 to 2147483647
unsigned long 32 bits 0 to 4294967295
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)
6
ASSIGNMENT STATEMENTSThe way of storing data in a variable i.e. storage location inside computer memory.E.g.
A = 5; num1 = 15;
ASSIGNMENT OPERATOR
RIGHT
HAND
SIDE
15
15 WILL GO IN num1
7
EXPRESSION STATEMENTSThe 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 + 33; (Value Expression)
Salary = allowance + bvar (Variable Expression)
Salary=salary * 12 + bvar; (Variable & Value Expression)
EXPRESSION
EXPRESSION
EXPRESSION
8
DATA TYPE FORMAT SPECIFIERSData Type Format Specifiers
int %i , %dfloat %fchar %c
double %dlong int %li
These format specifiers we use in Input / Output statements.
9
OUTPUT STATEMENT printf(“”);printf(“Welcome at KIC”); To display
simple textprintf(“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 valueprintf(“%i %i”, rate1, rate2); Display
multiple variableprintf(“%6i%6i%6i”, rate1, rate2, total);
10
FORMAT SPECIFIERS FIELD WIDTHField: the place where a value is displayedField width: the number of characters a value
occupies including any leading spacesprintf(“%6i%6i%6i”, rate1, rate2, total);
Field Width = 6
9800 240 10040
11
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);}
12
SAMPLE PROGRAM OUTPUTThe results shown to the monitor screen should be on three lines without any kind of comments:1234-5523
1234 -55231234-5523
13
FLOATING POINTUse the keyword float to declare float
variablesUse 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 %fprintf(“%9.3f”, 12345.123);
12345.123
14
FLOATING POINTShow the output obtained from:printf(“%9.3f%2.2f\n”, 41.57, 79.341);
printf(“%7.4f%10.2f\n”, 325.7, 324.125);325.7000 324.13
41.57079.34
15
DOUBLE NUMBERSThe keyword double is used to define double numbers
Double values have the range 1E-308 to 1E+308CHARACHTERS
Characters are letters and symbolsWhen 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.