Shortnotes c Programming Deva Sir
Shortnotes c Programming Deva Sir
CHARACTER SET IN C:
1) name should only consist of alphabets (both upper and lower case), digits and underscore (_)
sign.
2) The first character should be alphabet or underscore and should not be digit.
3) name should not be a keyword
4) since C is a case sensitive, the upper case and lower case considered differently
C PROGRAMMING SHORT NOTES By DEVA Sir
DATATYPES IN C:
There are two types of type qualifier in c: Size qualifier: short, long
Sign qualifier: signed, unsigned
C PROGRAMMING SHORT NOTES By DEVA Sir
• A variable is nothing but a name given to a storage area that our programs can
manipulate.
• Each variable in C has a specific type, which determines the size and layout of the
variable's memory.
• The range of values that can be stored within that memory and the set of operations that
can be applied to the variable.
TYPE CONVERSIONS:
Implicit Type Conversion:
There are certain cases in which data will get automatically converted from one type to
another:
• When data is being stored in a variable, if the data being stored does not match the type
of the variable.
• The data being stored will be converted to match the type of the storage variable.
• When an operation is being performed on data of two different types. The "smaller" data
type will be converted to match the "larger" type.
• The following example converts the value of x to a double precision value before
performing the division. Note that if the 3.0 were changed to a simple 3, then integer
division would be performed, losing any fractional values in the result.
average = x / 3.0;
• When data is passed to or returned from functions.
C PROGRAMMING SHORT NOTES By DEVA Sir
Explicit Type Conversion:
Data may also be expressly converted, using the typecast operator.
• The following example converts the value of x to a double precision value before
performing the division. ( y will then be implicitly promoted, following the guidelines
listed above)
average = ( double ) x / y;
• Note that x itself is unaffected by this conversion.
Expression:
lvalue:
• Expressions that refer to a memory location are called "lvalue" expressions.
• An lvalue may appear as either the left-hand or right-hand side of an assignment.
• Variables are lvalues and so they may appear on the left-hand side of an assignment
rvalue:
• The term rvalue refers to a data value that is stored at some address in memory.
• An rvalue is an expression that cannot have a value assigned to it which means an rvalue
may appear on the right-hand side but not on the left-hand side of an assignment.
• Numeric literals are rvalues and so they may not be assigned and cannot appear on the
left-hand side.
If Statement:
It takes an expression in parenthesis and a statement or block of statements. Expressions will be
assumed to be true, if evaluated values are non-zero.
Syntax of if Statement:
(i) if (condition) statement
(ii) if (condition){statement1statement2:}
C PROGRAMMING SHORT NOTES By DEVA Sir
If Else Statement:
Syntax of if else Statement:
if (condition){ statements }else{ statements }
The switch Statement:
The switch statement tests the value of a given variable (or expression) against a list of case
values and when a match is found, a block of statements associated with that case is executed:
Syntax of Switch Statement:
switch (control variable)
{
case constant-1: statement(s);
break;
case constant-2: statement(s);
break;
:
case constant-n: statement(s);
break;
default: statement(s);
}
LOOP CONTROL STRUCTURE:
while Loop:
Syntax of while Statement:
while(condition)
{
Statement 1;
Statement 2;
}
do while loop:
Syntax of do while Statement:
do
{
Statement;
}
while(condition);
C PROGRAMMING SHORT NOTES By DEVA Sir
There is minor difference between while and do while loop, while loop test the condition
before executing any of the statement of loop. Whereas do while loop test condition after
having executed the statement at least one within the loop.
For loop:
Syntax of for loop:
When break statement is encountered loop is terminated and control is transferred to the
statement, immediately after loop or situation where we want to jump out of the loop instantly
without waiting to get back to conditional state.
Continue statement is used for continuing next iteration of loop after skipping some statement
of loop. When it encountered control automatically passes through the beginning of the loop.It
is useful when we want to continue the program without executing any part of the program.
The difference between break and continue is, when the break encountered loop is
terminated and it transfer to the next statement and when continue is encounter control come
back to the beginning position.
C PROGRAMMING SHORT NOTES By DEVA Sir
ARRAYS:
array variable can store more than one value(of same type) at a time where other variable can
store one value at a time.
DECLARATION OF AN ARRAY :
Syntax :
Or we can say 2-d array is a collection of 1-D array placed one below the other.
Total no. of elements in 2-D array is calculated as row*column
STRING:
Array of characters is called a string if it is always terminated by the NULL character.
char name[]={‘j’,’o’,’h’,’n’,’\o’};
The terminating NULL is important because it is only the way that the function that work with
string can know, where string end.
C PROGRAMMING SHORT NOTES By DEVA Sir
SOME FUNCTIONS ON STRINGS:
strcmp()
This function is used to compare two strings. It compare the strings character by character and
the comparison stops when the end of the string is reached or the corresponding characters in
the two string are not same.
strcmp(s1,s2)
return a value:
• =0 when s1=s2
strcpy()
This function is used to copying one string to another string. The function strcpy(str1,str2)
copies str2 to str1 including the NULL character. Here str2 is the source string and str1 is the
destination string. The old content of the destination string str1 are lost. The function returns a
pointer to destination string str1.
strcat()
This function is used to append a copy of a string at the end of the other string. The NULL
character from str1 is moved and str2 is added at the end of str1. The 2nd string str2 remains
unaffected. A pointer to the first string str1 is returned by the function.
C PROGRAMMING SHORT NOTES By DEVA Sir
FUNCTIONS:
A function is a self contained block of codes or sub programs with a set of statements that
perform some specific task or coherent task when it is called.
Any ‘C’ program contain at least one function i.e main().
1. Library function
Note points:
• So every function in a program must be called directly or indirectly by the main()
function. A function can be called any number of times.
• A function can call itself again and again and this process is called recursion.
• A function can be called from other function but a function can’t be defined in another
function
There are two way through which we can pass the arguments to the function:
1. Call by value:
In the call by value copy of the actual argument is passed to the formal argument and the
operation is done on formal argument. When the function is called by ‘call by value’ method, it
doesn’t affect content of the actual argument.
2. Call by reference :
Instead of passing the value of variable, address or reference is passed and the function operate
on address of the variable rather than value. Here formal argument is alter to the actual
argument, it means formal arguments calls the actual arguments.
Local variable:-
variables that are defined with in a body of function or block. The local variables can be used
only in that function or block in which they are declared. Same variables may be used in
different functions
C PROGRAMMING SHORT NOTES By DEVA Sir
Global variable:-
The variables that are defined outside of the function is called global variable. All functions in
the program can access and modify global variables. Global variables are automatically
initialized at the time of initialization.
Static variables:
static variables are declared by writing the key word static.
syntax:-
static data type variable name;
static int a;
the static variables initialized only once and it retain between the function call. If its variable is
not initialized, then it is automatically initialized to zero.
Static variable can be local or global but it can not be external.
STORAGE CLASSES:
Storage class in c language is a specifier which tells the compiler where and how to store
variables, its initial value and scope of the variables in a program.
➢ Automatic
➢ Register
➢ Static
➢ External
C PROGRAMMING SHORT NOTES By DEVA Sir
i) Global variables are accessible across all function blocks, provided there is no local variable
with same name of global variables. If there is any name conflict, then local variable is given
more preference.
ii) If we use global variable anywhere before its definition then it will give compile time error,
to overcome this we use "extern" keyword.
C PROGRAMMING SHORT NOTES By DEVA Sir
ACTIVATION RECORD:
POINTERS:
A pointer is a variable that store memory address or that contains address of another variable
where addresses are the location number always contains whole number. It is called pointer
because it points to a particular location in memory by storing address of that location.
Syntax :
Data type *pointer name;
Here * before pointer indicate the compiler that variable declared as a pointer.
C PROGRAMMING SHORT NOTES By DEVA Sir
Two operators are used in the pointer :
i) address operator(&)
ii) indirection operator or dereference operator (*).
Indirection operator gives the values stored at a particular address.
Address operator cannot be used in any constant or any expression.
Pointer assignment
int i=10;
int *p=&i;//value assigning to the pointer
Here declaration tells the compiler that P will be used to store the address of integer value or in
other word P is a pointer to an integer and *p reads the value at the address contain in p.
We can assign constant 0 to a pointer of any type for that symbolic constant
‘NULL’ is used such as
*p=NULL;
It means pointer doesn’t point to any valid memory location.
NULL Pointers
Uninitialized pointers start out with random unknown values, just like any other variable type.
Accidentally using a pointer containing a random address is one of the most common errors
encountered when using pointers, and potentially one of the hardest to diagnose, since the errors
encountered are generally not repeatable.
Pointer Arithmetic
Pointer arithmetic is different from ordinary arithmetic and it is perform relative to the data
type(base type of a pointer).
1. Increment a pointer by constant is allowed
2. Decrement a pointer by constant is allowed
3. Two pointers can be subtracted
4. Division, Addition, Multiplication operations on pointers is not allowed
Example:-
If integer pointer contain address of 2000 on incrementing we get address of 2002 instead of
2001, because, size of the integer is of 2 bytes.
Note:-
When we move a pointer, somewhere else in memory by incrementing or decrement or adding
or subtracting integer, it is not necessary that, pointer still pointing to a variable of same data,
because, memory allocation to the variable are done by the compiler.
C PROGRAMMING SHORT NOTES By DEVA Sir
Constant pointer
A pointer is said to be constant pointer when the address its pointing to cannot be changed.
Syntax: <type-of-pointer> *const <name-of-pointer>
Example: int * const p;
C Pointer to Constant:
This type of pointer cannot change the value at the address pointed by it.
Syntax: const <type-of-pointer> *<name-of-pointer>;
Example: const int * p;
Pointer Comparison
Pointer variable can be compared when both variable, object of same data. Moreover pointer
variable are compared with zero which is usually expressed as null.
Pointer To Pointer
2-D ARRAY:
int X[5][5];
➢ X[i][j] =*(X+i)[j] = *(*(X+i)+j) =* (X[i] +j)
• *(*(X+i)+j) = value at row i, column j in 2D Array X.
• *(X+i)+j = address of X[i][j]
• X+I = Address of X[i]
• X = Name of array also called as address of Row 0
Array of Pointers:
An array of pointers can be declared as follows.
Declaration Syntax: <type> *<name>[<number-of-elements];
Example: int *p[5];
Combinations of * and ++
• *p++ accesses the thing pointed to by p and increments p
• (*p)++ accesses the thing pointed to by p and increments the thing pointed to by p
• *++p increments p first, and then accesses the thing pointed to by p
• ++*p accesses the thing pointed to by p and increments the thing pointed to by p.
C PROGRAMMING SHORT NOTES By DEVA Sir
STRUCTURES VS UNIONS:
MEMORY ALLOCATION:
These library function prototype are found in the header file, “alloc.h” where it has defined.
Function take memory from memory area is called heap and release when not required.
C PROGRAMMING SHORT NOTES By DEVA Sir
malloc():
• Its declaration is:
void*malloc(size);
• it returns the pointer to the 1st byte and allocate memory, and its return type is void,
which can be type cast such as: int *p=(datatype*)malloc(size)
• it returns null on unsuccessful.
• The memory allocated contains garbage value.
• The argument size specifies the number of bytes to be allocated.
calloc():
• calloc function use to allocate multiple block of memory.
• two arguments are there
1st argument specify number of blocks
2nd argument specify size of each block.
• Example:- int*p=(int *)calloc(5, size of (int));
• The memory allocated by calloc() is initialized to zero.
realloc():
• realloc use to change the size of the memory block without loosing the old data.
• It takes two argument such as;
int *ptr=(int *)malloc(size);
int*p=(int *)realloc(ptr, new size);
• If new size is larger than the old size, then old data is not lost and newly allocated bytes
are uninitialized.
• If old address is not sufficient then starting address then this reallocation function moves
content of old block into the new block and data on the old block is not lost.
free() :
• free() is used to release space allocated dynamically
• the memory released by free() is made available to heap again
• Syntax for free declaration .
void(*ptr) Or free(p)
C PROGRAMMING SHORT NOTES By DEVA Sir
SCOPE:
STATIC SCOPE:
➢ Referencing environment of a statement in a statically scoped language is the
collection of local variables and all other ancestor variables which are visible in the
statement.
➢ Static scope depends on syntax of program and scope of all variables known before
execution.
➢ Static scope in C also called as lexically scoped.
➢ C is statically scoped
DYNAMIC SCOPE:
➢ Referencing environment of a statement in a dynamically scoped language is the
collection of local variables and all other active subprograms variables which are visible
in the statement.
➢ Dynamic scope depends on calling sequence.
➢ Dynamic scoping is known during execution.
Thank you.
Mallesham Devasane (DEVA Sir)
Educator @GeeksForGeeks
Contact: 9911691155