Presentation is loading. Please wait.

Presentation is loading. Please wait.

By: Mr. Baha Hanene Chapter 4. LEARNING OUTCOMES This chapter will cover learning outcome no. 2 i.e. Use basic data-types and input / output in C programs.

Similar presentations


Presentation on theme: "By: Mr. Baha Hanene Chapter 4. LEARNING OUTCOMES This chapter will cover learning outcome no. 2 i.e. Use basic data-types and input / output in C programs."— Presentation transcript:

1 By: Mr. Baha Hanene Chapter 4

2 LEARNING OUTCOMES This chapter will cover learning outcome no. 2 i.e. Use basic data-types and input / output in C programs. (L02) 2

3 CONTENTS Declarations Data types Assignment Statements Expression Statements Format Specifiers 3

4 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. 4 (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 C Type1LengthRange int16 bits-32768 to 32767 float32 bits1e-38 to 1e+38 char8 bits-128 to 127 unsigned int16 bits0 to 65535 long int32 bits -2147483648 to 2147483647 unsigned long32 bits0 to 4294967295 double64 bits 1.7*(10**-308) to 1.7*(10**+308) 1.7*(E-308) to 1.7*(E+308) long double80 bits 4.4*(10**-4932) to 1.1*(10**+4932) 5

6 ASSIGNMENT STATEMENTS The way of storing data in a variable i.e. storage location inside computer memory.E.g. A = 5;num1 = 15; 6

7 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) 7 EXPRESSIO N

8 DATA TYPE FORMAT SPECIFIERS Data TypeFormat Specifiers int%i, %d float%f char%c double%d long int%li 8 These format specifiers we use in Input / Output statements.

9 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

10 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); 10 Field Width = 6 9800 240 10040

11 SAMPLE PROGRAM #include 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

12 SAMPLE PROGRAM OUTPUT The results shown to the monitor screen should be on three lines without any kind of comments: 12 1234-5523 1234 -5523 1234 -55231234-5523

13 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); 13 12345.123

14 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); 14 325.7000 324.13 41.57079.34 41.57079.34

15 DOUBLE NUMBERS The keyword double is used to define double numbers Double values have the range 1E-308 to 1E+308 15 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.


Download ppt "By: Mr. Baha Hanene Chapter 4. LEARNING OUTCOMES This chapter will cover learning outcome no. 2 i.e. Use basic data-types and input / output in C programs."

Similar presentations


Ads by Google