0% found this document useful (0 votes)
6 views12 pages

Module 1-2

The document outlines the structure of a C program, detailing sections such as documentation, preprocessor directives, global declarations, the main function, and subprograms. It also covers C language fundamentals including identifiers, keywords, constants, variables, data types, and operators. Additionally, it explains input/output statements and provides examples for better understanding.

Uploaded by

anamayank111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views12 pages

Module 1-2

The document outlines the structure of a C program, detailing sections such as documentation, preprocessor directives, global declarations, the main function, and subprograms. It also covers C language fundamentals including identifiers, keywords, constants, variables, data types, and operators. Additionally, it explains input/output statements and provides examples for better understanding.

Uploaded by

anamayank111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Structure of C Program

The structure of a C program is given below.

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>

3. Global Declaration section:


There are some variables that are used in more than one function. Such variabls are called
global variables and are declared in this section.
4. Main()function section:
Every C Program must have one main() function. This section contains two
parts,declaration part and executable part. The declaration part declares all the variables
used in the executable part. There is at least one statement in the executable part. These
two parts must appear between the opening and closing braces. All statements in the
declaration part and executable parts must end with a semicolon; is a statement terminator.

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.

Variables, Constants and Data types

C Character set or C tockens


Character set of C Language is given below
1 Alphabets Uppercase A …. Z and lower case a.
...................................................... z
2 Digits 0.…9
3 Special characters ,.;:?‘“!|\/~_$%#&^*-+<>
{}[]()
4 White spaces Blank space, carriage return,
horizontal
tab, new line and form feed

Identifiers:
Identifiers are the names given by user to various program elements like
variables, constants, functions and arrays.

General rules for identifiers :


 Letters, digits and underscore can be used.
 Must start with an alphabet
 Underscore should not be used in the beginning of identifier
 Keywords cannot be used as identifiers
 Identifiers are case sensitive. For example, salary is different from SALARY

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.

There are four basic types of constants in C. They are


i) integer constants
ii) floating point constants
iii) character constants
iv) string constants.
Integer Constants:
It is an integer valued number. It is classified into three different number systems.
They are
(i) decimal constants
(ii) octal constants
iii)hexadecimal constants.

General Rules for Integer constants:


 Constant must have at least one digit
 Must not have decimal point
 Constant can be preceded by minus(-) sign or positive(+) sign
 Commas and blank spaces cannot be included within the constant
 Value of constant should not exceed the minimum and maximum limit. Range of
integer constant is -32768 to 32767.

1.Decimal Integer constants:


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.

Octal Integer Constants


An octal integer constant is a combination of digits taken from the set 0 to 7. In this
case, first digit must be 0.

Hexadecimal Integer constants:


A hexadecimal integer is identified by ox or OX. Hexadecimal integer constant consists
of digits taken from the set of 0 to 9 and letters taken from the set A to F (supports both
upper and lower case). The letter a to f or A to F represent the decimal values from 10
to 15 i.e. a=10, b=11, c=12, d=13, e=14, f=15.

2.Floating point constants


It is also known as “real constants”. The real constant can be in two
form namely fractional form and exponential form.
It is used to represent values like distance, height, temperature, price, etc.,
Fractional form or Normal form:
It consists of a number followed by a decimal point and the fractional part.

Valid Real constants (Fractional): 0.0 -0.1 +123.456 .2 2.

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:

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

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:

"Welcome" String constant

"A" String constant having single


character
"Hello Prints string with newline
world\n"
“" Null string constant

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.

Rules for declaring a variable:

 It must begin with a letter


 It must consist of single letter or set of letters,digits or underscore letter
 It is case sensitive. For example, salary,SALARY and Salary are not same.
 Keywords are not allowed
 Blank space is not allowed
 Special characters other than underscore are not permitted

Datatypes in C

C supports following categories of data types:

4
1. Basic data types
2. User defined data types
3. Derived data types

Basic data types:


C supports following fundamental data types.
 Character
 Integer
 Float
 Double

Data type Description Memory


requirement
Char Single character 1 byte (8 bit)
Int Integer quantity 2 bytes (16bit)
Float Floating point number 4 bytes (32 bit)
Double Double precision 8 bytes (64 bit)
floating
point number

Data type Qualifiers


It is used to change the meaning of basic data
types.
The qualifiers used in C are :
 Signed
 Unsigned
 Long
 Short

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’;

Variables of same data type can be declared in the following method.


Example:
int x,y,z;
float a,b;
Assigning values to variables:
Assignment operator “=” is used to assign values to variables.
Syntax:
Variable name=value;

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)

Write a program to illustrate the use of all arithmetic operators


#include<stdio.h>
main()
{
int add,sub,mul,div,mod,x,y;
printf(“Enter values of x,y: “);
scanf(“%d %d”,&x,&y);
add=x+y;
printf(“Result of addition : %d”,add);
sub=x-y;
printf(“Result of subtraction : %d”,sub);
mul=x*y;
printf(“Result of multiplication : %d”,mul);
div=x/y;
printf(“Result of division : %d”,div);
mod=x%y;
printf(“Result of modulus : %d”,mod);
}

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

Operator Meaning Example Result


&& AND operator. x&&y 0
If both the operands
are one,then result is
1
|| OR operator. If any x||y 1
one of the operand is
one,then result is 1.
! NOT operator. If a !(x&&y) 1
condition is true,then
NOT operator will
make it 0.
Assignment Operator:
 It is used to assign the results of an expression into a variable. “=” is the
assignment operator.

Short hand assignment operator:


C provides “short hand” assignment operators in the following form

Variable operator = Expression

Short hand Meaning Example


Operator
+= It will add both left operand and right side x+=1
value and result is stored in left operand
-= It will subtract right side value from left x-=1
operand and result is stored in left operand
*= It will multiply both left operand and right x*=n+1
side
expression and result is stored in left operand
/= It will divide left operand by right side x/=n+1
expression and result is stored in left operand
%= It will divide left operand by right side x%=n+1
expression and remainder is stored in left
Operand

Increment and Decrement operators:


 Increment operator (++) adds 1 to its operand and decrement operator (--)
8
subtracts one from its operand.
For example,
Y=y+1 is written
as Y=y++
Different types of increment and decrement operators:
1. Prefix increment(++i) In this case, first increment and then do operation.
2. Postfix increment(i++) In this case, first do the operation and then increment.
3. Prefix decrement(--i) In this case, first decrement and then do operation.
4. Postfix decrement(i--) In this case, first do the operation and then decrement.

Example: Prefix increment

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;

Above statement is equal to following code:


If(x>y) large=x;
else large=y;
Bitwise Operators:
 The Bitwise operators are used to perform bit operations. All the decimal values will be
converted into binary values (sequence of bits i.e. 0100, 1100, 1000, 1001 etc) and
bitwise operator will work on these bits such as shifting them left to right or converting
bit value from 0 to 1 etc. Following table shows the different Bitwise operators in C.

For example, assume x=6 and y=8 and their values in binary

form are, x=00000110 , y=00001000

9
& Bitwise AND x&y=00000000

| Bitwise OR x|y=00001110

^ Bitwise exclusive OR x^y=00001110

~ Bitwise complement ~x=00001001

<< Shift Left x<<1=00001100

Shift right y>>1=00000100

(Bits will move 1 step right. If we use Write a


2 program to
or 3 then bits shift accordingly
I/O
Statements
All the input and output functions are defined in the “stdio.h” header file.

Types of input/ouput statements are given below

 Formatted input/output statements


 Unformatted input/output statements

Formatted Input Statements


Scanf() function: The scanf() function is to used to input values for variables of numeric,
string and character data types.
Syntax:
scanf("string of control", address);

string of control: Sequence of character groups is entered here. This is usually a


combination of one or more conversion characters following % symbol and this describes
types of values which are to be specified to the variables.
address: This is a list of addresses of variables which represents the address of memory
locations where the values are stored.

10
Table of conversion specifier characters

Examples of formatted input functions

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”, &section) ;
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);

Unformatted Input Function


These functions read character type data from keyboard and we have two functions namely:

 getchar()
 gets()

And both of these functions are included in stdio.h header file.


1. The getchar() function: This function is responsible for reading only one
character from standard input. We do not need parameter within the brackets.
Format: character=getchar();
2. The gets() function: This function is responsible for accepting input from keyboard and
continues to accept input until enter key or return key is pressed. Whatever we type is stored
as string.
Format: gets(str);
Unformatted Output Function
These functions will print vales of character data type on monitor. We have two functions
and they are also stored in stdio.h header file:

 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

You might also like