C Language Fundamental
C Language Fundamental
Prepared by
S. B. Mishra
Department of Computer Science
Viswa Niketan Secondary School
Tripureswor, Kathmandu
C Language
• C is a high-level and general purpose
programming language that is ideal for developing
firmware or portable applications.
• The C language is structured, middle level
programming language developed by Dennis
Ritchie.
• Operating system programs such as Windows,
Unix, Linux are written in C language.
• C has been written in assembly language.
C – Language History
• Originally developed for writing system software.
• C was developed at Bell Labs by Dennis Ritchie for
the Unix Operating System (OS) in the early
1970s.
• C programming language features were derived
from an earlier language called “B” (Basic
Combined Programming Language – BCPL).
Features of C Language
• Simple: C provides structured approach, rich
set of library functions, data types etc.
• Machine Independent or Portable
• Mid-level Programming Language: C supports
features of low level programming and high
level language. That is why it is known as mid-
level language.
• Structured Programming Language: We can
break the program into parts using functions.
So, it is easy to understand and modify.
• Rich Library: C provides a lot of inbuilt functions that
makes the development fast.
• Memory Management: It supports the feature of
dynamic memory allocation.
• Speed: The compilation and execution time of C
language is fast.
• Pointer: We can directly interact with the memory by
using the pointers.
• Recursion: In c, we can call the function within the
function.
• Extensible: C language is extensible because it can
easily adopt new features.
Advantages of C Language
• C is compiler based language rather than
interpreter based.
• C is portable language.
• C is the collection of lot of library files.
• C is easy to learn for beginners.
• C supports good graphics.
• C supports system programming.
• C supports number of operators.
• C supports string handling.
Disadvantages of C Language
• C language has no run time checking mechanism.
• C does not support OOP(Object Oriented
Programming features, so C++ language was
introduced.
• C does not have the concept of constructor or
destructor.
• The program takes more time to design and
implement the software.
• It is case sensitive so mixing case makes difficult
while writing a program.
Uses of C programming language
The C programming language is used for developing:
• System Software such as Windows, UNIX and Linux.
• Database systems
• Graphics packages
• Word processors
• Spreadsheets
• Compilers and Assemblers
• Network drivers
• Interpreters
Variable
• A variable is a symbol (memory address) which
hold value that may change during the execution
of program.
• Example: A = 5; where A is the variable name and
5 is value.
• Variable name consist with the combination of
letters, digits or underscore characters.
• The value of the C variable may get change in the
program.
Rules for naming C variable
• Variable name must begin with letter or underscore.
• Variables are case sensitive.
• The variable name should not be keywords.
• They can be constructed with digits, letters.
• White space and other special characters are not
allowed in between the name of variable.
• Different variables of the same name are not allowed.
• 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.
Declaring & initializing C variable
int x, y, z;
1 Variable declaration data_type variable_name;
char flat, ch;
static while
Format Specifiers
• Format Specifiers in C language tells us which type
of data to store and which type of data to print.
• This statement tells us that it is used in only 2
places
– In taking Input
– In displaying Output
Different Types of Format Specifiers
Defined in C
Data Types Format specifier
int %d
short %d
long %ld
char %c
float %f
double %lf
long double %Lf
string %s
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 data types but a little twisted or grouped
together like array, structure, union and pointer.
These are discussed in details later.
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.
C – Operators and Expressions
• The symbols which tells the computer 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
• Arithmetic operators
• Assignment operators
• Relational operators
• Logical operators
• Bit wise operators
• Conditional operators (ternary operators)
• Increment/decrement operators
• Special operators
Arithmetic Operators
C Arithmetic operators Arithmetic
Operation Example
are used to perform Operators
mathematical
+ Addition A+B
calculations like addition,
subtraction,
– Subtraction A–B
multiplication, division
and modulus in C
* multiplication A*B
programs.
/ Division A/B
% Modulus A%B
Example program for C arithmetic
operators
#include <stdio.h>
#include <conio.h> Output:
void main() Addition of a, b is : 60
{ Subtraction of a, b is : 20
int a=40, b=20, add, sub, mul, div, mod;
Multiplication of a, b is : 800
add = a+b;
sub = a-b; Division of a, b is : 2
mul = a*b; Modulus of a, b is : 0
div = a/b;
mod = a%b;
printf("Addition of a, b is : %d\n", add);
printf("Subtraction of a, b is : %d\n", sub);
printf("Multiplication of a, b is : %d\n",
mul);
printf("Division of a, b is : %d\n", div);
printf("Modulus of a, b is : %d\n", mod);
getch();
}
Special Operators in C
S. No. Operators Description
This is same as
• Other assignment /= sum /= 10 sum = sum / 10
operators in C
This is same as
language are: %= sum %= 10 sum = sum % 10
Example program for C assignment
operators
# include <stdio.h> Output
#include <conio.h> Total = 45
void main()
i =i+1
{
int Total=0,i; Total = Total + i
for(i = 0; i < 10; i++)
{
Total+=i;
}
printf("Total = %d", Total);
getch()
}
Relational operators in C
between two variable Operators Example Description
Relational operators are
used to find the > x > y x is greater than y
relations. i.e. to
< x < y x is less than y
compare the values of
two variables in a C x is greater than or
>= x >= y equal to y
program.
x is less than or
<= x <= y equal to y
== x == y x is equal to y
!= x != y x is not equal to y
Example program for C Relational
operators
#include <stdio.h> Output:
#include <conio.h> a>b:1
void main()
a >= b : 1
{
int a = 9, b = 4; a <= b : 0
printf(" a > b: %d \n", a > b); a<b:0
printf("a >= b: %d \n", a >= b); a == b : 0
printf("a <= b: %d \n", a <= b); a != b : 1
printf("a < b: %d \n", a < b);
printf("a == b: %d \n", a == b);
printf("a != b: %d \n", a != b);
getch();
}
Logical operators in C
• These operators Operators Name Example Description
are used to
It returns true when
perform logical && logical AND (x>5)&&(y<5)
both conditions are true
operations on the
It returns true when at-
given expressions. || logical OR (x>=10)||(y>=10) least one of the
• There are 3 logical condition is true
operators in C
language. They It reverses the state of
are, logical AND the operand “((x>5) &&
(y<5))”
(&&), logical OR ! logical NOT !((x>5)&&(y<5))
If “((x>5) && (y<5))” is
(||) and logical true, logical NOT
NOT (!). operator makes it false
Example of Logical Operators
#include<stdio.h> Output
#include<conio.h>
True
void main()
{ False
int n1 = 30, n2 = 40; True
if(n1>=40 || n2>=40)
printf(“True");
if(n1>=40 && n2>=40)
printf(“False");
If(n1!=40)
Printf(“ True”);
getch();
}
Bit wise operators in C
• These operators are used
Operator_symbol Operator_name
to perform bit operations.
Decimal values are
converted into binary & Bitwise_AND
values which are the
sequence of bits and bit | Bitwise OR
wise operators work on
these bits. ~ Bitwise_NOT
• Bit wise operators in C
language are & (bitwise ^ XOR
AND), | (bitwise OR), ~
(bitwise OR), ^ (XOR), << << Left Shift
(left shift) and >> (right
shift).
>> Right Shift
Conditional or ternary operators in C
• Conditional Syntax :
operators return one (Condition? true_value: false_value);
value if condition is Example : (A > 100 ? 0 : 1);
true and returns
another value is
condition is false. In above example, if A is
• This operator is also greater than 100 then 0 is
called as ternary returned else 1 is returned.
operator. This is equal to if else
conditional statements.
Increment/decrement Operators
Increment Syntax:
operators are used to Increment operator:
increase the value of
++var_name; (or) var_name++;
the variable by one
and decrement Decrement operator:
operators are used to --var_name; (or) var_name – -;
decrease the value of Example:
the variable by one in Increment operator : ++ i ; i ++ ;
C programs.
Decrement operator : – – i ; i – – ;
Difference between pre/post
increment & decrement operators
S. No. Operator Type Operator Description