Data Types and Type Conversion in C Programming

structured program development in c dr nouf n.w
1 / 47
Embed
Share

Explore the fundamentals of variables, data types, and type conversion in C programming. Learn about declaring variables, different data types in C, and how data is converted between types implicitly and explicitly. Dive into the nuances of type conversion and its impact on variable storage and arithmetic operations.

  • C Programming
  • Data Types
  • Type Conversion
  • Variables
  • Programming

Uploaded on | 4 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Structured Program Development in C Dr. Nouf Aljaffan Nouf Aljaffan (C) 2018

  2. Outlines Variables & Data types Control structures Additional Operators Nouf Aljaffan (C) 2018

  3. Variables and DataTypes Nouf Aljaffan (C) 2018

  4. Names that represent values in the program Similar to algebraic variables All variables have a type which must be declared Variables/Identifiers E.g. int x; float y; Type determines : how arithmetic is preformed, how much memory space is required. Nouf Aljaffan (C) 2018

  5. C has a small family of datatypes. Data types and sizes Numeric (int, float, double) Character (char) User defined (struct,union) Nouf Aljaffan (C) 2018

  6. Basic Data Types The individual sizes are machine/compiler dependent. However, the following is guaranteed: sizeof(char)<sizeof(short)< =sizeof(int)<=sizeof(long) sizeof(char)<sizeof(short)< =sizeof(float)<=sizeof(doubl e) Nouf Aljaffan (C) 2018

  7. Type Conversion Implicit Explicit Data will get automatically converted from one type to another. Data may also be expressly converted, using the typecast operator Nouf Aljaffan (C) 2018

  8. Implicit Type Conversion When data is being stored in a variable, if the data being stored does not match the type of the variable. The data being stored will be converted to match the type of the storage variable. When an operation is being performed on data of two different types. The "smaller" data type will be converted to match the "larger" type. For example, when an int is added to a double, the computer uses a double version of the int and the result is a double. The following example converts the value of nTotal to a double precision value before performing the division. Note that if the 3.0 were changed to a simple 3, then integer division would be performed, losing any fractional values in the result. average = nTotal / 3.0; When data is passed to or returned from functions. Nouf Aljaffan (C) 2018

  9. Explicit Type Conversion The following example converts the value of nTotal to a double precision value before performing the division. ( nStudents will then be implicitly promoted, following the guidelines listed above. ) average = ( double ) nTotal / nStudents; Note that nTotal itself is unaffected by this conversion. Nouf Aljaffan (C) 2018

  10. Variables and Variable Definitions Naming rules: Variable names can contain letters,digits and _ Variable names should start with letters. Keywords (e.g., for,while etc.) cannot be used as variable names Variable names are case sensitive. int x; int X declares two different variables. Nouf Aljaffan (C) 2018

  11. Pop quiz (correct/incorrect): int money$owed; int total_count int score2 int 2ndscore int long Nouf Aljaffan (C) 2018

  12. Variable declaration The general format for a declaration is type variable-name [=value] ; char x; char x= A ; char x= A ,y= B ; char x=y= Z ; / uninitialized / / intialized to A / / multiple variables initialized / / multiple initializations / Nouf Aljaffan (C) 2018

  13. Declaration of answer and assigning it a value The general form of the assignment statement is: variable = expression; Nouf Aljaffan (C) 2018

  14. Constants The const qualifier is used to tell C that the variable value can not change after initialisation. Constants are literal/fixed values assigned to variables or used directly in expressions. Examples: const char letter = a ; const float pi=3.14159; Nouf Aljaffan (C) 2018

  15. Exercises Define the variables x, y and z to be of type int where x= 9, y=2, z=0 Prompt the user to enter a float and store it in z. Define the variable result, compute the product of the numbers in the variables x, y and z, and use that product to initialize the variable result. Print "The product is" followed by the value of the integer variable result. Nouf Aljaffan (C) 2018

  16. Branching/ Selections Nouf Aljaffan (C) 2018

  17. Branching forms if (expression) { (1) If Statement Block of statements; } if (expression) { Block of statements; } else { Block of statements; } (2) If ..else Statement if (expression) { } else if(expression) { Block of statements; Block of statements; (3) Nested If..else Statement Nouf Aljaffan (C) 2018 } else { Block of statements; }

  18. if (expression) { Block of statements; } if Selection statement Nouf Aljaffan (C) 2018

  19. if (expression) { Block of statements; } else { Block of statements; } if else Selection statement Nouf Aljaffan (C) 2018

  20. Conditional operator ?: The ? : operator is just like an if ... else statement except that because it is an operator you can use it within expressions. ? : is a ternary operator in that it takes three values, this is the only ternary operator C has. ? : takes the following form: if condition is true ? then X return value : otherwise Y value; (expression) ? value1 : value2 Nouf Aljaffan (C) 2018

  21. Conditional operator ?: if condition is true ? then X return value : otherwise Y value; Nouf Aljaffan (C) 2018

  22. If..else vs ?: if ( x > = y ) max = x ; else max = y ; max = x >= y ? x : y ; Nouf Aljaffan (C) 2018

  23. Nested if...else Statements Nouf Aljaffan (C) 2018

  24. switch statement switch( expression ) { case constant-expression1: statements1; [case constant-expression2: statements2;] [case constant-expression3: statements3;] [default : statements4;] } limited by The expression can be integer or char and cannot be float or any other data type. No two case statement constants may be the same. Character constants are automatically converted to integer. Nouf Aljaffan (C) 2018

  25. switch( expression ) { switch statement case constant-expression1: statements1; break; [case constant-expression2: statements2; break;] [case constant-expression3: statements3; break;] [default : statements4;] } switch( Grade ) { case 'A' : printf( "Excellent\n" ); break; case 'B' : printf( "Good\n" ); break; case 'C' : printf( "OK\n" ); break; case 'D' : printf( "Mmmmm....\n" ); break; case 'F' : printf( "You must do better than this\n" ); break; default : printf( "What is your grade anyway?\n" ); break; } Nouf Aljaffan (C) 2018

  26. Exercise Test whether the value of the variable count is greater than 10. If it is, print Count is greater than 10. Nouf Aljaffan (C) 2018

  27. Operators Nouf Aljaffan (C) 2018

  28. Assignment Operators Nouf Aljaffan (C) 2018

  29. Increment and Decrement Operators Nouf Aljaffan (C) 2018

  30. Increment and Decrement Operators For Example :- int i, j = 2 ; i = ++ j ; /* prefix :- i has value 3, j has value 3 */ i = j++ ; /* postfix :- i has value 3, j has value 4 */ Nouf Aljaffan (C) 2018

  31. Nouf Aljaffan (C) 2018

  32. Iteration & Looping Nouf Aljaffan (C) 2018

  33. Means of iteration: Counter-Controlled Iteration definite iteration Sentinel-controlled iteration indefinite iteration It requires The precise number of iterations isn t known in advance, and 1. The name of a control variable (or loop counter). The loop includes statements that obtain data each time the loop is performed. 2. The initial value of the control variable. 3. The increment (or decrement) by which the control variable is modified each time through the loop. 4. The condition that tests for the final value of the control variable (i.e., whether looping should continue). Nouf Aljaffan (C) 2018

  34. Iteration & Looping Nouf Aljaffan (C) 2018

  35. Initialization While( condition){ statements } While Iteration Statements Nouf Aljaffan (C) 2018

  36. While Iteration Statements Nouf Aljaffan (C) 2018

  37. for Iteration Statement Nouf Aljaffan (C) 2018

  38. for Iteration Statement Nouf Aljaffan (C) 2018

  39. dowhile Iteration Statement Nouf Aljaffan (C) 2018

  40. break and continue Statements break and continue are used to alter the flow of control. Nouf Aljaffan (C) 2018

  41. break Statement break can be used in a while, for, do while or switch statement causes an immediate exit from that statement. Program execution continues with the next statement after that while, for, do while or switch. Nouf Aljaffan (C) 2018

  42. continue Statement continue can be used in a while, for or do while Cause skipping the remaining statements in that control statement s body and performs the next iteration of the loop. Nouf Aljaffan (C) 2018

  43. Confusing Equality (==) and Assignment (=) Operators Nouf Aljaffan (C) 2018

  44. Logical Operator Logical AND (&&) Operator Logical OR (||) Operator Logical Negation (!) Operator Nouf Aljaffan (C) 2018

  45. Exercises Sum the odd integers between 1 and 99 using a for statement. Use the unsigned integer variables sum and count. Print the integers from 1 to 20 using a while loop and the counter variable x. Print only five integers per line. [Hint: Use the calculation x % 5. When the value of this is 0, print a newline character, otherwise print a tab character.] Nouf Aljaffan (C) 2018

  46. Conclusion Variables Data Types Expression represent a single data item such as a number or character Statement caused the computer to carry out some action Branching If statement If..Else statement Nested if statement Switch statement Looping For statement While statement Do while statement Nouf Aljaffan (C) 2018

  47. Questions? Nouf Aljaffan (C) 2018

Related


More Related Content