0% found this document useful (0 votes)
67 views25 pages

Components of A C Program (Part 3)

This document discusses several key concepts in C programming: 1. It defines type as the kind of value, such as int, float, char, etc. and introduces the concept of type modifiers. 2. It explains variables as containers that store values and have an associated type. Variables must be declared before use. 3. It introduces keywords as special reserved words in C that cannot be used as variable names, and identifiers as the name given to variables. 4. It discusses assignment as putting a value into a variable using the = operator, and constant variables which cannot be reassigned once initialized.

Uploaded by

abhimanyu raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views25 pages

Components of A C Program (Part 3)

This document discusses several key concepts in C programming: 1. It defines type as the kind of value, such as int, float, char, etc. and introduces the concept of type modifiers. 2. It explains variables as containers that store values and have an associated type. Variables must be declared before use. 3. It introduces keywords as special reserved words in C that cannot be used as variable names, and identifiers as the name given to variables. 4. It discusses assignment as putting a value into a variable using the = operator, and constant variables which cannot be reassigned once initialized.

Uploaded by

abhimanyu raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Components of a C Program

(Part 3)

1
Topics
• Type
• Variables
• Keywords and Identifiers
• Assignments
• Constant Variables

2
Type
• The kind of a value is its “type”
• Not all values are of the same kind
– For example: 7 + “cat” makes no sense

Variable
Value 3
Type
• Built-in types: char, int, float
• Type modifiers: long, short, const
• User-defined types (arrays and records)
• What about “strings”?
– Strings are arrays of char (discussed later)

4
Character Representation
• Characters are stored as a small integer
• Each character has a unique integer
equivalent specified by its position in the
ASCII table (pronounced “as-key”)
– American Standard Code for Information Interchange

5
6
Character Representation
• The ASCII values range from 0 to 127
– value 0: special character ’\0’
(a.k.a. NULL character)
– value 127: special character <DEL>
– other special characters:
’\n’ ’\t’ ’\’’ ’\\’ etc.
– various “extended” sets from 128 to 255

7
Variable
• Is a logical name for a container
– (an actual piece of computer memory for
values)
• Has a type associated with it
– tells the computer how to interpret the bits
• Must be declared before use:
int i; float result;
int i=0; char initial=’K’;

8
Variable Declaration: Examples

int myID;

myID
9
Variable Declaration: Examples

int myID;

char myInitial = ’J’;

Single “forward quotes” or


apostrophe (’) rather than
“back quotes” (‘)

10
Variable Declaration: Examples

int myID;

char myInitial = ’J’; 01001010


myInitial

11
Variable Declaration: Examples

int myID;

char myInitial = ’J’; 01001010


myInitial
char myInitial = 74 ;

12
Variable Declaration: Examples
float commission = 0.05;

short int myHeight = 183; /* cm */

long int mySalary = 100000000000000000000;

long float chanceOfADate = 3e-500;

double chanceOfA2ndDate = 1.5e-500;


13
Variable Declaration: Examples
float commission = 0.05; “Keywords”
short int myHeight = 183; /* cm */

long int mySalary = 100000000000000000000;

long float chanceOfADate = 3e-500;

double chance_of_a_2nd_date = 1.5e-500;


14
Keyword
• ...has a special meaning in C
• ...is “case-sensitive”
• ...cannot be used as variable names
• Examples:
int, char, long, main, float,
double, const, while, for, if,
else, return, break, case,
switch, default, typedef,
struct, etc. (see D&D 2/e Appendix A or
15
D&D 3/e page 545, Figure 15.4)
Variable Declaration: Examples
float commission = 0.05; “Identifiers”
short int myHeight = 183; /* cm */

long int mySalary = 100000000000000000000;

long float chanceOfADate = 3e-500;

double chanceOfA2ndDate = 1.5e-500;


16
Identifier
• ...is a series of characters consisting of
letters, digits and underscores ( _)
• ...cannot begin with a digit
• ...must not be a keyword
• ...is “case-sensitive”
• Examples:
sUmoFA, x1, y2, _my_ID_, Main (careful!)

17
Assignment
• Puts a specified value into a specified
variable
• Assignment operator: =

<variable name> = <expression> ;

not to be
confused
with ==
18
Assignment: Examples
float x = 2.5 ;

char ch ;
int number ;

ch = ’\n’ ;

number = 4 + 5 ;
/* current value of number is 9. */

number = number * 2;
/* current value of number is now 18. */ 19
Assignment
• Value must have a type assignable to the
variable
• Value may be automatically converted to fit
the new container
• Example:
– various.c

20
#include <stdio.h>
integer = 33.33;
/* Do various assignment character = 33.33;
statements */
floatingPoint = 33.33;
int main()
{ integer = floatingPoint;
int integer; floatingPoint = integer;
char character;
float floatingPoint; return 0;
}
integer = 33;
character = 33;
floatingPoint = 33;

integer = 'A';
character = 'A';
floatingPoint = 'A'; 21
various.c
Constant Variables
• ...are variables that don’t vary
• ...may not be assigned to.
• ...must be initialized

const float Pi = 3.14159;


const int classSize = 100;

22
Constant Variables: Examples
const int myID = 192;

myID = 666; /* Error! */

const int passMark = 80;

short char pAsSgRaDe = ’P’;

const float pi = 3.1415926; /* oops */

const double golden_ratio = 1.61803398874989;


23
Example: Constants
#include <stdio.h>
/* Converts an angle in degrees to
Converts an angle from radians. */
degrees to radians
const float PI = 3.1415926;
int main()
output “Enter angle in {
degrees” float angleInDegs;
input angleInDegrees float angleInRads;

angleInRadians =  / printf("Enter angle in degrees:");


180 * scanf("%f", &angleInDegs);
angleInDegrees
angleInRads = PI/180*angleInDegs;
output angleInRadians
printf("%f\n", angleInRads);
return 0;
}

24
Example: Constants
#include <stdio.h>
/* Converts an angle in degrees to
radians. */
“Global” const float PI = 3.1415926;
constant variable
int main()
{
float angleInDegs;
float angleInRads;

“Local” variables printf("Enter angle in degrees: ");


scanf("%f", &angleInDegs);

angleInRads = PI/180*angleInDegs;

printf("%f\n", angleInRads);
return 0;
}
more on this later... 25

You might also like