Fundamentals of C
Fundamentals of C
Programming in C
Lab Manual
Lab 1
Fundamentals of C Programming
INDEX
Lab Objectives
Background
Some Examples
Exercises
Lab Objectives:
Explain the basic components of a C Program
To study about data type, identifiers and operators
To explain the operation of arithmetic operators, relational operators, increment &
decrement operators
To acquire knowledge for the evaluation of different types of expressions
Background:
Some basic header files used in C:
<stdio.h> Contains function prototypes for the standard input output library function and
the information to use them
<stdlib.h> Contains function prototypes for the conversion of number to text and text to
number, memory allocation, random number, and other utility functions
<math.h> Contains function prototypes for math library functions (such as: abs(), sqrt(),
pow()…)
<string.h> Contains function prototypes for string processing functions (such as: strlen(),
strrev()…)
double Double precision floating point number 8 bytes 1.7e +/- 308 (~15 digits)
There are two types of arithmetic operators in C:
Increment & Decrement Operators: C has two special operators for incrementing or
decrementing a variable by 1:
++ (increment)
-- (decrement)
Some Examples:
1. Write a program that displays “Why so serious ??? ...”.
Program code:
#include<stdio.h>
int main(){
printf("Why so serious ??? ... ");
return 0;
}
2. Write a program that read an integer, a floating point number, a long integer, a
character and displays all of them.
Program code:
#include<stdio.h>
int main(){
int integerNumber;
float floatNumber;
long longInteger;
char character;
// For Integer Number
printf("\nEnter an integer: ");
scanf("%d",&integerNumber);
// For Floating Point
printf("\nEnter a floating point number: ");
scanf("%f",&floatNumber);
// For Long Integer
printf("\nEnter a long number: ");
scanf("%ld",&longInteger);
// For Character
printf("\nEnter a character: ");
scanf(" %c",&character);
// Display All:
printf("\n your integer is %d ", integerNumber);
printf("\n your floating point number is %f ", floatNumber);
printf("\n your long number is %ld ", longInteger);
printf("\n your character is %c ", character);
return 0;
}
3. Write a program that reads two integers and displays their sum, difference ( i.e.,
Number1 – Number2) & product.
Program Code:
#include<stdio.h>
int main(){
int a,b;
printf("Enter first integer:");
scanf("%d",&a);
printf("\nEnter second integer:");
scanf("%d",&b);
printf("\nSum is %d",a+b); // for summation
printf("\nDifference is %d",a-b); // for subtraction
printf("\nProduct is %d",a*b); // for multiplication
return 0; }
4. Write a program that reads two integers and compares them by using different
Relational Operators ( < , > , <= , >= , == , != ).
Program code:
#include<stdio.h>
int main(){
int number1, number2;
printf("Enter 1st Integer(x): ");
scanf("%d",&number1);
printf("Enter 2nd Integer(y): ");
scanf("%d",&number2);
5. Write a program that reads the radius of a circle and display its area.
Program code:
#include<stdio.h>
#define PI 3.1416
int main(){
float radius, area;
printf("Enter the Radius of the Circle: ");
scanf("%f",&radius);
area = PI * radius * radius;
printf("\n The Area of the Circle = %.3f\n", area);
return 0; }
6. Write a program that displays the size of constant and different types of variables.
Program code:
#include <stdio.h>
#define PI 3.1416
int main(){
int a; float b; double c; char d;
printf("Size of Constant PI=%d bytes\n",sizeof(PI));
printf("Size of int=%d bytes\n",sizeof(a));
printf("Size of float=%d bytes\n",sizeof(b));
printf("Size of double=%d bytes\n",sizeof(c));
printf("Size of char=%d byte\n",sizeof(d));
return 0;
}
Exercise:
1. Write a program that displays your name, roll no. & department (each information
in a new line).
Sample Output:
NAME: James Bond
Roll no.: 643007
Department: Crime & Investigation