c notes
c notes
Features of C
1) Simple
C is a simple language in the sense that it provides a structured approach (to break the problem into
parts), the rich set of library functions, data types, etc.
2) Machine Independent or Portable
Unlike assembly language, c programs can be executed on different machines with some machine
specific changes. Therefore, C is a machine independent language.
3) Mid-level programming language
Although, C is intended to do low-level programming. It is used to develop system applications such
as kernel, driver, etc. It also supports the features of a high-level language. That is why it is known as
mid-level language.
4) Structured programming language
C is a structured programming language in the sense that we can break the program into parts using
functions. So, it is easy to understand and modify. Functions also provide code reusability.
5) Rich Library
C provides a lot of inbuilt functions that make the development fast.
6) Memory Management
It supports the feature of dynamic memory allocation. In C language, we can free the allocated
memory at any time by calling the free() function.
7) Speed
The compilation and execution time of C language is fast since there are lesser inbuilt functions and
hence the lesser overhead.
8) Pointer
C provides the feature of pointers. We can directly interact with the memory by using the pointers.
We can use pointers for memory, structures, functions, array, etc.
9) Recursion
In C, we can call the function within the function. It provides code reusability for every function.
Recursion enables us to use the approach of backtracking.
10) Extensible
C language is extensible because it can easily adopt new features.
Structure Of C Program
1.Documentation Section
It provides information about the program that is being written. we give this information in the form
of comments which are non executable statements. By this information we can improve the
readablility of the program.
There are 2 types of comment lines
1. Single line comment: It is represented using double forward slash
eg:-// program for addition for two numbers
2. Multiple line comment: It is represented using forward slash followed by astrick and end
with astrick followed by forwardslash i.e “/*” and “*/”
eg:- /* StudyGlance A Fully Loaded Notebook
Best Site for B-Tech Students*/.
2. Preprocessor Section
All the header files of the program will be declared in the preprocessor section of the program.
Header files help us to access other’s improved code into our code. A copy of these multiple files is
inserted into our program before the process of compilation.
Example:
#include<stdio.h>
#include<math.h>
3. Definition
Preprocessors are the programs that process our source code before the process of compilation. The
#define preprocessor is used to create a constant throughout the program. Whenever this name is
encountered by the compiler, it is replaced by the actual piece of defined code.
Example:
#define pi 3.14
4. Global Declaration
The global declaration section contains global variables, function declaration, and static variables.
Variables and functions which are declared in this scope can be used anywhere in the program.
5. Main() Function
Every C program must have a main function. The main() function of the program is written in this
section. Operations like declaration and execution are performed inside the curly braces of the main
program.
6. Sub Programs
User-defined functions are called in this section of the program. The control of the program is shifted
to the called function whenever they are called from the main or outside the main() function. These
are specified as per the requirements of the programmer.
Tokens in C
Tokens are the smallest units in a program that have meaningful representations. Tokens are the
building blocks of a C program, and they are recognized by the C compiler to form valid
expressions and statements.
1.Keywords
Keywords are reserved words that have predefined meanings in C. These cannot be used as
identifiers (variable names, function names, etc.). Keywords define the structure and
behavior of the program C language supports 32 keywords some of them are: int, for, if, else,
while, return, etc.
continu defaul
auto break case char const do
e
doubl exter
else enum float for goto if
2.Strings
Strings are an array of characters ended with a null character (‘\0’). This null character indicates the
end of the string. Strings are always enclosed in double quotes. Whereas a character is
enclosed in single quotes
3. Operators
Operators are symbols that trigger an action when applied to C variables and other objects. The data
items on which operators act are called operands.
4.Identifiers
Identifiers are names given to variables, functions, arrays, and other user-defined items. They must
begin with a letter (a-z, A-Z) or an underscore (_) and can be followed by letters, digits (0-9),
and underscores.
Rules for Naming Identifiers in C
Identifier can contain following characters:
o Uppercase (A-Z) and lowercase (a-z) alphabets.
o Numeric digits (0-9).
o Underscore (_).
The first character of an identifier must be a letter or an underscore.
Identifiers are case-sensitive.
Identifiers cannot be keywords in C
5.Constants
Constants are fixed values used in a C program. These values do not change during the execution of
the program. Constants can be integers, floating-point numbers, characters, or strings.
C Variables
In C, variable is a name given to the memory location that helps us to store data.
Rules for Naming Variables in C
We can assign any name to the variable as long as it follows the following rules:
1. A variable name must only contain alphabets, digits, and underscore.
2. A variable name must start with an alphabet or an underscore only. It cannot start with a
digit.
3. No white space is allowed within the variable name.
4. A variable name must not be any reserved word or keyword.
Data Types in C
Data types are the type of data stored in a C program. Data types are used while defining a variable
or functions in C. It’s important for the compiler to understand the type of predefined data it
is going to encounter in the program.
Integer Data Type
The integer datatype in C is used to store the integer numbers (any number including positive,
negative and zero without decimal part). The integer data type is represented by the ‘int’
keyword, and it can be both signed or unsigned. The integer data type is further divided into
short, int, and long data types. The short data type takes 2 bytes; int takes 2 or 4 bytes, and
long takes 8 bytes in 64-bit and 4 bytes in the 32-bit operating system.
Float Data Type
The float data type is used to store the floating-point numbers. The numbers that have a fractional
part are called floating-point numbers. For example, 3.0, 5.57, -31.2, -3.12, etc are all
floating-point numbers. The range of a float in C is approximately 3.4E-38 to 3.4E+38.
Character Data Type
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.
Double Data Type
A Double data type in C is used to store decimal numbers (numbers with floating point values) with
double precision. It is used to define numeric values which hold numbers with decimal
values in C. The range of double is 1.7E-308 to 1.7E+308. It occupies 8bytes.
Void Data Type:
The data type void actually refers to an object that does not have a value of any type.
Operators
Operators are symbols that represent operations to be performed on one or more operands. The
values and variables used with operators are called operands. So we can say that the
operators are the symbols that perform operations on operands.
Types of Operators in C
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Other Operators
1. Arithmetic Operator: The arithmetic operators are used to perform arithmetic/mathematical
operations on operands.
S. No. Symbol Operator Description Syntax
Adds two
+ Plus a+b
1 numeric values.
Subtracts right
– Minus operand from left a–b
2 operand.
Multiply two
* Multiply a*b
3 numeric values.
Divide two
/ Divide a/b
4 numeric values.
remainder after
diving the left
operand with the
right operand.
#include <stdio.h>
int main()
{
int a = 25, b = 5;
printf("a + b = %d\n", a + b);
printf("a - b = %d\n", a - b);
printf("a * b = %d\n", a * b);
printf("a / b = %d\n", a / b);
printf("a % b = %d\n", a % b);
return 0;
}
Relational Operators
The relational operators in C are used for the comparison of the two operands. All these operators
are binary operators that return true or false values as the result of comparison.
S. No. Symbol Operator Description Syntax
Returns true if
the left operand
< Less than is less than the a<b
right operand.
1 Else false
Returns true if
the left operand
is greater than
> Greater than a>b
the right
operand. Else
2 false
Returns true if
the left operand
Less than or is less than or
<= a <= b
equal to equal to the right
operand. Else
3 false
equal to right
operand. Else
false
Returns true if
both the
== Equal to a == b
operands are
5 equal.
Returns true if
both the
!= Not equal to a != b
operands are
6 NOT equal.
Returns true if
both the
&& Logical AND a && b
operands are
1 true.
Returns true if
both or any of
|| Logical OR a || b
the operand is
2 true.
false.
right operand
and assign this
value to the left
operand.
Assign the
remainder in the
Modulus and division of left
%= a %= b
assign operand with the
right operand to
6 the left operand.
Performs bit-by-
bit AND
& Bitwise AND operation and a&b
returns the
1 result.
Performs bit-by-
bit OR operation
| Bitwise OR a|b
and returns the
2 result.
S. No. Symbol Operator Description Syntax
Performs bit-by-
bit XOR operation
^ Bitwise XOR a^b
and returns the
3 result.
#include <stdio.h>
int main()
{
int x = 5, y = 23,a=13,b=50;
a++;
printf("postfix increment a: %d\n", a);
++b;
printf("prefix increment b: %c\n", b);
--x;
Printf(“prefix decrement x:%d\n”, x);
y--;
printf(“post decrement y:%d\n”,y);
return 0;
}
Conditional or Ternary Operator (?:)
We use the ternary operator in C to run one code when the condition is true and another code when
the condition is false.
The syntax of ternary operator is:
testCondition ? expression1 : expression 2;
Example: C Ternary Operator
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
(age >= 18) ? printf("You can vote") : printf("You cannot vote");
return 0;
}
sizeof operator
The sizeof operator in C is used to determine the size, in bytes, of a variable or data type. sizeof can
be applied to any data type, including primitive types such as integer and floating-point types,
pointer types, or compound datatypes such as Structure, union, etc.
Syntax:
sizeof(Expression);
where ‘Expression‘ can be a data type or a variable of any type.
#include <stdio.h>
int main()
{
int a = 16;
printf("Size of variable a: %d \n",sizeof(a));
printf("Size of int data type: %d \n",sizeof(int));
printf("Size of char data type: %d \n",sizeof(char));
printf("Size of float data type: %d \n",sizeof(float));
printf("Size of double data type: %d \n",sizeof(double));
return 0;
}
S
Formatted I/O functions Unformatted I/O functions
No.
printf(), scanf, sprintf() and sscanf() are getch(), getche(), gets() and puts(), are
5
examples of these functions. some examples of these functions.