Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Material
-By
G.S.PRAVEENA
Page2
Index
3. Pre-processor 11
4. Arrays 12
5. String Functions 14
6. Structures 15
7. I/O Function 17
8. Files 19
Page2
C Language Material:
BASICS OF PROGRAMMING FUNDAMENTALS:
1. What are the important aspects (properties) of programming
language?
Four important aspects of any language are the way it stores data, the way it
operates upon this data, how it accomplishes input and output and how it lets
you control the sequence of execution of instructions in a program.
c. Coding g. Maintenance
d. Documenting the
program
6. What is algorithm?
Page2
An algorithm is a step-by-step description of the solution to a
problem. It is defined as an ordered sequence of well-defined
and effective operations which, when carried out for a given
set of initial conditions, produce output, and terminate in a
finite time. The term “ordered sequence” specifies, after the
completion of each step in the algorithm, the next step must
be unambiguously defined.
9. What is coding?
17. What is C?
C is a programming language developed at AT & T’s Bell
Laboratories of USA in 1972. It was designed and written by a
man named Dennis Ritchie. It is often know as middle level
language as it inherits both the features of high level and low
level language.
Characteristics of C Language
The increasing popularity of C is due to its various desirable
qualities:
*C language is well suited for structured modular
programming.
* C is a robust language with rich set of built-in functions and
operators.
* C is smaller which has minimal instruction set and programs
written in C are efficient and fast.
* C is highly portable (code written in one machine can be
moved to other).
* C is highly flexible.
* C allows access to the machine at bit level (Low level
(Bitwise) programming).
* C supports pointer implementation - extensive use of
pointers for memory, array, structures and functions.
24. Define the term “key word”. Why can’t we use key
word as variable’s name?
Operator Name
Association
_______________________________________________________________
_________
! – sizeof()
+- Additive operators.
Left to Right
== != Equality comparators
Left to Right
?: Conditional.
Right to Left
= op= Assignment.
Right to Left
, Comma Left
to Right
_______________________________________________________________
__________
Operators Type
! Logical NOT
*/% Arithmetic and modulus
+- Arithmetic
< > <= >= Relational
== != Relational
&& Logical AND
|| Logical OR
= Assignment
1. For
2. While
3. Do...while
If the break is not used in a case statement the control will not
come out of the switch and it goes executing the following
cases till it encounters a break otherwise it all the statements
in the switch.
Takes the control where ever you want, is done by using goto
statement. Better don’t use it.
4. What is the life of the variable; i.e. how long would the
variable exist.
Use register storage class for only those variables that are
being used very often in a program. A typical application of
register storage class is loop counters, which get used a
number of times in a program.
Page2
PRE PROCESSOR
61. What is a pre processor?
#include "goto.c" This command would look for the file goto.c in the
current directory as well as the specified list of
directories as mentioned in the include search path
that might have been set up.
#include <goto.c> This command would look for the file goto.c in the
specified list of directories only.
Void fun1 ( );
Void fun2 ( );
#pragma startup fun1
#pragma exit fun2
main ( )
{
printf (“\nInside maim”);
}
void fun1 ( )
{
printf (“\nInside fun1”);
}
void fun2 ( )
{
printf (“\nInside fun2”);
}
Output :
Inside fun1
Inside main
Inside fun2
ARRAYS
68. What is the need for going an array?
STRING FUCTIONS
79. What is a string?
Function Use
strlen Finds length of a string
strlwr Converts a string to lowercase
strupr Converts a string to uppercase
strcat Appends one string at the end of another
strncat Appends first n characters of a string at the end of
another
strcpy Copies a string into another
strncpy Copies first n characters of one string into another
strcmp Compares two strings
strncmp Compares first n characters of two strings
strcmpi Compares two strings without regard to case ("i"
denotes that this function ignores case)
stricmp Compares two strings without regard to case
(identical to strcmpi)
strnicmp Compares first n characters of two strings without
regard to case
strdup Duplicates a string
strchr Finds first occurrence of a given character in a string
strrchr Finds last occurrence of a given character in a string
strstr Finds first occurrence of a given string in another
string
strset Sets all characters of string to a given character
strnset Sets first n characters of a string to a given character
strrev Reverses string
Page2
STUCTURES
82. Why do we use structure?
Example 1:
Struct emp
Int empno;
Float sal;
};
Example 2:
Struct emp {
Int empno;
Float sal;
} a, b, c;
Page2
struct book
{
char name;
float price;
int pages;
};
struct book b1, b2, b3;
Is same as...
struct book
{
char name;
float price;
int pages;
} b1, b2, b3;
Or even...
struct {
char name;
float price;
int pages;
} b1, b2, b3;
Syntax is
Structure name. field name;
Page2
If structure has one of its members type as its own type then
it is called self referential structure.
INPUT/OUTPUT FUNCTION:
93. What are the different I/O functions available?
FILES
Page2
ch = fgetc ( fp ) ;
fgetc( ) reads the character from the current pointer position,
advances the pointer position so that it now points to the next
character, and returns the character that is read, which we
collected in the variable ch.
Operator Meaning
~ One’s complement
>> Right shift
<< Left shift
& Bitwise AND
Page2
| Bitwise OR
^ Bitwise XOR(Exclusive
OR)
Example:
Function Task
Malloc Allocates memory requests size of bytes and returns a
pointer to the Ist byte of allocated space
Calloc Allocates space for an array of elements initializes them to
zero and returns a pointer to the memory
Free Frees previously allocated space
Realloc Modifies the size of previously allocated
space.
ptr=(cast-type*)malloc(byte-size);
Example: x=(int*)malloc(100*sizeof(int));
120. How to allocate multiple blocks of memory?
ptr=(cast-type*) calloc(n,elem-size);
free(ptr);
ptr=realloc(ptr,newsize);
SORTING TECHNIQUES:
123. What is sorting algorithm?
Page2
A sorting algorithm is an algorithm that puts elements of a list
in a certain order.
Step-by-step example:
Let us take the array of numbers "5 1 4 2 8", and sort the
array from lowest number to greatest number using
bubble sort algorithm. In each step, elements written in
bold are being compared.
First Pass:
( 5 1 4 2 8 ) ( 1 5 4 2 8 ) Here, algorithm compares the
first two elements, and swaps them.
(15428) (14528)
(14528) (14258)
( 1 4 2 5 8 ) ( 1 4 2 5 8 ) Now, since these elements
Page2
64 25 12 22 11
Page2
11 25 12 22 64
11 12 25 22 64
11 12 22 25 64
for i ← 0 to n-2 do
min ← i
for j ← (i + 1) to n-1 do
if A[j] < A[min]
min ← j
swap A[i] and A[min]
insertionSort(array A)
for i = 1 to length[A]-1 do
begin
value = A[i]
j = i-1
while j >= 0 and A[j] > value do
begin
A[j + 1] = A[j]
j = j-1
end
A[j+1] = value
end
Example:
function mergesort(m)
var list left, right, result
if length(m) ≤ 1
return m
Once the data list has been made into a heap, the root
node is guaranteed to be the largest element. When it is
removed and placed at the end of the list, the heap is
rearranged so the largest element remaining moves to
the root. Using the heap, finding the next largest
element takes O(log n) time, instead of O(n) for a linear
scan as in simple selection sort. This allows Heap sort to
run in O(n log n) time.
SAMPLE PROGRAMS:
1. Factorial using recursive function.
#include<stdio.h>
Page2
#include<conio.h>
long double facto(int n)
if(n>0)
return(n*facto(n-1));
else
return(1);
void main()
int n;
clrscr();
printf("enter number\n");
scanf("%d",&n);
if(n==0||n==1)
printf("%d!=%d",n,1);
else
printf("%d!=%Lf",n,facto(n));
getch();
2. Reverse of a number.
#include<stdio.h>
#include<conio.h>
void main()
int sum=0,x,n;
clrscr();
scanf("%d",&n);
while(n>0)
x=n%10;
sum=sum*10 + x;
n=n/10;
printf("%d",sum);
getch();
3. Armstrong number
#include<stdio.h>
#include<conio.h>
void main()
int s,sum=0,n,a;
clrscr();
scanf("%d",&n);
a=n;
while(n!=0)
s=n%10;
sum=sum+s*s*s;
n=n/10;
if(sum==a)
Page2
getch();
4. Reverse of a string.
#include<stdio.h>
#include<conio.h>
void main()
char a[10];
int i,j,count=0;
clrscr();
gets(a);//scanf("%s",&a);
for(i=0;a[i]!=0;i++)
count++;
for(j=count;j>=0;j--)
printf("%c",a[j]);
getch();
mat3[i][j]=mat3[i][j]+(mat1[i][k]*mat2[k][j]);
6. Fibonacci series
#include<stdio.h>
#include<conio.h>
Page2
void main()
{
int f1=0,f2=1,f3,n,i=3;
clrscr();
scanf("%d",&n);
printf("%d\t%d",f1,f2);
while(i<=n)
f3=f1+f2;
printf("\t%d",f3);
f1=f2;
f2=f3;
i++;
getch();
#include<stdio.h>
#include<conio.h>
void main()
int n,i=1,count=0;
clrscr();
scanf("%d",&n);
Page2
while(n<=1)
{
scanf("%d",&n);
for(i=1;i<=n;i++)
if(n%i==0)
c=c+1;
if(c==2)
else
getch();
#include<stdio.h>
#include<conio.h>
void main()
int n,i=1,c=0,x,count=0;
clrscr();
scanf("%d",&x);
Page2
for(n=1;n<=x;n++)
{
for(i=1;i<=n;i++)
if(n%i==0)
c=c+1;
if(c==2)
count=count+1;
printf("\t%d",n);
c=0;
getch();
#include<stdio.h>
#include<conio.h>
void main()
int i,j,a[3][3];
clrscr();
for(i=0;i<=2;i++)
Page2
{
for(j=0;j<=2;j++)
scanf("%d",&a[i][j]);
for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
printf("\t%d",a[i][j]);
printf("\n");
getch();
#include<stdio.h>
#include<conio.h>
if (s[i]==s[j])
if(i>=j)
Page2
printf("\n Palindrome");
else
pal(i+1,j-1,s);
else
return;
void main()
char a[20];
int i,l=0;
clrscr();
printf("enter string");
scanf("%s",a);
for(i=0;a[i]!=0;i++)
l++;
pal(0,l-1,a);
getch();
#include<stdio.h>
#include<conio.h>
void main()
Page2
{
int a,b;
clrscr();
a=a+b;
b=a-b;
a=a-b;
getch();
Page2