Lecture 6 PF

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 22

PROGRAMMING

FUNDAMENTALS
LECTURE # 06

C Data Types, Constants and Variables

C Development
Environment
Disk

Program is created using


the Editor and stored on
Disk.

Disk

Pre-processor program
processes the code.

Disk

Compiler creates object


code and stores it on Disk.

Disk
Disk
Disk

Linker links object code with


libraries, creates
executable code and
stores it on Disk
Loader puts Program in
Memory
CPU takes each instruction
and executes it, storing new
data values as the program
executes.

C Development
Environment

Entering, translating, and running a High-Level


Language Program

C Program Structure

An example of simple program in C


#include <stdio.h>
void main()
{
printf(I love programming\n);
printf(You will love it too once );
printf(you know the trick\n);
}

The output

The previous program will produce the


following output on your screen
I love programming
You will love it too once you know the
trick

Preprocessor directives

a C program line begins with # provides an


instruction to the C preprocessor
It is executed before the actual compilation
is done.
Two most common directives :

#include
#define

In our example (#include<stdio.h>)


identifies the header file for standard input
and output needed by the printf().

Function main

Identify the start of the program


Every C program has a main ( )
'main' is a C keyword. We must not
use it for any other variable.
4 common ways of main declaration
int main(void)

void main(void)

main(void)

main( )

return 0;

The curly braces { }

Identify a segment / body of a program

The start and end of a function


The start and end of the selection or
repetition block.

Since the opening brace indicates the


start of a segment with the closing
brace indicating the end of a segment,
there must be just as many opening
braces as closing braces (this is a
common mistake of beginners)

Statement

A specification of an action to be taken by the


computer as the program executes.
Each statement in C needs to be terminated with
semicolon (;)
Example:
#include <stdio.h>
void main()
{
printf(I love programming\n);
printf(You will love it too once );
printf(you know the trick\n);
}

statement
statement
statement

Statement cont

Statement has two parts :

Declaration
The

part of the program that tells the compiler


the names of memory cells in a program

Executable statements
Program

lines that are converted to machine


language instructions and executed by the
computer

C program skeleton

In short, the basic skeleton of a C


program looks like this:
#include <stdio.h>

Preprocessor directives

Function main
void main(void)
Start of segment
{

statement(s);
}
End of segment

Identifiers

Words used to represent certain program


entities (variables, function names, etc).
Example:

int my_name;
my_name

is an identifier used as a program

variable

void CalculateTotal(int value)


CalculateTotal

name

is an identifier used as a function

Rules for naming identifiers


Rules

Example

Can contain a mix of character and numbers.


However it cannot start with a number

H2o

First character must be a letter or underscore

Number1
_area

Can be of mixed cases including underscore


character

XsquAre
My_num

Cannot contain any arithmetic operators

R*S+T

or any other punctuation marks

#@x%!!

Cannot be a C keyword/reserved word

struct; printf;

Cannot contain a space

My height

identifiers are case sensitive

Tax != tax

Variables

Variable a name associated with a


memory cell whose value can change
Variable Declaration: specifies the
type of a variable

Example: int num;

Variable Definition: assigning a value


to the declared variable

Example:
num

= 5;

Basic Data Types

There are 4 basic data types :

int
float
double
char

int

used to declare numeric program variables of integer


type
whole numbers, positive and negative
keyword: int
int number;
number = 12;

Basic Data Types cont

float

fractional parts, positive and negative

keyword: float
float height;
height = 1.72;

double

used to declare floating point variable of higher


precision or higher range of numbers

exponential numbers, positive and negative

keyword: double
double valuebig;

valuebig = 12E-3; (is equal to 12X10-3)

Basic Data Types cont

char

equivalent to letters in English language


Example of characters:

Numeric digits: 0 - 9
Lowercase/uppercase letters: a - z and A - Z
Space (blank)
Special characters: , . ; ? / ( ) [ ] { } * & % ^ < > etc

single character
keyword: char
char my_letter;
my_letter = 'U';

The declared character must be


enclosed within a single quote!

Constants

Entities that appear in the program code as fixed


values.
Any attempt to modify a CONSTANT will result in error.
4 types of constants:

Integer constants

Positive or negative whole numbers with no fractional part


Example:
const

int MAX_NUM = 10;


const int MIN_NUM = -90;

Floating-point constants (float or double)

Positive or negative decimal numbers with an integer part, a


decimal point and a fractional part
Example:
const

double VAL = 0.5877e2; (stands for


0.5877 x 102)

Constants cont

Character constants
A

character enclosed in a single quotation mark


Example:

const char letter = n;


const char number = 1;
printf(%c, S);
Output would be: S

Constant example volume of a cone


#include <stdio.h>
#Include <conio.h>
void main()
{
clrscr();
const double pi = 3.142;
double height, radius, base, volume;
printf(Enter the height and radius of the cone:);
scanf(%If %If, &height , &radius);
base = pi * radius * radius;
volume = (1.0/3.0) * base * height;
printf(\nThe volume of a cone is %f , volume);
getch();
}

#define

You may also associate constant using #define preprocessor


directive

#include <stdio.h>
#Include <conio.h>
#define pi 3.412
void main()
{
double height, radius, base, volume;
printf(Enter the height and radius of the cone:);
scanf(%lf %lf, &height, &radius);
base = pi * radius * radius;
volume = (1.0/3.0) * base * height;
printf(\nThe volume of a cone is %f , volume);
getch();
}

Summary

We have learned the following items:

environment of C language and C programming


C language elements

Preprocessor directives,
curly braces, main (),
semicolon, comments, double quotes

4 basics data type and brief explanation on variable

Task to do:

Write a program to exchange the values of two


integer variables.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy