0% found this document useful (0 votes)
0 views

c notes

C is a mid-level programming language created by Dennis Ritchie in the early 1970s, known for its simplicity, portability, and structured programming features. It supports dynamic memory management, has a rich library of functions, and allows for direct memory manipulation through pointers. The document outlines the structure of a C program, types of tokens, data types, operators, and rules for naming identifiers and variables.

Uploaded by

Lavanya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

c notes

C is a mid-level programming language created by Dennis Ritchie in the early 1970s, known for its simplicity, portability, and structured programming features. It supports dynamic memory management, has a rich library of functions, and allows for direct memory manipulation through pointers. The document outlines the structure of a C program, types of tokens, data types, operators, and rules for naming identifiers and variables.

Uploaded by

Lavanya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Dennis Ritchie created C at Bell Laboratories in the early 1970s.

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

registe retur stati


int long short signed sizeof

switc typede unsigne volatil whil


struct union void
d

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.

5 % Modulus Returns the a%b


S. No. Symbol Operator Description Syntax

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

4 >= Greater than or Returns true if a >= b


equal to the left operand
is greater than or
S. No. Symbol Operator Description Syntax

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.

Example of C Relational Operators


#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);
printf("a != b : %d\n", a != b);
return 0;
}
Logical Operator in C
Logical Operators are used to combine two or more conditions/constraints or to complement the
evaluation of the original condition in consideration. The result of the operation of a logical operator
is a Boolean value either true or false.
S. No. Symbol Operator Description Syntax

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.

3 ! Logical NOT Returns true if !a


the operand is
S. No. Symbol Operator Description Syntax

false.

Example of Logical Operators in C


#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: %d\n", !a);
return 0;
}
Assignment Operator
Assignment operators are used to assign value to a variable. The left side operand of the assignment
operator is a variable and the right side operand of the assignment operator is a value.
S. No. Symbol Operator Description Syntax

Assign the value


Simple of the right
= a=b
Assignment operand to the
1 left operand.

Add the right


operand and left
operand and
+= Plus and assign a += b
assign this value
to the left
2 operand.

Subtract the right


operand and left
operand and
-= Minus and assign a -= b
assign this value
to the left
3 operand.

Multiply the right


operand and left
Multiply and operand and
*= a *= b
assign assign this value
to the left
4 operand.

5 /= Divide and assign Divide the left a /= b


operand with the
S. No. Symbol Operator Description Syntax

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.

Example of C Assignment Operators


#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);
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;
}
Bitwise Operator
The Bitwise operators are used to perform bit-level operations on the operands. The operators are
first converted to bit-level and then the calculation is performed on the operands. Mathematical
operations such as addition, subtraction, multiplication, etc. can be performed at the bit level for
faster processing.
S. No. Symbol Operator Description Syntax

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.

Example of Bitwise Operators


#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);
return 0;
}
Increment and Decrement Operators
The increment ( ++ ) and decrement ( — ) operators in C are unary operators for incrementing and
decrementing the numeric values by 1 respectively.
Types of Increment Operator
There are two types of increment operators – pre increment and post increment.
Pre (Prefix) Increment Operator
In an expression, the pre-increment operator increases the value of a variable by 1 before the use of
the value of the variable.
Syntax: ++variable_name;
Post (Postfix) Increment Operator
In an expression, the post-increment operator increases the value of a variable by 1 after the use of
the value of the variable.
Syntax: variable_name++;
Types of Decrement Operator
There are two types of decrement operators – pre decrement and post decrement.
Pre (Prefix) decrement Operator
In an expression, the pre-decrement operator decreases the value of a variable by 1 before the use
of the value of the variable.
Syntax: --variable_name;
Post (Postfix) Decrement Operator
In an expression, the post-decrement operator decreases the value of a variable by 1 after the use of
the value of the variable.
Syntax: variable_name--;

#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;
}

Formatted I/O functions:


Formatted I/O functions are used to take various inputs from the user and display multiple outputs
to the user. These types of I/O functions can help to display the output to the user in different
formats using the format specifiers. These functions are called formatted I/O functions because we
can use format specifiers in these functions
The following formatted I/O functions
1. printf()
2. scanf()
printf(): printf() function is used in a C program to display any value like float, integer, character,
string, etc on the console screen. It is a pre-defined function that is already declared in the
stdio.h(header file).
Syntax 1: printf(“Format Specifier”, var1, var2, …., varn);
scanf():
scanf() function is used in the C program for reading or taking any value from the keyboard by the
user, these values can be of any data type like integer, float, character, string, and many more. This
function is declared in stdio.h(header file), that’s why it is also a pre-defined function. In scanf()
function we use &(address-of operator) which is used to store the variable value on the memory
location of that variable.
Syntax: scanf(“Format Specifier”, &var1, &var2, …., &varn);

Unformatted Input/Output functions


The unformatted input functions in the C programming language are used only for character data
type or character string/array and cannot be used for any other datatype. These functions are used
to read a single input from the user and this allows the user to display the value at the console.
These functions are called unformatted I/O functions because we cannot use format specifiers in
these functions. The following unformatted I/O functions
1. getch()
2. getche()
3. getchar()
4. putchar()
5. gets()
6. puts()
7. putch()
getch(): getch() function reads a single character from the keyboard by the user but doesn’t display
that character on the console screen and immediately returned without pressing enter key.
getche(): getche() function reads a single character from the keyboard by the user and displays it on
the console screen and immediately returns without pressing the enter key.
getchar(): The getchar() function is used to read only a first single character from the keyboard
whether multiple characters is typed by the user and this function reads one character at one time
until and unless the enter key is pressed.
putchar(): The putchar() function is used to display a single character at a time by passing that
character directly to it or by passing a variable that has already stored a character.
gets(): gets() function reads a group of characters or strings from the keyboard by the user and these
characters get stored in a character array. This function allows us to write space-separated texts or
strings.
puts(): In C programming puts() function is used to display a group of characters or strings which is
already stored in a character array.
putch(): putch() function is used to display a single character which is given by the user and that
character prints at the current cursor location.

Formatted I/O vs Unformatted I/O

S
Formatted I/O functions Unformatted I/O functions
No.

These functions do not allow to take


These functions allow us to take input or
1 input or display output in user desired
display output in the user’s desired format.
format.

These functions do not support format


2 These functions support format specifiers.
specifiers.
These are used for storing data more user These functions are not more user-
3
friendly friendly.

Here, we can use only character and


4 Here, we can use all data types.
string data types.

printf(), scanf, sprintf() and sscanf() are getch(), getche(), gets() and puts(), are
5
examples of these functions. some examples of these functions.

You might also like