Programming in C language
Computer Science - B.Sc. A&OTT,
Semester 1
Prepared by: Ms. Mandeep Kaur
Assistant Professor
Computer Applications
Data Types
 Each variable in C has an associated data type.
 It specifies the type of data that the variable can store like integer,
character, floating, double, etc.
 Each data type requires different amounts of memory and has
some specific operations which can be performed over it.
Data Types
Primary Data Types
 Primary data types are also known as the fundamental data types because
they are pre-defined or they already exist in the C language.
 All the other types of data types (derived and user-defined data types) are
derived from these data types.
 Primary data types in C are of 4 types:
 int
 char
 float
 double.
Integer Data Type
 The integer datatype in C is used to store the integer numbers (any number
including positive, negative and zero without decimal part).
 Octal values, hexadecimal values, and decimal values can be stored in int
data type in C.
 Range: -2,147,483,648 to 2,147,483,647
 Size: 4 bytes
 Format Specifier: %d
 Syntax of Integer
 int var_name;
C program to print Integer data types.
#include <stdio.h>
void main()
{
int a = 9;
printf(" %d Integer value with positive data: n", a);
getch();
}
Output
Integer value with positive data: 9
Character Data Type
 Character data type allows its variable to store only a single character.
 The size of the character is 1 byte. It is the most basic data type in C.
 It stores a single character and requires a single byte of memory in almost
all compilers.
 Range: (-128 to 127) or (0 to 255)
 Size: 1 byte
 Format Specifier: %c
 Syntax of char
 char var_name;
C program to print Character data types.
#include <stdio.h>
int main()
{
char a = 'a’;
printf(" %cValue of a: n", a);
return 0;
}
Output
Value of a: a
Float Data Type
 In C programming float data type is used to store floating-point values.
 Float in C is used to store decimal and exponential values.
 The float data type is basically a precision sort of data type that is capable
of holding 32 bits of decimal numbers or floating points.
 It is used to store decimal numbers (numbers with floating point values)
with single precision.
 Range: 1.2E-38
to 3.4E+38
 Size: 4 bytes
 Format Specifier: %f
 Syntax of float
 float var_name;
C program to print Floating point data types.
#include <stdio.h>
int main()
{
float a = 9.0f;
float b = 2.5f;
printf("%fn", a);
printf("%fn", b);
return 0;
}
Output
9.000000
2.500000
Double Data Type
 A Double data type in C is used to store decimal numbers (numbers with
floating point values) with double precision.
 The double data type is basically a precision sort of data type that is capable
of holding 64 bits of decimal numbers or floating points.
 It can easily accommodate about 16 to 17 digits after or before a decimal
point.
 Range: 1.7E-308
to 1.7E+308
 Size: 8 bytes
 Format Specifier: %lf
 Syntax of Double
 double var_name;
C program to print Double data types.
#include <stdio.h>
int main()
{
double a = 123123123.00;
double b = 12.293123;
printf("%lfn", a);
printf("%lfn", b);
return 0;
}
Output
123123123.000000
12.293123
Void Data Type
 The void data type in C is used to specify that no value is present.
 It does not provide a result value to its caller.
 It has no values and no operations.
 It is used to represent nothing.
 Void is used in multiple ways as function return type, function arguments
as void, and pointers to void.
Size of Data Types in C
 The size of the data types in C is dependent on the size of the
architecture, so we cannot define the universal size of the data types.
 For that, the C language provides the sizeof() operator to check the size
of the data types.
C Program to print size of different data type in C
#include <stdio.h>
int main()
{
int size_of_int = sizeof(int);
int size_of_char = sizeof(char);
int size_of_float = sizeof(float);
int size_of_double = sizeof(double);
printf(" %d The size of int data type : n", size_of_int);
printf(" %d The size of char data type : n",size_of_char);
printf(" %d The size of float data type : n",size_of_float);
printf(" %d The size of double data type : ",size_of_double);
return 0;
}
C Program to print size of different data type in C
Output
The size of int data type : 4
The size of char data type : 1
The size of float data type : 4
The size of double data type : 8
Derived Data Types
 In C, the data types derived from the primitive or built-in data types are
called Derived Data Types.
 In other words, the derived data types are those data types that are created
by combining primitive data types and other derived data types.
 There are three derived data types available in C. They are as follows:
 Function
 Array
 Pointer
Functions
 A function is a block of code which only runs when it is called.
 You can pass data, known as parameters, into a function.
 Functions are used to perform certain actions, and they are important for
reusing code: Define the code once, and use it many times.
 In C, a function can be called by types: call by value and call by reference.
Syntax of Functions in C
 The syntax of function can be divided into 3 aspects:
 Function Declaration
 Function Definition
 Function Calls
Function Declarations
 In a function declaration, we must provide the function name, its return
type, and the number and type of its parameters.
 A function declaration tells the compiler that there is a function with the
given name defined somewhere else in the program.
 Syntax
 return_type name_of_the_function (parameter_1, parameter_2);
Function Declarations
Function Definition
 The function definition consists of actual statements which are executed
when the function is called (i.e. when the program control comes to the
function).
Function Call
 A function call is a statement that instructs the compiler to execute the
function. We use the function name and parameters in the function call.
C program to print add two numbers using function.
#include <stdio.h>
int sum(int a, int b)
{
return (a+b);
}
int main()
{
int num1=10;
int num2=10;
int num3;
num3 = sum(num1, num2);
printf("%d Sum of the entered numbers: ", num3);
return 0;
}
Output:
Sum of the entered numbers: 20
Conditions of Return Types and Arguments
In C programming language, functions can be called either with or without
arguments and might return values. They may or might not return values to the
calling functions.
 Function with no arguments and no return value
 Function with no arguments and with return value
 Function with argument and with no return value
 Function with arguments and with return value
Function with arguments and return value
Syntax:
Function declaration : int function ( int );
Function call : function( x );
Function definition:
int function( int x )
{
statements;
return x;
}
Function with arguments but no return value
Syntax:
Function declaration : void function ( int );
Function call : function( x );
Function definition:
void function( int x )
{
statements;
}
Function with no argument and no return value
Syntax:
Function declaration : void function();
Function call : function();
Function definition :
void function()
{
statements;
}
Function with no arguments but returns a value
Syntax:
Function declaration : int function();
Function call : function();
Function definition :
int function()
{
statements;
return x;
}
Passing Parameters to Functions
 The data passed when the function
is being invoked is known as the
Actual parameters.
 Formal Parameters are the variable
and the data type as mentioned in
the function declaration.
 We can pass arguments to the C
function in two ways:
 Pass by Value
 Pass by Reference
Pass by Value
 Parameter passing in this method copies values from actual parameters into
formal function parameters.
 As a result, any changes made inside the functions do not reflect in the
caller’s parameters.
#include <stdio.h>
void swap(int var1, int var2)
{
int temp = var1;
var1 = var2;
var2 = temp;
}
Pass by Value
// Driver code
int main()
{
int var1 = 3, var2 = 2;
printf("Before swap Value of var1 and var2 is: %d, %dn",var1, var2);
swap(var1, var2);
printf("After swap Value of var1 and var2 is: %d, %d",var1, var2);
return 0;
}
Output
Before swap Value of var1 and var2 is: 3,
2
After swap Value of var1 and var2 is: 3, 2
Pass by Reference
 The caller’s actual parameters and the function’s actual parameters refer to
the same locations, so any changes made inside the function are reflected in
the caller’s actual parameters.
#include <stdio.h>
void swap(int *var1, int *var2)
{
int temp = *var1;
*var1 = *var2;
*var2 = temp;
}
Pass by Reference
// Driver code
int main()
{
int var1 = 3, var2 = 2;
printf("Before swap Value of var1 and var2 is: %d, %dn",
var1, var2);
swap(&var1, &var2);
printf("After swap Value of var1 and var2 is: %d, %d",
var1, var2);
return 0;
}
Output
Before swap Value of var1 and var2 is: 3,
2
After swap Value of var1 and var2 is: 2, 3
Types of Functions
Library Function
 A library function is also referred to as a “built-in function”.
 A compiler package already exists that contains these functions, each of
which has a specific meaning and is included in the package.
 Built-in functions have the advantage of being directly usable without being
defined, whereas user-defined functions must be declared and defined
before being used.
 For Example:
 pow(), sqrt(), strcmp(), strcpy() etc.
Header Files for Library Functions in C Programming
 As mentioned earlier, all library functions are included in different header
files saved with a .h extension.
 To use any library functions, you need to use the header files at the
beginning of the program.
 Without including the header files, the program will not be executed as the
compiler will throw errors.
 Here are the header files available in C.
Header Files for Library Functions in C Programming
Header File Description
stdio.h
The input/output header file contains
all the library functions related to
input and output operations.
conio.h
It is the console I/O file and contains
library functions for console
input/output operations.
string.h
This is the string header file that
contains functions related to working
with strings.
math.h
It is the math header file and contains
all library functions related to math
operations.
User Defined Function
 Functions that the programmer creates are known as User-Defined
functions or “tailor-made functions”.
 User-defined functions can be improved and modified according to the need
of the programmer.
 Whenever we write a function that is case-specific and is not defined in any
header file, we need to declare and define our own functions according to
the syntax.
Advantages of Functions in C
Functions in C is a highly useful feature of C with many advantages as
mentioned below:
 The function can reduce the repetition of the same statements in the
program.
 The function makes code readable by providing modularity to our program.
 There is no fixed number of calling functions it can be called as many times
as you want.
 The function reduces the size of the program.
 Once the function is declared you can just use it without thinking about the
internal working of the function.
Disadvantages of Functions in C
The following are the major disadvantages of functions in C:
 Cannot return multiple values.
 Memory and time overhead due to stack frame allocation and transfer of
program control.

Programming_in_C_language_Unit5.pptx course ATOT

  • 1.
    Programming in Clanguage Computer Science - B.Sc. A&OTT, Semester 1 Prepared by: Ms. Mandeep Kaur Assistant Professor Computer Applications
  • 2.
    Data Types  Eachvariable in C has an associated data type.  It specifies the type of data that the variable can store like integer, character, floating, double, etc.  Each data type requires different amounts of memory and has some specific operations which can be performed over it.
  • 3.
  • 4.
    Primary Data Types Primary data types are also known as the fundamental data types because they are pre-defined or they already exist in the C language.  All the other types of data types (derived and user-defined data types) are derived from these data types.  Primary data types in C are of 4 types:  int  char  float  double.
  • 5.
    Integer Data Type The integer datatype in C is used to store the integer numbers (any number including positive, negative and zero without decimal part).  Octal values, hexadecimal values, and decimal values can be stored in int data type in C.  Range: -2,147,483,648 to 2,147,483,647  Size: 4 bytes  Format Specifier: %d  Syntax of Integer  int var_name;
  • 6.
    C program toprint Integer data types. #include <stdio.h> void main() { int a = 9; printf(" %d Integer value with positive data: n", a); getch(); } Output Integer value with positive data: 9
  • 7.
    Character Data Type Character data type allows its variable to store only a single character.  The size of the character is 1 byte. It is the most basic data type in C.  It stores a single character and requires a single byte of memory in almost all compilers.  Range: (-128 to 127) or (0 to 255)  Size: 1 byte  Format Specifier: %c  Syntax of char  char var_name;
  • 8.
    C program toprint Character data types. #include <stdio.h> int main() { char a = 'a’; printf(" %cValue of a: n", a); return 0; } Output Value of a: a
  • 9.
    Float Data Type In C programming float data type is used to store floating-point values.  Float in C is used to store decimal and exponential values.  The float data type is basically a precision sort of data type that is capable of holding 32 bits of decimal numbers or floating points.  It is used to store decimal numbers (numbers with floating point values) with single precision.  Range: 1.2E-38 to 3.4E+38  Size: 4 bytes  Format Specifier: %f  Syntax of float  float var_name;
  • 10.
    C program toprint Floating point data types. #include <stdio.h> int main() { float a = 9.0f; float b = 2.5f; printf("%fn", a); printf("%fn", b); return 0; } Output 9.000000 2.500000
  • 11.
    Double Data Type A Double data type in C is used to store decimal numbers (numbers with floating point values) with double precision.  The double data type is basically a precision sort of data type that is capable of holding 64 bits of decimal numbers or floating points.  It can easily accommodate about 16 to 17 digits after or before a decimal point.  Range: 1.7E-308 to 1.7E+308  Size: 8 bytes  Format Specifier: %lf  Syntax of Double  double var_name;
  • 12.
    C program toprint Double data types. #include <stdio.h> int main() { double a = 123123123.00; double b = 12.293123; printf("%lfn", a); printf("%lfn", b); return 0; } Output 123123123.000000 12.293123
  • 13.
    Void Data Type The void data type in C is used to specify that no value is present.  It does not provide a result value to its caller.  It has no values and no operations.  It is used to represent nothing.  Void is used in multiple ways as function return type, function arguments as void, and pointers to void.
  • 14.
    Size of DataTypes in C  The size of the data types in C is dependent on the size of the architecture, so we cannot define the universal size of the data types.  For that, the C language provides the sizeof() operator to check the size of the data types.
  • 15.
    C Program toprint size of different data type in C #include <stdio.h> int main() { int size_of_int = sizeof(int); int size_of_char = sizeof(char); int size_of_float = sizeof(float); int size_of_double = sizeof(double); printf(" %d The size of int data type : n", size_of_int); printf(" %d The size of char data type : n",size_of_char); printf(" %d The size of float data type : n",size_of_float); printf(" %d The size of double data type : ",size_of_double); return 0; }
  • 16.
    C Program toprint size of different data type in C Output The size of int data type : 4 The size of char data type : 1 The size of float data type : 4 The size of double data type : 8
  • 17.
    Derived Data Types In C, the data types derived from the primitive or built-in data types are called Derived Data Types.  In other words, the derived data types are those data types that are created by combining primitive data types and other derived data types.  There are three derived data types available in C. They are as follows:  Function  Array  Pointer
  • 18.
    Functions  A functionis a block of code which only runs when it is called.  You can pass data, known as parameters, into a function.  Functions are used to perform certain actions, and they are important for reusing code: Define the code once, and use it many times.  In C, a function can be called by types: call by value and call by reference.
  • 19.
    Syntax of Functionsin C  The syntax of function can be divided into 3 aspects:  Function Declaration  Function Definition  Function Calls
  • 20.
    Function Declarations  Ina function declaration, we must provide the function name, its return type, and the number and type of its parameters.  A function declaration tells the compiler that there is a function with the given name defined somewhere else in the program.  Syntax  return_type name_of_the_function (parameter_1, parameter_2);
  • 21.
  • 22.
    Function Definition  Thefunction definition consists of actual statements which are executed when the function is called (i.e. when the program control comes to the function).
  • 23.
    Function Call  Afunction call is a statement that instructs the compiler to execute the function. We use the function name and parameters in the function call.
  • 24.
    C program toprint add two numbers using function. #include <stdio.h> int sum(int a, int b) { return (a+b); } int main() { int num1=10; int num2=10; int num3; num3 = sum(num1, num2); printf("%d Sum of the entered numbers: ", num3); return 0; } Output: Sum of the entered numbers: 20
  • 25.
    Conditions of ReturnTypes and Arguments In C programming language, functions can be called either with or without arguments and might return values. They may or might not return values to the calling functions.  Function with no arguments and no return value  Function with no arguments and with return value  Function with argument and with no return value  Function with arguments and with return value
  • 26.
    Function with argumentsand return value Syntax: Function declaration : int function ( int ); Function call : function( x ); Function definition: int function( int x ) { statements; return x; }
  • 27.
    Function with argumentsbut no return value Syntax: Function declaration : void function ( int ); Function call : function( x ); Function definition: void function( int x ) { statements; }
  • 28.
    Function with noargument and no return value Syntax: Function declaration : void function(); Function call : function(); Function definition : void function() { statements; }
  • 29.
    Function with noarguments but returns a value Syntax: Function declaration : int function(); Function call : function(); Function definition : int function() { statements; return x; }
  • 30.
    Passing Parameters toFunctions  The data passed when the function is being invoked is known as the Actual parameters.  Formal Parameters are the variable and the data type as mentioned in the function declaration.  We can pass arguments to the C function in two ways:  Pass by Value  Pass by Reference
  • 31.
    Pass by Value Parameter passing in this method copies values from actual parameters into formal function parameters.  As a result, any changes made inside the functions do not reflect in the caller’s parameters. #include <stdio.h> void swap(int var1, int var2) { int temp = var1; var1 = var2; var2 = temp; }
  • 32.
    Pass by Value //Driver code int main() { int var1 = 3, var2 = 2; printf("Before swap Value of var1 and var2 is: %d, %dn",var1, var2); swap(var1, var2); printf("After swap Value of var1 and var2 is: %d, %d",var1, var2); return 0; } Output Before swap Value of var1 and var2 is: 3, 2 After swap Value of var1 and var2 is: 3, 2
  • 33.
    Pass by Reference The caller’s actual parameters and the function’s actual parameters refer to the same locations, so any changes made inside the function are reflected in the caller’s actual parameters. #include <stdio.h> void swap(int *var1, int *var2) { int temp = *var1; *var1 = *var2; *var2 = temp; }
  • 34.
    Pass by Reference //Driver code int main() { int var1 = 3, var2 = 2; printf("Before swap Value of var1 and var2 is: %d, %dn", var1, var2); swap(&var1, &var2); printf("After swap Value of var1 and var2 is: %d, %d", var1, var2); return 0; } Output Before swap Value of var1 and var2 is: 3, 2 After swap Value of var1 and var2 is: 2, 3
  • 35.
  • 36.
    Library Function  Alibrary function is also referred to as a “built-in function”.  A compiler package already exists that contains these functions, each of which has a specific meaning and is included in the package.  Built-in functions have the advantage of being directly usable without being defined, whereas user-defined functions must be declared and defined before being used.  For Example:  pow(), sqrt(), strcmp(), strcpy() etc.
  • 37.
    Header Files forLibrary Functions in C Programming  As mentioned earlier, all library functions are included in different header files saved with a .h extension.  To use any library functions, you need to use the header files at the beginning of the program.  Without including the header files, the program will not be executed as the compiler will throw errors.  Here are the header files available in C.
  • 38.
    Header Files forLibrary Functions in C Programming Header File Description stdio.h The input/output header file contains all the library functions related to input and output operations. conio.h It is the console I/O file and contains library functions for console input/output operations. string.h This is the string header file that contains functions related to working with strings. math.h It is the math header file and contains all library functions related to math operations.
  • 39.
    User Defined Function Functions that the programmer creates are known as User-Defined functions or “tailor-made functions”.  User-defined functions can be improved and modified according to the need of the programmer.  Whenever we write a function that is case-specific and is not defined in any header file, we need to declare and define our own functions according to the syntax.
  • 40.
    Advantages of Functionsin C Functions in C is a highly useful feature of C with many advantages as mentioned below:  The function can reduce the repetition of the same statements in the program.  The function makes code readable by providing modularity to our program.  There is no fixed number of calling functions it can be called as many times as you want.  The function reduces the size of the program.  Once the function is declared you can just use it without thinking about the internal working of the function.
  • 41.
    Disadvantages of Functionsin C The following are the major disadvantages of functions in C:  Cannot return multiple values.  Memory and time overhead due to stack frame allocation and transfer of program control.