variables in c
Download
Skip this Video
Download Presentation
Variables in C

Loading in 2 Seconds...

play fullscreen
1 / 15

Variables in C - PowerPoint PPT Presentation


  • 118 Views
  • Uploaded on

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:

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Variables in C' - amandla


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
variables in c
Variables in C

Declaring ,

Naming, and

Using Variables

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

legal variable names
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
naming conventions
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 !!
naming conventions continued
Naming Conventions(continued)
  • Use all uppercase for symbolic constants ( #define )
  • Example: PI (#define PI 3.14159 )
  • Function names follow the same rules as variables
case sensitive
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
more about variables
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
initializing variables
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;
keywords in c
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
which are legal identifiers
Which Are Legal Identifiers ?
  • AREA area_under_the_curve
  • 3D num45
  • Last-Chance #values
  • x_yt3 pi
  • num$ %done
  • lucky***
declarations and assignments wreck c
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

wreck c cont d
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.
floating point numbers
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 ;
floating point version of wreck c works for any depth shipwreck
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);
  • }
ad