1 / 15

Declarations, Assignments & Expressions in C

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

deion
Download Presentation

Declarations, Assignments & Expressions in C

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 4 Declarations, Assignments & Expressions in C By: Mr. BahaHanene

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

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

  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. 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

  6. 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

  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) EXPRESSION EXPRESSION EXPRESSION

  8. DATA TYPE FORMAT SPECIFIERS 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);

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

  11. 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); }

  12. 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

  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); 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); 41.57079.34 325.7000324.13

  15. 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.

More Related