Top Banner
Example ?: Declarations and Initializations char a = „a‟, b = „b‟; int i = 1, j = 2; double x = 7.07; Expression Value Type i == j ? a 1 : b + 1 j % 3 == 0 ? i + 4 : x j % 3 ? i + 4 : x 99 int 7.07 double 5.0 double
36

lecture 3

Jul 20, 2016

Download

Documents

NayanaKumar

lecture 2.pdf
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: lecture 3

Example ?:

Declarations and Initializations

char a = „a‟, b = „b‟;

int i = 1, j = 2;

double x = 7.07;

Expression Value Type

i == j ? a – 1 : b + 1

j % 3 == 0 ? i + 4 : x

j % 3 ? i + 4 : x

99 int

7.07 double

5.0 double

Page 2: lecture 3

Types, Constants

Types are “categories” of values.

C provides four fundamental data types:

char, int, float and double.

These are all keywords.

Other data types like arrays, pointers and

structures are derived from the

fundamental data types.

Constants are values of basic types –

„a‟, 7, 65.0 etc

Page 3: lecture 3

Character

Includes lower and upper case letters,

digits, punctuation and special

characters.

Size of a character is 1 byte.

There are no character constants, all

character constants are integers.

ANSI C provides three types of

character – char, signed char, unsigned

char

Page 4: lecture 3

Integers (int)

Natural nos. 0,1,2 .. and their negatives

also there are octal and hexadecimal

integers.

Size of an integer is dependent on the

m/c but usually 4 bytes.

If the value assigned to an integer

variable is greater than the maximum

allowable value, integer overflow occurs.

Page 5: lecture 3

Integral Types

short

provides less storage, usually 2 bytes.

long

provides more storage (than int but not

necessary), usually 4 bytes.

unsigned

have no sign.

Page 6: lecture 3

Floating Types

Use to hold real values

Three types – float (4 bytes), double (8

bytes) and long double (10 bytes).

Suffix can be appended to specify its

type – 1.2F, 1.2D, 1.2L

Precision and range.

Page 7: lecture 3

typedef

Explicitly associate a type with an

identifier

typedef int integer;

typedef unsigned long size_t;

Allows abbreviating long declarations,

provide meaningful type names

Page 8: lecture 3

Fundamental Data Types

Type Declarations

Character char/ signed

char

unsigned char

Integer short unsigned short

int unsigned

long unsigned long

float float

Double double long double

Page 9: lecture 3

Data Types and their Size

Page 10: lecture 3

Variables

Variables are name of a

location in memory that

hold values –

int i, sum;

All variables must be

declared before they can

be used

Page 11: lecture 3

Expressions

A constant is an expression, eg. 3.0

A variable is an expression, eg. a

A function call is an expression,Eg printf(..)

Any meaningful combinations of constants,

variables, operators and function calls,

eg. c=a +b

Page 12: lecture 3

Statments

Expression Statement:

sum+=i;

Selection Statement:

if .. else

Control a program‟s flow of control

(Iteration Statement)

while, do .. while, etc

Empty statement, compound statement

;, { … }

Page 13: lecture 3

sizeof Operator

Syntax: sizeof(object)

object can be type (int, float etc) or an

expression (a+b)

Returns the number of bytes needed to

store an object.

printf(“char: %u\n”, sizeof(char));

printf(“Int: %u%u%u\n”, sizeof(int));

printf(“char: %u%u%u\n”, sizeof(a+b));

Page 14: lecture 3

getchar() and putchar()

#include<stdio.h>

main(void) {

int c;

while((c=getchar()) != EOF) {

putchar(c);

putchar(c);

}

}

Page 15: lecture 3

getchar() and putchar() – (2)

#include<stdio.h>

main(void) {

int c;

while((c=getchar()) != EOF) {

putchar(c);

putchar(c);

}

}

Page 16: lecture 3

getchar() and putchar() – (3)

#include<stdio.h>

main(void) {

int c;

while((c=getchar()) != EOF) {

putchar(c);

putchar(c);

}

}

Page 17: lecture 3

getchar() and putchar() – (4)

#include<stdio.h>

main(void) {

int c;

while((c=getchar()) != EOF) {

putchar(c);

putchar(c);

}

}

Page 18: lecture 3

Another getchar() Example

Write a program that takes a string of

characters as input and prints the number

of blanks, digits, letters, newlines and

others.

Page 19: lecture 3

#include<stdio.h>

main() {

int count_blnk=0;

int count_digits=0;

int count_letters=0;

int count_nl=0;

int count_others=0;

int c;

……….

}

Declarations

Page 20: lecture 3

#include<stdio.h>

main() {

// Declaration

while((c=getchar())!=EOF) {

….

}

The Loop

Page 21: lecture 3

while((c=getchar())!=EOF) {

if (c == „ „) ++count_blnk;

else if (c >= „0‟ && c <= „9‟) ++count_digits;

else if (c >= „a‟ && c <= „z‟ || c >= „A‟ && c <= „Z‟)

++count_letters;

else if (c == '\n') ++count_nl;

else ++count_others;

}

printf("Blanks = %d\t …..);

}

The if – else statement

Page 22: lecture 3

Conversions and Casts

Type conversion or casting consists of

converting an entity of one data type to

another data type.

Implicit type conversion also known as

coercion is done by the compiler

Explicit type conversion is explicitly

defined within a program called casting

or casts

Page 23: lecture 3

Integral promotion

In an expression

if all the values of the original type can be

represented by an int, then the value is

converted to an int. Example:

char c = „A‟;

Otherwise it is converted to an unsigned

int.

Page 24: lecture 3

Arithmetic Conversion Rules

Operand

(a)

Other op-

erand (b)

Result

long double ? (any) b is converted to long double

double ? (any but not ld) b is converted to double

float ? (….) b is converted to float

unsigned long ? (…...) b is converted to unsigned long

long unsigned Either long or unsigned

long ? (…..) long

unsigned ? (…..) unsigned

The rules are applied in the following order

Page 25: lecture 3

Conversion Example

char c; short s; int i;

long l; unsigned u; unsigned long ul;

float f; double d; long double ld;

Expression Type Expression Type

c – s / i u * 7 – i

u * 2.0 – i 7 * s * ul

ld + c c + 3

2 * I / 1 d = i

int unsigned

double unsigned long

long double int

long double

Page 26: lecture 3

Casts

Explicit conversion is called casting

Syntax: (type) identifier

Examples

(double) i;

f = (float) ((int) d + 1);

Page 27: lecture 3

Functions Functions

Modularize a program

All variables defined inside functions are local variables

Parameters Communicate information between functions

Local variables

Benefits of functions Divide and conquer

Software reusability

Avoid code repetition

Page 28: lecture 3

Program to compute

factorial N#include<stdio.h>

int factorial(int n);

int main(void) {

int input, output;

scanf("%d", &input);

output = factorial(input);

printf("Factorial of %d is %d\n", input, output);

}

int factorial(int n) {

int i, prod = 1;

for (i = 2; i <= n; ++i)

prod *=i;

return prod;

}

Page 29: lecture 3

Program to compute

factorial N#include<stdio.h>

int factorial(int n);

int main(void) {

int input, output;

scanf("%d", &input);

output = factorial(input);

printf("Factorial of %d is %d\n", input, output);

}

int factorial(int n) {

int i, prod = 1;

for (i = 2; i <= n; ++i)

prod *=i;

return prod;

}

Page 30: lecture 3

Program to compute

factorial N – main() function#include<stdio.h>

int factorial(int n);

int main(void) {

int input, output;

scanf("%d", &input);

output = factorial(input);

printf("Factorial of %d is %d\n", input,

output);

}

Page 31: lecture 3

Function Declaration

Function declarations are generated in

various ways:

function invocation

compiler assumes int f();

no assumption about parameter list

function definition

provides both declaration and defintion.

no assumption about parameter list

function declaration/prototype

Page 32: lecture 3

Function Declaration (2)

Functions should be declared before

they can be used, called the function

prototype.

Tells the compiler the number and type

of argument that will be passed to the

function and the type of value that will

be returned.

Syntax: type function_name(parameter

type list)

Page 33: lecture 3

Program to compute

factorial N#include<stdio.h>

int factorial(int n);

int main(void) {

int input, output;

scanf("%d", &input);

output = factorial(input);

printf("Factorial of %d is %d\n", input, output);

}

int factorial(int n) {

int i, prod = 1;

for (i = 2; i <= n; ++i)

prod *=i;

return prod;

}

Page 34: lecture 3

Program to compute

factorial N – factorial() function

int factorial(int n) {

int i, prod = 1;

for (i = 2; i <= n; ++i)

prod *=i;

return prod;

}

Page 35: lecture 3

Function Definitions Function definition format

return-value-type function-name( parameter-list ){

declarations and statements}

Function-name: any valid identifier

Return-value-type: data type of the result

(default int) will be converted if necessary

void – indicates that the function returns nothing

Parameter-list: comma separated list,

declares parameters

Page 36: lecture 3

36

Function Definitions (2) Definitions and statements: function

body (block) Variables can be defined inside blocks (can be

nested)

Functions can not be defined inside other functions

Returning control If nothing returned

return;

or, until reaches right brace

If something returned

return expression;