Professional Documents
Culture Documents
By
Chandrashekar Reddy.G
Chandrashekar.gajula@gmail.com
Chandrashekar.gajula@trusstechnosofts.com
Contact
+91406524833
+919985199433
Welcome
C
Introduction
Dennis Ritchie
1972 Traditional C
ISO Committee
1990 ANSI/ISO C
Benefits
Documentation Section
Link Section
Definition Section
Letters
Digits
Special Character
White Space
, .Comma & .Ampersand
. .Period ^ .Caret
; .Semicolon * .Asterisk
% .Percentage Sign . .
Keywords
auto double int struct
Keywords are words that
have special meaning to
break else long switch
the C compiler.
These keywords cannot be
case enum register typedef
used as identifiers in the
program
char extern return union
do if static while
Identifiers
Identifiers" are the names you supply for variables,
types, functions, and labels in your program.
Identifier names must differ in spelling and case from
any keywords.
You cannot use keywords as identifiers; they are
reserved for special use.
Rules:
Constants
Single
Integer Real Character
String
Data Types
This enables the Data Type Bytes Default Range
programmer to select the
appropriate data type as signed char 1 -128 to 127
per the need of the
application Unsigned char 1 0 to 255
* / % Multiplication/division/modulus left-to-right
+ - Addition/subtraction left-to-right
|| Logical OR left-to-right
Assignment right-to-left
Addition/subtraction assignment
= Multiplication/division
+= -= assignment
*= /= Modulus/bitwise AND
%= &= assignment
^= |= Bitwise exclusive/inclusive OR
<<= >>= assignment
Bitwise shift left/right
assignment
if-statements
if-else statement
else-if statement
Nested if statement
switch-statement
If-Construct
Note:
True Boolean values are any integer different
from zero.
False Boolean value is the integer zero.
If-else Construct
default: /* optional */
{DefaultStatement;break;}
}
Types
while-statement
do-while statement
for-statement
While Constructs
Syntax:
while(expr)
while (expr) {
Statement; statement1;
statement2;
}
Note:
True Boolean values are any integer different from zero;
False Boolean value is the integer zero.
Example for While Construct
i=0; #include<stdio.h>
while(i<10) int main()
{ {
printf(precision\n); int counter, howmuch;
i++; scanf("%d", &howmuch);
counter = 0;
}
while ( counter < howmuch)
{
counter++;
printf("%d\n", counter);
}
return 0;
}
Do-while Construct
Syntax:
do
do {
Statement; statement1;
while (expr); statement2;
}while(expr);
Note:
i=0; #include<stdio.h>
do int main()
{ {
printf(precision\n); int counter, howmuch;
scanf("%d", &howmuch);
i++; counter = 0;
} while(i<10); do
{
counter++;
printf("%d\n", counter);
}
while ( counter <
howmuch); return 0;
}
For Construct
Syntax:
for(expr1;expr2;expr3)
for(expr1; expr2; expr3) {
Statement; statement1;
statement2;
}
Semantic: equivalent to
expr1;
while (expr2)
{
Statement;
expr3;
}
Example for For Construct
#include<stdio.h> #include<stdio.h>
int main() int main()
{ {
int i; int i;
for (i = 0; i < 10; i++) for (i =20; i >10; i--)
{ {
printf ("Hello\n"); printf ("Hello\n");
printf ("World\n"); printf ("World\n");
} i--;
return 0; }
} return 0;
}
Looping Related Statements
Break
Continue
Break statement
Syntax:
break;
Semantic:
terminates the execution of a loop or a switch
Example for Break Statement
#include<stdio.h>
int main()
{
int i;
i = 0;
while ( i < 20 )
{
i++;
if ( i == 10)
break;
}
return 0;
}
Continue statement
Syntax:
continue;
Semantic:
terminates the current iteration of a loop
Example for Continue Statement
#include<stdio.h>
int main()
{
int i;
i = 0;
while ( i < 20 )
{
i++;
continue;
printf("Nothing to see\n");
}
return 0;
}
Unconditional jump statement
Syntax:
goto Label;
where Label:Statement;
belongs to the program
Semantic:
forces control to go to the Statement;
Example for goto Statement
#include<stdio.h>
int main()
{
Int i=0;
START: i++;
printf(%d\n,i);
if(a<=10)
{
goto START;
}
else
{
goto END;
}
END: return 0;
}
Array
One-Dimensional array
Two-Dimensional array
Multi-Dimensional array
One-Dimensional Array
collection of elements of same data type
that are stored contiguous in memory.
data
10 20 30 25 35 6 50 45 28 14
1000 1002 1004 1006 1008 1010 1012 1014 1016 1018
Address
One-Dimensional Array
The subscript, or the index of each array
element is determined based on the
number of offset positions it is from the
starting position. The starting offset is
taken as 0.
offset
0 1 2 3 4 5 6 7 8 9
10 20 30 25 35 6 50 45 28 14
1000 1002 1004 1006 1008 1010 1012 1014 1016 1018
One-Dimensional Array
Syntax:
datatype variablename [size];
Example:
int a[10];
block of 10 contiguous elements in memory.
offset
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
One-Dimensional Array
Initialization
Examples:
char array1[ ] = {A, R, R, A, Y, \0};
Example:
#include<stdio.h>
main( )
{
char array1[ ] = {A, R, R, A, Y, \0};
int i = 0;
}
One-Dimensional Array
Initialization
int a[5]={7,3,8,2,25};
(or)
int a[5];
a[0]=7;
a[1]=3;
a[2]=8;
a[3]=2;
a[4]=25;
Input To Array
int a[20];
scanf(%d,&a[0]);
--- 4 lines
---
scanf(%d,&a[19]);
Which one is best ?
20 lines
Array Manipulation Using Subscripts
/* this function finds the length of a character string */
#include <stdio.h>
main( )
{
int i = 0;
char string[11]; /* char array decalaration */
printf(Enter a string of maximum ten characters\n);
In the declaration:
char string[11];
the name of the array refers to the starting
address of the area that gets allocated for
storing the elements of the array.
string
100
100 101 102 103 104 105 106 107 108 109 110
a b c d e f g h i j \0
0 1 2 3 4 5 6 7 8 9 10
Array Addressing
To arrive at the address of the particular element, the compiler
applies the following simple formula:
#include <stdio.h>
int main()
{
int iMarks[4];
short newMarks[4];
iMarks[0]=78;
iMarks[1]=64;
iMarks[2]=66;
iMarks[3]=74;
for(i=0; i<4; i++)
newMarks[i]=iMarks[i];
Example:
int a[4][3];
0 1 2 Column offset
a[0][0] value
Address 0
1000
Of a[0][0]
1006 1 a[0][2]
1004
Row offset 2
value
3 ?
Two-Dimensional Array
Initialization
Example:
int array1[ 2 ][ 3 ] = { { 1, 2, 3 }, { 4, 5, 6 } };
int array2[ 2 ][ 3 ] = { 1, 2, 3, 4, 5 };
int array3[ 2 ][ 3 ] = { { 1, 2 }, { 4 } };
Example:
#include <stdio.h>
#include <conio.h>
void main()
{
char arr[3][12]= { "Rose", "India", "technologies" };
clrscr();
printf("Array of String is = %s,%s,%s\n", arr[0], arr[1], arr[2]);
getch();
}
Input To 2-D Array
Note:
In this 2-D array example we will get input row by row. That means get rows
value one by one using inner for loop. Increment row using outer loop.
Example:
main ( )
Two-Dimensional Array
{
int stud [4] [3];
int i, j;
for (i =0; i < =3; i ++)
{
for(j=0;j<=2;j++)
{
printf ("\n Enter roll no. and marks");
scanf ("%d%d", &stud [i] [j], &stud [i] [j] );
}
}
A function is declared as
return-value-type function-name( parameter-list )
{
declarations and statements
}
Function definition format (continued)
return-value-type function-name( parameter-list )
{
declarations and statements
}
message( )
{
printf ( "\nCan't imagine life without C" ) ;
main( ) ;
}
A function can be called any number of times.
For example,
main( )
{
message( ) ;
message( ) ;
}
message( )
{
printf ( "\n Hai Welcome to C Language !!" ) ;
}
Advantages of Functions
void main()
{
Printf(welcome to c);
message();
printf(for good learning c);
}
Void message()
{
printf(all the best);
}
Function with arguments and no return values
#include<stdio.h>
Void main()
{
int a,b,c;
a=10;b=20;
add(a,b);
}
Void add( int x,int y)
{
printf(%d,(x+y));
}
Function without arguments and return values
#include<stdio.h>
Void main()
{
Int c;
c= add();
Printf(%d,c);
}
int add()
{
int a,b,c;
a=10;b=20;
return(a+b);
}
Function with arguments and return values
#include<stdio.h>
Void main()
{
int a,b;
a=10;b=20;
c= add(a,b);
Printf(%d,c);
}
int add (int x, int y)
{
return(x+y);
}
Invoking functions
Function can be be invoked by using two ways
1.Call by value
2.call by reference
Call by value
If a function is invoked by passing values of actual
arguments to is corresponding formal arguments.
Example:
int main()
{
int result, number;
...
result = factorial( number );
}
int factorial( int num ) /* Function definition */
{
...
if ( ( num > 0 ) || ( num <= 10 ) )
return( num * factorial( num - 1 ) ); /*call itself */
}
Structures
A structure is a collection of one or more variables, possibly of different
types, grouped together under a single name for convenient handling.
struct empdata
{
int empno;
char name[10];
char job[10];
float salary;
};
Declaring a Structure Variable
struct empdata
{
int empno;
char name[10];
char job[10];
float salary;
} emprec; /* emprec is a variable of structure type empdata */
struct empdata
{
int empno;
char name[10];
char job[10];
float salary;
}
struct empdata emprec;
emprec.empno=101;
/* referring to the element of the structure variable emprec */
Structure Example
#include< stdio.h >
void main() printf(Enter the age of the student);
{ scanf(%d,&new student.age);
struct
{ printf(Student information\n);
int id_no; printf(student
char name[20]; id_number=%d\n,newstudent.id_no);
char address[20]; printf(student
int age;
}newstudent; name=%s\n,newstudent.name);
printf(student
printf(Enter the student Address=%s\n,newstudent.address);
information);
printf(Now Enter the student printf(Age of
id_no);
scanf(%d,&newstudent.id_no); student=%d\n,newstudent.age);
printf(Enter the name of the }
student);
scanf(%s,&new student.name);
printf(Enter the address of the
student);
scanf(%s,&new student.address);
Initializing of a structure
struct empdata
{
int empno;
char name[10];
char job[10];
float salary;
}emprec={101,Arun,Developer,20000};
Array of Structures
Just as it is possible to declare arrays of primitive data types, it should also be
possible to declare arrays of structures as well.
Consider the structure declaration for the employee details used earlier.
struct empdata
{
int empno;
char name[10];
char job[10];
float salary;
};
struct empdata employee_array[4];
Structure Example
#include< stdio.h >
{
struct info
{
int id_no;
char name[20];
char address[20];
char combination[3];
int age;
}
struct info std[100];
int I,n;
printf(Enter the number of students);
scanf(%d,&n);
printf( Enter Id_no,name address combination age\m);
for(I=0;I < n;I++)
scanf(%d%s%s%s%d,&std[I].id_no,std[I].name,std[I].address,std[I].combin
ation,&std[I].age);
printf(\n Student information);
for (I=0;I< n;I++)
printf(%d%s%s%s%d\n,
,std[I].id_no,std[I].name,std[I].address,std[I].combination,std[I].age);
}
Structure within Structure
A structure may be defined as a member of another structure. In such structures the
declaration of the embedded structure must appear before the declarations of other
structures.
struct date
{
int day;
int month;
int year;
};
struct student
{
int id_no;
char name[20];
char address[20];
char combination[3];
int age;
structure date def;
structure date doa;
}oldstudent, newstudent;
Structures & Files
Consider a situation where your application needs to read records from a file
for processing.
The general approach would be to read the various fields of a record into
corresponding memory variables.
But, this approach would involve manipulating the current file offset for the
relevant fields that need to be updated.
Writing Records On To a File
The second parameter specifies the size of the data to be read into
the structure variable.
Structures with pointers
The pointer variable also should be the variable for the structure data
type;
#include<stdio.h>
struct student
{
int rollno;
int marks;
}s={101,99},*p;
Accessing structure by pointer
#include<stdio.h>
struct student
{
int rollno;
int marks;
}s={101,99},*p;
main()
{
p=&s;
printf("%d",(*p).rollno);
printf("%d",(*p).marks);
}
Accessing structure by pointer
With pointers, though, in accessing structure members an arrow notation is
used, rather than the dot notation:
struct student
{
int rollno;
int marks;
}s={101,99},*p;
main()
{
p=&s;
printf("%d",p->rollno);
printf("%d",p->rollno);
getch();
}
Passing structure in a function
struct student{
int rollno;
int marks;
}s={101,99},*p;
main()
{
p=&s;
printf("%d",p->rollno);
printf("%d", p->marks);
f1(&s);
printf("%d", p-> rollno);
printf("%d", p-> marks);
}
f1(struct student *s1)
{
s1->rollno=103;
}
Pointers
Pointers is a memory variable that stores a memory
address
It always denoted by (*) asterisk symbol
Features
Pointers save the memory space.
Direct access to memory location
Useful for representing two-dimensional & Multi- dimensional
array
Declaration
int *x;
float *f;
Example for Pointer
# include <stdio.h>
# include <conio.h>
void main()
{
int v=10, *p;
clrscr();
p=&v;
printf( \n Address of V = %u ,p);
printf( \n Value of V = %d ,*p);
printf( \n Address of P = %u ,&p);
}
Output
Address of V = 4060
Value of V = 10
Address of P = 4062
Pointers
Pointers are variables that contain memory
addresses as their values.
datatype * variablename;
identifier
Indirection operator
Example:
Pointer variable
int *a;
int *a;
float *b;
char *c;
#include <stdio.h>
int main ( )
{
int *aptr ; /* Declare a pointer to an int
*/
float *bptr ; /* Declare a pointer to a
float */
int a =10; /* Declare an int variable */
float b =2.5f; /* Declare a float variable */
aptr = &a ;
bptr = &b ;
printf(%u,&a); aptr a
printf(%u,aptr); 1000 10
printf(%d,a);
printf(%d\n,*aptr); 1000
printf(%u,&b);
bptr b
printf(%u,bptr); 1500 2.5
printf(%f,b);
printf(%f,*bptr);
1500
return 0 ;
}
Output:
1000 1000 10 10
1500 1500 2.500000 2.500000
Use of & and *
When is * used?
Example:
#include< stdio.h >
main()
{
int *ptr1,*ptr2;
int a,b,x,y,z;
a=30;b=6;
ptr1=&a;
ptr2=&b;
x=*ptr1+ *ptr2 6;
y=6*- *ptr1/ *ptr2 +30;
printf(\nAddress of a +%u,ptr1);
printf(\nAddress of b %u,ptr2);
printf(\na=%d, b=%d,a,b);
printf(\nx=%d,y=%d,x,y);
ptr1=ptr1 + 70;
ptr2= ptr2;
printf(\na=%d, b=%d,a,b);
}
Pointer to arrays
a[0] or as int *a
because a[0] is an address and *a is also an address
the form of declaration is equivalent.
The difference is pointer is a variable and can
appear on the left of the assignment operator that
is lvalue.
The array name is constant and cannot appear as
the left side of assignment operator.
Pointer to arrays
/* A program to display the contents of array using pointer*/
main()
{
int a[100];
int i,j,n;
printf(\nEnter the elements of the array\n);
scanf(%d,&n);
printf(Enter the array elements);
for(I=0;I< n;I++)
scanf(%d,&a[I]);
printf(Array element are);
for(ptr=a,ptr< (a+n);ptr++)
printf(Value of a[%d]=%d stored at address %u,j+=,*ptr,ptr);
}
The statement:
FILE *fp1, *fp2 ;
declares that fptr1 and fptr2 are pointer variables
of type FILE. They will be assigned the address
of a file descriptor, that is, an area of memory
that will be associated with an input or output
stream.
Syntax:
FILE *fp;
fp=fopen(filename,mode);
The statement:
fp1 = fopen ( "mydata", "r" ) ;
would open the file mydata for input (reading).
Opening Files
The statement:
fp = fopen ("results", "w" ) ;
would open the file results for output (writing).
Once the files are open, they stay open until you
close them or end the program (which will close
all files.)
Testing for Successful Open
while((c=getc(f1))!=EOF)
printf(%c,c);
fclose(f1);
getw() & putw()
int a, b ;
FILE *fptr1, *fptr2 ;
fptr1 = fopen ( "mydata", "r" ) ;
fscanf ( fptr1, "%d%d", &a, &b) ;
int a = 5, b = 20 ;
FILE *fptr2 ;
fptr2 = fopen ( "results", "w" ) ;
fprintf ( fptr2, "%d %d\n", a, b ) ;
The statements:
fclose ( fptr1 ) ;
fclose ( fptr2 ) ;
#include <stdio.h>
int main ( )
{
FILE *outfile, *infile ;
int b = 5, f ;
float a = 13.72, c = 6.68, e, g ;
printf ("%6.2f%2d%5.2f\n", a, b, c) ;
printf ("%6.2f,%2d,%5.2f\n", e, f, g) ;
}
12345678901234567890
****************************
13.72 5 6.68
13.72, 5, 6.68
fseek()
Ftell();
FILE *stream;
long ftell (stream);
Rewind():
FILE *stream;
void rewind (stream);
Pre Processing
in
C
Pre Processing
Syntax
#Macro Template Macro Expansion
Types of Macros
Defining Macros
File Inclusion
Conditional Macros
Special cases
Defining Macros
#define <Label>(parameterlist)<Expansion>
main()
{
.
<Label>
}
Defining Macros
Example:
#include <stdio.h>
#define PI 3.14
#define AREA(radius) (PI*radius*radius)
main()
{
float areas[3]={AREA(1),AREA(2),AREA(3)};
int count;
for (count=0; count <3; count++) {
printf("Circle area=%f\n",areas[count]);
}
return 0;
}
File Inclusion
#include<file name>
(or) #include filename
main()
{
..
..
}
Conditional Macros
Syntax:
#ifdef MACRO
controlled text
#endif /* MACRO */
Syntax:
# if expression
controlled text
# endif /* expression */
Conditional Macros
Syntax:
#if expression
text-if-true
#else /* Not expression */
text-if-false
#endif /* Not expression */
Example for elif
Example:
#if X == 1
...
#elif X == 2
...
#else /* X != 2 and X != 1*/
...
#endif /* X != 2 and X != 1*/
Thank You.