Docsity

C Programming: I/O Functions and Data Types, Slides of Information Technology

Partial preview of the text

Download C Programming: I/O Functions and Data Types and more Slides Information Technology in PDF only on Docsity!

PROGRAM STRUCTURE

Input /Output Statements

Reading ,processing and writing of data are the three

essential

functions

of

a

computer

program.Most programs take some data as inputand display the processed data, often known asinformation

or

results

on

a

suitable

medium.So

far we have used one method of providing valueto

the

program

users.

In

this

method

value

assigned

to

variables

through

the

assignment

statements such as

X=5 and Y=11.

Formatted Input function

The formatted input functions is scanf( )

Every scanf statement contains a control string that describes the format of the data to be input.

Syntax: scanf(“control string”,arg1,arg2…,argn);The

control string

specifies the field format in which the data

is to be entered also known as format specifier.

The

Arguments

arg1,arg2,…,argn specify the address of

locations where the data is stored.

Control string may include: Field specification, consisting of conversion character %, data type character.

Formatted output function is printf( )

Output data can be written from the computer onto a standard output device using the library function printf.

Syntax:

printf(“control,string”,arg1,arg2,...argn);

Formatted Output function

Unformatted functions

There are several library functions under this categories:those that can deal with a single character and those thatcan deal with a string of characters.

Single Character Input functions:

getchar( ), getche( ), getch( ).

Single Character Output:-

putch( ) and putchar( ).

Unformatted Input functions

getchar( )

Syntax :-

variable_ name=getchar( );

Reading a single character can be done by this

function.

getch() Syntax:-

getch();

This function returns a character that has been recently typed. There is no need to press enter key after typinga character.

Unformatted Output functions

putchar( )

like getchar, there is an analogous function putchar for writing or printing one character at a time on thescreen.

Syntax: putchar(variable_name);

Where variable_name is a char type variable containing a character. This statement displays thecharacter contained in the variable, name on thescreen.

Unformatted Output functions

Example:-answer =‘Y’;putchar (answer); will display the character Y on the screen.

The statement

putchar(‘\n’);

would cause the cursor

on the screen to move to the beginning of the next line.

Types of constants

Integer constants:- an integer constants refers to a sequence ofdigits. e.g. 345, -127,

+834, -82.

octal integer

constant I made of any combination of digits from 0-

both inclusive with a leading zero. eg. 043, 026, 057

hexadecimal integer

constant is formed by combination of digits 0-

9 and letter A-F or a-f.

Character constants:-a character represented within single quotesdenotes a character constants. Escape sequence are also characterconstant. eg. ‘c’, ‘4’, ‘X’, ‘\n’

Floating constants:- integer numbers are inadequate to r4epresntcertain quantities that vary continuously, such as distances, etc.e.g. 647.43, +0.321.

String constants: a string constant is a sequence of alphanumericcharacters enclosed in double quotation mark whose maximumlength is 255 characters.e.g. ”welcome to this world”

“$4375”

Variables

Variable is a storage location and an associated

symbolic name which contains some known orunknown quantity or information, a value.

Declaration

<> <>;

int a;

Definition

<>=<>;

a=10;

Usage

<>

a=a+1; //increments the value of a by 1

Data types in C 

Primary(fundamental) data types

int, float, double, char

Derived data types

Arrays

Pointers

functions

User defined data types

Structures, union and enumerated

Fundamental data types

F

undamental data types are the built in data types

and can be classified as follows:-

Integer type

an integer is an integral whole number

without a decimal point. They are of nine type: shortint, unsigned short int, signed int, unsigned int, signedint, long int, unsigned long int, signed long int.

DATA TYPE

SIZE (in bytes)

RANGE

Short intUnsigned short intSigned short intIntUnsigned intSigned intLong intUnsigned long intSigned long int

2

2 2 2 2 2 4 4 4

-32768 to 32767

0 to 65535

-32768 to 32767-32768 to

32767

0 to

65535

-32768 to 32767-2147483648 to 2147483647

0 to 4294967295

-2147483648 to 2147483647

charunsigned charsigned char

1 1 1

-128 to 127

0 to 255

-128 to 127

floatdoublelong double

4 8

10

3.4E-38 to 3.4E + 38

1.7E-308 to 1.7E +3083.4E-4932 to 3.4E + 4932

Derived Data Types

Array

An

array

is

collection

of

identical data objects which are storedin

consecutive

memory

locations

under a common heading or variablename.