Module 1-2
Module 1-2
1. Documentation section:
It consists of a set of comment lines which provides the name of the program,author and
other details which the programmer would like to use during maintenance stage. The
comments are enclosed in a C Program using /* and */
2. Preprocessor directive or Link section:
It provides instruction to the compiler to link some functions or do some processing
prior to the execution of the program. It is also used to define symbolic constants of the
program.
Header file used in the example is a standard input/output file(stdio.h). Programs must contain an
#include line for each header file. #include <stdio.h>
1
5. Sub program section:
It contains all the user defined functions that are called in the main () function. User
defined functions are generally placed after the main () function, although they may
appear in any order.
Identifiers:
Identifiers are the names given by user to various program elements like
variables, constants, functions and arrays.
Keywords
Keywords are already defined and informed to C Compiler. So, it cannot be used as variable
names. For example, char is a keyword. It informs the data type of a variable as character. It
is also known as Reserve words. Some of the keywords are given below.
Auto Extern Size of
Break Float Static
Case For Struct
Char Goto Switch
Const If Typedef
Continue Int Union
Default Long Unsigned
Do Register Void
Double Return Volatile
Else Short While
Enum Signed
Constants:
• A constant is a value or an identifier whose value cannot be altered or change in a program.
2
For example: 1, 2.5,
• As mentioned, an identifier also can be defined as a constant. eg. float PI = 3.14
• Here, PI is a constant. Basically, what it means is that, PI and 3.14 is same for this program.
3
Exponential form or Scientific form:
In exponential form, the real constant is represented in two parts.
Mantissa - The part appearing before e, the mantissa is either a real number
expressed in decimal notation or an integer.
Exponent - The part following e, the exponent is an integer with an optional
plus or
minus sign followed by a series of digits. The letter e separating the mantissa and the
exponent can be written in either lowercase or uppercase.
Example: 0.000342 can be represented in exponential form as 3.42e-4
7500000000 can be represented in exponential form as 7.5e9 or 75E8
3.Character Constants:
4.String constant:
String constants are the constants which are enclosed in a pair of double-quote
marks. Note that a character constant ‘A’ and string constant “A” are not equal.
Example:
Variables:
A variable is an identifier which is used to store values. Value of variable may
change during program execution. Variables are assigned a particular memory
location in memory unit where values can be stored.
Datatypes in C
4
1. Basic data types
2. User defined data types
3. Derived data types
5
Data type qualifiers and their memory requirements
Declaration of variables:
All variables must be declared before they appear in executable statements. The
general form of variable declaration and some examples are given below.
Syntax:
Data type ‘variable name list’;
Example:
Age=32
Salary=12500.
50
Initialization of variables:
The process of giving initial value to the variable is known as initialization of a variable.
Syntax:
Data type variable name= initial
value;
Example:
int x=20;
float y=100.45;
C Operators
C operators: An operator is a symbol that informs the computer to perform a particular
arithmetic or logical manipulations.
Different types of C operator are given below.
6
Arithmetic
Relational
Logical
Assignment
Increment and Decrement
Conditional
Bitwise
Arithmetic Operators:
C provides all the basic arithmetic operators like + , - , * , / , %
Integer Arithmetic:
When the operands in an expression are integers then the operation is known as Integer
Arithmetic. It always results an integer value. Example, x=40, y=4
Operator Purpose Example Result
+ Addition x+y 44
- Subtraction x-y 36
* Multiplication X*y 160
/ Division x/y 10
% Modulus (produces x%y 0
remainder of
division)
Relational Operator:
These operators can be used to compare arithmetic,logical and character
expressions. The value of relational expression can be either one or zero.
The relational operators in C are
Operator Meaning Example Result
< Less than 20<40 1
7
<= Less than or equal to 20<=20 1
> Greater than 20>40 0
>= Greater than or equal 20>=20 1
to
== Equal to 20==20 1
!= Not equal to 20!=20 0
Logical Operator:
Logical operators are used when we want to test more than one condition and make
decisions. The operands can be constants,variables and expressions. Example, x=1
and y=0
int i=1;
printf(“i=%d \n”,i);
printf(“i=%d \n”,++i);
printf(“i=%d \n”,i);
Result:
i=1
i=2
i=2
Conditional Operator:
C has special operator called ternary or conditional operator. If the condition is true
then value1 is assigned to the variable, otherwise value2.
Syntax:
Variable=(condition)?value1:value2
Example :
large=(x>y)?x:y;
For example, assume x=6 and y=8 and their values in binary
9
& Bitwise AND x&y=00000000
| Bitwise OR x|y=00001110
10
Table of conversion specifier characters
1) Integer Input
: We input decimal integer data using %d character.
Example: scanf(“%d”, &number1);
2) Character Input : We input character data using %c conversion character. The string
constant requires %s conversion character.
Exampe: scanf(“%c”, §ion) ;
3) Floatingpoint Input : We input floating point values by using %f conversion
character and in contrast to integer type we usually do not specify the field width for
real numbers.
Example: scanf(“%f”, &rate);
Formatted Output Function: Formatted output function is printf()function. This function
displays data on statndard output and that is monitor.
Syntax:
printf(“string of control”,variable);
string of control: This specifies the data type and format of values which are going to be
displayed on standard output
list_of_variables: This specifies list of variables associated with values meant to be
displayed on monitor
formatted output functions
1) Integer type: We use %d conversion character to display the integer type data on standard
output. It is similar to formatted input but scanf is replaced by printf and there is no (&)
ampersand symbol.
Example: printf(“%d”, number1);
2) Character type: We need the same printf function to display a character or a string.
11
Example: printf(“%c”, section);
3) Floating point type: Floating point number is displayed either in decimal form or scientific
notation. Usually we use %f as conversion character.
Example: printf(“%f”, z);
getchar()
gets()
putchar()
puts()
1. The putchar() function: This function is responsible for printing only one character
on monitor and the supported data type is char.
Format: putchar(character);
2. The puts() function: This function prints sequence of characters on monitor. The end
of string is newline character and it is not displayed on monitor.
Format: puts(str);
TYPE CASTING:
Type casting is the process in which the compiler automatically converts one data type in a program
to another data type. Type conversion is another name for type casting. The method of type casting helps
users to convert the values present in any data type into another data type with the help of the cast operator,
like:
(type_name) expression
Example:
int val_1;
float val_2;
// Body of program
val_2 = (float) val_1; // type casting of the values
Here the integer type Val_1 is converted in to float type .
12