SlideShare a Scribd company logo
Basics Of Programming
UNIT II
Introduction to C
● C is a procedural programming language.
● It was initially developed by Dennis Ritchie in the year 1972.
● It was mainly developed as a system programming language to write
an operating system.
● The main features of C language include low-level access to memory,
a simple set of keywords, and clean style, these features make C
language suitable for system programmings like an operating system
or compiler development.
● Many later languages have borrowed syntax/features directly or
indirectly from C language.
● Like syntax of Java, PHP, JavaScript, and many other languages are
mainly based on C language.
● C++ is nearly a superset of C language
History of ‘C’
➢ C programming language was developed in 1972 by Dennis Ritchie at
bell laboratories of AT&T (American Telephone & Telegraph), located
in the U.S.A.
➢ Dennis Ritchie is known as the founder of the c language.
➢ It was developed to overcome the problems of previous languages
such as B, BCPL, etc.
➢ Initially, C language was developed to be used in UNIX operating
system. It inherits many features of previous languages such as B and
BCPL.
General Structure of a ‘C’ program
A C program is divided into different sections. There are six main
sections to a basic c program.
The six sections are,
● Documentation
● Link
● Definition
● Global Declarations
● Main functions
● Subprograms
Copy of UNIT 2 -- Basics Of Programming.pptx
Write a program to print area of a circle.
In the following example we will find the area of a circle for a given
radius 10cm.
Formula
The formula to compute the area of a circle is πr2 where π is PI = 3.1416
(approx.) and r is the radius of the circle.
Lets write the C code to compute the area of the circle.
/*
description: program to find the area of a
circle
using the radius r
*/
#include <stdio.h>
#define PI 3.1416
void main( )
{
float r = 10, area; //A=3.14*r*r
area=PI * r * r;
printf("Area: %.2f", area );
}
The above code will give the following output.
Area: 314.16
Different sections of the above code:
Documentation Section--
➢ This section consists of comment lines which include the name of
programmer, the author and other details like time and date of
writing the program.
➢ Documentation section helps anyone to get an overview of the
program.
Example
/*
file: circle.c
author: yusuf shakeel
date: 2010-11-25
description: program to find the area of a circle using the
radius r
*/
● This section contains a multi line comment describing the code.
● In C, we can create single line comment using two forward slash //
and we can create multi line comment using /* */.
● Comments are ignored by the compiler and is used to write notes
and document code.
Link Section--
● The link section consists of the header files of the functions that
are used in the program.
● It provides instructions to the compiler to link functions from the
system library.
Example
#include<stdio.h>
#include<math.h>
● This section includes header file.
● We are including the stdio.h input/output header file from the C
library.
Definition Section--
All the symbolic constants are written in definition section.
#define PI=3.14
● This section contains constant.
● In the above code we have created a constant PI and assigned
3.1416 to it.
● The #define is a preprocessor compiler directive which is used to
create constants. We generally use uppercase letters to create
constants.
● The #define is not a statement and must not end with a ;
semicolon.
Global Declaration Section---
● The global variables that can be used anywhere in the
program are declared in global declaration section.
● This section also declares the user defined functions.
float area(float r);
● This section contains function declaration.
● We have declared an area function which takes a floating number
(i.e., number with decimal parts) as argument and returns floating
number.
Main Function Section:
➢ It is necessary have one main() function section in every C program.
➢ This section contains two parts, declaration and executable part.
➢ The declaration part declares all the variables that are used in
executable part.
➢ These two parts must be written in between the opening and
closing braces.
➢ Each statement in the declaration and executable part must end
with a semicolon (;).
➢ The execution of program starts at opening braces and ends at
closing braces.
Example--
void main(void)
{
float r = 10;
area=3.14*r*r;
printf("Area: %.2f", area(r));
}
● This is the main() function of the code.
● Inside this function we have created a floating variable r and
assigned 10 to it.
● Then we have called the printf() function. The first argument
contains "Area: %.2f" which means we will print floating number
having only 2 decimal place. In the second argument we are calling
the area() function and passing the value of r to it.
Sub Program Section--
➢ The subprogram section contains all the user defined functions that
are used to perform a specific task.
➢ These user defined functions are called in the main() function.
Example--
float area(float r) {
return PI * r * r;
}
● This section contains a subprogram, an area() function that is called
from the main() function.
● This is the definition of the area() function. It receives the value of
radius in variable r and then returns the area of the circle using the
following formula PI * r * r.
Header files
● C language has numerous libraries that include predefined
functions to make programming easier.
● In C language, header files contain the set of predefined standard
library functions.
● Your request to use a header file in your program by including it
with the C preprocessing directive “#include”.
● All the header file have a ‘.h’ an extension.
● By including a header file, we can use its contents in our program.
Syntax:
#include <filename.h>
or
#include "filename.h"
● It offers the above features by importing them into the program
with the help of a preprocessor directive “#include”.
● These preprocessor directives are used for instructing compiler
that these files need to be processed before compilation.
● In C program should necessarily contain the header file which
stands for standard input and output used to take input with the
help of scanf() and printf() function respectively.
Standard Header Files And Their Uses:
1. #include<stdio.h>: It is used to perform input and output
operations using functions scanf() and printf().
2. #include<iostream.h>: It is used as a stream of Input and Output
using cin and cout.
3. #include<string.h>: It is used to perform various functionalities
related to string manupulation like strlen(), strcmp(), strcpy(), size(),
etc.
4. #include<math.h>: It is used to perform mathematical operations
like sqrt(), log2(), pow(), etc.
Character set
➢ As every language contains a set of characters used to construct
words, statements, etc.,
➢ C language also has a set of characters which include alphabets,
digits, and special symbols.
➢ C language supports a total of 256 characters.
➢ Every C program contains statements. These statements are
constructed using words and these words are constructed using
characters from C character set.
C language character set contains the following set of characters...
1. Alphabets
2. Digits
3. Special Characters
4. White Spaces / Escape Sequence
ALPHABETS
Uppercase letters A-Z
Lowercase letters a-z
DIGITS 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
SPECIAL CHARACTERS
~ tilde % percent sign | vertical
bar
@ at symbol + plus sign < less than
_ underscore - minus sign > greater
than
^ caret # number sign = equal to
& ampersand $ dollar sign /
slash
( left parenthesis * asterisk  back
slash
) right parenthesis ′ apostrophe :
colon
[ left bracket " quotation mark ;
] right bracket ! exclamation mark ,
comma
{ left flower brace ? Question mark . dot operator
} right flower brace
WHITESPACE CHARACTERS / Escape Sequence--
b blank space t horizontal tab
v vertical tab r carriage return
f form feed n new line
 Back slash ’ Single quote
" Double quote ? Question mark
0 Null a Alarm (bell)
Tokens
➢ Tokens in C is the most important element to be used in
creating a program in C.
➢ We can define the token as the smallest individual element in C.
For `example, we cannot create a sentence without using words;
similarly, we cannot create a program in C without using tokens
in C.
➢ Therefore, we can say that tokens in C is the building block or
the basic component for creating a program in C language.
Classification of tokens in C
Tokens in C language can be divided into the following categories:
keywords
● Keywords in C can be defined as the pre-defined or the reserved
words having its own importance, and each keyword has its own
functionality.
● Since keywords are the pre-defined words used by the compiler, so
they cannot be used as the variable names.
● If the keywords are used as the variable names, it means that we
are assigning a different meaning to the keyword, which is not
allowed.
● C language supports 32 keywords given below:
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
Identifiers
● Identifiers in C are used for naming variables, functions, arrays,
structures, etc.
● Identifiers in C are the user-defined words.
● It can be composed of uppercase letters, lowercase letters,
underscore, or digits, but the starting letter should be either an
underscore or an alphabet.
● Identifiers cannot be used as keywords. Rules for constructing
identifiers in C are given below:
● The first character of an identifier should be either an alphabet or
an underscore, and then it can be followed by any of the character,
digit, or underscore.
● It should not begin with any numerical digit.
● In identifiers, both uppercase and lowercase letters are distinct.
Therefore, we can say that identifiers are case sensitive.
● Commas or blank spaces cannot be specified within an identifier.
● Keywords cannot be represented as an identifier.
● The length of the identifiers should not be more than 31 characters.
● Identifiers should be written in such a way that it is meaningful,
short, and easy to read.
Strings in C
● Strings in C are always represented as an array of characters
having null character '0' at the end of the string.
● This null character denotes the end of the string.
● Strings in C are enclosed within double quotes, while characters
are enclosed within single characters.
● The size of a string is a number of characters that the string
contains.
Now, we describe the strings in different ways:
char a[10] = "javatpoint"; // The compiler allocates the 10 bytes to the 'a'
array.
char a[] = "javatpoint"; // The compiler allocates the memory at the run
time.
char a[10] = {'j','a','v','a','t','p','o','i','n','t','0'}; // String is represented in the
form of characters.
Operators in C---
● Operators in C is a special symbol used to perform the operation.
The data items on which the operators are applied are known as
operands. Operators are applied between the operands.
Depending on the number of operands, operators are classified as
follows:
Unary Operator
A unary operator is an operator applied to the single operand. For
example: increment operator (++), decrement operator (--), sizeof,
(type)*.
Binary Operator
The binary operator is an operator applied between two operands. The
following is the list of the binary operators:
● Arithmetic Operators
● Relational Operators
● Shift Operators
● Logical Operators
● Bitwise Operators
● Conditional Operators
● Assignment Operator
● Misc Operator
Constant--
Constants in C
A constant is a value assigned to the variable which will remain the
same throughout the program, i.e., the constant value cannot be
changed.
There are two ways of declaring constant:
● Using const keyword
● Using #define pre-processor
Constant Example
Integer constant 10, 11, 34, etc.
Floating-point constant 45.6, 67.8, 11.2, etc.
Octal constant 011, 088, 022, etc.
Hexadecimal constant 0x1a, 0x4b, 0x6b, etc.
Character constant 'a', 'b', 'c', etc.
String constant "java", "c++", ".net", etc.
ASCII value---
● Character constants have integer value known as the ASCII value.
● If we want to print the ASCII value of a character we can use the
printf() function
printf("ASCII value of character 'A' is %d", 'A');
The above code will give the following output:
ASCII value of character 'A' is 65
Similarly, we can use the ASCII value to print the character.
printf("Character for the ASCII value 65 is %c", 65);
The above code will give us the following output.
Character for the ASCII value 65 is A
Variables --
● A variable is a named location to hold a data value.
int a=10;
● The value stored in a variable can change and is opposite to a
constant whose value never changes.
Example of variable: name, isGameOver, next_page, _nextgame,
stage123 , roll_no etc.
Rules for variable names
Following are the rules for naming a variable.
● First character must be a letter or underscore
● Can use letters (A-Z and a-z), digits (0-9) and underscore _
● Must not be a keyword
● Must not contain white space
NOTE:Variable names are case-sensitive meaning hello and Hello
are treated as two separate variables.
Data Types
Data Types are storage representation and tells the compiler what
type of data is stored in the variable.
C divides its data types into 3 categories.
● Primary
● Derived
● User defined
Primary Data Types---
C provides 5 primary or fundamental data types
1. character -- char ch=’8’ ;
2. integer -- int rollno=78 ;
3. floating point -- float b=1.1 ;
4. double precision floating point -- double k=123.9876
5. void.
int --
Integers are whole numbers that can have both zero, positive and
negative values but no decimal values.
For example, 0, -5, 10
We can use int for declaring an integer variable.
int id;
Here, id is a variable of type integer.
You can declare multiple variables at once in C programming. For
example,
int id, age;
The size of int is usually 4 bytes (32 bits).
float and double
float and double are used to hold real numbers.
float salary;
double price;
In C, floating-point numbers can also be represented in exponential.
For example,
float normalizationFactor = 22.442e2;
What's the difference between float and double?
The size of float (single precision float data type) is 4 bytes. And the
size of double (double precision float data type) is 8 bytes.
char
Keyword char is used for declaring character type variables. For
example,
char test = 'h';
The size of the character variable is 1 byte.
So, if we want to create a variable that will hold a character we use the
char data type.
char ch = 'a';
Similarly, if we want to store integer value we use the int data type.
int gameScore = 10;
OR
int gamescore;
gamescore=10;
short and long
If you need to use a large number, you can use a type specifier long. Here's how:
long a;
long int b;
long double c;
Here variables a and b can store integer values. And, c can store a floating-point
number.
If you are sure, only a small integer will be used, you can use short.
short d;
short int d;
signed and unsigned --
unsigned int x;
int y;
Here, the variable x can hold only zero and positive values because
we have used the unsigned modifier.
Void ---
● The void data type takes no value and is generally used with
functions to denote that the function is not going to return any
value.
● void is an incomplete type. It means "nothing" or "no type". You can
think of void as absent.
● For example, if a function is not returning anything, its return type
should be void.
● Note that, you cannot create variables of void type.
Declaring Variables:
When we declare a variable the compiler gets to know two things.
● Name of the variable
● Type of data that variable will hold
Example1: Create a variable that will hold level of a game.
int level;
Example2: Create a variable that will hold roll number of student.
int rollno;
Example3: Create a variable that will hold percentage of a student.
float percentage;
Example: Declaring multiple variables
int level;
int score;
int life;
We can merge the three variables having the same data type int in one
single line by separating them with comma.
int level, score, life;
Assign value to a variable---
We use the = assignment operator to assign value to a
variable.
Example: Create a variable to store score of a match and
assign 0 to it.
//declaring variable
int level;
//assigning value
We can merge the declaration and assignment step into one.
//declare and assign
int level = 0;
//sum=a+b;
int a,b,sum;
a=15;
b=26;
sum=a+b;
Derived Data Types--
These are the data types derived from the primary data type.
Example: Array, Function, Pointer
C operators
An operator is a symbol that operates on a value or a variable. For
example: + is an operator to perform addition.
C has a wide range of operators to perform various operations.
● Arithmetic Operators
● Relational Operators
● Logical Operators
● Bitwise Operators
● Assignment Operators
Arithmetic operators
An arithmetic operator performs mathematical operations such as
addition, subtraction, multiplication, division etc on numerical values
(constants and variables).
Operator Meaning of Operator
+ addition
- subtraction
* multiplication
/ division
% remainder after division (modulo division)
10/2 = 5
10%2 = 0
13/2 = 6
13%2 = 1
25 / 10 = 2
25 % 10 = 5
Arithmetic expression
int a = 9,b = 4, c;
c = a+b; //c=13
c = a-b; //c=5
c = a*b; //c=36
c = a/b; //c=2
c = a%b; //c=1
The operators +, - and * computes addition, subtraction, and multiplication
respectively as you might have expected.
In normal calculation, 9/4 = 2.25. However, the output is 2 in the program.
It is because both the variables a and b are integers. Hence, the output is also
an integer. The compiler neglects the term after the decimal point and shows
answer 2 instead of 2.25.
The modulo operator % computes the remainder. When a=9 is divided by b=4,
the remainder is 1. The % operator can only be used with integers.
Input and Output statements, using printf()
and scanf()
● Input means to provide the program with some data to be used in the
program and Output means to display data on screen.
● printf() and scanf() functions are inbuilt library functions in C
programming language. These functions are defined in “stdio.h”
which is a header file in C language.
● We have to include “stdio.h” file to make use of these printf() and
scanf() library functions in C language.
● scanf() function to take input from the user, and printf() function to display
output to the user.
scanf function:
● We use the scanf() function to get input data from keyboard.
Format of scanf() function.
scanf("control string", &variable1, &variable2, ...);
The control string holds the format of the data being received. variable1,
variable2, ... are the names of the variables that will hold the input value.
The & ampersand symbol is the address operator specifying the address of
the variable where the input value will be stored.
In the following example we will take two integer numbers as input from
the keyboard and then print the sum.
#include <stdio.h>
void main() {
int a, b, sum;
printf("Enter two integer numbers:n");
scanf("%d %d", &a, &b);
sum = a + b;
printf("Sum: %dn", sum);
}
Output
Enter two integer numbers:
10
20
Sum: 30
Format code for scanf function---
Format Code Description
%c Read a character
%d Read a integer
%ld Read a long integer
%hd Read a short integer
%e Read a floating point value
%f Read a floating point value
%lf Read a double number
%Lf Read a long double number
%g Read a floating point value
%h Read a short integer
%i Read a decimal, hexadecimal or octal integer
%o Read an octal integer
%s Read a string
%u Read an unsigned decimal integer
%x Read a hexadecimal integer
%[..] Read a string of word(s)
printf function--
We use the printf() function to print output.
The format of printf function to print output string.
printf("some-output-text");
In the following example we will print "Hello World" string
using the printf function.
#include <stdio.h>
void main()
{
printf("Hello World");
}
Output:
Hello World
Format of printf function to output result using format code.
printf("some-output-text format-code", variable1, variable2, ...);
In the following example we will take an integer, double and a
character value as input from the user and will output the entered
data.
#include <stdio.h>
void main(void)
{
//declare variables
char ch;
int i;
double d;
//take input from user
printf("Enter an integer, a character and a double value: ");
scanf("%d %c %lf", &i, &ch, &d);
//output
printf("Entered datan");
printf("Integer: %dn", i);
printf("Character: %cn", ch);
printf("Double: %lfn", d);
}
Output
Enter an integer, a character and a double value: 10 a 12.34
Entered data
Integer: 10
Character: a
Double: 12.340000
Format code for printf function
Format Code Description
%c Print a single character
%d Print a decimal integer
%ld Print a long integer
%hd Print a short integer
%e Print a floating point value in exponent form
%f Print a floating point value without exponent
%lf Print a double number
%Lf Print a long double number
%g Print a floating point value either e-type or f-type
depending on condition.
%i Print a signed decimal integer
%o Print an octal integer, without leading zero
%s Print a string
%x Print a hexadecimal integer, without leading 0x
WAP to add two numbers using the addition operator.
#include <stdio.h>
void main(void){
int a = 10, b = 20;
int sum = a + b;
printf("Sum: %dn", sum);
}
Output
Sum: 30
WAP to subtract two numbers using the subtraction operator.
#include <stdio.h>
void main()
{
int a = 10, b = 20;
int diff = a - b;
printf("Difference: %dn", diff);
}
Output
Difference: -10
In the following example we will divide two numbers using the
division operator.
#include <stdio.h>
void main()
{
int a = 100, b = 10;
int quot = a / b;
printf("Quotient: n%d", quot);
}
Output
Quotient:
10
In the following example we will divide two numbers and get the
remainder using the modulo operator.
#include <stdio.h>
void main(){
int a = 9, b = 4;
int rem = a % b;
printf("Remainder: %dn", rem);
}
Output
Remainder: 1
Write a program in C to multiple two integer values and print the result
To multiple we use the * multiplication operator.
// program to find the product of two numbers
#include <stdio.h>
int main(void){
int a = 10, b = 20;
int product = a * b;
printf("Product of axb = %dn", product);
return 0;
// program to find the product of two numbers
#include <stdio.h>
void main(){
int a = 10, b = 20;
int product = a * b;
printf("Product of axb = %dn", product);
}
Output
Product of axb = 200
Write a program in C to find the area of a rectangle
Formula to find the area of a rectangle is length * width.
// program to find the area of rectangle
#include <stdio.h>
void main()
{
float length = 12.34, width = 56.78;
float area = length * width;
printf("Area: %fn", area);
}
Output
Area: 700.665222
Write a program in C to find the speed of a train if it covers 300 km in 2
hours
Formula to find speed is speed = distance/time.
To divide numbers we use the / division operator
#include <stdio.h>
void main()
{
float distance = 300, time = 2;
float speed = distance / time;
printf("Speed of train= %fn", speed);
}
Output
Speed of train= 150.000000
Write a program in C to convert 100 degree Celsius into Fahrenheit
Formula to convert temperature from Celsius to Fahrenheit is given
below.
F = C * 1.8 + 32
#include <stdio.h>
void main()
{
float temp_c = 100;
float temp_f = temp_c * 1.8 + 32;
printf("%f deg C = %f deg Fn", temp_c, temp_f);
}
Output
100.000000 deg C = 212.000000 deg F
Write a C program to convert Fahrenheit to Celsius
#include <stdio.h>
void main()
{
float fh,cl;
printf("n Enter temperature in Fahrenheit: ");
scanf("%f",&fh);
cl= (fh - 32) / 1.8;
printf("Temperature in Celsius: %.2f",cl);
}
Enter temperature in Fahrenheit: 98.6
Temperature in Celsius: 37.00
C Program to convert kilometers into miles and meters
#include<stdio.h>
void main()
{
double kilo_meter,meter,miles;
printf("Enter Kilo Meter:");
scanf("%lf",&kilo_meter);
miles=kilo_meter/1.609;
meter=kilo_meter*1000;
printf("Kilo Meter to Miles: %0.2lf",miles);
printf("nKilo Meter to Meter: %0.2lf",meter);
}
Output:
Enter Kilo Meter:95
Kilo Meter to Miles: 59.04
Kilo Meter to Meter: 95000.00
C program converts temperature given in Celsius to Kelvin.
#include<stdio.h>
int main()
{
float celsius, kelvin;
clrscr();
printf("Enter temperature in celsius: ");
scanf("%f", &celsius);
kelvin = 273.15 + celsius;
printf("%0.2f Celsius = %0.2f Kelvin",celsius,
kelvin);
getch();
return(0);
}
Output of the above program:
Enter temperature in celsius: 47
47.00 Celsius = 320.15 Kelvin
write a c program to find square of any
number using mathematical functions
pow() function:
● pow( ) function in C is used to find the power of the given number.
● ”math.h” header file supports pow( ) function in C language.
Syntax for pow( ) function in C is given below.
double pow (double base, double exponent);
#include <stdio.h>
#include <math.h>
int main()
{
printf ("2 power 4 = %fn", pow (2.0, 4.0) );
printf ("5 power 3 = %fn", pow (5, 3) );
return 0;
}
OUTPUT--
2 power 4 = 16.000000
5 power 3 = 125.000000
write a c program to swap two numbers
without third variable---Program 1: Using + and -
#include<stdio.h>
int main()
{
int a=10, b=20;
printf("Before swap a=%d b=%d",a,b);
a=a+b;//a=30 (10+20)
b=a-b;//b=10 (30-20)
a=a-b;//a=20 (30-10)
printf("nAfter swap a=%d b=%d",a,b);
return 0;
}
OUTPUT--
Before swap a=10 b=20
After swap a=20 b=10
Program 2: Using * and /
#include<stdio.h>
int main()
{
int a=10, b=20;
printf("Before swap a=%d b=%d",a,b);
a=a*b;//a=200 (10*20)
b=a/b;//b=10 (200/20)
a=a/b;//a=20 (200/10)
printf("nAfter swap a=%d b=%d",a,b);
return 0;
}
OUTPUT--
Before swap a=10 b=20
After swap a=20 b=10
Copy of UNIT 2 -- Basics Of Programming.pptx
Copy of UNIT 2 -- Basics Of Programming.pptx
Copy of UNIT 2 -- Basics Of Programming.pptx
Copy of UNIT 2 -- Basics Of Programming.pptx

More Related Content

PPTX
Introduction of c programming unit-ii ppt
PPTX
UNIT 5 C PROGRAMMING, PROGRAM STRUCTURE
PPT
490450755-Chapter-2.ppt
PPT
490450755-Chapter-2.ppt
PPTX
Unit-1 (introduction to c language).pptx
DOC
PDF
Unit 2 introduction to c programming
PPTX
C language updated
Introduction of c programming unit-ii ppt
UNIT 5 C PROGRAMMING, PROGRAM STRUCTURE
490450755-Chapter-2.ppt
490450755-Chapter-2.ppt
Unit-1 (introduction to c language).pptx
Unit 2 introduction to c programming
C language updated

Similar to Copy of UNIT 2 -- Basics Of Programming.pptx (20)

PDF
C programming.pdf
PPT
Chapter-2 edited on Programming in Can refer this ppt
DOCX
UNIT 1 NOTES.docx
PPTX
c_pro_introduction.pptx
PPTX
c-introduction.pptx
DOCX
Chapter 3(1)
PPT
C Lang notes.ppt
DOCX
C and DS -unit 1 -Artificial Intelligence and ML.docx
PPTX
2.Overview of C language.pptx
PDF
Learn c language Important topics ( Easy & Logical, & smart way of learning)
PDF
Programming in c
PPTX
C lang7age programming powerpoint presentation
PPTX
Unit-2_Getting Started With ‘C’ Language (3).pptx
PPTX
structure of a c program - slideshare.pptx
PPTX
C programming slide day 01 uploadd by md abdullah al shakil
DOC
Basic c
PPT
8844632.ppt
PPTX
C programming
DOCX
C notes
PPTX
C language
C programming.pdf
Chapter-2 edited on Programming in Can refer this ppt
UNIT 1 NOTES.docx
c_pro_introduction.pptx
c-introduction.pptx
Chapter 3(1)
C Lang notes.ppt
C and DS -unit 1 -Artificial Intelligence and ML.docx
2.Overview of C language.pptx
Learn c language Important topics ( Easy & Logical, & smart way of learning)
Programming in c
C lang7age programming powerpoint presentation
Unit-2_Getting Started With ‘C’ Language (3).pptx
structure of a c program - slideshare.pptx
C programming slide day 01 uploadd by md abdullah al shakil
Basic c
8844632.ppt
C programming
C notes
C language
Ad

Recently uploaded (20)

PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Classroom Observation Tools for Teachers
PPTX
Lesson notes of climatology university.
PPTX
Cell Types and Its function , kingdom of life
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Sports Quiz easy sports quiz sports quiz
PDF
Insiders guide to clinical Medicine.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Pharma ospi slides which help in ospi learning
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
O7-L3 Supply Chain Operations - ICLT Program
Microbial disease of the cardiovascular and lymphatic systems
O5-L3 Freight Transport Ops (International) V1.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Classroom Observation Tools for Teachers
Lesson notes of climatology university.
Cell Types and Its function , kingdom of life
Module 4: Burden of Disease Tutorial Slides S2 2025
Supply Chain Operations Speaking Notes -ICLT Program
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Sports Quiz easy sports quiz sports quiz
Insiders guide to clinical Medicine.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
Pharma ospi slides which help in ospi learning
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Complications of Minimal Access Surgery at WLH
Anesthesia in Laparoscopic Surgery in India
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
O7-L3 Supply Chain Operations - ICLT Program
Ad

Copy of UNIT 2 -- Basics Of Programming.pptx

  • 2. Introduction to C ● C is a procedural programming language. ● It was initially developed by Dennis Ritchie in the year 1972. ● It was mainly developed as a system programming language to write an operating system. ● The main features of C language include low-level access to memory, a simple set of keywords, and clean style, these features make C language suitable for system programmings like an operating system or compiler development.
  • 3. ● Many later languages have borrowed syntax/features directly or indirectly from C language. ● Like syntax of Java, PHP, JavaScript, and many other languages are mainly based on C language. ● C++ is nearly a superset of C language
  • 4. History of ‘C’ ➢ C programming language was developed in 1972 by Dennis Ritchie at bell laboratories of AT&T (American Telephone & Telegraph), located in the U.S.A. ➢ Dennis Ritchie is known as the founder of the c language. ➢ It was developed to overcome the problems of previous languages such as B, BCPL, etc. ➢ Initially, C language was developed to be used in UNIX operating system. It inherits many features of previous languages such as B and BCPL.
  • 5. General Structure of a ‘C’ program A C program is divided into different sections. There are six main sections to a basic c program. The six sections are, ● Documentation ● Link ● Definition ● Global Declarations ● Main functions ● Subprograms
  • 7. Write a program to print area of a circle. In the following example we will find the area of a circle for a given radius 10cm. Formula The formula to compute the area of a circle is πr2 where π is PI = 3.1416 (approx.) and r is the radius of the circle. Lets write the C code to compute the area of the circle.
  • 8. /* description: program to find the area of a circle using the radius r */ #include <stdio.h> #define PI 3.1416
  • 9. void main( ) { float r = 10, area; //A=3.14*r*r area=PI * r * r; printf("Area: %.2f", area ); }
  • 10. The above code will give the following output. Area: 314.16
  • 11. Different sections of the above code: Documentation Section-- ➢ This section consists of comment lines which include the name of programmer, the author and other details like time and date of writing the program. ➢ Documentation section helps anyone to get an overview of the program.
  • 12. Example /* file: circle.c author: yusuf shakeel date: 2010-11-25 description: program to find the area of a circle using the radius r */
  • 13. ● This section contains a multi line comment describing the code. ● In C, we can create single line comment using two forward slash // and we can create multi line comment using /* */. ● Comments are ignored by the compiler and is used to write notes and document code.
  • 14. Link Section-- ● The link section consists of the header files of the functions that are used in the program. ● It provides instructions to the compiler to link functions from the system library. Example #include<stdio.h> #include<math.h>
  • 15. ● This section includes header file. ● We are including the stdio.h input/output header file from the C library.
  • 16. Definition Section-- All the symbolic constants are written in definition section. #define PI=3.14
  • 17. ● This section contains constant. ● In the above code we have created a constant PI and assigned 3.1416 to it. ● The #define is a preprocessor compiler directive which is used to create constants. We generally use uppercase letters to create constants. ● The #define is not a statement and must not end with a ; semicolon.
  • 18. Global Declaration Section--- ● The global variables that can be used anywhere in the program are declared in global declaration section. ● This section also declares the user defined functions. float area(float r);
  • 19. ● This section contains function declaration. ● We have declared an area function which takes a floating number (i.e., number with decimal parts) as argument and returns floating number.
  • 20. Main Function Section: ➢ It is necessary have one main() function section in every C program. ➢ This section contains two parts, declaration and executable part. ➢ The declaration part declares all the variables that are used in executable part. ➢ These two parts must be written in between the opening and closing braces. ➢ Each statement in the declaration and executable part must end with a semicolon (;). ➢ The execution of program starts at opening braces and ends at closing braces.
  • 21. Example-- void main(void) { float r = 10; area=3.14*r*r; printf("Area: %.2f", area(r)); }
  • 22. ● This is the main() function of the code. ● Inside this function we have created a floating variable r and assigned 10 to it. ● Then we have called the printf() function. The first argument contains "Area: %.2f" which means we will print floating number having only 2 decimal place. In the second argument we are calling the area() function and passing the value of r to it.
  • 23. Sub Program Section-- ➢ The subprogram section contains all the user defined functions that are used to perform a specific task. ➢ These user defined functions are called in the main() function. Example-- float area(float r) { return PI * r * r; }
  • 24. ● This section contains a subprogram, an area() function that is called from the main() function. ● This is the definition of the area() function. It receives the value of radius in variable r and then returns the area of the circle using the following formula PI * r * r.
  • 25. Header files ● C language has numerous libraries that include predefined functions to make programming easier. ● In C language, header files contain the set of predefined standard library functions. ● Your request to use a header file in your program by including it with the C preprocessing directive “#include”. ● All the header file have a ‘.h’ an extension. ● By including a header file, we can use its contents in our program.
  • 27. ● It offers the above features by importing them into the program with the help of a preprocessor directive “#include”. ● These preprocessor directives are used for instructing compiler that these files need to be processed before compilation. ● In C program should necessarily contain the header file which stands for standard input and output used to take input with the help of scanf() and printf() function respectively.
  • 28. Standard Header Files And Their Uses: 1. #include<stdio.h>: It is used to perform input and output operations using functions scanf() and printf(). 2. #include<iostream.h>: It is used as a stream of Input and Output using cin and cout. 3. #include<string.h>: It is used to perform various functionalities related to string manupulation like strlen(), strcmp(), strcpy(), size(), etc. 4. #include<math.h>: It is used to perform mathematical operations like sqrt(), log2(), pow(), etc.
  • 29. Character set ➢ As every language contains a set of characters used to construct words, statements, etc., ➢ C language also has a set of characters which include alphabets, digits, and special symbols. ➢ C language supports a total of 256 characters. ➢ Every C program contains statements. These statements are constructed using words and these words are constructed using characters from C character set.
  • 30. C language character set contains the following set of characters... 1. Alphabets 2. Digits 3. Special Characters 4. White Spaces / Escape Sequence
  • 31. ALPHABETS Uppercase letters A-Z Lowercase letters a-z DIGITS 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
  • 32. SPECIAL CHARACTERS ~ tilde % percent sign | vertical bar @ at symbol + plus sign < less than _ underscore - minus sign > greater than ^ caret # number sign = equal to
  • 33. & ampersand $ dollar sign / slash ( left parenthesis * asterisk back slash ) right parenthesis ′ apostrophe : colon [ left bracket " quotation mark ;
  • 34. ] right bracket ! exclamation mark , comma { left flower brace ? Question mark . dot operator } right flower brace
  • 35. WHITESPACE CHARACTERS / Escape Sequence-- b blank space t horizontal tab v vertical tab r carriage return f form feed n new line Back slash ’ Single quote " Double quote ? Question mark 0 Null a Alarm (bell)
  • 36. Tokens ➢ Tokens in C is the most important element to be used in creating a program in C. ➢ We can define the token as the smallest individual element in C. For `example, we cannot create a sentence without using words; similarly, we cannot create a program in C without using tokens in C. ➢ Therefore, we can say that tokens in C is the building block or the basic component for creating a program in C language.
  • 37. Classification of tokens in C Tokens in C language can be divided into the following categories:
  • 38. keywords ● Keywords in C can be defined as the pre-defined or the reserved words having its own importance, and each keyword has its own functionality. ● Since keywords are the pre-defined words used by the compiler, so they cannot be used as the variable names. ● If the keywords are used as the variable names, it means that we are assigning a different meaning to the keyword, which is not allowed. ● C language supports 32 keywords given below:
  • 39. auto double int struct break else long switch case enum register typedef char extern return union const float short unsigned continue for signed void default goto sizeof volatile do if static while
  • 40. Identifiers ● Identifiers in C are used for naming variables, functions, arrays, structures, etc. ● Identifiers in C are the user-defined words. ● It can be composed of uppercase letters, lowercase letters, underscore, or digits, but the starting letter should be either an underscore or an alphabet. ● Identifiers cannot be used as keywords. Rules for constructing identifiers in C are given below:
  • 41. ● The first character of an identifier should be either an alphabet or an underscore, and then it can be followed by any of the character, digit, or underscore. ● It should not begin with any numerical digit. ● In identifiers, both uppercase and lowercase letters are distinct. Therefore, we can say that identifiers are case sensitive. ● Commas or blank spaces cannot be specified within an identifier. ● Keywords cannot be represented as an identifier. ● The length of the identifiers should not be more than 31 characters. ● Identifiers should be written in such a way that it is meaningful, short, and easy to read.
  • 42. Strings in C ● Strings in C are always represented as an array of characters having null character '0' at the end of the string. ● This null character denotes the end of the string. ● Strings in C are enclosed within double quotes, while characters are enclosed within single characters. ● The size of a string is a number of characters that the string contains.
  • 43. Now, we describe the strings in different ways: char a[10] = "javatpoint"; // The compiler allocates the 10 bytes to the 'a' array. char a[] = "javatpoint"; // The compiler allocates the memory at the run time. char a[10] = {'j','a','v','a','t','p','o','i','n','t','0'}; // String is represented in the form of characters.
  • 44. Operators in C--- ● Operators in C is a special symbol used to perform the operation. The data items on which the operators are applied are known as operands. Operators are applied between the operands. Depending on the number of operands, operators are classified as follows: Unary Operator A unary operator is an operator applied to the single operand. For example: increment operator (++), decrement operator (--), sizeof, (type)*.
  • 45. Binary Operator The binary operator is an operator applied between two operands. The following is the list of the binary operators: ● Arithmetic Operators ● Relational Operators ● Shift Operators ● Logical Operators ● Bitwise Operators ● Conditional Operators ● Assignment Operator ● Misc Operator
  • 46. Constant-- Constants in C A constant is a value assigned to the variable which will remain the same throughout the program, i.e., the constant value cannot be changed. There are two ways of declaring constant: ● Using const keyword ● Using #define pre-processor
  • 47. Constant Example Integer constant 10, 11, 34, etc. Floating-point constant 45.6, 67.8, 11.2, etc. Octal constant 011, 088, 022, etc. Hexadecimal constant 0x1a, 0x4b, 0x6b, etc. Character constant 'a', 'b', 'c', etc. String constant "java", "c++", ".net", etc.
  • 48. ASCII value--- ● Character constants have integer value known as the ASCII value. ● If we want to print the ASCII value of a character we can use the printf() function printf("ASCII value of character 'A' is %d", 'A'); The above code will give the following output: ASCII value of character 'A' is 65
  • 49. Similarly, we can use the ASCII value to print the character. printf("Character for the ASCII value 65 is %c", 65); The above code will give us the following output. Character for the ASCII value 65 is A
  • 50. Variables -- ● A variable is a named location to hold a data value. int a=10; ● The value stored in a variable can change and is opposite to a constant whose value never changes. Example of variable: name, isGameOver, next_page, _nextgame, stage123 , roll_no etc.
  • 51. Rules for variable names Following are the rules for naming a variable. ● First character must be a letter or underscore ● Can use letters (A-Z and a-z), digits (0-9) and underscore _ ● Must not be a keyword ● Must not contain white space NOTE:Variable names are case-sensitive meaning hello and Hello are treated as two separate variables.
  • 52. Data Types Data Types are storage representation and tells the compiler what type of data is stored in the variable. C divides its data types into 3 categories. ● Primary ● Derived ● User defined
  • 53. Primary Data Types--- C provides 5 primary or fundamental data types 1. character -- char ch=’8’ ; 2. integer -- int rollno=78 ; 3. floating point -- float b=1.1 ; 4. double precision floating point -- double k=123.9876 5. void.
  • 54. int -- Integers are whole numbers that can have both zero, positive and negative values but no decimal values. For example, 0, -5, 10 We can use int for declaring an integer variable. int id;
  • 55. Here, id is a variable of type integer. You can declare multiple variables at once in C programming. For example, int id, age; The size of int is usually 4 bytes (32 bits).
  • 56. float and double float and double are used to hold real numbers. float salary; double price; In C, floating-point numbers can also be represented in exponential. For example, float normalizationFactor = 22.442e2;
  • 57. What's the difference between float and double? The size of float (single precision float data type) is 4 bytes. And the size of double (double precision float data type) is 8 bytes.
  • 58. char Keyword char is used for declaring character type variables. For example, char test = 'h'; The size of the character variable is 1 byte.
  • 59. So, if we want to create a variable that will hold a character we use the char data type. char ch = 'a'; Similarly, if we want to store integer value we use the int data type. int gameScore = 10; OR int gamescore; gamescore=10;
  • 60. short and long If you need to use a large number, you can use a type specifier long. Here's how: long a; long int b; long double c; Here variables a and b can store integer values. And, c can store a floating-point number.
  • 61. If you are sure, only a small integer will be used, you can use short. short d; short int d;
  • 62. signed and unsigned -- unsigned int x; int y; Here, the variable x can hold only zero and positive values because we have used the unsigned modifier.
  • 63. Void --- ● The void data type takes no value and is generally used with functions to denote that the function is not going to return any value. ● void is an incomplete type. It means "nothing" or "no type". You can think of void as absent. ● For example, if a function is not returning anything, its return type should be void. ● Note that, you cannot create variables of void type.
  • 64. Declaring Variables: When we declare a variable the compiler gets to know two things. ● Name of the variable ● Type of data that variable will hold Example1: Create a variable that will hold level of a game. int level;
  • 65. Example2: Create a variable that will hold roll number of student. int rollno; Example3: Create a variable that will hold percentage of a student. float percentage;
  • 66. Example: Declaring multiple variables int level; int score; int life; We can merge the three variables having the same data type int in one single line by separating them with comma. int level, score, life;
  • 67. Assign value to a variable--- We use the = assignment operator to assign value to a variable. Example: Create a variable to store score of a match and assign 0 to it. //declaring variable int level; //assigning value
  • 68. We can merge the declaration and assignment step into one. //declare and assign int level = 0;
  • 70. Derived Data Types-- These are the data types derived from the primary data type. Example: Array, Function, Pointer
  • 71. C operators An operator is a symbol that operates on a value or a variable. For example: + is an operator to perform addition. C has a wide range of operators to perform various operations. ● Arithmetic Operators ● Relational Operators ● Logical Operators ● Bitwise Operators ● Assignment Operators
  • 72. Arithmetic operators An arithmetic operator performs mathematical operations such as addition, subtraction, multiplication, division etc on numerical values (constants and variables).
  • 73. Operator Meaning of Operator + addition - subtraction * multiplication / division % remainder after division (modulo division)
  • 74. 10/2 = 5 10%2 = 0 13/2 = 6 13%2 = 1 25 / 10 = 2 25 % 10 = 5
  • 75. Arithmetic expression int a = 9,b = 4, c; c = a+b; //c=13 c = a-b; //c=5 c = a*b; //c=36 c = a/b; //c=2 c = a%b; //c=1
  • 76. The operators +, - and * computes addition, subtraction, and multiplication respectively as you might have expected. In normal calculation, 9/4 = 2.25. However, the output is 2 in the program. It is because both the variables a and b are integers. Hence, the output is also an integer. The compiler neglects the term after the decimal point and shows answer 2 instead of 2.25. The modulo operator % computes the remainder. When a=9 is divided by b=4, the remainder is 1. The % operator can only be used with integers.
  • 77. Input and Output statements, using printf() and scanf() ● Input means to provide the program with some data to be used in the program and Output means to display data on screen. ● printf() and scanf() functions are inbuilt library functions in C programming language. These functions are defined in “stdio.h” which is a header file in C language. ● We have to include “stdio.h” file to make use of these printf() and scanf() library functions in C language.
  • 78. ● scanf() function to take input from the user, and printf() function to display output to the user.
  • 79. scanf function: ● We use the scanf() function to get input data from keyboard. Format of scanf() function. scanf("control string", &variable1, &variable2, ...); The control string holds the format of the data being received. variable1, variable2, ... are the names of the variables that will hold the input value. The & ampersand symbol is the address operator specifying the address of the variable where the input value will be stored.
  • 80. In the following example we will take two integer numbers as input from the keyboard and then print the sum. #include <stdio.h> void main() { int a, b, sum; printf("Enter two integer numbers:n"); scanf("%d %d", &a, &b); sum = a + b; printf("Sum: %dn", sum); }
  • 81. Output Enter two integer numbers: 10 20 Sum: 30
  • 82. Format code for scanf function--- Format Code Description %c Read a character %d Read a integer %ld Read a long integer %hd Read a short integer %e Read a floating point value %f Read a floating point value
  • 83. %lf Read a double number %Lf Read a long double number %g Read a floating point value %h Read a short integer %i Read a decimal, hexadecimal or octal integer %o Read an octal integer %s Read a string %u Read an unsigned decimal integer %x Read a hexadecimal integer %[..] Read a string of word(s)
  • 84. printf function-- We use the printf() function to print output. The format of printf function to print output string. printf("some-output-text");
  • 85. In the following example we will print "Hello World" string using the printf function. #include <stdio.h> void main() { printf("Hello World"); }
  • 86. Output: Hello World Format of printf function to output result using format code. printf("some-output-text format-code", variable1, variable2, ...);
  • 87. In the following example we will take an integer, double and a character value as input from the user and will output the entered data. #include <stdio.h> void main(void) { //declare variables char ch; int i; double d;
  • 88. //take input from user printf("Enter an integer, a character and a double value: "); scanf("%d %c %lf", &i, &ch, &d); //output printf("Entered datan"); printf("Integer: %dn", i); printf("Character: %cn", ch); printf("Double: %lfn", d); }
  • 89. Output Enter an integer, a character and a double value: 10 a 12.34 Entered data Integer: 10 Character: a Double: 12.340000
  • 90. Format code for printf function Format Code Description %c Print a single character %d Print a decimal integer %ld Print a long integer %hd Print a short integer %e Print a floating point value in exponent form %f Print a floating point value without exponent
  • 91. %lf Print a double number %Lf Print a long double number %g Print a floating point value either e-type or f-type depending on condition. %i Print a signed decimal integer %o Print an octal integer, without leading zero %s Print a string %x Print a hexadecimal integer, without leading 0x
  • 92. WAP to add two numbers using the addition operator. #include <stdio.h> void main(void){ int a = 10, b = 20; int sum = a + b; printf("Sum: %dn", sum); } Output Sum: 30
  • 93. WAP to subtract two numbers using the subtraction operator. #include <stdio.h> void main() { int a = 10, b = 20; int diff = a - b; printf("Difference: %dn", diff); } Output Difference: -10
  • 94. In the following example we will divide two numbers using the division operator. #include <stdio.h> void main() { int a = 100, b = 10; int quot = a / b; printf("Quotient: n%d", quot); } Output Quotient: 10
  • 95. In the following example we will divide two numbers and get the remainder using the modulo operator. #include <stdio.h> void main(){ int a = 9, b = 4; int rem = a % b; printf("Remainder: %dn", rem); } Output Remainder: 1
  • 96. Write a program in C to multiple two integer values and print the result To multiple we use the * multiplication operator. // program to find the product of two numbers #include <stdio.h> int main(void){ int a = 10, b = 20; int product = a * b; printf("Product of axb = %dn", product); return 0;
  • 97. // program to find the product of two numbers #include <stdio.h> void main(){ int a = 10, b = 20; int product = a * b; printf("Product of axb = %dn", product); }
  • 99. Write a program in C to find the area of a rectangle Formula to find the area of a rectangle is length * width. // program to find the area of rectangle #include <stdio.h> void main() { float length = 12.34, width = 56.78; float area = length * width; printf("Area: %fn", area); }
  • 101. Write a program in C to find the speed of a train if it covers 300 km in 2 hours Formula to find speed is speed = distance/time. To divide numbers we use the / division operator #include <stdio.h> void main() { float distance = 300, time = 2; float speed = distance / time;
  • 102. printf("Speed of train= %fn", speed); } Output Speed of train= 150.000000
  • 103. Write a program in C to convert 100 degree Celsius into Fahrenheit Formula to convert temperature from Celsius to Fahrenheit is given below. F = C * 1.8 + 32 #include <stdio.h> void main() { float temp_c = 100;
  • 104. float temp_f = temp_c * 1.8 + 32; printf("%f deg C = %f deg Fn", temp_c, temp_f); } Output 100.000000 deg C = 212.000000 deg F
  • 105. Write a C program to convert Fahrenheit to Celsius #include <stdio.h> void main() { float fh,cl; printf("n Enter temperature in Fahrenheit: "); scanf("%f",&fh); cl= (fh - 32) / 1.8; printf("Temperature in Celsius: %.2f",cl); }
  • 106. Enter temperature in Fahrenheit: 98.6 Temperature in Celsius: 37.00
  • 107. C Program to convert kilometers into miles and meters #include<stdio.h> void main() { double kilo_meter,meter,miles; printf("Enter Kilo Meter:"); scanf("%lf",&kilo_meter); miles=kilo_meter/1.609; meter=kilo_meter*1000;
  • 108. printf("Kilo Meter to Miles: %0.2lf",miles); printf("nKilo Meter to Meter: %0.2lf",meter); } Output: Enter Kilo Meter:95 Kilo Meter to Miles: 59.04 Kilo Meter to Meter: 95000.00
  • 109. C program converts temperature given in Celsius to Kelvin. #include<stdio.h> int main() { float celsius, kelvin; clrscr(); printf("Enter temperature in celsius: "); scanf("%f", &celsius);
  • 110. kelvin = 273.15 + celsius; printf("%0.2f Celsius = %0.2f Kelvin",celsius, kelvin); getch(); return(0); }
  • 111. Output of the above program: Enter temperature in celsius: 47 47.00 Celsius = 320.15 Kelvin
  • 112. write a c program to find square of any number using mathematical functions pow() function: ● pow( ) function in C is used to find the power of the given number. ● ”math.h” header file supports pow( ) function in C language. Syntax for pow( ) function in C is given below. double pow (double base, double exponent);
  • 113. #include <stdio.h> #include <math.h> int main() { printf ("2 power 4 = %fn", pow (2.0, 4.0) ); printf ("5 power 3 = %fn", pow (5, 3) ); return 0; } OUTPUT-- 2 power 4 = 16.000000 5 power 3 = 125.000000
  • 114. write a c program to swap two numbers without third variable---Program 1: Using + and - #include<stdio.h> int main() { int a=10, b=20; printf("Before swap a=%d b=%d",a,b);
  • 115. a=a+b;//a=30 (10+20) b=a-b;//b=10 (30-20) a=a-b;//a=20 (30-10) printf("nAfter swap a=%d b=%d",a,b); return 0; } OUTPUT-- Before swap a=10 b=20 After swap a=20 b=10
  • 116. Program 2: Using * and / #include<stdio.h> int main() { int a=10, b=20; printf("Before swap a=%d b=%d",a,b); a=a*b;//a=200 (10*20) b=a/b;//b=10 (200/20) a=a/b;//a=20 (200/10)
  • 117. printf("nAfter swap a=%d b=%d",a,b); return 0; } OUTPUT-- Before swap a=10 b=20 After swap a=20 b=10