Cunit I
Cunit I
Cunit I
1) Structure of C Program:-
Documentation Section
Link Section
Definition Section
Global Declaration Section
Main( ) Function Section\=
{
Declaration Part
Executable Part
}
Sub Program Section
Function 1
Function 2
..
..
Function n
Documentation Section:-the documentation section consists of a set of comment line giving the
name of the program, the author and other details.
Link Section:-the section provides instructions to the compiler to link functions from the system
library.
Global Declaration:- there are some variables that are used in more then one function. Such
variables are called global variables and are declared in the global declaration section that is
outside of all the functions.
Main( ):- Every c program must have one main function section. This section contains two
parts, declaration part and executable part, the declaration part declares all the variable used in
the executable part. There is at least one statement in the executable part. These two part must
appear between the opening and closing of braces.
Subprogram section:-the subprogram section contains all the user defined functions that are
called in the main function. User defined functions are generally placed immediately after the
main function.
2 introduction to C
G)Keywords in C language:-
H)Variable:-C variable is a named location in a memory where a program can manipulate the
data. This location is used to hold the value of the variable.The value of the C variable may get
change in the program.C variable might be belonging to any of the data type like int, float, char
etc.
Rules for naming C variable:
1. Variable name must begin with letter or underscore.
2. Variables are case sensitive
3. They can be constructed with digits, letters.
4. No special symbols are allowed other than underscore.
5. sum, height, _value are some examples for variable name
I)Constants:-C Constants are also like normal variables. But, only difference is, their values can
not be modified by the program once they are defined. Constants refer to fixed values. They are
also called as literals. Constants may be belonging to any of the data type.
Syntax:
3 introduction to C
Types of C constant:
1. Integer constants
2. Real constants
3. Character and String constants
4. Backslash character constants
1. Integer Constants in C:
2. Real constants in C:
printf() and scanf() functions are inbuilt library functions in C which are available in C
library by default. These functions are declared and related macros are defined in
“stdio.h” which is a header file.
We have to include “stdio.h” file as shown in below C program to make use of these
printf() and scanf() library functions.
1. C printf() function:
printf() function is used to print the “character, string, float, integer, octal and
hexadecimal values” onto the output screen.
We use printf() function with %d format specifier to display the value of an integer
variable.
Similarly %c is used to display character, %f for float variable, %s for string
variable, %lf for double and %x for hexadecimal variable.
To generate a newline,we use “\n” in C printf() statement.
C scanf() function:
scanf() function is used to read character, string, numeric data from keyboard
Consider below example program where user enters a character. This value is assigned to
the variable “ch” and then displayed.
Then, user enters a string and this value is assigned to the variable ”str” and then
displayed.
K)Data type: The data type is a collection of data with values having fixed values, meaning as
well as its characteristics. There are three basic data types are
1)Primitive Data Types:-The primitive data types in c language are the inbuilt data types
provided by the c language itself. Thus, all c compilers provide support for these data types.
i) Integer Data Type(int):-Integer data type is used to declare a variable that can store numbers
without a decimal. The keyword used to declare a variable of integer type is “int”. Thus, to declare
integer data type following syntax should be followed:
ii) Float data Type(float):-Float data type declares a variable that can store numbers containing a
decimal number.
iii) Double Data Type(double):-Double data type also declares variable that can store floating
point numbers but gives precision double than that provided by float data type. Thus, double data
type are also referred to as double precision data type.
Syn:-Double variable_name;
iv) Character Data Type(char):-Character data type declares a variable that can store a
character constant. Thus, the variables declared as char data type can only store one single
character.
Syn:- char variable_name;
V) Void Data Type(void):-void data type does not create any variable but returns an empty set of
values. Thus, we can say that it stores null.
Syn:-Void variable_name;
This code tells the compiler that we want to define a type Boolean, that is equivalent to
an integer. In order to use this new type, we could use code similar to:
Boolean bResult;
if (bResult == 0)
{
printf ("False\n");
}
ii) Enumerated Types (enum):-An enumerated data type is a list of possible values,
each of which is assigned a sequential number. This allows us to write code that can
compare values easily. So, for our Boolean example:
typedef enum {FALSE, TRUE} Boolean;
This tells the compiler that we would like a Boolean type that evaluates to FALSE, or
TURE. The compiler will assign the values 0 and 1 to these new types, enabling comparisons
such as:
Boolean bResult;
if (bResult == FALSE)
6 introduction to C
{
printf ("False\n");
}
3)Derived Data Type:-Those data types which are derived from fundamental data
types are called derived data types.
There are basically three derived data types .
i) Array:-An array is a group of similar kinds of finite entities of the same type. These entities or
elements can be referred to by their indices respectively. The indexing starts from 0 to
(array_size-1) conventionally
Syn:Datatype var[size];
Example: int x[20];
ii) Functions:-A function is a piece of code that performs some specific task when invoked in
the program. It can be called from anywhere and any number of times in the program.
Syntax:-return_type function_name(parameters);
iii)Pointers:-A pointer can be defined as a variable that stores the address of other variables.
This address signifies where that variable is located in the memory.
Syntax:-type *pointer_name;
L)Operators:-The symbols which are used to perform logical and mathematical operations in a
C program are called C operators.These C operators join individual constants and variables to
form expressions.They are classified into following categories:
1) Arithmetic operators:- C provides all the basic arithmetic operators which are
following:
Operator Description Example
+ Addition or unary plus 5+4 Result 9
- Subtraction or unary minus 5-4 Result 1
* Multiplication 5*4 Result 20
/ Division 5/4 Result 1
% Modulo division 5/4 Result 1
2)Relational operators:-C provides all the basic relation operators to compare two or more
quantities and depending on their relation taking decision. These operators and their meanings are as
below.
Operator Description Example
> greater than 5>4
>= greater than or equal to mark >= score
< less than height < 75
<= less than or equal to height <= input
7 introduction to C
8)Special operators:-C supports some special operators like ‘,’ and sizeof() operators.
The ‘,’ (comma) operator can be used to link the related expression together.
The sizeof() is a compile time operator and, when used with an operand it returns the numbers of
bytes the operand occupies. The operand may be variable, a constant or a data type qualifier.
M)Condtional Statements:-
i)If Statement:-In “if” control statement, respective block of code is executed when condition is
true
1 int main()
2 {
3 int m=40,n=40;
4 if (m == n)
5 {
6 printf("m and n are equal");
7 }
8 }
Output:
m and n are equal
9 introduction to C
ii) if else statement:- In C if else control statement, group of statements are executed when
condition is true. If condition is false, then else part statements are executed.
1 #include <stdio.h>
2 int main()
3 {
4 int m=40,n=20;
5 if (m == n)
6 {
7 printf("m and n are equal");
8 }
9 else
10 {
11 printf("m and n are not equal");
12 }
13
14 }
Output:
m and n are not equal
iii) nested if statement:- In “nested if” control statement, if condition 1 is false, then condition 2
is checked and statements are executed if it is true.
If condition 2 also gets failure, then else part is executed.
1 #include <stdio.h>
2 int main()
3 {
4 int m=40,n=20;
5 if (m>n) {
6 printf("m is greater than n");
7 }
8 else if(m<n) {
9 printf("m is less than n");
10 }
11 else {
12 printf("m is equal to n");
13 }
14 }
Output:
m is greater than n
10 introduction to C
Switch statement:- Switch statement is used to solve multiple option type problems for menu
like program, where one value is associated with each option. The expression in switch case
evaluates to return an integral value, which is then compared to the values in different cases,
where it matches that block of code is executed, if there is no match, then default block is
executed. The general form of switch statement is,
switch(expression)
{
case value-1: block-1;
break;
case value-2: block-2;
break;
case value-3: block-3;
break;
case value-4: block-4;
break;
default-block;
}
Example of Switch Statement
#include<stdio.h>
#include<conio.h>
void main( )
{
int num=8;
clrscr( );
switch(num)
{
case 7: printf("Value is 7");
break;