Docsity

Introduction to C Programming Language, Slides of Computer Science

Partial preview of the text

Download Introduction to C Programming Language and more Slides Computer Science in PDF only on Docsity!

Lecture 06: C Language

  • • • (^) Computer program/Memory architectureTypical C program elements
  • • C statements and commentsVariables and Data typesVariables vs. Constants/Literals
  • (^) Standard Inputs/Outputs

Objectives

A C Program

  • • (^) Preprocessor directives provide instructions to the preprocessor, to include functions from the system library, to define the symbolic constants and macro.Every C program must have one main() function. Your

application will start from this function.

  • • (^) Every C statement is terminated with semicolon ‘;’We could write comments to explain code, and comments will not be executed
    • – (^) Comment a single line with //Comment a block with /…......./

C statements/comments

Components of a computer program

Memory

To be able to complete a task, program needs to have the ability to store its data Program’s data is stored in computer’s memory Computer Memory stores data in a sequence of bits (each bit can either store 0 or 1) Even though the smallest unit is a bit, smallest usable unit in memory is a byte Each byte (8 bits) in the memory has an unique location (its counting index)

Variable

Variable^ Variable Data^ Data Variable is used to store data

VariableVariable

Data^ Data

Variable

Variable is used to store data “Variable” means it could store different data at different time

Data^ Data

  • (^) Variable must have a data type, which specifies – – (^) Which kind of data it can containSize of data to be stored in the memory (bytes)
  • (^) Data types can be divided into^ – –^ How to convert from them from/to binary formatPrimitive data types e.g., int, float double, etc.
    • (^) Reference data types e.g., array, pointer, etc.

Data types

Data Types

variable Data Data Data

Basic data types

Basic data types

E.g., 1, 2, 3, etc.^ E.g., 1, 2, 3, etc.^ Size: 4 bytes^ Size: 4 bytesint^ int E.g., 1.1, 1.2, etc.^ E.g., 1.1, 1.2, etc.^ Size: 4 bytes^ Size: 4 bytesfloat^ float^ E.g., 1.234, 2.3,^ E.g., 1.234, 2.3,^ Size: 8 bytes^ Size: 8 bytesdouble^ double etc. etc.

E.g., ‘a’, ‘b, ‘c’,^ E.g., ‘a’, ‘b, ‘c’,^ Size: 1 byte^ Size: 1 bytechar^ char etc. etc.^ Usage: datatype varName;^ E.g., E.g., with initialization int x = 10;^ int^ x; void^ void

  • • (^) Stores numeric dataDeclaration: – (^) Cannot store other type e.g., “Alan” or “abc” int num;
  • • (^) Size (Depends on the Operating System)Integers in the range: -32768 to 32767 – (^) Normally 32 bits (4 bytes)
    • (^) Examples: 12322, 0, -

Type int

  • (^) Stores values containing decimal places – – (^) Cannot store other type e.g., “Alan” or “abc”Precision of up to 10 digits
  • • (^) Declaration:Size (Depends on the Operating System) – (^) Normally 64 bits (8 bytes) double num;
  • (^) Examples: 15.0, 2.456, 1245

Type double

  • • (^) Stores single character informationDeclaration: – (^) Cannot store other type e.g., “Alan” or “abc” char c;
  • • (^) Size (Depends on the Operating System)Examples: ‘a’, ‘b’, ‘$’, ‘%’, ‘1’, ‘2’ – (^) Normally 8 bits (1 byte)

Type char