Unit I C
Unit I C
The main features of C language include low-level access to memory, a simple set of
keywords, and clean style, these features make C language suitable for system
programming like an operating system or compiler development.
Most of the programs of UNIX are written and run with the help of 'C'.
Many of the important ideas of 'c' stem are from BCPL by Martin Richards.
Many of the ideas of C language were derived and taken from 'B' language. BCPL and
CPL are previous versions of 'B' language.
1. History of C
C was developed by Dennis Ritchie at the Bell Laboratories in 1972. Added new features
and concepts like “data types”.
The committee approved a version of C in December 1989 which is now known as ANSI
C.
In 1990 International Standards Organization (ISO) has approved C and this version of C
is referred to as C89.
In 1999 improved version of C99 was introduced, but many compilers do not support all
of the new features incorporated in C99.
It is a robust language with rich set of built-in functions and operators that can be used to
write any complex program.
Programs Written in C are efficient and fast. This is due to its variety of data type and
powerful operators.
C is easy to debug.
C is highly portable.
This means that programs written for one computer can be run on another with no
modification.
C language is structures programming. Thus requiring the user to think of a problem in
terms of function modules or blocks.
3. Structure of C Programs
• The documentation section consists of set of comment lines giving the name of the
program, the author details.
• The link section provides instruction to the compiler to link functions from the system
library.
• The declaration part declares all the variables used in executable part.
• These two parts must appear between the opening and closing braces.
• The program execution begins at the opening brace and ends at the closing brace.
• The closing brace of the main function section is the logical end of the program.
• All statements in the declaration and executable parts must ends with a semicolon.
• The subprogram section contains all the user defined function that are called in main
function.
• User defined function are generally placed immediately after the main function.
Sample C program
4. Character Set
The characters that can be used to form words, numbers and expressions depend upon the
computer on which the program is run. The characters in C are grouped into the following
categories:
1. Letters
2. Digits
3. Special characters
4. White spaces
The sequence consists of three characters ( two question mark followed by another character)
5. Tokens
6. Keywords
7.Identifiers
• These are user-defined names and consist of a sequence of letters and digits, with a letter
as a first character.
8.Constant
• A constant in C refer to a fixed value that doesn't change during the execution of a
program.
Numeric Constants
Numeric constants, as the name itself indicates, are those which consist of numerals. They are
further divided into two types:
• They are decimal constants (base 10), Octal constants (base 8) and hexadecimal
constants (base 16).
Decimal Integer
• A decimal integer constant consists of any combination of digits taken from the set
0 to 9.
• If the constant consists of two or more digits, the first digit must be other than 0.
• Valid example
Octal Integer
• An octal integer constant consists of combination of digits taken from the set 0
through 7.
• However the first digit must be 0, in order to identify the constant as an octal number.
• 00 06 0753 0663
• Hexadecimal integer constant consists of digits taken from the set of 0 to 9 and also
include alphabets from A to F (either upper or lower case).
Real constant
• In real constant, numbers are shown in decimal notation, having a whole number
followed by decimal notation and fractional part.
• Use of blank space and comma is not allowed between real constants.
• When representing a number in its exponential form, the real constant is represented in
two parts : the mantissa and the exponent.
• The part of the number appearing before the alphabet e is the mantissa and the part
following e is the exponent.
• mantissa e exponent
• The mantissa can be positive or negative with the default sign of the mantissa being
positive.
• - The exponent should have atleast one digit which is a positive or negative integer.
• A Single Character constant contains a single character enclosed within a pair of single
quote marks.
String Constant :
“Hello”
“WELL DONE”
C supports some special backslash character constants that are used in output functions.
A list of backslash character constants is given in the table.
Constants Name
\a Alert (bell)
\b Backspace
\f Form feed
\n New line
\r Carriage returns
\t Horizontal tab
\v Vertical tab
\’ Apostrophe
\” Double quote
\? Question mark
\\ Backslash
\0 Null character
9. Datatypes
Primary Datatype
Modifiers are used to alter the meaning of basic data types to fit various needs. Except
the type void, all others data type can have various modifiers preceding them
i) Signed
ii) Unsigned
iii) Long
iv) Short
Integer types
Integers are whole numbers. C has three classes of integer storage namely, short int, int
and long int in both signed and unsigned forms.
Floating point numbers are real numbers with 6 digits precision. A double data type is a
precision of 14 digits.
Character types
A single character can be defined as character type data. The qualifier signed and
unsigned may be explicitly applied.
Void types
The void has no values. This is usually used to specify the type of functions. The type of
function is said to be void it does not return any value to the calling program.
The table shows the allowed combination of basic data types and qualifiers and their size
and range of values
Those data types which are defined by the user as per his/her will are called user-defined data
types. Examples of such data types are structure, union and enumeration.
1. typedef
C supports a feature known as type definition that allows users to define an identifier
that represent an existing data type. It takes the general form as
Examples
Enumeration is a special data type that consists of integral constants, and each of them is
assigned with a specific name. "enum" keyword is used to define the enumerated data type. It is
defined as follows
The identifier is a user-defined enumerated data type which can be used to declare variables that
can have one of the values enclosed within the braces. After definition, variables can be declared
to this type.
Example
10.Variables
Rules
The declaration of variable must be done before they are used in the program.
Declaration of variable does two things
Datatype variablename;
where data type indicates the type and v1, v2, ..., vn indicate the names of the variables.
If you are declaring multiple variables in the same type declaration, then they should be
separated by commas. The type declaration statement itself should end with the semicolon.
int a;
char first;
double ratio;
int sum;
Here in the first statement the variable a is declared to be of type int, percent and average are real
variables whose type is declared as float, first is a character variable whose type is declared as
char.
If two are more variables of the same data type has to be declared they can be clubbed
as shown in example given below.
int a, b, c;
float d, e, f;
variablename = value;
Example
It is also possible to assign the value of an expression on the right to the variable on the left.
c = a + b;
z = p/q * r;
p = p + 10;
Variables can also be assigned values at the time of their declaration. The following
statements are valid in C :
int i = 10;
float pi = 3.14;
11.Operators
an operator is a symbol that tells the compiler or interpreter to perform a specific mathematical, logical,
or manipulative operation on one or more operands (values or variables).
1. Operands: These are the variables or values on which the operator acts. For example, in
a + b, a and b are operands, and + is the operator.
2. Expressions: Operators combine with operands to create expressions. For example, a +
b is an expression that adds a and b.
Types of operators
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Increment and Decrement Operators
Conditional (Ternary) Operator
Sizeof Operator
Comma Operator
1. Arithmetic Operators
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (remainder)
Note: The division operator (/) yields an integer quotient if both operands are integers;
otherwise, it gives a floating-point result.
Example program
2. Relational Operators
Operator Description
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
Examples :
Expression Evaluate to
3. Logical Operators
Operator Description
&& Logical AND (true if both are true)
|| Logical OR
! Logical NOT (reverses truth value)
Logical NOT
Logical AND
Logical OR
Operator Description
++ Increment (adds 1)
-- Decrement (subtracts 1)
Prefix vs. Postfix: ++a (prefix) increments before using the value, while a++ (postfix) uses the
value first, then increments.
7. Conditional (Ternary) Operator
Syntax Description
condition ? expr1 : expr2 If condition is true, expr1 is evaluated; otherwise, expr2.
Bitwise Operators
Operator Description
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
~ Bitwise NOT
<< Left shift
>> Right shift
Sizeof Operator
Syntax Description
sizeof(data_type) Returns the size in bytes of the specified type
Formatted Input
Formatted functions facilitate supplying input in fixed formats. Formatted
input refers to an input data that has been arranged in a particular format. The
general form of scanf is:
%d : Integer
%f : Floating-point
%c : Character
%s : String
%lf : Double
Example: scanf() to read integer, real (floating-point), character, and string inputs in C:
#include <stdio.h>
int main() {
int integer;
float real;
char character;
char string[50]; // Large enough to hold the input string
return 0;
}
Explanation:
1. Width Specifier: Defines the minimum width for printing a value (e.g., %5d for an
integer with at least 5 spaces).
2. Precision: Controls the number of digits after the decimal point for floating-point values
(e.g., %.2f for two decimal places).
3. Alignment: By default, printf() right-aligns values. Left alignment can be specified
with - (e.g., %-5d).
int main() {
int age = 25;
float height = 5.923;
char initial = 'A';
char name[] = "Alice";
return 0;
}
Output Explanation
5 Mark
15 Mark