• No results found

C Building Blocks.pdf

N/A
N/A
Protected

Academic year: 2020

Share "C Building Blocks.pdf"

Copied!
11
0
0

Loading.... (view fulltext now)

Full text

(1)

1

C Building Blocks

Ch: 2

2

The printf() Function

¾

Powerful and versatile output function.

¾

Uses a unique format for printing constants and

variables.

printf(“This is the number two: 2”);

¾

The function printf() can be given more than 2

arguments.

(2)

3

Format Specifier

A format specifier is used to control where to put a value in a string and what format will be used by printf( ) to print out a particular value.

Here is a listing of the format specifiers.

%c for single character %d for signed decimal integer %f for floating point value

%s for string

%e floating point (exponential notation)

%Lf for long double

%u unsigned decimal integer

%x unsigned hexadecimal integer

%o unsigned octal integer %ld for long integer

%lf for double float

4

Example

1)void main(void) {

printf(“%s is %d million miles from the sun.”, “Venus”,67); }

Output:

Venus is 67 million miles from the sun

2)void main(void) {

printf(“My percentage is %f”, 67.5); }

(3)

5

Field Width Specifier

„

To control how many digits will be printed after the

decimal point, we use the field width specifiers.

Example

void main(void) {

printf(“My percentage is %.2f”, 67.5); }

Output:

My percentage is 67.50

6

Escape Sequences

¾ Special sequence of two characters (\ and code characters) that

provide formatting instructions to a line printed.

¾ So called because the backslash symbol is considered an escape

character. It causes an escape form the normal interpretation of a string so that the next character is recognized as having a special meaning.

\n New Line \t Horizontal Tab \v Vertical tab \b Backspace \r Carriage return \f Form Feed \’ Single quote \” Double Quotes \\ Backslash

\xdd ASCII code in hexadecimal notation, each d represents a hexa-digit, range 0-ff

(4)

7

1) void main (void)

{ clrscr();

printf(“Each \t word \t is \n”); printf(“Tabbed \t over\t once\n”); }

Output

Each Word is

Tabbed over once

2) void main(void)

{ clrscr();

printf(“Department of Automotive \n”); printf(“\n B.E programme \n”);

}

Output:

Department of Automotive B.E Programme

8

Variables & Constants

„

Variable is a space in computer memory set aside for

a certain kind of data and given a name for easy

reference. Naturally the content of the variable can

change.

„

A Constant is a quantity that doesn’t change. This

(5)

9

Example:

1)void main(void) {

printf(“My lucky number is %d”,2); }

Output: My lucky number is 2.

¾ Prints the constant 2 by plugging it in the format specifier. ¾ Can be achieved by writing above program as:

2)void main(void) {

printf(“My lucky number is two : 2”); }

10

3)

void main(void)

{ int num;

num =2;

printf(“My lucky number is %d”,num);

}

Output:

My lucky number is 2.

¾

Give the same output as before but achieved in a

different way.

(6)

11

Variable Definition

„

All variables must be defined to specify their name

and type and set aside storage.

„

E.g.

int num;

¾When you define a variable, the compiler set side an appropriate amount of memory to store that variable.

¾Variable declarationspecify name and data type but doesn't set aside memory.

Data type Variablename

2 Each integer occupies 2 bytes of memory

An integer in memory

Each rectangle is one bytes

12

Rules for Constructing Variable Names

(a). The first character in the variable name must be an alphabet.

(c). No commas or blanks are allowed within a variable name.

(d). No special symbol other than an underscore can be used in a variable name. E.g.: Name, roll_no, fee

C Keywords

Keywords are the words whose meaning has already been explained to the C compiler. The keywords are also called ‘Reserved words’. The keywords cannot be used as variables names.

For example: int, float, if, for, while, etc.

Unsigned Data types

The integer and character types also have unsigned versions

¾ For e.g.. Unsigned int type holds numbers from 0- 65,535 rather than

-32768 to 32767.

(7)

13

Exploring the data types

Floating point variable type float occupies four bytes in memory and can hold numbers from 1038 to 1038 with six digits of precision. Floating point variable type double, occupies eight bytes with 15 digits of precision, and can hold numbers from 10308 to 10-308.Long doubletype occupies 10 bytes

Floating Point

A Character variable occupies one byte in memory in which the characters constant are stored. The type name for a character is char.

Characters

The integer variable occupies two bytes in memory and can hold numbers from -32768 to 32767. The type name for the integer is int. Integer variable type long

or long int, occupies four bytes and can hold numbers from -2147, 483, 648 to 2147, 483, 647.

Integer

14 void main(void)

{

int event = 2; float time=23.45; char heat = ‘c’;

printf(“The winning time in heat %c”, heat); printf(“of event %d was %f”, event, time); }

Output:The winning time in heat C of event 2 was 23.45

Example

(8)

15

The scanf() Function

¾

C has a large collection of input and output functions

but printf() and scanf() are the most versatile and can

handle different types of variables and their

formatting.

¾

Argument on the left side is the string that contains

the format specifiers and on the right is the variable

name with the ampersand (&) preceding the variable

name.

16 void main(void)

{

int event; float time; char heat;

printf(“Type event number, heat letter and time”);

scanf(“%d %c %f”, &event, &heat, &time);

printf(“The winning time in heat %c”, heat); printf(“of event %d was %f”, event, time); }

Output:The winning time in heat B of event 4 was 36.34

(9)

17

How scanf() know we finished typing

¾

As we type three input values say ‘4’, ‘B’ and 36.34,

we separate them by spaces.

¾

scanf() matches each space we type with

corresponding space between the conversion type

characters in scanf() format string %d %c %f

¾

Spaces serves as delimiters because it matches the

space in the format string.

4 B 36.34

scanf(“%d %c %f”, &event, &heat, &time);

18

void main(void)

{

float years, days;

printf(“Enter age in years”);

scanf(“%f, &years);

days = years * 365;

printf(“you are %.1f days old”, days);

}

Output:

Enter age in years:10

(10)

19

Address Operator (&)

¾

C compiler requires the arguments to the scanf() to be

the addresses of variables rather than the variables

themselves

.

¾

Each variable occupies a certain location in memory

and its address is that of the 1

st

byte it occupies.

22 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 Address of integer variable

Address of variable

void main(void) { int n = 2;

n =2;

printf(“Value = %d,address=%d”, n, &n); }

Output: Value is 2, address is 2035

20

getch() and getche() Function

¾ getch() is a limited input function, only characters can be taken

as input where as scanf() is not limited.

¾ In scanf() you need to press [Return] before the function will

know what you have typed. If we want a function that reads a single character the instant it’s typed without waiting for [Return], then we use the getche() function.

¾ If any character is typed wrongly you can’t go back.

¾ It stores and writes the value at runtime i.e. the function

itself takes on or returns the value of each character typed.

¾ get – to get something (i/p function) ¾ ch – it gets a character

(11)

21

void main(void)

{

char ch;

printf(“type any character:”);

ch = getche();

printf(“\n The character you typed was %c”, ch);

}

Output:

Type any character: T

References

Related documents

PrP C silencing downregulates the expression of molecules associated with cancer stem cells, upregulates markers of cell differentiation and affects GSC self-renewal, pointing to

Figure 4: Mean difference (1980–2015) between the (corrected) MERRA-2 precipitation seen by the land surface and the model-generated precipitation within the MERRA-2 system. Results

Convert the source operand (single-precision floating-point) from 2s-comple- ment floating-point format to IEEE floating-point format, and store the result into the 32 MSBs of

Employability Skills & WHOQOL Global Scores from three Social Sciences Faculties in

EBB4.2 Except where new or amended charges are directed or determined by Ofcom or where otherwise provided in this Condition, the Dominant Provider shall send to Ofcom and to

We have implemented a simulation framework to ex- perimentally investigate the behavior and the performance of the online schedulers. The parameters of the simula- tor include

Lengi hefur verið rætt um skort á samvinnu við börn í barnaverndarstarfi, að sjónarmiða þeirra sé ekki leitað eða þau tekin til greina (Guðrún Kristinsdóttir,

DSF: sowing-flowering duration; DSM : sowing-maturity duration; DFM: flowering-maturity duration; HMat: height at maturity; DBT: diameter at the base of the stem; LPed: