cnotes
cnotes
What is C language?
C Language is a general-purpose, programming language. Which was created by
Dennis Ritchie in 1972 at AT & T’S Bell Telephone Laboratories. Dennis Ritchie
wanted to create an operating system named Unix Operating System. to develop
it, the C language was developed.
The special thing is that we can do low-level programming with the help of C
language, due to this feature, C programming language is used to make system
software such as Operating system, Device Driver, Compiler, etc.
Features of C Language
1. C is a simple and easy programming language.
2. There are commands/instructions like English in C language which are very
easy for a programmer to read, understand, and code.
3. C is a Procedure Oriented Programming Language.
4. C is a very powerful and case sensitive programming language.
5. C Language is a Compiler based dynamic programming language.
6. C language is a middle level language due to which both low level and high
level programming can be done.
7. The C language is the most commonly used programming language in
developing operating systems and embedded systems.
8. C language is a very portable and powerful programming language.
9. C language is a Syntax Based Language.
10. C is a general purpose programming language that covers basic
features of all other languages.
Robust libraries and functions in C help even a beginner coder to code with
ease.
2. Middle-Level Language
As it is a middle-level language so it has the combined form of both capabilities
of assembly language and features of the high-level language.
3. Portability
C language is lavishly portable as programs that are written in C language can
run and compile on any system with either no or small changes.
4. Easy to Extend
Programs written in C language can be extended means when a program is
already written in it then some more features and operations can be added to it.
5.Rich set of built-in Operators
It is a diversified language with a rich set of built-in operators which are used in
writing complex or simplified C programs.
Why is C so important?
If C language is your first programming language, then believe me you will
understand programming quite well because C language Is very helpful in
building our programming skills, as well as C language teaches us to understand
the logic.
Applications of C Language
1. You can make a good operating system using C Language like- Windows,
Linux, Mac. because the major part of this operating system is written in C
language.
2. With the help of C Language you can create a Compiler which converts
programming language code into machine language.
3. With this, you can create a Text Editor.
4. Utility Software can be created.
5. Can create software with database management like – Oracle, Mysql, etc.
DR. VIDYA PAWAR, VEDANTA DEGREE COLLEGE 2
C Programming
Definition Section
The definition of Symbolic Constant is defined in this section, so this section is
called Definition Section. Macros are used in this section.
Example -:
#define PI 3.14
In the Global Declaration section, we also declare functions that we want to use
anywhere in our program, and such functions are called Global Function.
Example -:
int area (int x); //global function
int n; // global Variable
C Character set
As every language contains a set of characters used to construct words,
statements, etc., C language also has a set of characters which
include alphabets, digits, and special symbols. C language supports a total of
256 characters.
TOKENS
Every C program is a collection of instructions and every instruction is a collection
of some individual units. Every smallest individual unit of a c program is called
token. Every instruction in a c program is a collection of tokens. Tokens are used
to construct c programs and they are said to the basic building blocks of a c
program.
Keywords are special words in C programming which have their own predefined
meaning.
Identifiers
Identifiers are user-defined names of variables, functions and arrays. It comprises
of combination of letters and digits. In C Programming, while declaring identifiers,
certain rules have to be followed viz.
1. It must begin with an alphabet or an underscore and not digits.
2. It must contain only alphabets, digits or underscore.
3. A keyword cannot be used as an identifier
4. Must not contain white space.
5. Only first 31 characters are significant.
Let us again consider an example
int age1;
float height_in_feet; Here, age1 is an identifier of integer data type
Constant in C
Constant in C is defined as the value which cannot be modified during the
execution of the program. The constants in C are used for representing the fixed
values which do not change.
Integer Constants
An integer constant is a sequence of digits from 0 to 9 without decimal points
or fractional part or any other symbols. There are 3 types of integers namely
decimal integer, octal integers and hexadecimal integer.
RealConstants
Real Constants consists of a fractional part in their representation. Integer
constants are inadequate to represent quantities that vary continuously. These
quantities are represented by numbers containing fractional parts like
26.082. Example of real constants are
float x = 6.3; //here 6.3 is a double constant.
float y = 6.3f
SingleCharacterConstants
A Single Character constant represent a single character which is enclosed in a
pair of quotation symbols.
Example for character constants are
char y ='u';
String Constants
A string constant is a set of characters enclosed in double quotation marks. The
characters in a string constant sequence may be a alphabet, number, special
character and blank space. Example of string constants are
1 "VISHAL" "1234" "God Bless""!.....?"
DECLARING A CONSTANT
there are two ways to declare this constant
1.By using the const keyword in a variable declaration which will reserve a
storage memory
#include <stdio.h>
void main()
{
const double PI = 3.14;
printf("%f", PI);
/*PI++; This will generate an error as constants
cannot be changed */
}
2. By using the #define pre-processor directive which doesn't use memory for
storage and without putting a semicolon character at the end of that statement
#include <stdio.h>
#define PI 3.14
void main()
{
printf("%f", PI);
}
C Variables
OR
data_type variable_name = value;
where,
type = Data type of the variable
identifier = Variable name
value = Data to be stored in the variable (Optional field)
int a, b, c; // declare 3 variables.
Data Type is the classification of the data that is taken as input, processed,
and results in an output
Each variable in C has an associated data type. It specifies the type of data that
the variable can store like integer, character, floating, double, etc
Character data type allows its variable to store only a single character. The size
of the character is 1 byte. It is the most basic data type in C. It stores a single
character and requires a single byte of memory in almost all compilers.
Range: (-128 to 127) or (0 to 255)
Size: 1 byte, Format Specifier: %c
char a = 'a';
Syntax of char char
var_name;
The char keyword is used to declare the variable of character type:
Format Specifiers
To print values of different data types using the printf() statement and while
taking input using the scanf() function, it is mandatory to use format specifiers.
It is a way to tell the compiler what type of data is in a variable.
Some examples are %c, %d, %f, etc.
Example
printf("%d", a + b);
Rules for declaring formatted input function
2. Control string should be enclosed within double quotation marks
3. Every input variable should have individual character group
Unformatted I/O functions are used only for character data type or character
array/string and cannot be used for any other datatype. These functions are
used to read single input from the user at the console and it allows to display
the value at the console.
Operators in C
C OPERATORS AND EXPRESSION
The operators are types of symbols that inform a compiler for performing some
specific logical or mathematical functions
Increment Operator in C
The increment operator ( ++ ) is used to increment the value of a variable in
an expression by 1. It can be used on variables of the numeric type such as
integer, float, character, pointers, etc.
2. Post-Increment
In post-increment, the increment operator is used as the suffix of the operand.
The increment operation is performed after all the other operations are done. It
is also known as postfix increment.
Example
result = var1++;
The above expression is equivalent
result = var;
var = var + 1;
Decrement Operator in C
The decrement operator is used to decrement the value of a variable in an
expression. In the Pre-Decrement, the value is first decremented and then used
inside the expression. Whereas in the Post-Decrement, the value is first used
inside the expression and then decremented.
Syntax
Just like the increment operator, the decrement operator can also be used in two
ways:
// AS PREFIX
--m
// AS POSTFIX
m--
where m is variable.
DR. VIDYA PAWAR, VEDANTA DEGREE COLLEGE 17
C Programming
1. Pre-Decrement Operator
The pre-decrement operator decreases the value of the variable immediately
when encountered. It is also known as prefix decrement as the decrement
operator is used as the prefix of the operand.
Example
result = --m;
2. Post-Decrement Operator
The post-decrement happens when the decrement operator is used as the suffix
of the variable. In this case, the decrement operation is performed after all the
other operators are evaluated.
Example
result = m--;
EG :int a = 5; int b = 5;
int prefix = --a; int postfix = b--;
Ans: Prefix = 4 Postfix = 5
Operator
* Dereference Operator
Operator
12 || Logical OR Left-to-Right
= Assignment
14
Assignment Operator (= )
Use the assignment operator (=) to copy a constant, literal, variable expression
result, or function result to a variable. The script language does not support
multiple assignments in a single statement (such as a=b=c=0). String lengths
are defined based on the size of the string assigned to the variable and can
change dynamically at runtime.
Symb
Operation Example Description
ol
AND True if both expr1 and expr2 are
AND Expr1 $$ expr2
&& true.
OR || OR Expr1 OR expr2 True if either expr1 or expr2 are true.
Table 2. Logical Operators
Relational Operators
Relational operators are as follows:
SymbolOperation Example Description
< Less than a<b True if a is less than b.
> Greater than a GT b True if a is greater than b.
== Equal a == b True if a is equal to b.
!= Not equal a NE b True if a is not equal to b.
Less than or
<= a <= b True if a is less than or equal to b.
equal
>= Greater than ora GE b True if a is greater than or equal to b.
Control Statements
Flow Chart:
If-else Statement
In some situations, you may have to execute statements based on true or false
under certain conditions, therefore; you use if-else statements. If the condition is
true, then if block will be executed otherwise the else block is executed.
Syntax of the if-else statement is as given below:
Flow Chart:
Flow Chart:
Flow Chart:
Flow Chart:
Goto Statements in C
The goto statements are used to transfer the flow of control in a program, goto
statement is also known as a jump control statement because it is used to jump
to the specified part of the program. The label in goto statement is a name used
to direct the branch to a specified point in the program.
Syntax of the goto statement is as given below:
After goto statements in C, you are now heading towards the loop control
statements in C.
Loop Control Statements in C
While Loop
A while loop is also known as an entry loop because in a while loop the condition
is tested first then the statements underbody of the while loop will be executed.
If the while loop condition is false for the first time itself then the statements
under the while loop will not be executed even once.
The syntax of the while loop is as given below:
Flow Chart:
do-while Loop
The do-while is also known as an exit loop because in the do-while loop, the
statements will be executed first and then the condition is checked.
If the condition of the while loop is true then the body of the loop will be executed
again and again until the condition is false. Once the condition is false, the control
will transfer outside the do-while loop and execute statements followed soon after
the do-while loop.
The syntax of the do-while loop is as given below:
Flow Chart:
Example:
For Loop
The for loop is also known as a pre-test loop. From the following syntax,
expression1 is an initialization, expression2 is the conditional expression and
expression3 is an updation. The variables can be initialized in for the statement
itself.
The syntax of the do-while loop is as given below:
ARRAYS
An array is a collection of data items, all of the same type, accessed using a
common name.
Declaring Arrays
Array variables are declared identically to variables of their data type, except that
the variable name is followed by one pair of square [ ] brackets for each
dimension of the array.
Array indexes start with 0: [0] is the first element. [1] is the second element, etc.
This statement accesses the value of the first element [0] in myNumbers:
1D array
Syntax for Declaration of Single Dimensional Array
Below is the syntax to declare the single-dimensional array
data_type array_name[array_size];
2D array
A 2D array is also known as a matrix (a table of rows and columns).
For Example
int nums[5][10];
3. Three-dimensional array:
3D array
Syntax for Declaration of Three-Dimensional Array
Below is the syntax to declare the Three-dimensional array
data_type array_name[sizeof_1st_dimension][sizeof_2nd_dimension]
[sizeof_3rd_dimension];
where,
data_type: is a type of data of each array block.
array_name: is the name of the array using which we can refer to it.
sizeof_dimension: is the number of blocks of memory array going to have in
the corresponding dimension.
For Example
int nums[5][10][2];
STRINGS
The fgets function reads a line of input from any standard input stream and saves
it in a character array. Here’s its syntax.
scanf("%s",
str);
Writing C Strings
If you want to write or output C strings, you can use the printf function or any
other output functions such as fputs or puts.
Syntax of printf function:
printf("String: %s\n",
str);
String Handling Functions in C
strupr(): It takes a string as input and converts all the letters in the string to
uppercase.
strupr(string1) Converts all the characters
of string1 to upper case.
Syntax of strupr():
strlwr(): It takes a string as input and converts all the letters in the string to
lowercase.
Syntax of strlwr():
Strlwr(string); eg strlwr(‘RICH’); Output is rich
() string2) same;
less than 0 if string1<string2; greater
than 0 if string1>string2
Note:
if Return value if < 0 then it indicates string1 is less than string2
if Return value if > 0 then it indicates string2 is less than string1
if Return value if = 0 then it indicates string1 is equal to string2
What is a Structure in C?
A Structure is a user-defined data type in C/C++ that is used to store similar,
different data types or a combination of both under a single variable. .
Or
Structures (also called structs) are a way to group several related variables into
one place. Each variable in the structure is known as a member of the structure.
What Is a Union?
A Union is a type of data that is user-defined. It is just like the structure. The
Union combines various objects of different sorts and sizes together. A user can
define a Union using many members, but only one of them holds a value at any
given time. It provides you with an efficient way of using a single memory location
for various purposes. Thus, varying objects can share a similar location. Syntax
of Declaring a Union
union [union name] Example of Union
{ union Student {
type member_1; char name[25];
type member_2; int age;
Pointers
A pointer is a variable that contains the address of a variable. This variable can be
of type int, char, array, function, or any other pointer.