Fundamentals of C
Fundamentals of C
Fundamentals of C
Features of C Language
• Simple
• Machine Independent or Portable
• Mid-level programming language
• structured programming language
• Rich Library
• Memory Management
• Fast Speed
• Pointers
• Recursion
• Extensible
• Case sensitive
• Compiler Based
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.
Prof. Madhuri Desai
lOMoAR cPSD| 20323518
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 adapt new features.
11) Case sensitive
C is a case sensitive language, where int and INT makes a difference in upper-case and lower-case.
12) Compiler Based
Every program written in c programming language needs to get compiled first before execution.
History
• In 1972 Dennis Ritchie at Bell Labs writes C and in 1978 the publication of The C
Programming Language by Kernighan & Ritchie caused a revolution in the
computing world
Terminologies of ‘C’
1. Keywords
2. Identifiers
3. Variables
4. Constants
5. Special Symbols
6. Operators
7. Character & String
1. Keywords
2. Identifiers
• Identifiers refer to the name of variables, functions and arrays.
• These are user-defined names and consist of sequence of letters
and digits, with a letter as a first character.
• Both uppercase and lowercase letters are permitted, although
lowercase letters are commonly used .
• The underscore character is also permitted in identifiers. It is
usually used as a link between two words in long identifiers.
• Must begin with a letter or underscore
• Should not be a keyword
• Must not contain white space
Identifier Names
✓
Some correct identifier names are - arena, s_count
marks40
class_one
_id
Some erroneous identifier names are -
X
1stsst
oh!god
start….end
3. Variables
• Variables are named locations in memory that are used to
hold a value that may be modified by the program.
• Unlike constants that remain unchanged during the
execution of a program.
• A variable may take different values at different times
during execution.
• The syntax for declaring a variable is –
DataType IdentifierName ;
• Example- int num; float
sum , a;
4. Constants
• Constants are the fixed values that do not
change during the execution of a program.
• C supports several types of constants.
• Numeric Constants
• Integer constants
• Real constants
• Character Constants
• Single character constant
• String Constants
5. Special Symbols
• ! , @, #, $ , & , * , ….. These all symbols that can be
find on Keyboard, are called Special Symbols.
6. Operators
• Operator is a symbol that operates on one or
more operands and produces output.
Example - c=a+b;
A simple Program of C
/* Write a program to print a message */
#include<stdio.h>
void main()
{
printf(“ C Programming”);
}
▪
Comment about the program should be enclosed within ‘/*’ & ‘*/’.
▪
printf() is a function which is used to print messages on the screen.
▪
main() is a function from which execution of the program starts.
▪
Here stdio.h is a library file which contains standard input/output functions ,
keywords etc.
▪
#include<> is used to define the library file that is to be used in the program for
compiler information.
▪
In a C program, all lines that start with # are processed by
preprocessor which is a program invoked by the compiler. In a very
basic term, preprocessor takes a C program and produces another C
program. The produced program has no lines starting with #, all such
lines are processed by the preprocessor.
Commands
• After Compilation : Run a Program
• 1>LINUX-UBUNTU : gcc fileneme.c ./a.out
• 2> WINDOWS TURBO C:alt+F9 ctrl+F9
• 3>ONLINE C EDITIOR:DIRECT COMPILE AND RUN
C Preprocessor
C Compiler
Target Assembly
Code Linker
Executable Code
Loader
Output
Escape Sequence
• \n new line
• \t tab
• \r carriage return
• \a alert
• \\ backslash
• \” double quote
Data types
Type casting
Widening(Implicit)
Narrowing(Explicit)
• Narrowing − Converting a higher datatype to a lower datatype is
known as narrowing.
• In this case the casting/conversion is not done automatically, you
need to convert explicitly using the cast operator “( )” explicitly.
Therefore, it is known as explicit type casting.
• In this case both datatypes need not be compatible with each other.
Operators in C
An operator is a symbol that tells the computer to perform
certain mathematical or logical manipulation.
28
Prof. Madhuri Desai ([email protected])
lOMoAR cPSD| 20323518
Operands
• The data item that operators act upon are called Operands.
Operator
a+b
Operands
29
Prof. Madhuri Desai ([email protected])
lOMoAR cPSD| 20323518
Types of Operators
On the basis of number of operands, operators are divided into
three types:-
30
Prof. Madhuri Desai ([email protected])
lOMoAR cPSD| 20323518
Types of Operators:-
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Assignment Operators
• Increment or Decrement Operator
• Conditional Operators
• Bitwise Operators
• Special Operators
31
Prof. Madhuri Desai ([email protected])
lOMoAR cPSD| 20323518
Arithmetic Operators
Operator Symbol Action Example
32
Prof. Madhuri Desai ([email protected])
lOMoAR cPSD| 20323518
Relational Operators
• Comparisons can be done with the help of relational operators
• These consist of:-
Operator Meaning
< less than
<= less than equal to
> greater than
>= greater than equal to
== Equal to
!= Not equal to
33
Prof. Madhuri Desai ([email protected])
lOMoAR cPSD| 20323518
Logical Operators
• Expression which combines two or more relational expression is termed as
a logical or compound relational expression
The result of these operators is either TRUE or FALSE.
Logical expressions are:-
&& logical AND
|| Logical OR
! Logical NOT
34
Prof. Madhuri Desai ([email protected])
lOMoAR cPSD| 20323518
Assignment Operator
• Used to assign the result of an expression to a variable.
ex- int i=20;
35
Prof. Madhuri Desai ([email protected])
lOMoAR cPSD| 20323518
Operators continued……..
x + = y+1;
is same as x = x + (y+1)
36
Prof. Madhuri Desai ([email protected])
lOMoAR cPSD| 20323518
37
Prof. Madhuri Desai ([email protected])
lOMoAR cPSD| 20323518
Ex…
int m = 5 , y ;
y = ++m ;
printf ( “ %d%d " y,m );
Result???
int m = 5 , y ;
y = m++ ;
printf ( “ %d%d " y,m );
Result?????
38
Prof. Madhuri Desai ([email protected])
lOMoAR cPSD| 20323518
Conditional Operator..
• A ternary operator pair “?:” is available in C to construct conditional
expressions of the form.
exp1 ? exp2 : exp3;
39
Prof. Madhuri Desai ([email protected])
lOMoAR cPSD| 20323518
Example….
a=10 ;
b=15;
x=(a>b) ? a : b;
It can be written as …
if ( a > b )
x = a;
else
x=b;
40
Prof. Madhuri Desai ([email protected])
lOMoAR cPSD| 20323518
Bitwise Operator….
• C has distinction of supporting special operators known as
Bitwise Operators for manipulation of data at bit level.
These are used to test bits.
Operators Meaning
& bitwise AND
| bitwise OR
^ bitwise exclusive OR
(Ex-OR)
<< shift left
>> shift right
~ One’s complement
41
Prof. Madhuri Desai ([email protected])
lOMoAR cPSD| 20323518
Bitwise Operator….
Examples - int a = 4, b = 3
a = 0000 0100
b = 0000 0011
Bitwise operators
• The shift operator:
• x << n
• Shifts the bits in x n positions to the left, shifting in zeros on the right.
• If x = 1111 1111 1111 00002
x << 1 equals 1111 1111 1110 00002
• x >> n
• Shifts the bits in x n positions right.
• shifts in 0 if it is an unsigned integer
• x >> 1 is 0111 1111 1111 10002 (unsigned)
43
Prof. Madhuri Desai ([email protected])
lOMoAR cPSD| 20323518
Special Operators….
C supports some special types of operators….
such as comma operators, sizeof operator, pointer operators
( & and * ) and member selection operators ( . and -> ).
Comma Operator:- The comma operator can be used to link the related
expressions together. A comma-linked list of expressions are evaluated left to
right and the value of right-most .
Ex:- value = (x=10, y=5, x +y);
44
Prof. Madhuri Desai ([email protected])
lOMoAR cPSD| 20323518
45
Prof. Madhuri Desai ([email protected])
lOMoAR cPSD| 20323518
46
Prof. Madhuri Desai ([email protected])
lOMoAR cPSD| 20323518
47
Prof. Madhuri Desai ([email protected])
lOMoAR cPSD| 20323518
48
Prof. Madhuri Desai ([email protected])
lOMoAR cPSD| 20323518
Formatted output
• Integer
• Float
• String
Output