150 likes | 302 Views
Chapter 4. Declarations, Assignments & Expressions in C. 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). CONTENTS. Declarations Data types Assignment Statements
E N D
Chapter 4 Declarations, Assignments & Expressions in C By: Mr. BahaHanene
LEARNING OUTCOMES • This chapter will cover learning outcome no. 2 i.e. • Use basic data-types and input / output in C programs. (L02)
CONTENTS • Declarations • Data types • Assignment Statements • Expression Statements • Format Specifiers
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. 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.
ASSIGNMENT STATEMENTS ASSIGNMENT OPERATOR • The way of storing data in a variable i.e. storage location inside computer memory. E.g. A = 5; num1 = 15; 15WILL GO IN num1 RIGHT HAND SIDE 15
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 + 33; (Value Expression) • Salary = allowance + bvar(Variable Expression) • Salary=salary * 12 + bvar; (Variable & Value Expression) EXPRESSION EXPRESSION EXPRESSION
DATA TYPE FORMAT SPECIFIERS These format specifiers we use in Input / Output statements.
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);
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 printf(“%6i%6i%6i”, rate1, rate2, total); 9800 240 10040 Field Width = 6
SAMPLE PROGRAM #include <stdio.h> void main() { intfield_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); }
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
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
FLOATING POINT • Show the output obtained from: • printf(“%9.3f%2.2f\n”, 41.57, 79.341); • printf(“%7.4f%10.2f\n”, 325.7, 324.125); 41.57079.34 325.7000324.13
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.