Sunteți pe pagina 1din 15

Experiment 3

Printf and Specifiers

OBJECTIVES:

1. To be familiar with the function of each data specifier.


2. To write a program that will implement several data specifiers.
3. To create a program that will include for loop statement.

INTRODUCTION:

Printf (and a few related functions) use the first string as a template in which specifiers
are replaced by formatted values taken from the other parameters.

Specifiers

Specifier Meaning
%c print a character
%d print an integer in decimal format
%e print a float in exponential format
%f print a float in fixed point format
%g print like %f or %e, whichever uses fewer characters
%o print an integer in octal format
%s print a character string
%u print an unsigned number
%x print in hexadecimal format

General Syntax for a for loop:


for (initial statement; condition for entry; final statement)
statement or block
PROCEDURE:

1. Write, compile and execute a program that will display the sum of digits from 1 to 100
using for loop statement.
2. Record the output of your program.
3. Display the value of the output variable in the for loop statement per step.
4. Encode the program shown below.
#include <stdio.h>
int main(void)
{
int i, j, k;
i = 0;
j = i++;
k = ++i;
printf("i = %d\tj = %d\tk = %d\n", i, j, k);
getch();
return(0);
}
5. Explain the output of the program.
6. Differentiate the function of statements 3 and 4.

CONCLUSION:

________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
__________________________________________________________.
Experiment 4

CHARACTER INPUT/OUTPUT

OBJECTIVES:

1. To familiarize the usage of INPUT/OUTPUT functions.


2. To emphasize on how to copy characters from Input to Output.
3. To write a program that will illustrate the process of copying characters from input to
output.
INTRODUCTION:

The following program echoes single characters:


#include <stdio.h>
int main(void)
{
int ch;
putchar('?'); /* display a prompt */
ch = getchar();
putchar(ch);
getch();

return (0);
}

But it will not accept the character or display it until the Return or Enter key is pressed.

PROCEDURE:

1. Execute and write the output of the program written below.


#include <stdio.h>
int main(void)
{
int c;
c = getchar();
while (c != EOF)
{
putchar(c);
c = getchar();
}
return(0);
}
2. What is the function of the expression “c != EOF “?
3. Briefly explain the function of the variable assignment c=getchar(); .
4. Modify the given program. The new program must also display the number of
characters copied.

CONCLUSION:

________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
__________________________________________________________.
Experiment 5

If and if-else Conditional Statement


OBJECTIVES:

1. To know the function of if and if-else statements.


2. To create a program that uses an if and if-else conditional statements.
3. To give emphasis on the differences of while, if and if-else conditional statements.
INTRODUCTION:

Format of the if statement


if (condition)
statement;
or
if (condition) {
statements
}

Format of the if-else statement


if (condition)
statement or block
else
statement or block

PROCEDURE:

1. Write the program shown below using C compiler.


#include <stdio.h>
int main(void)
{
int c, nl;
nl = 0;
while ((c = getchar()) != EOF)
if (c == '\n')
nl++;
printf("%d\n", nl);
return(0);
}

2. Compile the program and execute it. What is the output of the program?
3. How does the if condition work in the program?
4. What is the role of the while statement?
5. Create a new blank file in C compiler and include the following commands.
#include <stdio.h>
#define YES 1
#define NO 0
int main(void)
{
int c, nl, nw, nc, inword;
inword = NO;
nl = nw = nc= 0;

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


nc++;
if (c == '\n')
nl++;
if (c == ' ' || c == '\n' || c == '\t')
inword = NO;
else if (inword == NO) {
inword = YES;
nw++;
}
}
printf("Lines = %d\tword = %d\tcharacters = %d\n",nl, nw, nc);
return(0);
}

6. What is the output of the program?


7. Compare and give the differences of the two control loop statements (if and if else).
8. What is the advantage of using if-else statement rather than if statement only?
9. Is there a minimum or maximum limit of using if and if-else conditions? Explain.

CONCLUSION:

________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
__________________________________________________________.
Experiment 6

ARRAYS
OBJECTIVES:

1. To understand the array declaration in C programming.


2. To initialize and modify the content of an array.
3. To evaluate and display the final content of an array.
INTRODUCTION:
Arrays are defined in C by writing int a[3];
which allocates storage for 3 integers, referred to as a[0], a[1], a[2].

PROCEDURE:
1. What is the function of the program shown below?
#include <stdio.h>
#define SIZE 20
int main(void)
{
int i, n, x[SIZE];
float sum, average;
printf("Enter the number of values\t?");
scanf("%d", &n);
sum = 0;
for (i = 0; i < n; i++) {
printf("Enter value #%d\t?", i);
scanf("%d", &x[i]);
sum += x[i];
}
average = sum / n;
printf("The average is %f\n", average);
for (i = 0; i < n; i++)
printf("x[%d] = %d\n", i, x[i]);
return(0);
}

2. What is the function of the “&” operator in the scanf function?


3. What is the purpose of the statement, sum+=x[i]; .
4. Briefly explain the purpose of the first for loop statement in the given program.
5. Write the output of the second for loop statement per step.
6. Modify the given program such that the lowest and highest numbers will be displayed
also into the screen.
CONCLUSION:

________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
__________________________________________________________.
Experiment 7

FUNCTIONS
OBJECTIVES:

1. To present and create a new program that uses a function.

2. To emphasize how a function works.

3. To demonstrate the significance of a function.

INTRODUCTION:

A C program is a collection of functions. Since C has no exponentiation operators, we


will write a function for it and a driver to use in testing it.

PROCEDURE:

1. Compile and execute the program below.


#include <stdio.h>
int power(int x, int n);
int main(void)
{
int i;
for (i = 0; i < 10; i++)
printf("%d\t%d\t%d\n", i, power(2, i),power(-3, i));
return(0);
}

int power(int x, int n)


{
int i, p = 1;
for (i = 1; i <= n; i++)
p *= x;
return(p);
}

2. Observe the output of the program and describe how the function was used in the
program.
3. Describe the significance of using a function.
4. Create a program that computes the area of a circle, triangle, rectangle and square using
four functions.

CONCLUSION:
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
__________________________________________________________.
Experiment 8
PASSING PARAMETERS
OBJECTIVES:

1. To understand the process of passing parameters to a function.


2. To be able to create functions in a C program.
3. To design a C program that is capable of performing arithmetic operations using
passing parameters.

INTRODUCTION:
C uses call by value. The function gets a copy of the original values.
Any changes made in the function to the parameters' values are never seen by the
function that did the calling.

PROCEDURE:

1. What is the advantage of passing parameters to a function?


2. Write the function of the program written below.
#include <stdio.h>
void try_to_change_it(int);
int main(void)
{
int a = 1;
printf("%d\n", a);
try_to_change_it(a);
printf("%d\n", a);
return(0);
}
void try_to_change_it(int a)
{
a = 777;
}
3. Modify the program. The sum of the first and second values of variable a must be
displayed also into the screen.
4. Add a second function called try_to_change_it_again. The value of the variable inside
this function will be displayed.
5. Write a program that is called simple calculator. The source program includes four
functions namely add, subtract, multiply, and divide. The program will prompt the
user to enter two integers. The sum, different, product, and quotient will be displayed.
CONCLUSION:

________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
__________________________________________________________.
Experiment 9
STRINGS

OBJECTIVES:

1. To understand the syntax and the method of string declaration.


2. To be able to store and retrieve the content of a memory location.
3. To analyze a program that will count the number of characters of each line.
INTRODUCTION:

There are no strings in C, only arrays of characters.


Our string "hello, world\n" is stored like this:
h e l l o , w o r l d \n \o

PROCEDURE:

1. What is the total number of memory cells required to store n-characters of data?
2. What is the syntax for string declaration capable of holding 100 characters?
3. Compile and write the output of the following program.
#include <stdio.h>
#define MAXLINE 1000;
int getline(char s[], int lim);
void copy(char s1[], char s2[]);
int main(void)
{
int len;
int max;
char line[MAXLINE];
char save[MAXLINE];
max = 0;
while ((len = getline(line, MAXLINE)) > 0)
if (len > max) {
max = len;
copy (line, save);
}
if (max > 0)
printf("%s", save);
return(0);
}
int getline(char s[], int lim)
{
int c, i;
for (i = 0; i < lim - 1&& (c = getchar()) != EOF && c != '\n'; )
s[i++] = c;
if (c == '\n')
s[i++] =c;
s[i] = '\0';
return(i);
}
void copy(char s1[], char s2[])
{
int i = 0;
while ((s2[i] = s1[i]) != '\0')
i++;
}

4. What is the function of the expression c == '\n' in the given program?


5. What is the purpose of using the directive #DEFINE?
6. Describe the output of the function called getline().
7. Describe also the role of the function called copy().

CONCLUSION:

________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
__________________________________________________________.

S-ar putea să vă placă și