Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
9 views16 pages

C Notes

C language, developed by Dennis Ritchie in 1972 at Bell Laboratories, is a structure-oriented programming language primarily used for system applications like operating systems and compilers. It features reliability, portability, and efficiency, and is classified as a middle-level language, bridging high-level and low-level programming. The document also covers C programming structure, data types, variables, operators, and the compilation process.

Uploaded by

Prakash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views16 pages

C Notes

C language, developed by Dennis Ritchie in 1972 at Bell Laboratories, is a structure-oriented programming language primarily used for system applications like operating systems and compilers. It features reliability, portability, and efficiency, and is classified as a middle-level language, bridging high-level and low-level programming. The document also covers C programming structure, data types, variables, operators, and the compilation process.

Uploaded by

Prakash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

C – LANGUAGE

History

 C language is a structure oriented programming language, was developed at Bell Laboratories in


1972 by Dennis Ritchie
 C language features were derived from earlier language called “B” (Basic Combined Programming
Language – BCPL)
 C language was invented for implementing UNIX operating system
 In 1978, Dennis Ritchie and Brian Kernighan published the first edition “The C Programming
Language” and commonly known as K&R C
 In 1983, the American National Standards Institute (ANSI) established a committee to provide a
modern, comprehensive definition of C. The resulting definition, the ANSI standard, or “ANSI C”,
was completed late 1988.

Features of C language:

 Reliability
 Portability
 Flexibility
 Interactivity
 Modularity
 Efficiency and Effectiveness

Uses of C language:

C language is used for developing system applications that forms major portion of operating systems such as
Windows, UNIX and Linux. Below are some examples of C being used.

 Database systems
 Graphics packages
 Word processors
 Spread sheets
 Operating system development
 Compilers and Assemblers
 Network drivers
 Interpreters

Which level the C language is belonging to?

S.no High Level Middle Level Low Level

Middle level languages don’t provide all Low level languages


High level languages provides
the built-in functions found in high level provides nothing other
almost everything that the
1 languages, but provides all building than access to the
programmer might need to do as
blocks that we need to produce the result machines basic instruction
already built into the language
we want set

Examples:
2 C, C++ Assembler
Java, Python

1
TRANSLATORS

It is used to convert one form of language into another form.

Assembler

It is used to convert assembly language program into machine language (0’s & 1’s ).

Interpreter

It is used to convert high level language into machine language (0’s,1’s). Also it checks the error in a
program line by line.

Eg: VB

Compiler

It is used to convert middle level language into machine language into machine language. Also it
checks the error in a whole program only once at the end.

Eg: C

C uses compiler - A C compiler translate a C program into a machine language program.

STEPS FOR MAKING A C PROGRAM


Source program

The program that is prepared by the uses is called as source program.

Save the Program

Save the source program that we written in C as “filename.c”.

Compiling a program

While compiling a source program, the compiler checks errors in the program. If there is no error the
program is converted into machine language. This also includes the linking process.

Press F9 to compile. It creates a file called “filename.obj”. This is an object file and it has only
machine languages.

Running a program

If there is no compile error, press alt + F9 to run a program. The file called “filename.exe” is created
on that time. This file contains the output of the program.

MODULAR PROGRAMMING

C is a module language. Modules are smaller segments these are interrelated to a program.

2
C PROGRAM STRUCTURE

Documentation section \\ Example program for Addition


\* -----------------------
----------------------------*\
Link section # include<stdio.h>

Global section int a=10;

Definition section # define PI 3.14

Main ( )
{
Declaration part; int a,b; char name;
Executable statement; printf(“Brilliant”);

}
Sub program section
Function 1
Function 2
Function 3

F1,F2 user defined function

Example program
# includes < stdio.h >
# includes < conio . h >
Void main ( )
{
Printf(“ hai “) ; // puts ( “hai” ) ;
}

Stdio.h standard input / output header file.

printf , scanf

conio.h console input / output header file

getch( ), clrscr( )

Get character, clear screen.

3
C CHARACTER SET

The C character set consists of upper and lower case alphabets, digits, special character and white
spaces. The alphabets and digits are together called the alphanumeric characters.

I. Alphabets a-z A-Z


II. Digits 0-9
III. Special characters $, ^, *, /, \, [ ].
IV. White space characters. Blank space new line In “ \ n”

IDENTIFIERS AND KEYWORDS


In C language every word is either classified as an identifier or a keyword.

Identifiers

Identifiers are the name given to variables, symbolic constants functions etc.

Rules for identifiers

 Identifier should start with a letter.


 No special character except (- ) underscore is used.
 Keywords are never used as identifier.
 C is case sensitive, so name should be in lower case.

Example:
Valid : marks, total – mark, S12
Invalid: $ value , int, 1mem.
Keywords
These are called as reserved words. This has a standard and predefined meaning these are basic
building blocks to write a program.
Example
Auto double int struct
Break else long switch
Case enun register typedef
Char extern return union
Const float short unsigned
Continue for signed void
Default goto size of volatile
Do if static while

C TOKENS
Every individual element in a C program is called as c tokens the C tokens are
 Identifiers
 Keywords
 Constants
 Strings
 Operators
 Special symbols

4
C – data types:

There are four data types in C language. They are,

S.no Types Data Types


1 Basic data types int, char, float, double
2 Enumeration data type enum
3 Derived data type pointer, array, structure, union
4 Void data type void

1. Basic data types in C:

1.1. Integer data type:

 Integer data type allows a variable to store numeric values.


 “int” keyword is used to refer integer data type.
 The storage size of int data type is 2 or 4 or 8 byte.
 It varies depend upon the processor in the CPU that we use. If we are using 16 bit processor, 2 byte
(16 bit) of memory will be allocated for int data type.
 Like wise, 4 byte (32 bit) of memory for 32 bit processor and 8 byte (64 bit) of memory for 64 bit
processor is allocated for int datatype.
 int (2 byte) can store values from -32,768 to +32,767
 int (4 byte) can store values from -2,147,483,648 to +2,147,483,647.
 If you want to use the integer value that crosses the above limit, you can go for “long int” and “long
long int” for which the limits are very high.

Note:

 We can’t store decimal values using int data type.


 If we use int data type to store decimal values, decimal values will be truncated and we will get only
whole number.
 In this case, float data type can be used to store decimal values in a variable.

1.2. Character data type:

 Character data type allows a variable to store only one character.


 Storage size of character data type is 1. We can store only one character using character data type.
 “char” keyword is used to refer character data type.
 For example, ‘A’ can be stored using char datatype. You can’t store more than one character using
char data type.
 Please refer C – Strings topic to know how to store more than one characters in a variable.

1.3. Floating point data type:

Floating point data type consists of 2 types. They are,

1. float
2. double

1. float:

 Float data type allows a variable to store decimal values.


5
 Storage size of float data type is 4. This also varies depend upon the processor in the CPU as “int”
data type.
 We can use up-to 6 digits after decimal using float data type.
 For example, 10.456789 can be stored in a variable using float data type.

2. double:

 Double data type is also same as float data type which allows up-to 10 digits after decimal.
 The range for double datatype is from 1E–37 to 1E+37.

1.3.1. sizeof() function in C:

sizeof() function is used to find the memory space allocated for each C data types.

1 #include <stdio.h>
2 #include <limits.h>
3
4 int main()
5 {
6
7 int a;
8 char b;
9 float c;
10 double d;
11 printf("Storage size for int data type:%d \n",sizeof(a));
12 printf("Storage size for char data type:%d \n",sizeof(b));
13 printf("Storage size for float data type:%d \n",sizeof(c));
14 printf("Storage size for double data type:%d\n",sizeof(d));
15 return 0;
16 }

Output:

Storage size for int data type:4


Storage size for char data type:1
Storage size for float data type:4
Storage size for double data type:8

MODIFIERS IN C:

 The amount of memory space to be allocated for a variable is derived by modifiers.


 Modifiers are prefixed with basic data types to modify (either increase or decrease) the amount of
storage space allocated to a variable.
 For example, storage space for int data type is 4 byte for 32 bit processor. We can increase the range
by using long int which is 8 byte. We can decrease the range by using short int which is 2 byte.

 There are 5 modifiers available in C language. They are,

1. short
2. long
3. signed
4. unsigned
6
5. long long

 Below table gives the detail about the storage size of each C basic data type in 16 bit processor.
Please keep in mind that storage size and range for int and float datatype will vary depend on the
CPU processor (8,16, 32 and 64 bit)

storage
S.No C Data types Range
Size
1 char 1 –127 to 127
2 int 2 –32,767 to 32,767
1E–37 to 1E+37 with six digits of
3 float 4
precision
1E–37 to 1E+37 with ten digits of
4 double 8
precision
1E–37 to 1E+37 with ten digits of
5 long double 10
precision
6 long int 4 –2,147,483,647 to 2,147,483,647
7 short int 2 –32,767 to 32,767
8 unsigned short int 2 0 to 65,535
9 signed short int 2 –32,767 to 32,767
10 long long int 8 –(2power(63) –1) to 2(power)63 –1
11 signed long int 4 –2,147,483,647 to 2,147,483,647
12 unsigned long int 4 0 to 4,294,967,295
unsigned long long
13 8 2(power)64 –1
int

C – VARIABLE

 C variable is a named location in a memory where a program can manipulate the data. This location
is used to hold the value of the variable.
 The value of the C variable may get change in the program.
 C variable might be belonging to any of the data type like int, float, char etc.

Rules for naming C variable:

1. Variable name must begin with letter or underscore.


2. Variables are case sensitive
3. They can be constructed with digits, letters.
4. No special symbols are allowed other than underscore.
5. sum, height, _value are some examples for variable name

Declaring & initializing C variable:

 Variables should be declared in the C program before to use.


 Memory space is not allocated for a variable while declaration. It happens only on variable
definition.
 Variable initialization means assigning a value to the variable.

S.No Type Syntax Example

7
1 Variable declaration data_type variable_name; int x, y, z; char flat, ch;
2 Variable initialization data_type variable_name = value; int x = 50, y = 30; char flag = ‘x’, ch=’l’;

There are three types of variables in C program They are,

1. Local variable
2. Global variable
3. Environment variable

C – Operators and Expressions

 The symbols which are used to perform logical and mathematical operations in a C program are
called C operators.
 These C operators join individual constants and variables to form expressions.
 Operators, functions, constants and variables are combined together to form expressions.
 Consider the expression A + B * 5. where, +, * are operators, A, B are variables, 5 is constant and A
+ B * 5 is an expression.

Types of C operators:

C language offers many types of operators. They are,

1. Arithmetic operators
2. Assignment operators
3. Relational operators
4. Logical operators
5. Bit wise operators
6. Conditional operators (ternary operators)
7. Increment/decrement operators
8. Special operators

Continue on types of C operators:

o Click on each operators name below for detail description and example programs.

S.no Types of Operators Description


These are used to perform mathematical calculations like addition,
1 Arithmetic_operators
subtraction, multiplication, division and modulus
2 Assignment_operators These are used to assign the values for the variables in C programs.
3 Relational operators These operators are used to compare the value of two variables.
These operators are used to perform logical operations on the given two
4 Logical operators
variables.
5 Bit wise operators These operators are used to perform bit operations on given two variables.
Conditional (ternary) Conditional operators return one value if condition is true and returns
6
operators another value is condition is false.
Increment/decrement These operators are used to either increase or decrease the value of the
7
operators variable by one.
8 Special operators &, *, sizeof( ) and ternary operators.

8
Arithmetic Operators in C:

o C Arithmetic operators are used to perform mathematical calculations like addition,


subtraction, multiplication, division and modulus in C programs.

S.no Arithmetic Operators Operation Example


1 + Addition A+B
2 - Subtraction A-B
3 * multiplication A*B
4 / Division A/B
5 % Modulus A%B

Example program for C arithmetic operators:

o In this example program, two values “40″ and “20″ are used to perform arithmetic operations
such as addition, subtraction, multiplication, division, modulus and output is displayed for
each operation.

1 #include <stdio.h>
2 int main()
3 {
4 int a=40,b=20, add,sub,mul,div,mod;
5
6 add = a+b;
7 sub = a-b;
8 mul = a*b;
9 div = a/b;
10 mod = a%b;
11
12 printf("Addition of a, b is : %d\n", add);
13 printf("Subtraction of a, b is : %d\n", sub);
14 printf("Multiplication of a, b is : %d\n", mul);
15 printf("Division of a, b is : %d\n", div);
16 printf("Modulus of a, b is : %d\n", mod);
17 }

Output:

Addition of a, b is : 60
Subtraction of a, b is : 20
Multiplication of a, b is : 800
Division of a, b is : 2
Modulus of a, b is : 0

9
Logical operators in C:

o These operators are used to perform logical operations on the given expressions.
o There are 3 logical operators in C language. They are, logical AND (&&), logical OR (||) and
logical NOT (!).

S.no Operators Name Example Description


logical
1 && (x>5)&&(y<5) It returns true when both conditions are true
AND
2 || logical OR (x>=10)||(y>=10) It returns true when at-least one of the condition is true
It reverses the state of the operand “((x>5) && (y<5))”
logical
3 ! !((x>5)&&(y<5)) If “((x>5) && (y<5))” is true, logical NOT operator makes it
NOT
false

Example program for logical operators in C:

1 #include <stdio.h>
2 int main()
3 {
4 int m=40,n=20;
5 int o=20,p=30;
6
7 if (m>n && m !=0)
8 {
9 printf("&& Operator : Both conditions are true\n");
10 }
11 if (o>p || p!=20)
12 {
13 printf("|| Operator : Only one condition is true\n");
14 }
15 if (!(m>n && m !=0))
16 {
17 printf("! Operator : Both conditions are true\n");
18 }
19 else
20 {
21 printf("! Operator : Both conditions are true. " \
22 "But, status is inverted as false\n");
23 }
24 }

Output:

&& Operator : Both conditions are true


|| Operator : Only one condition is true
! Operator : Both conditions are true. But, status is inverted as false

10
o In this program, operators (&&, || and !) are used to perform logical operations on the given
expressions.
o && operator – “if clause” becomes true only when both conditions (m>n and m! =0) is true.
Else, it becomes false.
o || Operator – “if clause” becomes true when any one of the condition (o>p || p!=20) is true. It
becomes false when none of the condition is true.
o ! Operator – It is used to reverses the state of the operand.
o If the conditions (m>n && m!=0) is true, true (1) is returned. This value is inverted by “!”
operator.
o So, “! (m>n and m! =0)” returns false (0).

Bit wise operators in C:

o These operators are used to perform bit operations. Decimal values are converted into binary
values which are the sequence of bits and bit wise operators work on these bits.
o Bit wise operators in C language are & (bitwise AND), | (bitwise OR), ~ (bitwise OR), ^
(XOR), << (left shift) and >> (right shift).

Truth table for bit wise operation Bit wise operators


x y x|y x&y x^y Operator_symbol Operator_name

0 0 0 0 0 & Bitwise_AND

0 1 1 0 1 | Bitwise OR

1 0 1 0 1 ~ Bitwise_NOT

1 1 1 1 0 ^ XOR

<< Left Shift

>> Right Shift

 Consider x=40 and y=80. Binary form of these values are given below.

x = 00101000
y= 01010000

 All bit wise operations for x and y are given below.

x&y = 00000000 (binary) = 0 (decimal)


x|y = 01111000 (binary) = 120 (decimal)
~x =1111111111111111111111111111111111111111111111111111111111010111
.. ..= -41 (decimal)
x^y = 01111000 (binary) = 120 (decimal)
x << 1 = 01010000 (binary) = 80 (decimal)
x >> 1 = 00010100 (binary) = 20 (decimal)

Note:

 Bit wise NOT : Value of 40 in binary is


0000000000000000000000000000000000000000000000000000000000101000. So, all 0′s are
converted into 1′s in bit wise NOT operation.

11
 Bit wise left shift and right shift : In left shift operation “x << 1 “, 1 means that the bits will be left
shifted by one place. If we use it as “x << 2 “, then, it means that the bits will be left shifted by 2
places.

Example program for bit wise operators in C:

o In this example program, bit wise operations are performed as shown above and output is
displayed in decimal format.

1
2 #include <stdio.h>
3 int main()
4 {
5 int m=40,n=80,AND_opr,OR_opr,XOR_opr,NOT_opr ;
6 AND_opr = (m&n);
7 OR_opr = (m|n);
8 NOT_opr = (~m);
9 XOR_opr = (m^n);
10 printf("AND_opr value = %d\n",AND_opr );
11 printf("OR_opr value = %d\n",OR_opr );
12 printf("NOT_opr value = %d\n",NOT_opr );
13 printf("XOR_opr value = %d\n",XOR_opr );
14 printf("left_shift value = %d\n", m << 1);
15 printf("right_shift value = %d\n", m >> 1);
16 }
17

Output:

AND_opr value = 0
OR_opr value = 120
NOT_opr value = -41
XOR_opr value = 120
left_shift value = 80
right_shift value = 20

12
Conditional or ternary operators in C:

o Conditional operators return one value if condition is true and returns another value is
condition is false.
o This operator is also called as ternary operator.

Syntax : (Condition? true_value: false_value);


Example : (A > 100 ? 0 : 1);
. In above example, if A is greater than 100, 0 is returned else 1 is returned. This is equal to if else
conditional statements.

Example program for conditional/ternary operators in C:

1 #include <stdio.h>
2 int main()
3 {
4 int x=1, y ;
5 y = ( x ==1 ? 2 : 0 ) ;
6 printf("x value is %d\n", x);
7 printf("y value is %d", y);
8 }

C – Increment/decrement Operators

 Increment operators are used to increase the value of the variable by one and decrement operators are
used to decrease the value of the variable by one in C programs.

Syntax:

Increment operator : ++var_name; (or) var_name++;


Decrement operator : – - var_name; (or) var_name – -;

 Example:

Increment operator : ++ i ; i ++ ;
Decrement operator: –-i; i–-;

Example program for increment operators in C:

o In this program, value of “i” is incremented one by one from 1 up to 9 using “i++” operator
and output is displayed as “1 2 3 4 5 6 7 8 9”.

1 //Example for increment operators


2
3 #include <stdio.h>
4 int main()
5 {
6 int i=1;
7 while(i<10)
8 {
9 printf("%d ",i);
13
10 i++;
11 }
12 }

Example program for decrement operators in C:

o In this program, value of “I” is decremented one by one from 20 up to 11 using “i–” operator
and output is displayed as “20 19 18 17 16 15 14 13 12 11”.

1 //Example for decrement operators


2
3 #include <stdio.h>
4 int main()
5 {
6 int i=20;
7 while(i>10)
8 {
9 printf("%d ",i);
10 i--;
11 }
12 }

Difference between pre/post increment & decrement operators in C:

o Below table will explain the difference between pre/post increment and decrement operators
in C.

S.no Operator type Operator Description


++i
1 Pre increment Value of i is incremented before assigning it to variable i.
i++
2 Post-increment Value of i is incremented after assigning it to variable i.
– –i
3 Pre decrement Value of i is decremented before assigning it to variable i.
i– –
4 Post_decrement Value of i is decremented after assigning it to variable i.

Example program for pre – increment operators in C:

1 //Example for increment operators


2
3 #include <stdio.h>
4 int main()
5 {
6 int i=0;
7 while(++i < 5 )
8 {
9 printf("%d ",i);
10 }
11 return 0;
12 }

14
Example program for post – increment operators in C:

1 #include <stdio.h>
2 int main()
3 {
4 int i=0;
75 while(i++ < 5 )
6 {
7 printf("%d ",i);
8 }
9 return 0;
10 }

Example program for pre - decrement operators in C:

1 #include <stdio.h>
2 int main()
3 {
4 int i=10;
5 while(--i > 5 )
6 {
7 printf("%d ",i);
8 }
9 return 0;
10 }

Example program for post - decrement operators in C:

1 #include <stdio.h>
2 int main()
3 {
4 int i=10;
5 while(i-- > 5 )
6 {
7 printf("%d ",i);
8 }
9 return 0;
10 }

Special Operators in C:

o Below are some of special operators that C language offers.

S.no Operators Description


This is used to get the address of the variable.
1 &
Example : &a will give address of a.
This is used as pointer to a variable.
2 *
Example : * a where, * is pointer to the variable a.
This gives the size of the variable.
3 Sizeof ()
Example : size of (char) will give us 1.

Example program for & and * operators in C:

15
o In this program, “&” symbol is used to get the address of the variable and “*” symbol is used
to get the value of the variable that the pointer is pointing to. Please refer C – pointer topic to
know more about pointers.

1 #include <stdio.h>
2
3 int main()
4 {
5 int *ptr, q;
6 q = 50;
7 /* address of q is assigned to ptr */
8 ptr = &q;
9 /* display q's value using ptr variable */
10 printf("%d", *ptr);
11 return 0;
12 }

16

You might also like