0% found this document useful (0 votes)
2 views

Lab 2_Introducing_C_Programming_1-1

The document provides an introduction to C programming, covering topics such as writing C programs, data types, pointers, arrays, and strings. It includes example code snippets for variable declaration, input handling, and array manipulation, as well as lab tasks for practical application. The lab tasks involve writing various C programs to reinforce the concepts learned.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab 2_Introducing_C_Programming_1-1

The document provides an introduction to C programming, covering topics such as writing C programs, data types, pointers, arrays, and strings. It includes example code snippets for variable declaration, input handling, and array manipulation, as well as lab tasks for practical application. The lab tasks involve writing various C programs to reinforce the concepts learned.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

CSE321 - Operating Systems Lab

Lab 02: Introducing C


Programming (01)
Summary
● How to write a C program
● Data types in C
● Pointer in C
● Array in C
● String in C

How to write a C program:

1
Data types in C

Declare variables:

#include <stdio.h>
int main ()
{
// Variable definition:
int a, b;
int c;
float f;
// actual initialization
a =10;
b =20;
c = a + b;
printf("value of c : %d \n", c);
f = 70.0/3.0;
printf("value of f : %f \n", f);
return 0;
}

2
Taking Inputs
scanf(const char *format, ...) function reads input from the standard input stream stdin and scans
that input according to format provided.

Character input:

3
Arrays in C

#include <stdio.h>
int main ()
{
int n[ 10 ]; /* n is an array of 10 integers */
int i,j;
/* initialize elements of array n to 0 */
for ( i = 0; i < 10; i++ )
{
n[ i ] = i + 100; /* set element at location i to i + 100 */
}
/* output each array element's value */
for (j = 0; j < 10; j++ )
{
printf("Element[%d] = %d\n", j, n[j] );
}
return 0;
}

Multidimensional Arrays:

#include <stdio.h>
int main ()
{
/* an array with 5 rows and 2 columns*/
int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};
int i, j;
/* output each array element's value */
for ( i = 0; i < 5; i++ )
{
for ( j = 0; j < 2; j++ )
{
printf("a[%d][%d] = %d\n", i,j, a[i][j] );
}
}
return 0;
}

4
Pointers in C
A pointer is a variable whose value is the address of another variable, i.e., direct address of the
memory location. Following are the valid pointer declaration:

An example:

Output will be -

5
Incrementing a Pointer

We prefer using a pointer in our program instead of an array because the variable pointer can be
incremented, unlike the array name which cannot be incremented because it is a constant
pointer. An example is -

The output will be -

6
Strings in C:
Strings are an array of characters ending with NULL in C.

Example program:

Output will be-

Functionalities provided by C -

7
Struct in C:
The struct statement defines a new data type, with more than one member for your program.
The format of the struct statement is -

An example program with struct -

8
The output will be -

9
Lab Tasks:
1. Write a C program to print your name, date of birth. and mobile number.
2. Write a C program to compute the perimeter and area of a rectangle with a height of 7 inches. and
width of 5 inches.
3. Write a C program to compute the perimeter and area of a circle with a given radius.
4. Write a program in C to store elements in an array and print it.
5. Write a program in C to read n number of values in an array and display it in reverse order
6. Write a C program to convert a given integer (in seconds) to hours, minutes and seconds.
7. Write a C Program to Swap Values of Two Variables.
8. Write a C Program to Print ASCII Value of a given user input.
9. Write a C program to concatenate two strings given by the user.
10. Write a C program to store students information in a struct. Take input from users for a student and
print it.

10

You might also like