Programming Language Concepts

Week 3: Constants in Programming

Overview of Programming Languages

  • Various programming languages mentioned include:

    • Microsoft Language Pascal

    • DATATRIEVE

    • ALGOL PL

    • SALSA ATS

    • Assembly, ELAN, Fancy SuperTalk, ABC

    • C++, GM Constraint, XOTd, de Cayenne, Oxygene

    • PEARL, BUSS, notation, Cyclone, TACPOL, shell, Visual

    • Go, ABAP, C/AL, Cg

    • Rust, GDL, Bistro, JOSS, PT, Co Scale, Genie

    • Inform, Sense Talk, Blue, HAL/S, SIMSCRIPT, Revolution, Sawzall, Chapel

    • Agora, Clojure, Ajax, Not Trac, Adenine, A+

    • Object, Rapira, CSKA, SIMPLE, Autolt, Microcode

    • Lynx, SAM76, Redcode, language LispVB, JavaScript

    • Delphi, DASL, Script, PHP, Common, ORCA/Modula-2

    • Arc, Moby, Uniface, Caral, WATFOR, Eustisp

    • ObjectScript, D, R, Forth, GameMonkey, MicroScript

    • Logo, Squirral, Windows/Dos, Kaleidoscope, IMP

    • Newapeak, b, T, Caml, Z, Prolog, JASS

    • Java, Programming, Ruby, EXEC, RPG, CorVision

    • Action, REXX, NETO, Simula, Python

    • Strand, Robot, SP/k, Boomerang, COBOL

    • GraphTalk, Turing, ISWIM, Haxe, Eiffel

    • S/SPL/SQL, A, Chuck

Lecture 2 Recap: Key Concepts

  • High vs low level language

  • Structure of a C program

    • Example: "Hello World" program

  • C Basic Syntax

  • Variables and their definitions

  • Basic input and output

C Program Structure

  • A C program generally consists of the following parts:

    • Preprocessor Commands

    • Functions

    • Variables

    • Statements & Expressions

    • Comments

C Basic Syntax

Variables

  • Definition: A variable is a name given to a storage area that programs can manipulate. It is a symbolic representation of a memory location.

  • Composition: Variable names can be comprised of letters, digits, and the underscore character and must start with a letter or underscore. C is case-sensitive, treating upper and lowercase letters distinctly.

Hello World Example

#include <stdio.h>
int main() {
    /* my first program in C */
    printf("Hello, World!\n");
    return 0;
}

Today's Lecture

  • Focus: C Basic Syntax, Data Types, Constants

Data Types in C

  • Data types are essential for declaring variables or functions of various types—determining space in storage and interpretation of the bit pattern.

  • C allows creating numerous distinct types derived from four fundamental data types:

    • Integer

    • Character

    • Float

    • Double

Qualifiers in C

  • Qualifiers modify base data types:

    • Sign Qualifiers (Signed/Unsigned)

    • Size Qualifiers (Long/Short)

Sign Qualifiers

  • Both integers and floating-point variables can hold negative and positive values.

  • Use of signed or unsigned types:

    • Example: unsigned int student_id; // Will not store negative numbers

  • Storage ranges for a signed integer:

    • 4 bytes: from -2^{31} to 2^{31}-1

  • Storage for an unsigned integer:

    • 4 bytes: from 0 to 2^{32}-1

Size Qualifiers

  • Alters the size of basic types:

    • Long: largest range of signed values.

    • Example: long int memory_address;

    • Short: limited range of signed values.

    • Example: short int day_of_week, week_of_year;

  • Guarantees: The size of a short type is smaller than long. The rationale for short qualifiers is memory conservation. Calculations on long types are slower than on short types.

Standard Integer Types

Type

Storage Size

Value Range

char

1 byte

-128 to 127 or 0 to 255

unsigned char

1 byte

0 to 255

signed char

1 byte

-128 to 127

int

2 or 4 bytes

-32,768 to 32,767 or -2,147,483,648 to 2,147,483,647

unsigned int

2 or 4 bytes

0 to 65,535 or 0 to 4,294,967,295

short

2 bytes

-32,768 to 32,767

unsigned short

2 bytes

0 to 65,535

long

4 bytes

-2,147,483,648 to 2,147,483,647

unsigned long

4 bytes

0 to 4,294,967,295

Standard Floating-Point Types

Type

Storage Size

Value Range

Precision

float

4 bytes

1.2E-38 to 3.4E+38

6 decimal places

double

8 bytes

2.3E-308 to 1.7E+308

15 decimal places

long double

10 bytes

3.4E-4932 to 1.1E+4932

19 decimal places

Character Data Type

  • Keyword: char is used for character type variables

    • Example: char test = 'h'; // test is a character variable with the value 'h'

  • Size of char: 1 byte.

Constants

Definition and Characteristics

  • A constant is a data item storing a fixed value that cannot be altered during execution.

  • Characteristics:

    • Both constants and variables share types.

    • Can be declared as:

    • Integer constant

    • Floating constant

    • Double constant

    • Character constant

  • Best Practice: Use upper-case names for constants.

Defining Constants

  • Constants can be defined via two methods:

    • Using #define preprocessor:

    • Format: #define identifier value

      • Example: #define YEAR 2020

    • Using the const keyword:

    • Format: const type variable = value;

      • Example: const int YEAR=2020;

Types of Constants

  • Primary Constants: Integer, Real, Character

  • Secondary Constants: Array, Pointer, Structure, Union, Enum, etc.

Integer Constants

  • Whole numbers that can be: positive, zero, or negative.

  • Integer constants specified in:

    • Decimal: e.g., 1234, 255

    • Octal: starts with 0, e.g., 0377

    • Hexadecimal: starts with 0x or 0X, e.g., 0xFF, 0x4B

    • Suffix can be included for unsigned or long.

Examples of Integer Constants

85   /* decimal */
0213 /* octal */
0x4b /* hexadecimal */
30   /* int */
30u  /* unsigned int */
30l  /* long */
30ul /* unsigned long */

Integer Constants: Example Code

#include <stdio.h>
int main() {
    const int SIDE = 10; // constant declared
    int area;
    area = SIDE * SIDE; // using constant
    printf("The area of the square with side: %d is: %d sq. units", SIDE, area);
    return 0;
}

Floating-Point Constants

  • Structure:

    • An integer part

    • A decimal point

    • A fractional part

    • An exponent part

  • Representation:

    • Decimal form: must include decimal point or exponent.

    • Exponential form: signed exponent introduced by e or E.

Examples of Floating-Point Constants

3.14159
314159E-5L
510E2
.e55     // Illegal: incomplete exponent

Example Using Constant:

#include <stdio.h>
#define PI 3.14159 // defined constant for PI
int main() {
    float earth_radius = 6378.1; // earth radius float
    int circumference = 2 * PI * earth_radius;
    printf("The circumference of planet earth is: %d km", circumference);
    return 0;
}

Character Constants

  • Enclosed in single quotes (e.g., 'x').

  • Can represent:

    • Plain character

    • Escape sequence (e.g., '\t')

    • Universal character (e.g., '\u02C0')

Escape Sequences

Escape Sequence

Meaning

\

\ character

\'

' character

\