10 Seconds - C Programming
10 Seconds - C Programming
Ramaprasad H.C
9945426447
[email protected]
10 SECONDS 1
a – 1000, b – 2000, c -3000
10 SECONDS 2
register int a, b, c;
One.c
1000 a
CPU 2000 b
3000 c
a
b
c
RAM / PM
registers
HD/SM
10 SECONDS 3
int a; register int b; volatile int c;
10 SECONDS 4
10 % 3 =
-10 % 3 =
10 % -3 =
-10 % -3 =
3 % 10 =
10 SECONDS 5
int a; int a;
a = 5; a = 5;
a++; ++a;
printf (“%d”, a); printf (“%d”, a);
10 SECONDS 6
int a, b, c, d;
a = 5;
b = 6;
c = a++ + ++b;
d = ++a + b++;
10 SECONDS 7
int a = 5;
Printf (“%d%d%d%d%d%d”, a++, ++a, a--, --a, a++, ++a);
10 SECONDS 8
Comma operator
int a, b, c;
c = (a = 5, b = 6, a+b);
printf (“%d”, c);
10 SECONDS 9
The if statement
Syntax
if (this condition is true)
execute one statement;
10 SECONDS 10
Relational operators
==
!=
<
>
<=
>=
10 == 5 != 4 >= 5 < 9 > 10 <= 0
10 SECONDS 11
The if..else statement
Syntax: if (this condition is true)
if (this condition is true) {
execute one statement; 1st stmt;
else 2nd stmt;
execute one statement; . More than one statement
.
Nth stmt;
}
else
{
1st stmt;
2nd stmt;
. More than one statement
.
Nth stmt;
}
10 SECONDS 12
Logical operators
&& Logical and
|| Logical or
! Logical Not
!10&&5||0
0&&5||0
0||0
0
10 SECONDS 13
10==5 != 0 <= 4 > 45 < 90 >= 10
0!= 0 <= 4 > 45 < 90 >= 10
10 SECONDS 14
Note:
10 SECONDS 15
Note:
10 SECONDS 16
Symbolic constants
Example 1: program without symbolic constants
# include <stdio.h>
void main ( )
{
float r, area;
printf (“Enter radius \n”);
scanf (“%f”, &r);
area = 3.1412 * r * r;
printf (“area of a circle = %f”, area);
}
10 SECONDS 17
Symbolic constants cont..
Example 2:
# include <stdio.h>
# define PI 3.1412
void main ( )
{
float r, area;
printf (“Enter radius \n”);
scanf (“%f”, &r);
area = PI * r * r;
printf (“area of a circle = %f”, area);
}
10 SECONDS 18
Keywords / Reserved words
Extended keywords – specific to
compiler –
Different compilers?
Turbo C – Microsoft
Dev C++ -
Gcc
ANSI - _ _ asm, _ _ far, _ _ near;
10 SECONDS 19
Decision Control Problems
10 SECONDS 20
# include <stdio.h>
int main ( )
{
int a = 300, b, c;
if (a >= 400)
b = 300;
c = 200;
printf (“%d%d”, b, c);
return 0;
}
10 SECONDS 21
# include <stdio.h>
int main ( )
{
int a = 500, b, c;
if (a >= 400)
b = 300;
c = 200;
printf (“%d%d”, b, c);
return 0;
}
10 SECONDS 22
# include <stdio.h>
int main ( )
{
int x = 10, y = 20;
if ( x == y);
printf (“%d%d”, x, y);
return 0;
}
10 SECONDS 23
# include <stdio.h>
int main ( )
{
int x = 3, y, z;
y = x = 10;
z = x < 10;
printf (“x = %d y = %d z = %d”, x, y,
z);
return 0;
}
10 SECONDS 24
# include <stdio.h>
int main ( )
{
int i = 65;
char j = ‘A’;
if (i == j)
printf (“C is good”);
else
printf (“C is headache”);
return 0;
} 10 SECONDS 25
# include <stdio.h>
void main ()
{
if (condition)
printf (“Hello”);
else
printf (“world”);
}
10 SECONDS 26
for statement
Syntax
for (initialize counter; test counter; increment
counter)
execute one statement;
for (initialize counter; test counter; increment
counter)
{
do this;
and this; More than one statement.
and this;
}
10 SECONDS 27
Note:
is not necessary that a loop counter
must only be an int. It can even be a
float
10 SECONDS 28
Example:
# include <stdio.h>
void main ( )
{
int i, j;
for (i = 1; i <= 2; i++)
{
for (j = 1; j <= 2; j++)
{
if (i == j)
continue;
printf (“%d%d”, i, j);
}
}
}
10 SECONDS 29
Loop Control Problems
10 SECONDS 30
# include <stdio.h>
int main ( )
{
int i = 1;
while (i <= 10);
{
printf (“%d”, i);
i++;
}
return 0;
}
10 SECONDS 31
# include <stdio.h>
int main ( )
{
int x = 4;
while (x == 1)
{
x = x – 1;
printf (“%d”, x);
--x;
}
return 0;
}
10 SECONDS 32
# include <stdio.h>
int main ( )
{
int x = 4, y, z;
y = --x;
z = x--;
printf (“%d%d%d”, x, y, z);
return 0;
X=2
Y=3
Z=3
10 SECONDS 33
}
# include <stdio.h>
int main ( )
{
int x = 4, y = 3, z;
z = x-- - y;
printf (“%d%d%d”, x, y, z);
return 0;
}
10 SECONDS 34
# include <stdio.h>
int main ( )
{
while (‘a’ < ‘b’)
printf (“Malayalam is a palindrome
\n”);
return 0;
}
10 SECONDS 35
# include <stdio.h>
int main ( )
{
int i;
while (i = 10)
{
printf (“%d”, i);
i = i+1;
}
return 0;
}
10 SECONDS 36
# include <stdio.h>
int main ( )
{
int x = 4, y = 0, z;
while (x >= 0)
{
x--;
y++;
if (x == y)
continue;
else
printf (“%d%d”, x, y);
}
return 0;
}
10 SECONDS 37
# include <stdio.h>
int main ( )
{
int x = 4, y = 0, z;
while (x >= 0)
{
if (x == y)
break;
else
printf (“%d%d”, x, y);
x--;
y++;
}
return 0;
}
10 SECONDS 38
Case Control Instruction
10 SECONDS 39
# include <stdio.h>
int main ( )
{
int k;
float j = 2.0;
switch (k = j + 1)
{
case 3:
printf (“Trapped \n”);
break;
default:
printf (“Caught \n”);
}
return 0;
} 10 SECONDS 40
# include <stdio.h>
int main ( )
{
int ch = ‘a’ + ‘b’;
switch (ch)
{
case ‘a’:
case ‘b’:
printf (“You entered b \n”);
case ‘A’:
printf (“a as in ashar”);
case ‘b’ + ‘a’:
printf (“You entered a and b”);
}
return 0;
} 10 SECONDS 41
# include <stdio.h>
int main ( )
{
int i = 1;
switch (i – 2)
{
case -1:
printf (“Feeding fish \n”);
case 0:
printf (“Weeding grass \n”);
case 1:
printf (“Mending roof \n”);
default:
printf (“Just to survive \n”);
}
return 0;
}
10 SECONDS 42
Unit 4: Arrays & Datatypes
Homogeneous group of elements are
called arrays.
Declaration
int a[10];
float b[20];
10 SECONDS 43
Array Initialization
10 SECONDS 44
Bound checking
# include <stdio.h>
void main ( )
{
int i;
void display (int);
int marks[ ] = {10, 20, 30, 40, 50};
for (i = 0; i < 5; i++)
display (marks[i]);
}
void display (int m)
{
printf (“%d”, m)
}
10 SECONDS 46
Passing array elements to a
function (Call by reference)
# include <stdio.h>
void main ( )
{
int i;
void display (int *);
int marks[ ] = {10, 20, 30, 40, 50};
for (i = 0; i < 5; i++)
display (&marks[i]);
}
void display (int *m)
{
printf (“%d”, *m)
}
10 SECONDS 47
Integers, long and short
10 SECONDS 48
Integers, long and short cont..
Compiler Short Int Long
16 bit 2 2 4
(Turbo C /
C++)
32 bit 2 4 4
(Visual
C++)
10 SECONDS 49
Integers, long and short cont..
Ex:
short int a;
short b;
long int c;
long d;
10 SECONDS 50
Integers, signed and unsigned
unsigned int num_students;
Note:
With unsigned int, the range of permissible integer
values (for 16-bit ) will shift from -32767 to + 32767
to the range 0 to 65535.
Thus declaring an integer as unsigned almost doubles
the size of the largest possible value that it can
otherwise take.
This so happens because on declaring the integer as
unsigned, the left-most is now free and is not used to
store the sign of the number.
Unsigned integer still occupies two bytes.
10 SECONDS 51
Chars, signed and unsigned
Ordinary char has a range from -128
to +128.
Unsigned char has a range from 0 to
255.
10 SECONDS 52
Floats, doubles and long
doubles
float – 4 bytes %f
double – 8 byes %lf
long double – 10 bytes %Lf
Note:
The sizes and ranges of int, short
and long are compiler dependent.
The above are on 16-bit compiler.
10 SECONDS 53
Data Type Range Bytes Format
Signed char -128 to + 127 1 %c
Unsigned char 0 to 255 1 %c
Short signed -32767 to 2 %d
int +32767
Short unsigned 0 to 65535 2 %u
int
Signed int 4 %d
Unsigned int 4 %u
Long signed int 4 %ld
Long unsigned 4 %lu
int
Float 4 %f
Double 8 %lf
Long double 10 %Lf
10 SECONDS 54
Unit 5: Functions & Pointers
C program to add 2 integer numbers without
using functions
# include <stdio.h>
void main ( )
{
int a, b, res;
printf (“Enter 2 numbers \n”);
scanf (“%d%d”, &a, &b);
res = a + b;
printf (“Result = %d”, res);
getch ( );
}
10 SECONDS 55
10 SECONDS 56
C program to add 2 integer numbers with the use of function
# include <stdio.h>
void main ( )
{
int a, b, res;
int add (int p, int q); // Function declaration Calling Function
printf (“Enter 2 numbers \n”);
scanf (“%d%d”, &a, &b);
res = add (a, b); // Function call or Function activation or Function invocation or
Function instantiation. In this function variable a and b
are called actual parameters.
printf (“Result = %d”, res);
getch ( );
}
10 SECONDS 57
void praveen ( ) int suman (int p, int q)
{ {
int a, b, c; int r;
int suman (int, r = p + q;
int); return r;
a = 10; }
b = 20;
c = suman (a, b);
printf (“%d”, c);
}
10 SECONDS 58
C program to add two floating point
numbers without using functions
# include <stdio.h>
void main ( )
{
float p, q, res;
printf (“Enter 2 floating point numbers
\n”);
scanf (“%f%f”, &p, &q);
res = p + q;
printf (“Result = %f”, res);
}
10 SECONDS 59
Void main ( ) int add (int p, int q)
{ {
int a, b, c;
int r;
int add (int, int);
r = p + q;
a = 10;
b = 20; retrun r;
c =add (a, b); }
printf (“%d”, c);
}
10 SECONDS 60
C program to add two floating point numbers
with the use of function
# include <stdio.h>
void main ( )
{
float p, q, res;
float sum (float, float);
printf (“Enter 2 floating point numbers \n”);
scanf (“%f%f”, &p, &q);
res = sum (p, q);
printf (“Result = %f”, res);
}
float sum (float p, float q)
{
float res;
res = p + q;
return res;
}
10 SECONDS 61
Pointers
Ordinary Variable Pointer Variable
10 SECONDS 63
C program to swap contents of 2 variables
without using pointers
# include <stdio.h> void swap (int p, int q)
void main ( ) {
{ int temp;
int a, b;
void swap (int p, int q); temp = p;
printf (“Enter 2 numbers p = q;
\n”); q = temp;
scnaf (“%d%d”, &a, &b); }
printf (“Elements before
swapping \n”);
printf (“a = %d \n b = %d”,
a, b);
swap (a, b);
printf (“Elements after
swapping \n”);
printf (“a = %d \n b = %d”,
a, b);
}
10 SECONDS 64
C program to swap contents of
2 variables using pointers
# include <stdio.h> void swap (int *p, int *q)
void main ( ) {
{ int temp;
int a, b;
void swap (int * , int *); temp = *p;
printf (“Enter 2 numbers *p = *q;
\n”); *q = temp;
scnaf (“%d%d”, &a, &b); }
printf (“Elements before
swapping \n”);
printf (“a = %d \n b = %d”,
a, b);
swap (&a, &b);
printf (“Elements after
swapping \n”);
printf (“a = %d \n b = %d”,
a, b);
}
10 SECONDS 65
Important points on array
int num[]={24,34,12,44,56,17};
By mentioning the name of the array, we get
its base address.
Thus by saying *num,we would refer to the
zeroth element of the array(i.e 24 in this case).
*num and *(num+0) both refers to 24.
When we say num[i], the c compiler internally
converts it to *(num+i).This means that all the
following notations are same.
num[i].....*(num+i)...*(i+num)...i[num]
10 SECONDS 66
Pointers and Arrays
pa 9700
#include <stdio.h>
int main()
{ int a[10],*pa,i,n=10,sum=0;
pa=&a[0];
for(i=0;i<10;i++)
scanf(“%d”,pa+i); /* or scanf(“%d”,&pa[i]); */
for(i=0;i<10;i++)
sum=sum + *(pa+i); /* or sum=sum + pa[i] */
printf(“\n sum=%d”,sum);
}
10 SECONDS 69
Pointers and strings
char str[]=“Infosys”;
char *cptr=“Infosys”;
Both declarations are same.
The compiler allocates enough space to hold the
string along with its terminating null character.
A pointer pointing to a string contains the base
address of the character array and entire array
or part of array can be accessed using this
pointer.
10 SECONDS 70
Program to illustrate pointers and strings
#include <stdio.h>
int main() cptr=cptr+5;
{ char str1[]=“INFO puts(cptr);
TECH”;
cptr=str2;
char str2[]=“Java and
puts(cptr);
Internet”;
cptr=cptr+9;
char *cptr;
puts(cptr);
puts (str1); puts(str2);
}
cptr=str1;
puts(cptr);
10 SECONDS 71
Output:
INFO TECH
Java and Internet
INFO TECH
TECH
Java and Internet
Internet
10 SECONDS 72
Program:Length of string using pointers
10 SECONDS 73
Pointers and strings
NOTE 1 NOTE 2
char str1[]=“hello”; char str1[]=“hello”;
char str2[10]; char *p=“good”;
char *s=“good”; str1=“bye”;/*error*/
char *q; p=“bye”;/* correct */
We cannot assign a string to
another
str2=str1;/* error */
Whereas ,we can assign a
char pointer to another
char pointer.
q=s; /* correct */
10 SECONDS 74
Limitations of Array of Pointers
and strings.
#include <stdio.h>
int main()
{char *names[6];
int i;
for(i=0;i<6;i++)
{ printf(“enter name\n”);
scanf(“%s”,names[i]);/* error */
}
return 0;}
solution to this problem dynamically allot memory
10 SECONDS 75
solution
#include <stdio.h> p=(char *)malloc(len+1);
#include <string.h> strcpy(p,n);
int main() names[i]=p;
{char *names[6]; }
char n[50]; for(i=0;i<6;i++)
int len,i; printf(“%s\n”,names[i]);
char *p; return 0;
for(i=0;i<6;i++) }
{printf(“Enter name\n”);
scanf(“%s”,n);
len=strlen(n);
10 SECONDS 76
Pointers and multidimensional array
int arr[3][5];
int (*ptr)[5];
ptr=arr;
10 SECONDS 77
Program to demonstrate pointers and multi
dimensional array
#include <stdio.h>
int main()
{ int arr[3][5]={{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15}};
int (*ptr)[5], i, j;
ptr=arr;
for(i=0;i<3;i++)
{ printf(“\n”);
for(j=0;j<5;j++)
printf(“%d “,(*ptr)[j]);
ptr++; /*moves pointer to first element of next row */
}}
10 SECONDS 78
Diagramitic -2 dim array----- int arr[3][5];
0 1 2 3 4
arr 0 3501 3503 3505 3507 3509
int (*ptr)[5];
ptr=arr; /*ptr will have address 3501 */
ptr++; or ptr+1; -> now address 3511
ptr+2 -> address will be 3521
10 SECONDS 79
Dynamic memory allocation- malloc()
int *arr;
arr=(int *)malloc (5*sizeof(int));
arr
The header file stdlib.h contains memeory
management functions
malloc(),calloc(),realloc(),free()
10 SECONDS 80
program allocates a required block using malloc()
displays addresses,reads and prints data.
10 SECONDS 83
contd
Prev output
Address of p
Address of a[0]
Content of a[0]...i.e 0
Note:
to print content of a[3],
*(*(p+3)) or *(p[3])
10 SECONDS 84
2 dim array, using array of pointers-malloc()
#include <stdio.h>
#include <stdlib.h> /* display */
int main()
for (i=0;i<m;i++)
{ int *arr[10];
int m,n,i,j,sum=0; for(j=0;j<n;j++)
printf(“Enter number of rows nd printf(“%d”,*(arr[i]+j));
columns\n”); }
scanf(“%d %d”,&m,&n);
for(i=0;i<m;i++)
arr[i]=(int *malloc(n*sizeof(int));
/* input */
for (i=0;i<m;i++)
for(j=0;j<n;j++)
scanf(“%d”,arr[i]+j);
10 SECONDS 85
Arrays of pointers to strings
#include <stdio.h>
int main() Output:
{ char *names[]={ Original:raman srinivas
New:srinivas raman
“akshay”,”parag”,”raman”,” In this program, the
srinivas”,”gopal”,”rajesh”}; addresses (of the
char *temp; names }stored in
array of pointers got
printf(“Original string: %s
exchanged.
%s\n”,names[2],names[3]);
temp=names[2];
names[2]=names[3];
names[3]=temp;
printf(“New %s
%s\n”,names[2],names[3]);
return 0;}
10 SECONDS 86
Storage Classes in C
If we don’t specify the storage class
of a variable in its declaration, the
compiler will assume a storage class
depending on the context in which
the variable is used.
Thus, variables have certain default
storage classes.
10 SECONDS 87
Storage Classes in C cont..
Locations in computer where value will be stored:
CPU registers and Memory.
Storage class tells following:
1. Where the variable would be stored.
2. What will be the initial value of the variable, if initial
value is not specifically assigned. (i.e. default initial
value)
3. What is the scope of the variable, i.e., in which
functions the value of the variable would be
available.
4. What is the life of the variable, i.e., how long would
variable exist.
10 SECONDS 88
Storage Classes in C cont..
There are four storage classes in C
Automatic storage class
Register storage class
Static storage class
External storage class
10 SECONDS 89
Automatic storage class
Storage: Memory
Default initial value: An unpredictable
value, which is often called a garbage
value.
Scope: Local to the block in which the
variable is defined.
Life: Till the control remains within
the block in which the variable is
defined.
10 SECONDS 90
Automatic storage class cont..
# include <stdio.h>
int main ( )
{
auto int i, j; // int i, j;
printf (“%d\n%d”, i, j);
return 0;
}
Output:
10 SECONDS 91
Automatic storage class cont..
# include <stdio.h>
int main ( )
{
auto int i = 1;
{
{
{
printf (“%d”, i);
}
printf (“%d”, i);
}
printf (“%d”, i);
}
return 0;
}
Output:
10 SECONDS 92
Automatic storage class cont..
# include <stdio.h>
int main ( )
{
auto int i = 1;
{
auto int i = 2;
{
auto int i = 3;
printf (“%d”, i);
}
printf (“%d”, i);
}
printf (“%d”, i);
return 0;
}
10 SECONDS 93
Automatic storage class cont..
Note:
In the above program, the compiler
treats the three i’s as totally different
variables, since they are defined in
different blocks.
10 SECONDS 94
Register storage class
Storage: CPU registers
Default initial value: Garbage value
Scope: Local to the block in which the
variable is defined.
Life: Till the control remains within
the block in which the variable is
defined.
10 SECONDS 95
Register storage class cont..
Accessing register is always fast compared to
accessing memory.
Ex:
# include <stdio.h>
int main ( )
{
register int i;
for (i = 1; i <= 10; i++)
printf (“%d\n”, i);
return 0;
}
10 SECONDS 96
Register storage class cont..
Even though we have declared the storage
class of i as register, we cannot say for sure
that the value of i would be stored in CPU
register.
Because the number of CPU registers are
limited, and they may be busy doing some
other task.
If registers are not available, the variable
works as if its storage class is auto.
10 SECONDS 97
void main () int add (int p, int q)
{ {
int a, b, c; int r;
int add (int, int); r = p + q;
a = 10; return r;
b = 20; }
c = add (a, b);
printf (“%d”, c);
}
10 SECONDS 98
Static Storage Class
Storage: Memory
Default Initial Value: Zero
Scope: Local to the block in which the
variable is defined
Life: Value of the variable persists
between different function calls.
10 SECONDS 99
Static Storage Class cont..
# include <stdio.h> # include <stdio.h>
void increment ( ); void increment ( );
void main ( ) void main ( )
{ {
increment ( ); increment ( );
increment ( ); increment ( );
increment ( ); increment ( );
} }
void increment ( ) void increment ( )
{ {
auto int i = 1; static int i = 1;
printf (“%d\n”, i); printf (“%d\n”, i);
i = i + 1; i = i + 1;
} }
10 SECONDS 100
a.C b.C c.c
int account; extern int account; static int account;
Void main () Void main () Void main ()
{ { {
account++; account++; account++;
} } }
int add () int fun1 ( )
{ {
account--; account--;
} }
10 SECONDS 101
Static Storage Class cont..
If the storage class is static, then the
statement static int i = 1 is executed
only once, irrespective of how many
times the same function is called.
10 SECONDS 102
a.c b.c c.c
10 SECONDS 103
a.C b.C c.c
10 SECONDS 104
External storage class
Storage: Memory
Default initial value: zero
Scope: Global
Life: As long as the program’s
execution does not come to an end.
10 SECONDS 105
External storage class cont..
External variables are declared
outside all functions, yet are available
to all functions.
10 SECONDS 106
External storage class cont..
# include <stdio.h> void increment ( )
int i; {
void increment ( ); int i = 5;
void decrement (); i = i + 1;
int main ( ) printf (“On incrementing
{ i = %d\n”, i);
printf (“i = %d\n”, i); }
increment ( ); void decrement ( )
increment ( ); {
decrement ( ); static int i = 3;
decrement ( ); i = i – 1;
return 0; printf (“On decrementing
} i = %d\n”, i);
10 SECONDS 107
External storage class cont..
# include <stdio.h>
int x = 21;
int main ( )
{
extern int y;
printf (“%d%d\n”, x, y);
return 0;
}
int y = 31;
10 SECONDS 108
External storage class cont..
extern int y;
int y = 31;
1. In the above program fragment first
statement is a declaration and second is
the definition.
2. When we declare a variable no space is
reserved for it, whereas, when we define it
space gets reserved in memory.
3. We had to declare y since it is being used
in printf ( ) before it is definition is
encountered.
10 SECONDS 109
External storage class cont..
#include <stdio.h>
int x = 10;
void display ( );
int main ( )
{
int x = 20;
extern int x;
printf (“%d\n”, x);
display ( );
return 0;
}
void display ( )
{
printf (“%d\n”, x);
}
10 SECONDS 110
Structures
# include <stdio.h> printf (“This is what you entered
int main ( ) \n”);
{ printf (“%c%f%d”, b1.name,
struct book b1.price, b1.pages);
{ printf (“%c%f%d”, b2.name,
b2.price, b2.pages);
char name; printf (“%c%f%d”, b3.name,
float price; b3.price, b3.pages);
int pages; return 0;
}; }
struct book b1, b2, b3;
printf (“Enter names, prices and
no of pages of 3 books\n”);
scanf (“%c%f%d”, &b1.name,
&b1.price, &b1.pages);
scanf (“%c%f%d”, &b2.name,
&b2.price, &b2.pages);
scanf (“%c%f%d”, &b3.name,
&b3.price, &b3.pages);
10 SECONDS 111
Structures cont..
struct book struct book
{ {
char name; char name;
float price; float price;
int pages; int pages;
}; }b1, b2, b3;
struct book b1, b2,
b3;
10 SECONDS 112
Structures cont..
struct
{
char name;
float price
int pages;
}b1, b2, b3;
10 SECONDS 113
Structures cont..
Initializing structures:
struct book
{
char name[10];
float price;
int pages;
};
struct book b1 = {“Basic”, 130.0, 550};
struct book b2 = {“Physics”, 150.80, 800};
struct book b3 = {0};
10 SECONDS 114
Functions
10 SECONDS 115
# include <stdio.h>
int main ( )
{
printf (“c to it that c survives \n”);
main ( );
return 0;
}
10 SECONDS 116
# include <stdio.h>
int i = 0;
void val ( );
int main ( )
{
printf (“main’s i = %d\n”, i);
i++;
val ( );
printf (“main’s i = %d\n”, i);
val ( );
return 0;
}
void val ( )
{
i = 100;
printf (“val’s i = %d\n”, i);
i++;
}
10 SECONDS 117
# include <stdio.h> int f (int a)
int f (int); {
int g (int); a += -5;
int main ( ) t -= 4;
{ return (a + t);
int x, y, s = 2; }
s*=3;
int g (int a)
y = f (s);
{
x = g (s);
a = 1;
printf (“%d%d%d”, s, y,
x); t += a;
return 0; return (a + t);
} }
int t = 8;
10 SECONDS 118
# include <stdio.h>
int main ( )
{
static int count = 5;
printf (“count = %d\n”, count--);
if (count != 0)
main ( );
return 0;
}
10 SECONDS 119
# include <stdio.h> int g (int x)
int g (int); {
int main ( ) static int v = 1;
{ int b = 3;
int i, j; v += x;
for (i = 1; i < 5; return (v + x +
i++) b);
{ }
j = g( i );
printf (“%d\n”,
j);
}
return 0; 10 SECONDS 120
# include <stdio.h> i++, j++, k++;
int main ( ) printf (“%d%d%d”,
{ i, j, k);
func ( ); }
func ( );
return 0;
}
void func ( )
{
auto int i = 0;
register int j = 0;
static int k = 0; 10 SECONDS 121
# include <stdio.h>
int x = 10;
int main ( )
{
int x = 20;
{
int x = 30;
printf (“%d”, x);
}
printf (“%d”, x);
return 0;
} 10 SECONDS 122
Unions
Union offers a way for a section of
memory to be treated as a variable of
one type on one occasion, and as a
different variable of a different type
on another occasion
10 SECONDS 123
# include <stdio.h>
int main ( )
{
union student;
{
int age;
char grade;
float per;
};
union student s1;
printf (“Enter grade of student \n”);
scanf (“%c”, &s1.grade);
printf (“Grade is: %c”,s1. grade);
printf (“Enter age of a student \n”);
scanf (“%d”, &s1.age);
printf (“Age is: %d”, s1.age);
printf (“Enter percentage of student \n”);
scanf (“%f”, &s1.per);
printf (“Percentage is %f\n”, s1.per);
} 10 SECONDS 124
Unions cont..
In the above program, at any point of
time, one can store age or grade or
percentage. At a time one cannot
store all the details. This is the major
difference between structure and
union.
10 SECONDS 125