C LANGUAGE
INTRODUCTION
C is a programming language developed at AT&T’s BELL Laboratory of USA in
1972. Dennis
Ritchie designed it. Because of its reliability. C is very popular. C is highly
portable & it is well
suited for structured programming. C program consists of collection of functions.
Hardware Requirement: Desktop Computer / labtop computer
Software Requirement: Linux Operating System with GCC/ TURBOC IN
WINDOWS OS
GCC
Released by the Free Software Foundation. gcc is a Linux-based C compiler
usually operated
via the command line. It often comes distributed with a linux installation, so if you
are running
Unix or a Linux variant you likely have it on your system. You can invoke gcc on
a source code
file simply by typing:-
gcc filename
The default executable output of gcc is "a.out", which can be run by typing “
./a.out”. It is also
possible to specify a name for the executable file at the command line by using the
syntax
-o output file, as shown in the following example: -
gcc filename -o output file
Again, you can run your program with "./outputfile". (The ./ is there to ensure you
run the
program for the current working directory.)
Note: If you need to use functions from the math library (generally functions from
math.h such
as sin or sqrt), then you need to explicitly ask it to link with that library with the -l
flag and the
library 'm':
gcc filename -o outputfile –lm
Turbo C/C++
Open Turbo C from your Desktop or Programs menu. Select “File” from Menu bar
and select
option “New” and Save C program in filename .C extension.
To do compiling – Select -> Compile from menu and click-> compile.
If the compilation is success – you will see a “success” message. Else you will see
the number
of errors.
To RUN the program – you may select ->Run from menu and click -> Run
Now you will see the output screen.
Title: Write a Program to display “Hello World”.
Program:
#include <stdio.h>
void main()
{
printf(“Hello World\n”);
}
Output:
Hello World
Experiment No. 1
Title: Write a C program to declare and initialize variables of different data types
and display their sizes.
Aim: A program to declare and initialize variables of different data types and
display their sizes.
Theory:
Declaration of a variable in a computer programming language is a statement
used to specify the variable name and its data type. Declaration tells the
compiler about the existence of an entity in the program and its location. When
you declare a variable, you should also initialize it.
Initialization is the process of assigning a value to the Variable. Every
programming language has its own method of initializing the variable. If the
value is not assigned to the Variable, then the process is only called a
Declaration.
To declare and initialize variables of different data types in C, you can use the
following syntax:
Declaration: type variable Name = value
Initialization: data_type variable_name = value
A program that declares and initializes variables of different data types and
displays their sizes can be written in C using
the sizeof() operator. The sizeof() operator calculates the memory size of
variables, arrays, pointers, or expressions in bytes.
Here are some steps to write a program in C to declare and initialize variables of
different data types and display their sizes:
Include the header file <stdio.h> for input/output operations
Start the main() function
Declare and initialize variables of different data types in a single line
Use printf() statements to display the values of the variables in a formatted
string
Use format specifiers like %d, %c, and %f to indicate the data type of the
value
Use the newline escape sequence (\n) to move the cursor to the next line
after each output
Procedure: Save program in a file, Compile program, debug errors, Execute or
Run program with necessary inputs. Verify the outputs obtained.
Program:
#include <stdio.h>
int main()
{
int age=25;
float height=5.9;
double weight=70.5;
char initial='A';
printf("age:%d\n", age);
printf("height:%.1f\n", height);
printf("weight:%.1lf\n", weight);
printf("initial:%c\n", initial);
return 0;
}
Outputs:
Experiment No. 2
Title: Implement arithmetic, relational, and logical operations in C programs and
display the results.
Aim: A program to implement arithmetic, relational, and logical operations in C
programs and display the results.
Theory:
Arithmetic operators:
Arithmetic operators in C are certain special symbols, predefined to perform
arithmetic operations like addition, subtraction, multiplication and division.
In addition to the above operations assigned to the four symbols +, −, *,
and / respectively, C has another arithmetic operator called the modulo
operator for which we use the %symbol.
Operator Description
+ Adds two operands.
− Subtracts second operand from the first.
* Multiplies both operands.
/ Divides numerator by denominator.
% Modulus Operator and remainder of after an integer division.
Relational operators:
In C, relational operators are the symbols that are used for comparison between
two values to understand the type of relationship a pair of numbers shares. The
result that we get after the relational operation is a boolean value, that tells whether
the comparison is true or false. Relational operators are mainly used in conditional
statements and loops to check the conditions in C programming.
Logical Operators:
Logical operators in C are used to combine multiple conditions/constraints.
Logical Operators returns either 0 or 1, it depends on whether the expression
result is true or false. In C programming for decision-making, we use logical
operators.
We have 3 logical operators in the C language:
Logical AND ( && )
Logical OR ( || )
Logical NOT ( ! )
Procedure: Save program in a file, Compile program, debug errors, Execute or
Run program with necessary inputs. Verify the outputs obtained.
Arithmetic Operators
Algorithm:
To perform Arithmetic operations on two given numbers:
Step 1: Read A, B
Step 2: Let Sum= A+B
Step 3: Let Difference=A-B
Step 4: Let Product=A*B
Step 5: Let Quotient=A/B
Step 6: Let Remainder=A%B
Step 7: Print Sum, Product
Step 8: Stop.
Flowchart:
Start
Declare Variables
Initialize Variables
Apply Arithmetic operators
Store the Results
Print the Result
End
Program:
Addition of Two numbers:
1) By using Constant values
#include <stdio.h>
int main()
{
int x = 15;
int y = 25;
int result = x+y;
printf(“Sum:%d\n”, result);
return 0;
}
2) By accepting user’s input:
#include <stdio.h>
int main()
{
int a, b, c;
printf(“enter any two values\n”);
scanf("%d%d", &a, &b);
c=a+b;
printf("Ans=%d\t", c);
return 0;
}
Outputs:
1) By using Constant values
2) By accepting user’s input:
Subtraction of Two numbers:
1) By using Constant values
#include <stdio.h>
int main()
{
int x = 50;
int y = 30;
int result = x-y;
printf(“Difference:%d\n”, result);
return 0;
}
2) By accepting user’s input:
#include <stdio.h>
int main()
{
int a, b, c;
printf(“enter any two values\n”);
scanf("%d%d", &a, &b);
c=a-b;
printf("Ans=%d\t", c);
return 0;
}
Outputs:
1) By using Constant values
2) By accepting user’s input:
Multiplication of Two numbers:
1) By using Constant values
#include <stdio.h>
int main()
{
int x = 7;
int y = 6;
int result = x*y;
printf(“Product:%d\n”, result);
return 0;
}
2) By accepting user’s input:
#include <stdio.h>
int main()
{
int a, b, c;
printf(“enter any two values\n”);
scanf("%d%d", &a, &b);
c=a*b;
printf("Ans=%d\t", c);
return 0;
}
Outputs:
1) By using Constant values:
2) By accepting user’s input:
Division of Two numbers:
1) By using Constant values
#include <stdio.h>
int main()
{
int x = 45;
int y = 9;
nt result = x/y;
printf(“Quotient:%d\n”, result);
return 0;
}
2) By accepting user’s input:
#include <stdio.h>
int main()
{
int a, b, c;
printf(“enter any two values\n”);
scanf("%d%d", &a, &b);
c=a/b;
printf("Ans=%d\t", c);
return 0;
}
Outputs:
3) By using Constant values
4) By accepting user’s input:
Modulus of Two numbers:
1) By using Constant values
#include <stdio.h>
int main()
{
int x = 29;
int y = 4;
int result = x%y;
printf(“Remainder:%d\n”, result);
return 0;
}
2) By accepting user’s input:
#include <stdio.h>
int main()
{
int a, b, c;
printf(“enter any two values\n”);
scanf("%d%d", &a, &b);
c=a%b;
printf("Ans=%d\t", c);
return 0;
}
Outputs:
1) By using Constant values
2) By accepting user’s input: