Learning C
Learning C
- ABHISHEK DWIVEDI
Letters a, b, c, z
Digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Operator Meaning
+ Addition
* Multiplication
/ Division
Operator Meaning
> Greater than
>= Greater than or Equal to
< Less than
<= Less than or Equal to
== Equal to
!= Not equal to
ABHISHEK DWIVEDI 5 September 2015
Logical Operators
Logical Operators are symbols that are used to combine or negate
expressions containing relational operators.
C has three logical operators as defined below.
Operator Meaning
|| Logical OR
! Logical NOT
A string can also be initialized at the time of declaration in the following ways.
char name[ ] = Millennium ;
or
char name[ ] ={ M , i , l , l , n , n , i , u , m , \ };
Auto Local garbage within the function within the function until function is no Memory
or value where it is declared longer active
none
Register Local garbage within the function within the function until function is no CPU
value where it is declared longer active registers
Static Local Zero within the function within the function until program Memory
where it is declared ends, value of the
variable persists
between different
function calls
Extern Global Zero A heat of all All files including While any of these Memory
functions within a other files where files are active.
file declared extern That is, as long as
the programs
execution doesnt
come to an end
union item
{
int m;
float x;
char c;
}code;
ABHISHEK DWIVEDI 5 September 2015
SIZE OF STRUCTURES
We normally use structures, unions, and arrays to create variables of large
sizes. The actual size of these variables in terms of bytes may change
from machine to machine. We may use the unary operator sizeof to tell
us the size of a structure (or any variable).
The expression
sizeof(struct x)
will evaluate the number of bytes required to hold all the members of the
structure x. If y is a simple structure variable of type struct x, then the
expression
sizeof(y)
would also give the same answer.
#include<stdio.h>
#include<conio.h>
void main()
{
char a;
int x;
float p, q;
clrscr();
a= A;
x = 125;
p = 10.25, q = 18.76;
printf %c is stored at address %u , a, &a ;
printf \n%d is stored at address %u , x, &x ;
printf \n%f is stored at address %u , p, &p ;
printf \n%f is stored at address %u , q, &q ;
getch();
}
ABHISHEK DWIVEDI 5 September 2015
DECLARING POINTERS
In C, every variable must be declared for its For example,
type. Since pointer variables contain int *p;
addresses that belong to a separate data declares the variable p as a pointer
type, they must be declared as pointers variable that points to an integer data
before we use them. type.
The declaration of a pointer variable takes Remember that the type int refers to
the following form: the data type of the variable being
datatype *pt_name; pointed to by p and not the type of the
value of the pointer.
This tells the compiler three things about
the variable pt_name. Similarly, the statement
float *x;
The asterisk (*) tells the variable declares x as a pointer to a floating
pt_name is a pointer variable.
point variable.
pt_name needs a memory location.
pt_name points to a variable of type
datatype.
malloc Allocates requested size of bytes and returns a pointer to the first byte
of the allocated space.
calloc Allocates space for an array of elements, initializes them to zero and then
returns a pointer to the memory.
Program
We know that a string is an array of #include<stdio.h>
characters, terminated with a null #include<conio.h>
character. void main()
{
Like in one-dimensional arrays, we can char *str;
use a pointer to access the individual int i=0;
characters in a string. clrscr();
printf Enter a string: ;
gets(str);
while(*str!= \
{
i++;
str++;
}
printf \nThe length is:%d ,i);
getch();
}
ABHISHEK DWIVEDI 5 September 2015
POINTERS AND FUNCTIONS
Program : Pointers as function Parameters
#include<stdio.h>
In functions we can pass the duplicate #include<conio.h>
values for the actual parameters, but void main()
we can pass the address of a variable as {
an argument to a function in the int a,b;
normal fashion. clrscr();
printf Enter numbers: ;
When we pass addresses to a function, scanf %d %d ,&a,&b);
the parameters receiving the addresses printf \nBefore exchange: a=%d, b=%d ,a, b);
should be pointers. The process of swap(&a,&b);
calling a function using pointers to pass printf \nAfter exchange: a=%d, b=%d ,a, b);
the addresses of variable is known as getch();
}
call by address. void swap(int *x, int *y)
{
int z;
z = *x;
*x = *y;
*y = z;
}
ABHISHEK DWIVEDI 5 September 2015
POINTERS AND STRUCTURES
We know that the name of an array stands for Program: Pointers to Structure variables
the address of its zeroth element. The same #include<stdio.h>
thing is true of the names of arrays of structure #include<conio.h>
variables. struct invent
{
Suppose product is an array variable of struct char name[20] ;
type. The name product represents the int number;
address of its zeroth element. }product[3], *ptr;
Consider the following declaration: void main()
{
struct inventory clrscr();
{ printf )NPUT\n\n ;
printf Enter name and number records : ;
char name[30]; for(ptr = product;ptr<product+3;ptr++)
int number; scanf %s %d ,ptr->name,&ptr->number);
printf \n\nOUTPUT ;
} product[3], *ptr;
for(ptr = product;ptr<product+3;ptr++)
printf \n%s\t%d ,ptr->name,ptr->number);
getch();
}
The first statement declares the variable fp as a pointer to the data type
FILE . As stated earlier, FILE is a structure that is defined in the I/O library.
The second statement opens the file named filename and assigns as
identifier to the FILE the pointer fp. This pointer which contains all the
information about the file is subsequently used as a communication link
between the system and the program.
The second statement also specifies the purpose of opening this file. The
mode does this job. Mode can be one of the following:
r open the file for reading only.
w open the file for writing only.
a open the file for appending (or adding) data to it.
Note that both the filename and mode are specified as strings. They
should be enclosed in double quotation marks.
When trying to open a file, one of the Consider the following statements:
following things may happen: FILE *p1, *p2;
When the mode is writing a file with p1 = fopen data , r ;
the specified name is created if the file p2 = fopen results , w ;
does not exist. The contents are
deleted, if the file already exists. Many recent compilers include
additional modes of operation. They
When the purpose is appending , the
include:
file is opened with the current contents
r+ The existing file is opened to the
safe. A file with the specified name is
beginning for both reading and writing.
created if the file does not exist.
w+ Same as w except both for reading
)f the purpose is reading , and if it and writing
exists, then the file is opened with the a+ Same as a except both for reading
current contents safe; otherwise an and writing.
error occurs. We can open and use a number of files
at a time. This number however
depends on the system we use.
Once a file is opened, reading out of or writing to it is accomplished using the standard
I/O routines that are listed.
The getc and putc Functions
The simplest file I/O functions are getc and putc. These are analogous to getchar and
putchar functions and handle one character at a time. Assume that a file is opened
with mode w and file pointer fp1. Then, the statement
putc(c, fp1);
writes the character contained in the character variable c to the file associated with
FILE pointer fp1. Similarly, getc is used to read a character from a file that has been
opened in read mode. For example, the statement
c = getc(fp2);
would read a character from the file whose file pointer is fp2.
The file pointer moves by one character position for every operation of getc
or putc. The getc will return an end-of-file marker EOF, when end of the file has been
reached. Therefore, the reading should be terminated when EOF is encountered.