0% found this document useful (0 votes)
4 views51 pages

Fundamentals of C

Uploaded by

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

Fundamentals of C

Uploaded by

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

lOMoAR cPSD| 20323518

Fundamentals of C

Prof. Madhuri Desai


lOMoAR cPSD| 20323518

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

Prof. Madhuri Desai


lOMoAR cPSD| 20323518

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.

Prof. Madhuri Desai


lOMoAR cPSD| 20323518

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

• 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.

Prof. Madhuri Desai


lOMoAR cPSD| 20323518

C is Middle Level Language

• C stands in between these two categories.


• Since it was designed to have both:
• A relatively good programming efficiency as compared to
Machine Oriented Languages .
• A relatively good Machine efficiency as compared to
Problem Oriented Languages .

• That’s why it is called a Middle Level Language.

Prof. Madhuri Desai


lOMoAR cPSD| 20323518

Terminologies of ‘C’

1. Keywords
2. Identifiers
3. Variables
4. Constants
5. Special Symbols
6. Operators
7. Character & String

Prof. Madhuri Desai


lOMoAR cPSD| 20323518

1. Keywords

• Keywords are the reserved words whose meaning has


already been explained to the C compiler.
• C has 32 keywords.
• These keywords combined with a formal syntax form a
C programming language.
• Rules to be followed for all programs written in C:
All keywords are lower-cased.

C is case sensitive, do-while is different from DO WHILE.

Keywords cannot be used as a variable or function name.

Prof. Madhuri Desai


lOMoAR cPSD| 20323518

Prof. Madhuri Desai


lOMoAR cPSD| 20323518

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

Prof. Madhuri Desai


lOMoAR cPSD| 20323518

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

An identifier cannot be the same as a C keyword

Prof. Madhuri Desai


lOMoAR cPSD| 20323518

Prof. Madhuri Desai


lOMoAR cPSD| 20323518

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;

Prof. Madhuri Desai


lOMoAR cPSD| 20323518

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

Prof. Madhuri Desai


lOMoAR cPSD| 20323518

5. Special Symbols
• ! , @, #, $ , & , * , ….. These all symbols that can be
find on Keyboard, are called Special Symbols.

• Every symbol has its special meaning in different respect


at different place that’s why it is called Special Symbols.

Prof. Madhuri Desai


lOMoAR cPSD| 20323518

6. Operators
• Operator is a symbol that operates on one or
more operands and produces output.

Example - c=a+b;

• In the above Example the symbols + and = are


operators that operate on operands a, b , and c .

Prof. Madhuri Desai


lOMoAR cPSD| 20323518

7. Character & String


• The Characters that can be used to form words,
numbers and expressions depend upon the computer
on which the program is running.
• The characters in C are grouped into the
following categories :
• Letters
• Digits
• Special characters
• White Spaces
• Remember that a character ‘a’ is not equivalent to
the string “a”.

Prof. Madhuri Desai


lOMoAR cPSD| 20323518

A simple Program of C
/* Write a program to print a message */
#include<stdio.h>
void main()
{
printf(“ C Programming”);
}

Prof. Madhuri Desai


lOMoAR cPSD| 20323518


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.

Prof. Madhuri Desai


lOMoAR cPSD| 20323518

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

Prof. Madhuri Desai


lOMoAR cPSD| 20323518

Compilation & Execution of a Program


C Source Code

C Preprocessor

Expanded C Source Code

C Compiler

Target Assembly
Code Linker

Executable Code

Loader
Output

Prof. Madhuri Desai


lOMoAR cPSD| 20323518

Escape Sequence
• \n new line
• \t tab
• \r carriage return
• \a alert
• \\ backslash
• \” double quote

Prof. Madhuri Desai


lOMoAR cPSD| 20323518

Data types

Prof. Madhuri Desai


lOMoAR cPSD| 20323518

Prof. Madhuri Desai


lOMoAR cPSD| 20323518

Type casting

Converting one primitive datatype into another is known as type


casting (type conversion) C.
You can cast the primitive datatypes in two ways namely,
1) Widening(Implicit)
2) Narrowing(Explicit).

Prof. Madhuri Desai


lOMoAR cPSD| 20323518

Widening(Implicit)

Widening − Converting a lower datatype to a higher datatype is


known as widening. In this case the casting/conversion is done
automatically therefore, it is known as implicit type casting. In this
case both datatypes should be compatible with each other.

Prof. Madhuri Desai


lOMoAR cPSD| 20323518

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.

Prof. Madhuri Desai


lOMoAR cPSD| 20323518

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:-

• Unary Operators – Acts upon one operand


• Binary Operators – Acts upon two operands
• Ternary Operators – Acts upon three operands.

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

Addition + Adds operands x+y


Subtraction - Subs second from first x-y
Negation - Negates operand -x

Multiplication * Multiplies operands x*y


Division / Divides first by second x/y
(integer quotient)
Modulus % Remainder of divide op x%y

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

Ex- if (age>55 && salary <1000)

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;

1. Simple Assignment e.g. =


2. Compound Assignment e.g. +=, -=, *=, /=, &=
3. Expression Assignment e.g. a=5+(b=8 + (c=2)) - 4

35
Prof. Madhuri Desai ([email protected])
lOMoAR cPSD| 20323518

Operators continued……..
x + = y+1;
is same as x = x + (y+1)

i.e. += means ‘add y+1 to x’


Similarly……..
a=a-1 a - = 1;
a=a*(n+1) a *= n+1;
a=a/(n+1) a/= n+1;

36
Prof. Madhuri Desai ([email protected])
lOMoAR cPSD| 20323518

Increment and Decrement


• C has two very useful operators not generally found in other languages. These
are the increment and decrement operators.
i.e ++ and --

++ operator adds 1 to the operand and -- operator subtracts 1 to the operand .


Both these operators are unary operators and take the following form.
++m (prefix operator) ; or m++ (postfix operator);
--m; or m--;
++m = m+1; --m= m-1;

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;

Operator ?: works as follows: exp1 is evaluated first . If it is nonzero (true) ,then


the expression exp2 is evaluated and becomes the value of the expression
otherwise vice versa.

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

a&b = 0000 0000


a|b = 0000 0111
a^b = 0000 0111
In Ex-OR (if both bits are same : 0) (if both bits are diff : 1)
a = 10
b = ~ a => ~ (1010) => 0101
42
Prof. Madhuri Desai ([email protected])
lOMoAR cPSD| 20323518

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

Operator precedence and associativity


• Operator precedence determines which operator is performed first
in an expression with more than one operators with different
precedence.
• Operators Associativity is used when two operators of
same precedence appear in an expression. Associativity can
be either Left to Right or Right to Left.

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

Prof. Madhuri Desai ([email protected])


lOMoAR cPSD| 20323518

Output

Prof. Madhuri Desai ([email protected])


lOMoAR cPSD| 20323518

Prof. Madhuri Desai ([email protected])

You might also like