C Program Learning Part-4 - Engineer Simple

Search This Blog

C Program Learning Part-4

 MANAGING INPUT AND OUTPUT OPERATORS

READING A CHARACTER

Reading a single character can be done by using the function getchar. The getchar takes the following form: variable_name = getchar();

C Program Exercise Part-04


For example:

 

char name;

name = getchar();


main() 

char answer;

printf(“Would you want to know my name.\n”);

printf(“Enter Y for YES and N for NO”);

answer = getchar();

if(answer == ‘Y’ || answer == ‘y’)

printf(“My name is Billah.”);

else 

printf(“You are good for nothing.”); 

}

 WRITING A CHARACTER 

putchar is used for writing character one at a time as shown below putchar (variable_name);

main()

{

char alphabet;

printf(“Enter an alphabet.\n”);

putchar(‘\n’);

alphabet = getchar();

if(islower(alphabet))

putchar(toupper(alphabet));

else

putchar(tolower(alphabet));

}

Formatted Input: 

scanf(“control string”, arg1, arg2,……….., argN); 

Inputting Integer Numbers:
%w d 
scanf(“%2d %5d”, &num1, &num2); 
int a,b,c,x,y,z; printf(“Enter three integer numbers:”); 
scanf(“%d %*d %d”, &a, &b, &c); 
printf(“Enter two 4 digit number:”); 
scanf(“%2d %4d ”, &x, &y); 
printf(“Enter two integer numbers:”); 
scanf(“%d %d ”, &a, &x);
Inputting Real Numbers : 
scanf(“%f %f %f”, &x, &y, &z); 

Inputting Character Strings:

%wc or %wc

int no;

char name1[15], name2[15], name3[15];

printf(“Enter serial and name one. \n”);

scanf(“%d %15c”, &no, name1);

printf(“%d %15s”, &no, name1);

printf(“Enter serial and name two. \n”);

scanf(“%d %s”, &no, name2);

printf(“%d %15s”, &no, name2);

printf(“Enter serial and name three. \n”);

scanf(“%d %15s”, &no, name3);

printf(“%d %15s”, &no, name3);

Formatted Output:

For Printing an integer number is : %w d

Format

Output

printf(“%d”,9876);

9

8

7

6

printf(“%6d”,9876);

 

 

9

8

7

6

printf(“%2d”,9876);

9

8

7

6

printf(“%-6d”,9876);

9

8

7

6

printf(“%06d”,9876);

0

0

9

8

7

6

 

For Printing an integer number is : %w p f

For printing the output of the number y=98.7654

 

 

Format

Output

printf(“%7.4f”,y);

9

8

.

7

6

5

4

printf(“%7.2f”,y);

 

 

9

8

.

7

7

printf(“%-7.2f”,y);

9

8

.

7

7

 

 

printf(“%f”,y);

9

8

.

7

6

5

4

printf(“%10.2e”,y);

 

 

9

.

8

8

e

+

0

1

printf(“%11.4e”,-y);

-

9

.

8

7

6

5

e

+

0

1

printf(“%-10.2e”,y);

9

.

8

8

e

+

0

1

 

 

printf(“%e”,y);

9

.

8

7

6

5

4

0

e

+

0

1

 

Printing of a Single Character:

A single character can be displayed using the format: %wc

Printing of Strings:

The format is : %w.ps

For Printing a string “NEW DELHI 110001”

Format

Output

%s

N

E

W

 

D

E

L

H

I

 

1

1

0

0

0

1

 

 

 

 

%20s

 

 

 

 

N

E

W

 

D

E

L

H

I

 

1

1

0

0

0

1

%20.10s

 

 

 

 

 

 

 

 

 

 

N

E

W

 

D

E

L

H

I

 

%.5s

N

E

W

 

D

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

%-20.10s

N

E

W

 

D

E

L

H

I

 

 

 

 

 

 

 

 

 

 

 

%5s

N

E

W

 

D

E

L

H

I

 

1

1

0

0

0

1

 

 

 

 

 


Next Post Previous Post
No Comment
Add Comment
comment url