Features and Applicationsof C
• C is a general purpose, structured programming language.
• C is powerful, efficient, compact and flexible.
• C is highly portable (i.e., It can be run in different operating systems environment)
• C is a robust language whose rich set of built in functions and operators can be used to
write any complex program.
• ‘C’ has the ability to extend itself. We can continuously add our own functions to the
existing library functions.
• ‘C’ is well suited for writing System Software as well as Application software.
• ‘C’ program can be run on different operating systems of the different with little or no
alternation.
• ‘C’ is a middle level language, i.e. it supports both the low level language and high level
language features.
3.
Features and Applicationsof C
• ‘C’ languages allow reference to a memory allocation with the help of pointers,
which holds the address of the memory location.
• ‘C’ languages allow dynamic memory allocation i.e. a program can request the
operating system to allocate or release memory at runtime.
• ‘C’ languages allow manipulation of data at the lowest level i.e., bit level
manipulation. This feature is extensively useful in writing system software program.
• ‘C’ is a widely available, commercial ‘’ compiler is available on most PC’s.
• ‘C’ programs are fast and efficient.
• ‘C’ has got rich set of operators.
• ‘C’ can be applied in systems programming areas like Compilers, Interpreters and
Assemblers etc.
4.
Structure of aC program
documentation section
preprocessor section
definition section
global declaration section
main()
{
declaration part;
executable part;
}
Sub program section
{
Body of the subprogram;
}
Size and rangesof basic data types in C for a
16-bit computer
7.
Size and rangesof basic data types in C for a
32-bit computer
8.
Specifiers or Modifiersand Qualifiers
• Size Specifiers:
• short
• long
• Sign Specifiers:
• signed
• unsigned
• Type Qualifiers:
• const
• volatile
• restrict
9.
Size in numberof bytes of the short int, int
and long int data types in different machines
Data type 16-bit machine 32-bit machine 64-bit machine
short int 2 2 2
int 2 4 4
long int 4 4 4
Types of Programstatements
• Declarations
• Expressions
• Compound
• Labelled
• Control
• Selection
• Iteration
• Jump
13.
Declarations
• Declaration canbe done at three places
• Local variables – declared inside a function
• Global variables – declared outside all functions
• Formal parameter – declared in the definition of function parameters
• Syntax:
• datatype variable_name_1, variable_name_2,.., variable_name_n;
• Example:
• int a, b, c;
14.
Expressions
• Sequence ofoperators and operands that specifies computation of a
value
• Eg: x=4
• Where as, x=4; is a statement
15.
Compound and Labelledstatements
• Compound statements: Sequence of statements that may be treated
as a single statement in the construction of larger statements.
• Labeled statements: Used to mark any statement so that control may
be transferred to that statement.
Rules for Cprogramming
• Every program should have a main() function
• Terminated by a semi-colon (;)
• All statements are usually written in lowercase letters; Uppercase is
commonly used for symbolic constants
• The opening and closing parenthesis should be balanced
• Variables should be declared as used as per rules
• no keywords, no spaces, no special characters except underscore, starting
character should be an alphabet or underscore
18.
The C characterset
• Letters
• Uppercase A to Z
• Lowercase a to z
• Digits
• Decimal digits 0 to 9
• White spaces
• Special characters
19.
White Spaces
S.No CharacterEscape Sequence
1 Bell (Alert) a
2 Newline (Line feed) n
3 Back space b
4 Carriage return r
5 Horizontal tab t
6 Vertical tab v
7 Form feed f
8 Quotation Mark (Double quotes) ”
9 Single quote ’
10 Question Mark ?
11 Back Slash
12 Null 0
13 Octal number ooo
14 Hexadecimal number xhh
20.
Special Characters
S.No SymbolName
1 ( Lest parenthesis
2 ) Right parenthesis
3 { Left brace
4 } Right brace
5 [ Left bracket
6 ] Right bracket
7 < Left angle bracket
8 > Right angle bracket
9 ‘ Single quotes
10 “ Quotation mark
S.No Symbol Name
11 ` Apostrophe
12 / Forward slash or
slash
13 Back slash
14 | Vertical bar
15 = Equal to sign
16 + Plus sign
17 - Minus sign
18 * Asterisk
19 % Percent sign
20 ~ Tilde
S.No Symbol Name
21 _ Underscore
22 ! Exclamation mark
23 ^ Caret
24 # Number sign or hash
25 : Colon
26 ; Semicolon
27 ? Question mark
28 , Comma
29 . Period
30 $ Dollar sign
31 & Ampersand
21.
How does computerstore data in memory
• Word size
• computer’s preferred size for moving units of information
• Width of processor’s registers
22.
How integers arestored?
• unsigned integers – ranges from 0 to 65535
1111111111111111
0000000000000000
0 65535
23.
Cyclic view ofthe range of an unsigned
integer stored in a 16-bit word
24.
How integers arestored for signed numbers?
• signed integers – ranges from 0 to 65535
• Bits of the object representation are divided into three groups:
• Value bits
• Padding bits (optional)
• One sign bit
• If sign bit is 0, it is a positive number
• If sign bit is 1, it is a negative number; the value shall be modified in one of the
following ways.
• the corresponding value with sign bit 0 is negated (sign and magnitude);
• the sign bit has the value −(2^N) (2’s complement);
• the sign bit has the value −(2^N − 1) (1’s complement)
Type Casting
• Convertingan expression of a given type into another type.
• It is best practice to convert lower data type to higher data type to
avoid data loss.
• Data will be truncated when the higher data type is converted to
lower.
• For example, if a float is converted to int, data which is present after
the decimal point will be lost.
Type casting- Types
•Implicit conversion
• Automatically performed when a value is copied to a compatible type in the program.
• Example: int i=20; double p; p=i; printf(“%d”,p);
• Here, the value of ‘p’ has been promoted from int to double and we have not had to
specify any type-casting operator. This is known as a standard conversion.
• Explicit conversion
• Conversions that imply a different interpretation of the value, require an explicit
conversion. We have already seen two notations for explicit type conversion.
• They are not automatically performed when a value is copied to a compatible type in
the program.
33.
Tokens
• 5 types
•Identifiers
• Reserved words or keywords
• Operators
• Separators
• Constants
34.
Identifiers
• User definednames
• A symbolic name to refer a
• Variable
• Constant
• Function
• Arrays
• Structure
• Union
• etc
35.
Rules for definingvariables
• Should begin with an alphabet (lower case or upper case) or an
underscore
• Maximum length of variable name is 31 characters
• Should not be a keyword in C
• Blanks and other special characters are not allowed
Reserved words orKeywords
Reserved words or Keywords in C
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
38.
Operators- Properties
• Precedence
•Priority given to the operator
• When an expression contains many operators, the operations are carried out
according to the priority of the operators.
• Associativity
• Direction of execution
• When an expression has operators with equal precedence, the associativity
property decided which operation to be carried out.
Relational Operators
Operator MeaningExample
Return
Value
> Greater than 5>4 1
< Lesser than 5<4 0
>= Greater than or equal to 5>=4 1
<= Lesser than or equal to 5<=4 0
!= Not equal to 5!=4 1
== Equal to 5==4 0
Bitwise Operators
Operator MeaningExample Answer
& Bitwise AND 8&4 0
| Bitwise OR 8|4 12
^ Bitwise XOR 8^2 10
~ One’s complement ~8 7
>> Right Shift 8>>2 2
<< Left Shift 2<<3 16
>> - n=y/2^s
<< - n=y*2^s
For any integer n, bitwise complement of n will be -(n+1)
Comma operator
• Evaluationof multiple expressions
• Left to right
• The values are discarded except for the last one
• Syntax: expr_M= (expr_1, expr_2,… ,expr_N);
• Exmaple:
int i=0;
int j;
j= ( i +=1, i +=2, i+3);
48.
Operator precedence andAssociativity
Precedence Operator Meaning Associativity
1st
()
[]
->
.
Functional call
Array element reference
Indirect member selection
Direct member selection
Left to right
2nd
!
~
+
-
++
--
&
*
sizeof
(type)
Logical negation
Bitwise(1 's) complement
Unary plus
Unary minus
Increment
Decrement
Dereference Operator(Address)
Pointer reference
Returns the size of an object
Type cast(conversion)
Right to left
49.
Operator precedence andAssociativity
Precedence Operator Meaning Associativity
3rd
*
/
%
Multiply
Divide
Remainder
Left to right
4th +
-
Binary plus(Addition)
Binary minus(subtraction)
Left to right
5th <<
>>
Left shift
Right shift
Left to right
6th
<
<=
>
>=
Less than
Less than or equal
Greater than
Greater than or equal
Left to right
50.
Operator precedence andAssociativity
Precedence Operator Meaning Associativity
7th
==
!=
Equal to
Not equal to
Left to right
8th
& Bitwise AND Left to right
9th
^ Bitwise exclusive OR Left to right
10th
| Bitwise OR Left to right
11th
&& Logical AND Left to right
12th
|| Logical OR Left to right
13th
?: Conditional Operator Right to left
51.
Operator precedence andAssociativity
Precedence Operator Meaning Associativity
14th
=
*=
/=
%=
+=
-=
&=
^=
|=
<<=
>>=
Simple assignment
Assign product
Assign quotient
Assign remainder
Assign sum
Assign difference
Assign bitwise AND
Assign bitwise XOR
Assign bitwise OR
Assign left shift
Assign right shift
Right to left
15th
, Separator of expressions Left to right
52.
Example of precedence
(1> 2 + 3 && 4)
The expression above is equivalent to:
((1 > (2 + 3)) && 4)
i.e, (2 + 3) executes first resulting into 5
then, first part of the expression (1 > 5) executes resulting into 0 (false)
then, (0 && 4) executes resulting into 0 (false)
53.
Example of Associativity
1== 2 != 3
The expression above is equivalent to :
((1 == 2) != 3)
i.e, (1 == 2) executes first resulting into 0 (false)
then, (0 != 3) executes resulting into 1 (true)
Single character input– getchar()
• getchar()
• reads a single unsigned character from the standard input device.
• getchar() reads a single character from the input data stream; but does not
return the character to the program until the ‘n’ ( or ) key is pressed.
• The character, obtained from the input stream, is treated as an unsigned char
and is converted to an int, which is the return value.
• int getchar(void); //function for character input
• Example:
int ch;
ch = getchar();
58.
Single character input– getch() and getche()
• getch() - This input function reads, without echoing on the screen, a
single character from the keyboard and immediately returns that
character to the program.
• General statement form: ch = getch();
• getche() - This input function reads, with echo on the screen, a single
character from the keyboard and immediately returns that character
to the program.
• General statement form: ch = getche();
59.
Single character output– putchar(), putch()
• putchar()
• writes a single unsigned character on the standard output device
• int putchar(int c); //function of character output
• putch()
• putch() - This output function writes the character directly to the screen.
• On success, the function putch() returns the character printed.
• On error, it returns EOF.
• General statement form: putch(ch);
60.
Non formatted Inputand Output Functions
• gets()
• reads a stream of characters from the keyboard
• puts()
• writes a stream of characters on the screen
61.
Formatted Input andOutput Functions
• scanf()
• input function
• printf()
• output function
62.
printf()
• The firstparameter is always a control string
• General form:
• printf(“control_string”,variable1,variable2, variable3,...);
Parts of conversionspecifier field for printf()
• The percent sign and conversion
code are required
• Other modifiers such as width and
precision are optional.
• width specifies the total number of
characters used to display the
value
• precision indicates the number
of characters used after the
decimal point. The precision
option is only used with floats or
strings.
• Default precision value is 6
Flag characters usedin printf()
Flag Meaning
- Left justify the display
+ Display positive or negative sign of value
Space Display space if there is no sign
0 Pad with leading zeros
# Use alternate form of specifier
scanf()
• General form:
•scanf(“control_string”,variable1_address, variable2_address,...);
• scanf() returns the number of input fields successfully scanned,
converted, and stored
• May contain
• White space
• ordinary character string
• conversion specifier field
72.
scanf()
• The formatstring in scanf() has the following general form:
• “< character string >< % conversion specifier field >”
• Character string is optional
• Each ‘conversion specifier field’ is coded as follows:
• %[*] <width><size><conversion-code>
• * - states that the value being read will not be assigned to an
argument, but will be dropped.