Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Cunit I

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 10

1 introduction to C

Definition of ‘C’:- C is a general-purpose programming language that is extremely popular,


simple, and flexible to use. It is a structured programming language that is machine-independent
and extensively used to write various applications and Operating Systems.

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.

Definition Section:-the definition section defines all symbolic constants

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

F)Identifiers in C langu/age:-Each program elements in a C program are given a name called


identifiers.Names given to identify Variables, functions and arrays are examples for identifiers.
eg. x is a name given to integer variable in above program.

Rules for constructing identifier name in C:

1. First character should be an alphabet or underscore.


2. Succeeding characters might be digits or letter.
3. Punctuation and special characters aren’t allowed except underscore.
4. Identifiers should not be keywords.

G)Keywords in C language:-

 Keywords are pre-defined words in a C compiler.


 Each keyword is meant to perform a specific function in a C program.
 Since keywords are referred names for compiler, they can’t be used as variable name.

C language supports 32 keywords which are given below.

auto double int struct const float short unsigned


break else long switch continue for signed void
case enum register typedef default goto sizeof volatile
char extern return union do if static while

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

const data_type variable_name; (or) const data_type *variable_name;

Types of C constant:

1. Integer constants
2. Real constants
3. Character and String constants
4. Backslash character constants

1. Integer Constants in C:

 An integer constant must have at least one digit.


 It must not have a decimal point.
 It can either be positive or negative.
 No commas or blanks are allowed within an integer constant.
 If no sign precedes an integer constant, it is assumed to be positive.
 The allowable range for integer constants is -32768 to 32767.

2. Real constants in C:

 A real constant must have at least one digit


 It must have a decimal point
 It could be either positive or negative
 If no sign precedes an integer constant, it is assumed to be positive.
 No commas or blanks are allowed within a real constant.

3. Character and string constants in C:

 A character constant is a single alphabet, a single digit or a single special symbol


enclosed within single quotes.
 The maximum length of a character constant is 1 character.
 String constants are enclosed within double quotes.

4. Backslash Character Constants in C:

 There are some characters which have special meaning in C language.


 They should be preceded by backslash symbol to make use of special function of them.
4 introduction to C

J)Input and Output Statements:-

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

The following primitive data types in c are available:

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:

Syn:- int variable_name;


5 introduction to C

ii) Float data Type(float):-Float data type declares a variable that can store numbers containing a
decimal number.

Syn:- float variable_name;

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;

2) User Defined Data Types in C:-


i) typedef:-
The first keyword we shall explore is typedef. With this keyword we can define a new
type, and give it a user-friendly name. For example:
Typedef int Boolean;

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

== equal to score == mark


!= not equal to 5 != 4
3)Logical operators:-In addition to the relational operators, C has the following three logical
operators. The logical operators && and || are used when we want to test more than one
condition and make decision.

Operator Description Example


&& Logical AND If(1< i && I <10) ...
|| Logical OR If (i <5 || I =5) ...
! Logical NOT If ( i !=5) ...

4)Assignment operators:-Assignment operators are used to assign the result of an expression to a


variable.
Operator Description Example
= Assignment Operator a= 5

5) Increment and decrement operators:-C provides shortcuts to add or subtract constant 1 to a


variable.

Operator Description Example


++ Increment a=a++
-- Decrement a=a--

6)Conditional operators:-The conditional operator (ternary operator) pair “?:” is available in C


to construct conditional expressions of the form
Expr1?expr2:expr3
For example
a=100;
b=200;
c=(a>b)?a:b;
Result: c=200
This is same as if..else statement as follows:
if (a>b)
c=a;
else
c=b;
8 introduction to C

7) Bitwise operators:-The bitwise operators operate on integers thought of as binary numbers or


strings of bits. These operators let us work with the individual bits of a variable; one common
use is to treat an integer as a set of single-bit flags.

Operator Description Example


& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
<< Shift left
>> Shift right

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.

Operator Description Example


Value=(x=10, y=5, x+y) Results:
, Comma Operator
value=15
sizeof() Sizeof() Operator k= sizeof(235L)

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

N)Select Control Statement:-

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;

case 8: printf("Value is 8");


break;

case 9: printf("Value is 9");


break;

default : printf("out of range");


}
getch();
}

You might also like