Beruflich Dokumente
Kultur Dokumente
PROGRAMMING IN C
PREFACE
As you know that we live in a world of computers. Knowing how to use a computer is not enough,as the
actual interaction with computers happens with programming. This book will make you a programmer
by teaching you programming in c language which is one of the oldest,basic and most preferred,
programming language in one day.
This book is customized for beginners (no knowledge in programming concepts) .
GO AHEAD WITH PATIENCE AND BE A programmer
-MURTUZA CHAWALA
CONTENTS
1)INTRODUCTION
2)INPUT/OUTPUT OPERATIONS
3)DECISION MAKING AND LOOPING
4)FUNCTIONS
5)POINTERS
6)ARRAYS
7)STRINGS
8)USER DEFINED DATA TYPE
9) C PROGRAMS
Introduction
C is a general purpose
Programming language which was created by Denis Ritchie at At’t bell laboratory in 1972 in USA.
C is one of the most earliest,popular,powerful and easiest programming language.
The computer(digital system) cannot understand
normal English language ,it only understands machine language(binary code) that is (1s and 0s). thus c is
used to tell the computer what exactly is to be performed.
All software,application are written in some programming language(C,C++,Java ) etc
The Linux(Unix) operating system was totally written on c language.
C is also used to make compilers,kernels of an OS(operating system).
C is a programming language , whatever task we carry out on our mobiles,laptops etc is actually done
through programming(which is inbuilt) {unknown to end users}
Eg:-whenever a button is pressed its equivalent code is generated,that generated code is then compiled by
a compiler .
Compiler(transforms programming code into binary code(1s,0s) this binary code is sent to the CPU and
data processed there(in binary) ,it is sent back from the CPU to the monitor and binary code is again
converted to output form(audio,decimal,photo etc)
This is the working of a programming language
Q) Benefits of c?
A) 1) c is one of the basic and easiest programming language ie it the first step towards becoming a
programmer.
2)c is machine independent ie (code)
can be run on any computer.
3)as it is a structured and procedure oriented language,it gives step by step knowledge
4)Many operation can be performed using c ,also it is flexible(different methods to solve one problem).
5)It has high level language(java) features along with low level language features.
C concepts(explanation)
1) #include is a preprocessor directive(to be studied later)
<stdio.h> it is a header file which will allow you to perform some in built functions(printf) .
2) void main()
Every c program starts with main() . void means that the function main does not return any value.
3)printf :
This is an inbuilt function from header file <stdio.h> which will print anything which is written inside
print statement .
5) ;
This (semicolon) is used to denote end of line .each statement in main() function [barring few like loops]
, which have an ; at the end
NOTE:-
Every c program will have header file
#include<stdio.h> compulsorily as without that printf(printing output) and scanf(taking input) functions
cannot be performed.
Main() can be written as void main()
Or int main() and then return 0(null) at end.
2) Constant:
It is also an identifier.
The value of constant is fixed(unlike variable) and cannot be changed during programming.
Eg:-int a=5;
Here a is a constant holding value 5.
3)Keywords
These are reserved words used in programming and cannot be used as an variable or constant EG:-
int,char(datatypes) ,void,for,while etc.
4)Data types
They are keywords .
They are used for assigning a type to a variable .
The basic data types.
1)int: It specifies that the variable associated with it holds a number
Eg:- int a;
It means a will be holding some number .
NOTE
:-FORMAT SPECIFIER
If we want to print some integer value as we cannot directly write variable name in printf statement, we
use %d
In “ ”
And then ,write the variable name
Eg:- main()
{
int a=5;
Printf(“%d”,a)
}
Thus the compiler will know that it has to print an integer (a) [when %d used]
2)char:it specifies that the variable associated with it holds a single character
Eg:- char a;
It means a will be holding some character.
FORMAT SPECIFIER
%c.
3)float:it specifies that the variable associated with it holds a floating value (in decimals ) Eg:-4.32
Eg:- float a;
A will be holding a float value.
FORMAT SPECIFIER
%f
4)void:-
It tells that the variable,function does not hold any value.[mostly used in main() function]
THERE ARE OTHER DATA TYPES TOO
Long int (4bytes) (%ld) [larger number]
Unsigned int(2 bytes) (%u) [positive number ,mostly used for pointers]
Double(8 bytes) (%lf) [ large float value]
Long double(16 bytes) (%Lf)
Above bytes size ie 2,4 etc indicate amount of memory taken by each number,char etc.
NOTE:-
Variable declaration means to declare variable without assigning any value
Eg:- int a;
Variable definition means to declare variable along with its fixed value
Eg:- int a=5;
OPERATORS
Operators are symbols which operate a value or variable.
They represent an operation.
Eg: +,-,*,>,< etc
Types of operators
An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. C
language is rich in built-in operators and provides the following types of operators −
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
• Misc Operators
Arithmetic Operators
The following table shows all the arithmetic operators supported by the C language. Assume
variable A holds 10 and variable B holds 20 then −
Show Examples
Relational Operators
The following table shows all the relational operators supported by C. Assume variable A holds 10 and
variable B holds 20 then −
Show Examples
== Checks if the values of two operands are equal or not. If yes, then the (A == B)
condition becomes true. is not true.
!= Checks if the values of two operands are equal or not. If the values are not (A != B) is
equal, then the condition becomes true. true.
> Checks if the value of left operand is greater than the value of right (A > B) is
operand. If yes, then the condition becomes true. not true.
< Checks if the value of left operand is less than the value of right operand. If (A < B) is
yes, then the condition becomes true. true.
>= Checks if the value of left operand is greater than or equal to the value of (A >= B)
right operand. If yes, then the condition becomes true. is not true.
<= Checks if the value of left operand is less than or equal to the value of right (A <= B)
operand. If yes, then the condition becomes true. is true.
Logical Operators
Following table shows all the logical operators supported by C language. Assume variable A holds 1 and
variable B holds 0, then −
Show Examples
&& Called Logical AND operator. If both the operands are non-zero, then the (A &&
condition becomes true. B) is
false.
|| Called Logical OR Operator. If any of the two operands is non-zero, then the (A || B)
condition becomes true. is true.
! Called Logical NOT Operator. It is used to reverse the logical state of its !(A &&
operand. If a condition is true, then Logical NOT operator will make it false. B) is
true.
Assignment Operators
The following table lists the assignment operators supported by the C language −
Show Examples
= Simple assignment operator. Assigns values from right side C = A + B will assign
operands to left side operand the value of A + B to
C
+= Add AND assignment operator. It adds the right operand to the C += A is equivalent
left operand and assign the result to the left operand. to C = C + A
sizeof() Returns the size of a variable. sizeof(a), where a is integer, will return 4.
& Returns the address of a &a; returns the actual address of the variable.
variable.
Operators Precedence in C
Operator precedence determines the grouping of terms in an expression and decides how an expression
is evaluated. Certain operators have higher precedence than others; for example, the multiplication
operator has a higher precedence than the addition operator.
For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has a higher precedence
than +, so it first gets multiplied with 3*2 and then adds into 7.
OPERANDS
They are the variable(or constant) on which operation are performed
Eg:- a+b;
a and b are operands and + is an operator.
Expression
An expression is an combination of operators,operands
Eg: a*b+c;
Is an expression
#include<stdio.h>
Void main()
{
int a=10,b=20,c;
c=a+b;
printf(“%d”,c);
}
NOTE:- // means comment which means that line of code will not get executed [used for self
understanding purposes]
Explanation
#include<stdio.h>
// [ to include header file stdio.h which will allow us to use the printf statement.]
Void main()
//[function which contains source code and returns nothing]
{
// [to start source code]
Int a=20,b=10,c;
//[ a and b are integer constant having values 20,10
c is an integer variable whose value is undefined .]
C=a+b;
//[operation now c=30]
Printf(“%d”,c);S
//[
Print “%d”, that is an integer c]
OUTPUT:
30.
CH :- 2
INPUT/OUTPUT OPERATION
Every computer has one primary goal that is to take data as input ,process it and give the required output
to user.
Similarly if we want to execute a c program we need to input some values,give some processing
information(add two numbers) and then write an output statement.
2)scanf() function:
To read(accept) data from user we use scanf() function.
If we want to store value of a variable say x as 4 then we use scanf statement which will accept x as 4;
In scanf () we use format specifier of variable and later(after comma,)use &(ampersand) operator which
will store x and its value in the memeory.
EG:-
int a;
Printf(“enter value for a”);
Scanf(“%d”,&a);
int c;
char str[100];
return 0;}
When the above code is compiled and executed, it waits for you to input some text. When you enter a
text and press enter, then the program proceeds and reads the complete line till end, and displays it as
follows −
CHAPTER 3:
DECISION MAKING
Decision making statements are one of the most fundamental concepts in c language.
A wide variety of programs(tasks) can be executed by decision making statement.
These statements can help us to check multiple conditions(possibilities)
And print the desired condition
They are also called as selection or conditional statements..
CONDITIONAL STATEMENTS:
1) If statement
It is a conditional statement used in c to check condition or control execution of statements.
It is very simple to use.
Syntax
If(condition)
{
Statements
}
In the above syntax the condition is checked first if it is true then the program control flow goes inside
braces and executes block of statements.
If condition is false the statements do not get executed
NOTE:-
WE DO NOT ISSUE A SEMICOLEN; FOR AN IF STATEMENT.
EG:-
Int a =5;
If(a==5) //[NO ;]
{
Printf(“condition is true”);
}
OUTPUT:
Condition is true.
2) if-else statement
This statement is also very useful ,
And unlike if statement it can check two conditions
SYNTAX:-
If(condition)
{
Statement(true);
}
else
{
Statement(false);
}
EG:-
Int a =4;
If(a==3)
{
Printf(“condition is true”);
}
Else
{
Printf(“condition is false”);
}
OUTPUT:-
Condition is false.
Nested if statement
Nested if-else statement
3) Switch statement
Switch statement is a multiple decision making statement.
When if-else condition is used to check statement more than one condition the complexity of program
increases.
Thus to overcome this problem c provides switch statement.
It is used to check multiple conditions efficiently.
SYNTAX:
Switch(expression)
{
Case 1:
Statements;
Break;
Case 2:Statements;
Break;
Case 3:Statements;
Break;
Case n:Statements;
Break;
}
#include <stdio.h>
int main () {
switch(grade) {
case 'A' :
printf("Excellent!\n" );
break;
case 'B' :
case 'C' :
printf("Well done\n" );
break;
case 'D' :
printf("You passed\n" );
break;
case 'F' :
printf("Better try again\n" );
break;
default :
printf("Invalid grade\n" );
}
return 0;}
When the above code is compiled and executed, it produces the following result −
Well done
Your grade is B
Chapter 4
LOOP
Looping is also one of the important concepts in programming .
Loops are used for executing a set of statements again and again.
The data inside loops keeps on executing until condition is false.
It is used for writing condition.
A loop execution can be done by
Starting a loop with a terminating condition
Initialize a variable and start a loop
Execution of statements inside loop
Increment or decrement of variable(till condition)
Types of looping statement
Syntax:-
While(condition)
{
Statement;
Increment/decrement;
}
ALGORITHM :
Eg:-
A=3,b=5;
While(a<b)
Print a;
A++;
Output 3 4
B) For loop
It is more efficient and useful than while loop .
One of the most important actions of for loop is that three action can be taken at a time ie
A)variable initialization
B)Condition checking
C)Increment/decrement of a variable.
Syntax:-
For(initialization;condition;incr/decr)
{
Statements;
}
Eg:-
A=3,I;
For(i=1;i<a;i++)
{
Print(i)
}
Output:-
123
Here after I is printed for the first time the control goes back to the loop increments I to 2, checks
whether I<a(3) if yes than again print …..
Else
Control will come out of loop.
JUMP STATEMENTS
These statements are used to transfer flow of statements from one process to another.
1)Goto
2)Break
3)Continue
4)Exit
1)Goto
This statement transfers the control of the program from one point to another.
Syntax:-
Goto label:
In the above algorithm variable (I) should have been printed 5 times but because of goto statement, I
after printing twice will because of jump statement goto ,
Jump to here: and print yippeee
Output:- 0 1 yippeee
2)Break
It is one of the useful jump statements in loops and switch cases
It is used to transfer control out of loop(exit from loop or switch cases)
It causes loop termination,(which is sometimes necessary when some condition is satisfied.
Syntax:-
If(condition)
Break;
3)Continue
This statement is used when it is required to skip a part of the body under some condition.
If condition is true and continue statement is used then control does not go to print or other statement in
a loop it will directly go to start of loop.
Syntax:- continue;
It does not cause loop termination
And just like break it is also used with if (conditional) statement.
4)Exit():-
This is a simple jump statement which terminates program completely if statement is called.
If exit is used control does not go ahead [till getch()] and directly stops compiling.
Syntax:-exit();
NOTE:- unlike conditional(if..,,)
And looping (for,..) statements, jump statements are used only for reference and for small tasks and
jump statements work on conditional and looping statement.
Chapter 5:
FUNCTIONS
1) Function declaration
If any user defined function has to used,it has to be declared first[before main() is better]
Syntax:-
datatype function_name(parameter)
2) Function call
This statement transfers the control from the main() part to the actual function outside main()
{basically calls the function}
Syntax:-
Function_name(parameters)
Eg:-sum(a,b)
(its ok if datatype not used with parameters when calling function)
3) Function definition
This is the main part of the function
It contains all codes that are to be performed.
It may take zero,one or more inputs and may or may not give back some output but performs processing
of data.
It contains a header which contains of the function name(earlier declared) and body which consist of
steps.
Syntax_ Return datatype function_name(parameters)
{
Statements/……
}
NOTE:- here return datatype means value(int,char) to be returned to main program.
If return type is void then no value is returned.
Function declaration and call are used for declare and call of function.
Function defintion is for processing of data,
Program.
#include<stdio.h>
void main()
{
int a,b;
clrscr();
printf("Enter Two Number : ");
scanf("%d%d",&a,&b);
sum(a,b);
getch();
}
sum(int x,int y)
{
int z;
z=x+y;
printf("Sum of Two Number is : %d",z);
return 0;
}
Output :
Enter Two Number : 10 20
Sum of Two Number is : 30
Function categories.
In call by value, a copy of actual arguments is In call by reference, the location (address) of
passed to formal arguments of the called function actual arguments is passed to formal
and any change made to the formal arguments in arguments of the called function. This means
the called function have no effect on the values by accessing the addresses of actual arguments
of actual arguments in the calling function. we can alter them within from the called
function.
In call by value, actual arguments will remain In call by reference, alteration to actual
safe, they cannot be modified accidentally. arguments is possible within from called
function; therefore the code must handle
arguments carefully else you get unexpected
results.
Storage classes :
It refers to scope of variable and memory allocated to store that variable.
In other words it tells us what extra features are given to the variable.
There are 4 storage class.
1) AUTO
2) REGISTER
3) STATIC
4) EXTERNAL
The auto Storage Class
The auto storage class is the default storage class for all local variables.
{
int mount;
auto int month;}
The example above defines two variables with in the same storage class. 'auto' can only be used within
functions, i.e., local variables.
The register Storage Class
The register storage class is used to define local variables that should be stored in a register instead of
RAM. This means that the variable has a maximum size equal to the register size (usually one word) and
can't have the unary '&' operator applied to it (as it does not have a memory location).
{
register int miles;}
The register should only be used for variables that require quick access such as counters. It should also
be noted that defining 'register' does not mean that the variable will be stored in a register. It means that
it MIGHT be stored in a register depending on hardware and implementation restrictions.
Recursion:
It is a programming technique,
where a function calls itself again and again.
The C programming language supports recursion, i.e., a function to call itself. But while using recursion,
programmers need to be careful to define an exit condition from the function, otherwise it will go into
an infinite loop.
Recursive functions are very useful to solve many mathematical problems, such as calculating the
factorial of a number, generating Fibonacci series, etc.
Example: Factorial of a Number Using Recursion
#include <stdio.h>long int multiplyNumbers(int n);
int main(){
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;}long int multiplyNumbers(int n){
if (n >= 1)
return n*multiplyNumbers(n-1);
else
return 1;}
Output
Factorial of 6 = 720
Initially, the multiplyNumbers() is called from the main() function with 6 passed as an argument.
Then, 5 is passed to the multiplyNumbers() function from the same function (recursive call). In each
recursive call, the value of argument n is decreased by 1.
Chapter 6:
Pointers
INTRODUCTION:
Pointer is a variable that stores memory address of another variable.
Pointer enhances dynamic handling of data.
A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory
location. Like any variable or constant, you must declare a pointer before using it to store any variable
address. The general form of a pointer variable declaration is −
type *var-name;
Here, type is the pointer's base type; it must be a valid C data type and var-name is the name of the
pointer variable. The asterisk * used to declare a pointer is the same asterisk used for multiplication.
However, in this statement the asterisk is being used to designate a variable as a pointer. Take a look at
some of the valid pointer declarations −
return 0;}
When the above code is compiled and executed, it produces the following result −
C programming allows passing a pointer to a function. To do so, simply declare the function parameter
as a pointer type.
Following is a simple example where we pass an unsigned long pointer to a function and change the
value inside the function which reflects back in the calling function −
return 0;}
void getSeconds(unsigned long *par) {
/* get the current number of seconds */
*par = time( NULL );
return;}
When the above code is compiled and executed, it produces the following result −
Dynamic memory allocation allows your program to obtain more memory space while running, or to
release it if it's not required.
In simple terms, Dynamic memory allocation allows you to manually handle memory space for your
program.
Although, C language inherently does not have any technique to allocate memory dynamically, there are
4 library functions under "stdlib.h" for dynamic memory allocation.
malloc() Allocates requested size of bytes and returns a pointer first byte of allocated space
Function Use of Function
Allocates space for an array elements, initializes to zero and then returns a pointer to
calloc()
memory
malloc()
The name malloc stands for "memory allocation".
The function malloc() reserves a block of memory of specified size and return a pointer of
type void which can be casted into pointer of any form.
Syntax of malloc()
Here, ptr is pointer of cast-type. The malloc() function returns a pointer to an area of memory with size
of byte size. If the space is insufficient, allocation fails and returns NULL pointer.
This statement will allocate either 200 or 400 according to size of int 2 or 4 bytes respectively and the
pointer points to the address of first byte of memory.
calloc()
The name calloc stands for "contiguous allocation".
The only difference between malloc() and calloc() is that, malloc() allocates single block of memory
whereas calloc() allocates multiple blocks of memory each of same size and sets all bytes to zero.
Syntax of calloc()
This statement will allocate contiguous space in memory for an array of n elements. For example:
free()
Dynamically allocated memory created with either calloc() or malloc() doesn't get freed on its own. You
must explicitly use free() to release the space.
syntax of free()
free(ptr);
This statement frees the space allocated in the memory pointed by ptr.
C realloc()
If the previously allocated memory is insufficient or more than required, you can change the previously
allocated memory size using realloc().
Syntax of realloc()
Write a C program to find sum of n elements entered by user. To perform this program, allocate
memory dynamically using malloc() function.
Chapter 7:
Array and String.
Array is a data structure which contains group of elements under a single name.
A very useful technique that reduces time and helps in looping statements
Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare
one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent
individual variables. A specific element in an array is accessed by an index.
All arrays consist of contiguous memory locations. The lowest address corresponds to the first element
and the highest address to the last element.
Instead of scanning(accepting) 5 numbers, we can store all the 5 numbers in a variable. This technique is
called as an array.
Declaration of array
Int a[5];
It means that variable a will store 5 numbers ,each element in array can be accessed using index .
a[0],a[1],a[2] and so on.
Type of array
1 dimensional
2 dimensional
Multi dimensional
1 #include<stdio.h>
2
3 int main() {
4 int i, arr[50], sum, num;
5
6 printf("\nEnter no of elements :");
7 scanf("%d", &num);
8
9 //Reading values into Array
10 printf("\nEnter the values :");
11 for (i = 0; i < num; i++)
12 scanf("%d", &arr[i]);
13
14 //Computation of total
15 sum = 0;
16 for (i = 0; i < num; i++)
17 sum = sum + arr[i];
18
19 //Printing of all elements of array
20 for (i = 0; i < num; i++)
21 printf("\na[%d]=%d", i, arr[i]);
22
23 //Printing of total
24 printf("\nSum=%d", sum);
25
26 return (0);
27 }
Output :
1 Enter no of elements : 3
2 Enter the values : 11 22 33
3 a[0]=11
4 a[1]=22
5 a[2]=33
6 Sum=66
return (0);
}
Output :
1 Enter no of elements : 5
2 11 44 22 55 99
3 Smallest Element : 11
Output :
1 Enter no of elements : 5
2 11 22 33 44 55
3 Enter the elements to be searched : 44
4 Number found at the location = 4
STRINGS
Strings are basically an array of characters.
Strings are actually one-dimensional array of characters terminated by a nullcharacter '\0'. Thus a null-
terminated string contains the characters that comprise the string followed by a null.
The following declaration and initialization create a string consisting of the word "Hello". To hold the
null character at the end of the array, the size of the character array containing the string is one more than
the number of characters in the word "Hello."
Actually, you do not place the null character at the end of a string constant. The C compiler automatically
places the '\0' at the end of the string when it initializes the array. Let us try to print the above mentioned
string −
#include <stdio.h>
int main () {
1 strcpy(s1, s2);
Copies string s2 into string s1.
2 strcat(s1, s2);
Concatenates string s2 onto the end of string s1.
3 strlen(s1);
Returns the length of string s1.
4 strcmp(s1, s2);
Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
5 strchr(s1, ch);
Returns a pointer to the first occurrence of character ch in string s1.
6 strstr(s1, s2);
Returns a pointer to the first occurrence of string s2 in string s1.
All these function can be accessed using standard header file <string.h>
Length of string: 9
int main() {
char str[20], ch;
int count = 0, i;
if (count == 0)
printf("\nCharacter '%c'is not present", ch);
else
printf("\nOccurence of character '%c' : %d", ch, count);
return (0);
}
Output :
Chapter 8:
User defined datatype.
STRUCTURE
Arrays allow to define type of variables that can hold several data items of the same kind.
Similarly structure is another user defined data type available in C that allows to combine data items of
different kinds.
Structures are used to represent a record. Suppose you want to keep track of your books in a library. You
might want to track the following attributes about each book −
• Title
• Author
• Subject
• Book ID
Defining a Structure
To define a structure, you must use the struct statement. The struct statement defines a new data type,
with more than one member. The format of the struct statement is as follows −
Struct name
{
Datatype member_name;
Datatype member_name;
};
Eg:- struct student
{
Int data;
Char name[10];
};
S.data;
S.name;
// storing information
for(i=0; i<10; ++i)
{
s[i].roll = i+1;
printf("\n");
}
printf("Displaying Information:\n\n");
// displaying information
for(i=0; i<10; ++i)
{
printf("\nRoll number: %d\n",i+1)
printf("Name: ");
puts(s[i].name);
printf("Marks: %.1f",s[i].marks);
printf("\n");
}
return 0;}
Output
Enter marks: 98
Displaying Information:
Roll number: 1
Name: Tom
Marks: 98
If structure is passed by value, changes made to the structure variable inside the function definition does
not reflect in the originally passed structure variable.
C program to create a structure student, containing name and roll and display the information.
If structure is passed by reference, changes made to the structure variable inside function definition
reflects in the originally passed structure variable.
C program to add two distances (feet-inch system) and display the result without the return
statement.
printf("First distance\n");
printf("Enter feet: ");
scanf("%d", &dist1.feet);
printf("Enter inch: ");
scanf("%f", &dist1.inch);
printf("Second distance\n");
printf("Enter feet: ");
scanf("%d", &dist2.feet);
printf("Enter inch: ");
scanf("%f", &dist2.inch);
//passing structure variables dist1 and dist2 by value whereas passing structure variable dist3 by
reference
printf("\nSum of distances = %d\'-%.1f\"", dist3.feet, dist3.inch);
return 0;}void add(struct distance d1,struct distance d2, struct distance *d3) {
//Adding distances d1 and d2 and storing it in d3
d3->feet = d1.feet + d2.feet;
d3->inch = d1.inch + d2.inch;
UNION
A union is a special data type available in C that allows to store different data types in the same memory
location. You can define a union with many members, but only one member can contain a value at any
given time. Unions provide an efficient way of using the same memory location for multiple-purpose.
Defining a Union
To define a union, you must use the union statement in the same way as you did while defining a
structure. The union statement defines a new data type with more than one member for your program.
The format of the union statement is as follows −
Union name
{
Datatype name;
Datatype name;
};
Union and structure are same just that members of union use same memory,unlike structure
members whose each member will occupy seperate space.
Chapter 9:
C programs
return 0;}
Output
1.12
Product = 2.69
return 0;}
Output
Enter an integer: -7
-7 is odd.
Enter a character: G
ASCII value of G = 71
if (flag==0)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number.",n);
return 0;}
Output
29 is a prime number.
return 0;}
Output 1
Output 2