NOTES OF C LANGUAGE
C
programing language
DEPARTMENT OF COMPUTER SCIENCE & INFORMATION TECHNOLOGY
for Computer Science & Information Technology
I am a Shubham Sharma student of the Information Technology Branch, and I prepared some notes related to computer programming, mathematics and, Database management system(DBMS).
This type of notes is beneficial for the semester exam for revision Theory.
Thanks.
This type of notes is beneficial for the semester exam for revision Theory.
Thanks.
s.no
|
Content list
|
Page no.
|
1.
|
History of C
|
4
|
2.
|
Variables and constants
|
5
|
3.
|
Data type operators
|
5-9
|
4.
|
Loop controls and array
|
10
|
5.
|
Function
|
11
|
6.
|
Pointer
|
12
|
7.
|
File handling
|
13
|
Brief
History of C Programming Language
C is a general-purpose programming language which features
economy of expression, modern control flow and data structures, and a rich set
of operators. C is not a "very high level" language, nor a
"big" one, and is not specialized to any particular area of
application. But its absence of restrictions and its generality make it more
convenient and effective for many tasks than supposedly more powerful
languages.
The history of C programming language is quite interesting. C
was originally designed for and implemented on the UNIX operating system on the
DEC PDP-ll, by Dennis Ritchie. C is the result of a development process that
started with an older language called BCPL. BCPL was developed by Martin
Richards, and it influenced a language called B, which was invented by Ken
Thompson. B led to the development of C in the 1970s.
For many years, the de facto standard for C was the version
supplied with the UNIX operating system. In the summer of 1983 a committee was
established to create an ANSI (American National Standards Institute) standard
that would define the C language. The standardization process took six years
(much longer than anyone reasonably expected).
The ANSI C standard was finally adopted in December 1989
Constants and Variables in C
language
CONSTANT
As the name suggests the name constants are given
to such variables or values in C programming language which cannot be modified
once they are defined. They are fixed values in a program. There can be any
types of constants like integer, float, octal, hexadecimal, character constants-etc. Every constant has some range. The integers that are too big to fit into
an int will be taken as along. Now there are various ranges that differ from
unsigned to signed bits. Under the signed bit, the range of an int varies from
-128 to +127 and under the unsigned bit, int varies from 0 to 255.
IDENTIFIER
The Name of the variable of any data type is called an identifier.
e.g. int x (here x is identifier).
Variables in c
In programming, a variable is a container (storage area) to
hold data.
To
indicate the storage area, each variable should be given a unique name (identifier). Variable names
are just the symbolic representation of a memory location
Data types in C Language
Data types specify how we enter
data into our programs and what type of data we enter. C language has some
predefined set of data types to handle various kinds of data that we can use in
our program. These data types have different storage capacities.
C language supports 2 different
type of data types:
•
Primary data
types:
These are fundamental data types in C namely integer(int), floating-,point(float), character(char) and void.
•
Derived data
types:
Derived data types are nothing but primary datatypes but a
little twisted or grouped together like an array, structure, union, and pointer. These are discussed in details
later.
The Data type determines the type of
data a variable will hold. If a variable x is declared as int. it means x can hold only integer values. Every variable
which is used in the program must be declared as what data-type it is.
Integer type
Integers are used to store whole
numbers.
Size
and range of Integer type on a 16-bit machine:
Type
|
Size(bytes)
|
Range
|
int
or signed int (%d)
|
2
|
-32,768
to 32767
|
unsigned
int (%l)
|
2
|
0
to 65535
|
short
int or signed short int
|
1
|
-128
to 127
|
unsigned
short int
|
1
|
0
to 255
|
long
int or signed long int
|
4
|
-2,147,483,648
to 2,147,483,647
|
unsigned
long int
|
4
|
0
to 4,294,967,295
|
size of data type like int, float, char, etc is machine-dependent.
In a 64-bit machine, the size of int is 4 bytes.,
Floating point type
In a 64-bit machine, the size of int is 4 bytes.,
Floating point type
Floating types are used to store
real numbers.
Size
and range of Integer type on a 16-bit machine
Type
|
Size(bytes)
|
Range
|
Float (%f)
|
4
|
3.4E-38
to 3.4E+38
|
Double
|
8
|
1.7E-308
to 1.7E+308
|
long
double
|
10
|
3.4E-4932
to 1.1E+4932
|
Character type
Character types are used to store
characters value.
Size
and range of Integer type on a16-bit machine
Type
|
Size(bytes)
|
Range
|
char
or signed char(%c)
|
1
|
-128
to 127
|
unsigned
char
|
1
|
0
to 255
|
void type
void type means no value. This is usually used to specify
the type of functions that return nothing. We will get acquainted with this
datatype as we start learning more advanced topics in C language, like
functions, pointers, etc.
C Programming Operators
C
Arithmetic Operators
An arithmetic operator performs mathematical operations such
as addition, subtraction, multiplication, division, etc on numerical values
(constants and variables).
C
Increment and Decrement Operators
C
programming has two operators increment ++ and
decrement -- to change the value of an operand
(constant or variable) by 1.
Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1. These two operators are unary
operators, meaning they only operate on a single operand.
C
Assignment Operators
The assignment operator is used for assigning a value to a variable. The most
common assignment operator is =
C
Relational Operators
A relational operator checks the relationship between two operands.
If the relation is true, it returns 1; if the relation is false, it returns
value 0.
C
Logical Operators
The expression containing logical operator returns either 0 or 1 depending upon
whether expression results true or false. Logical operators are commonly used
in decision making in
C programming.
Operator
|
Meaning
|
Example
|
&&
|
Logical
AND. True only if all operands are true
|
If
c = 5 and d = 2 then, expression ((c==5)
&& (d>5)) equals to 0.
|
||
|
Logical
OR. True only if either one operand is true
|
If
c = 5 and d = 2 then, expression ((c==5)
|| (d>5)) equals to 1.
|
!
|
Logical
NOT. True only if the operand is 0
|
C
Bitwise Operators
During computation, mathematical operations like addition,
subtraction, multiplication, division, etc are converted to bit-level which
makes processing faster and saves power.
Bitwise operators are used in C programming to perform
bit-level operations.
Operators
|
Meaning
of operators
|
&
|
Bitwise
AND
|
|
|
Bitwise
OR
|
^
|
Bitwise
exclusive OR
|
~
|
Bitwise
complement
|
<<
|
Shift
left
|
>>
|
Shift
right
|
TYPES OF LOOP CONTROL STATEMENTS
IN C:
There are 3
types of loop control statements in C language. They are,
• for• while• do-while
Syntax for
each C loop control statement is given in the below table with description.
Name
|
|
For
|
for (exp1; exp2; expr3)
{ statements; }Where, exp1 – variable initialization ( Example: i=0, j=2, k=3 ) exp2 – condition checking ( Example: i>5, j<3, k=3 ) exp3 – increment/decrement ( Example: ++i, j–, ++k ) |
While
|
while (condition)
{ statements; }where, condition might be a>5, i<10 |
do while
|
do { statements; }
while (condition);where, condition might be a>5, i<10 |
ARRAY-
Arrays
a kind of data structure that can store a fixed-size sequential collection of
elements of the same type. An array is used to store a collection of data, but
it is often more useful to think of an array as a collection of variables of
the same type.
Instead
of declaring individual variables, such as number0, number1, ..., and number99,
you declare one array variable such as numbers and use numbers[0], numbers[1],
and ..., numbers[99] to represent individual variables. A specific element in
an array is accessed by an index.
All
arrays consist of contiguous memory locations. The lowest address corresponds
to the first element and the highest address to the last element.
C - Functions
A function is a group of statements that together perform a task. Every C program
has at least one function, which is main(),
and all the most trivial programs can define additional functions.
You
can divide up your code into separate functions. How you divide up your code
among different functions is up to you, but logically the division is such that
each function performs a specific task.
A
function declaration tells
the compiler about a function's name, return type, and parameters. A
function definition provides
the actual body of the function.
The C
standard library provides numerous built-in functions that your program can
call. For example, strcat
What are the Pointers?
A pointer is a variable whose value
is the address of another variable, i.e., direct address of the memory
location. Like any variable or constant, you must declare a pointer before
using it to store any variable address. The general form of a pointer variable
declaration is −
type *var-name;
Here, type is the pointer's base type;
it must be a valid C data type and var-name is
the name of the pointer variable. The asterisk * used to declare a pointer is
the same asterisk used for multiplication. However, in this statement the
asterisk is being used to designate a variable as a pointer. Take a look at
some of the valid pointer declarations −
int
*ip; /* pointer to an integer
*/
double *dp; /* pointer to a double */
float
*fp; /* pointer to a float */
char
*ch /* pointer to a character
*/
The
actual data type of the value of all pointers, whether integer, float,
character, or otherwise, is the same, a long hexadecimal number that represents
a memory address. The only difference between pointers of different data types
is the data type of the variable or constant that the pointer points to.
() to concatenate two strings, memcpy() to copy one memory location to another location, and
many more functions.
A unction can also be referred to a method or a sub-routine or a procedure, etc.
Basics of File Handling in C
So far the operations using C program are done
on a prompt / terminal which is not stored anywhere. But in the software
industry, most of the programs are written to store the information fetched
from the program. One such way is to store the fetched information in a file.
Different operations that can be performed on a file are:
• Creation of a new file (fopen with attributes as
“a” or “a+” or “w” or “w++”)
•
Opening an
existing file (fopen)
•
Reading
from file (fscanf or fgetc)
•
Writing to
a file (fprintf or fputs)
•
Moving to
a specific location in a file (fseek,
rewind)
•
Closing a
file (fclose)
•
FILE
*filePointer;
•
So, the file
can be opened as
•
filePointer =
fopen(“fileName.txt”, “w”)
opening a file
FILE * filePointer;
filePointer =
fopen(“fileName.txt”, “r”);
fscanf(filePointer, "%s %s %s
%d", str1, str2, str3, &year);
writing a file
FILE *filePointer ;
filePointer =
fopen(“fileName.txt”, “w”);
fprintf(filePointer, "%s %s
%s %d", "We", "are", "in", 2012);
STRING
collection of characters is called a string.
There are flowing types of functions related to a string.
1. strlen()
This function is used to calculate the number of characters used in the string.
2. strlwr()
This function is used to convert the capital letter into a small letter.
3. strupr()
This function is used to convert the small letter into a capital letter.
4. strncat()
This function is used to the addition of two string.
5. strcpy()
This function is used to copy of one string into another string.
e.g. strncpy(B,A)
Here B string copy the string A i.e sting A will be given.
6. strcmp()
This function is used to compare the two strings. If both string the same then it gives zero(0) value, otherwise, it gives the difference between of the ASCII value.
Different meanings of the two same small letter string and capital letter string.
e.g. RAM and ram have a difference.
7. strcmpi()
This function is used to compare the two strings. If both string same then it gives zero(0) value, otherwise, it gives the difference between of ASCII value.
No Different meanings of the two same small letter string and capital letter string.
e.g. RAM and ram have the same meaning.
8. strrev()
This function is used to reverse the character.
STRING
collection of characters is called a string.
There are flowing types of functions related to a string.
1. strlen()
This function is used to calculate the number of characters used in the string.
2. strlwr()
This function is used to convert the capital letter into a small letter.
3. strupr()
This function is used to convert the small letter into a capital letter.
4. strncat()
This function is used to the addition of two string.
5. strcpy()
This function is used to copy of one string into another string.
e.g. strncpy(B,A)
Here B string copy the string A i.e sting A will be given.
6. strcmp()
This function is used to compare the two strings. If both string the same then it gives zero(0) value, otherwise, it gives the difference between of the ASCII value.
Different meanings of the two same small letter string and capital letter string.
e.g. RAM and ram have a difference.
7. strcmpi()
This function is used to compare the two strings. If both string same then it gives zero(0) value, otherwise, it gives the difference between of ASCII value.
No Different meanings of the two same small letter string and capital letter string.
e.g. RAM and ram have the same meaning.
8. strrev()
This function is used to reverse the character.
Very helpful content for beginners.....
ReplyDeleteThanks
DeleteThanks for this material that help me for quick revision of C and C++.
ReplyDelete