html5-img
1 / 15

Variables in C

Variables in C. Declaring , Naming, and Using Variables . Using Variables. You may declare variables in C. The declaration includes the data type you need. Examples of variable declarations: int meatballs ; float area ;. Declaring Variables. When we declare a variable:

amandla
Télécharger la présentation

Variables 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. Variables in C Declaring , Naming, and Using Variables

  2. Using Variables • You may declare variables in C. • The declaration includes the data type you need. • Examples of variable declarations: • int meatballs ; • float area ;

  3. Declaring Variables • When we declare a variable: • space in memory is set aside to hold that data type • That space is associated with the variable name • Visualization of the declaration • int meatballs ; meatballs FE07

  4. Legal Variable Names • Variable names in C must be valid identifiers • Consists of letters, digits and underscores. • May be as long as you like, but only the first 31 characters are significant. • May NOT begin with a number • May not be a C keyword

  5. Naming Conventions • Begin variable names with lowercase letters • Use meaningful identifiers • Separate “words” within identifiers with underscores or mixed upper and lower case. • Example: surfaceArea surface_Area or surface_area • Be consistent !!

  6. Naming Conventions(continued) • Use all uppercase for symbolic constants ( #define ) • Example: PI (#define PI 3.14159 ) • Function names follow the same rules as variables

  7. Case Sensitive • C is case sensitive • It matters whether something is upper or lower case • Example: area is different than Area which is different than AREA

  8. More About Variables • C has 3 basic predefined data types • Integers • int, long int, short int, unsigned int • Floating point numbers • float, double • Characters • char

  9. Initializing Variables • Variables may be initialized • int x = 7; • float y = 5.9; • char c = ‘A’; • Do not “hide” the initialization • put initialized variables on a separate line • a comment is probably a good idea • int y = 6; /* feet per fathom */ • NOT int x, y = 6, z;

  10. Keywords in C • int long • register return • short signed • sizeof static • struct switch • typedef union • unsigned void • volatile while • auto break • case char • const continue • default do • double else • enum extern • float for • goto if

  11. Which Are Legal Identifiers ? • AREA area_under_the_curve • 3D num45 • Last-Chance #values • x_yt3 pi • num$ %done • lucky***

  12. Declarations and assignmentswreck.c inches • #include <stdio.h> • main ( ) • { • int inches, feet, fathoms ; • fathoms = 7 ; • feet = 6 * fathoms ; • inches = 12 * feet ; • } feet fathoms fathoms 7 feet 42 inches 504

  13. wreck.c (cont’d) • main ( ) • { • printf (“Its depth at sea: \n”) ; • printf (“ %d fathoms \n”, fathoms) ; • printf (“ %d feet \n”, feet); • printf (“ %d inches \n”, inches); • } • %d is a place holder - indicates that the value of the integer variable is to be printed in decimal form (rather than binary or hex) at that location.

  14. Floating point numbers • Are numbers that can contain decimal points. • What if the depth were really 5.75 fathoms ? ... Our program, as it is, couldn’t handle it. • We can declare floating point variables like this : float fathoms ; • float feet ;

  15. Floating point version of wreck.c(works for any depth shipwreck) • #include <stdio.h> • main ( ) • { • float fathoms, feet; • printf (“Enter the depth in fathoms : ”); • scanf (“%f”, &fathoms); • feet = 6.0 * fathoms; • printf (“She’s %f feet down.\n”, feet); • }

More Related