C_Unit1
C_Unit1
Programming in C
UNIT_I
A computer language have been evolved from low level to high level language.
➢ Machine language
➢ Assembly language
➢ High level language
➢ High level language: High level language is the upper level language and also known as
third generation programming language. It does consider as high level because, which
language comes under this category are closer to human languages. Hence this is highly
understood programming language by human. There have many examples of high level
languages such as, FORTRAN, Pascal, C, C++, JAVA, ADA, COBOL, LISP, Prolog etc.
Eg: C = A + B;
3) Discuss about Software Development and flowcharts?
➢ Testing: The software is thoroughly tested to ensure that it meets the requirements and
works correctly.
➢ Deployment: After successful testing, The software is deployed to a production
environment and made available to end-users.
➢ Maintenance:This phase includes ongoing support, bug fixes, and updates to the
software.
Flowcharts:
✓ A flowchart is a diagram that depicts a process, system or computer algorithm. They
are widely used in multiple fields to document, study, plan, improve and communicate
often complex processes in clear, easy-to-understand diagrams.
✓ Flow charts, use rectangles, ovals, diamonds and potentially numerous other shapes to
define the type of step, along with connecting arrows to define flow and sequence.
✓ In other word a pictorial representation of a textual algorithm is done using a
flowchart.It is a pictorial representation of algorithm.
4) What is Number system?Explain about Binary, Octal, hexadecimal
number system?
➢ Number System: The number system or the numeral system is the system of
naming or representing numbers. We know that a number is a mathematical
value that helps to count or measure objects and it helps in performing various
mathematical calculations. There are different types of number systems in
Maths like decimal number system, binary number system, octal number
system, and hexadecimal number system.
➢ Binary number system: The base of the binary number system is 2 it uses
only two digits-0 and 1.Data represented in the computer system by either the
presence or absence of electronic signals.In the binary system all number
expressed as a groups of binary digits i.e, groups of 0’s and 1’s.The value are
based on the right or left position of digits in a binary number,using power of
2 as position value.
Octal Binary
0 000
1 001
2 010
3 011
4 100
5 101
6 110
7 111
➢ Hexadecimal number system: The word hexadecimal can be divided into 'Hexa' and
'deci', where 'Hexa' means 6 and 'deci' means 10. The hexadecimal number system is
described as a 16 digit number representation of numbers from 0 - 9 and digits from A -
F. In other words, the first 9 numbers or digits are represented as numbers while the next
6 digits are represented as symbols from A - F.
5) What is C language? Explain about Background and C program?
➢ Documentation Section: This section consists of the description of the program, the
name of the program, and the creation date and time of the program. It is specified at the
start of the program in the form of comments. Documentation can be represented as:
✓ // description, name of the program, programmer name, date, time etc.-------For single
line.
✓ /* description, name of the program, programmer name, date, time etc.*/--------- For
multiple lines.
✓ Anything written as comments will be treated as documentation of the program and this
will not interfere with the given code. Basically, it gives an overview to the reader of the
program.
Example:
#include<stdio.h>
#include<math.h>
➢ Definition:Preprocessors are the programs that process our source code before the
process of compilation. There are multiple steps which are involved in the writing and
execution of the program. Preprocessor directives start with the ‘#’ symbol. The #define
preprocessor is used to create a constant throughout the program. Whenever this name is
encountered by the compiler, it is replaced by the actual piece of defined code.
Example:
#define PI 3.1415
➢ Global Declaration: The global declaration section contains global variables, function
declaration, and static variables. Variables and functions which are declared in this scope
can be used anywhere in the program.
Example:
int num = 18;
➢ Main( ) Function: Every C program must have a main function. The main( ) function of
the program is written in this section. Operations like declaration and execution are
performed inside the curly braces of the main program. The return type of the main( )
function can be int as well as void too. void( ) main tells the compiler that the program
will not return any value. The int main( ) tells the compiler that the program will return
an integer value.
Example:
void main( )
or
int main( )
➢ Sub Programs:
User-defined functions are called in this section of the program. The control of the program
is shifted to the called function whenever they are called from the main or outside the main()
function. These are specified as per the requirements of the programmer.
Example:
int sum(int x, int y)
{
return x+y;
}
✓ Operators can be defined as the symbols that help us to perform specific mathematical,
relational, bitwise, conditional, or logical computations on operands.
✓ Arithmetic operators are used to perform arithmetic operations.
✓ Arithmetic operators are classified as:
✓ Integer Arithmetic.
✓ Real Arithmetic.
✓ Mixed mode arithmetic.
➢ Integer Arithmetic: In this type of operation both the operands are integer in an integer
arithmetic.It always yields an integer value.
Syntax: exp1 Integerarithmetic exp2
Eg: a+b
➢ Real Arithmetic: In this type of operation,both the operands are real in real arithmetic.It
yields real values as result.It cannot be applied for % operator.
Syntax: exp2 real arithmetic exp2
Eg: float a = 20.0,b = 3.0; a+b= 23.000000
➢ Mixed Mode Arithmetic: In this type of operation,one operand is integer and other is
real.Here the result will always be real.
Syntax: exp1 mixedmode arithmetic exp2
Eg: int a = 20.0,b = 3; a+b = 23.0
Expression:
➢ An expression is a collection of operators and operands that represents a specific value.
➢ An Operator is a symbol that performs tasks like arithmetic operations,logical and
conditional operations.
➢ Operands are the values on which the operators perform the task.Here operand can be
direct value or variable or address of memory location.
Types:
➢ Infix: The expression in which the operator is used between operands is called infix
expressions.
Eg: a+b
➢ postfix: The expression in which the operator is used after operands is called postfix
expression.
Eg: ab+
➢ Prefix: The expression in which the operator is used before operands is called a prefix
expression
Eg: +ab
Expression evaluation:
✓ In c language an expression is evaluated based on the operator precedence and
associativity.
✓ When there are multiple operators in an expression they are evaluated according to their
precedence and associativity.
✓ The operator which has the highest precedence is evaluated first and the operator with the
least precedence is evaluated last.
✓ An expression is evaluated based on the precedence and associativity of the operators in
that expression.
✓ Consider the following Statements:
y = a – b / ( 3 + c) * (2 -1 );
when a = 10, b = 12 and c = 3
y = 10 – 12 / (3 + 3 ) * (2 -1) is evaluated as follows:
Step 1 : y = 10 - 12 / 6 * ( 2 -1)
Step 2 : y = 10 - 12 / 6 * 1
Step 3: y = 10 - 2 * 1
Step 4: y = 10 - 2
Step 5: y = 8
❖ Operator Precedence in C:
✓ Operator precedence determines which operation is performed first in an expression with
more than one operator with different precedence.
✓ 10 + 20 * 30
✓ The expression contains two operators, + (plus), and * (multiply). According to the given
table, the * has higher precedence than +
❖ Operator Associativity:
✓ Operator associativity is used when two operators of the same precedence appear in an
expression. Associativity can be either from Left to Right or Right to Left.
✓ 100 / 5 % 2
✓ Both / (division) and % (Modulus) operators have the same precedence, so the order of
evaluation will be decided by associativity.
✓ According to the given table, the associativity of the multiplicative operators is from Left
to Right. So,
➢ (100 / 5) % 2
➢ After evaluation, the expression will be
➢ 20 % 2
➢ Now, the % operator will be evaluated.
➢ 0
➢ Type conversion:
The type conversion is the process of converting a data value from one datatype to another
datatype automatically by the compiler.It is also called as Implicit type conversion or
Automatic type conversion.
Eg: #include<stdio.h>
#include<conio.h>
void main( ){
int i =95;
float x = 90.00;
char ch = ‘A’;
i = x;
printf(“i value is %d\n:”,i);
x = i;
printf(“ x value is %f\n:”,x);
i = ch;
printf(“ i value is %c:”,i);
}
O/P:
i value is : 90
x value is : 90.000000
I value is: 65
➢ Typecasting:
✓ Also called Explicit type conversion.Compiler converts data from one datatype to
another dataatype implicitly.
✓ When compiler converts implicitily there may be data loss.
✓ In such cases convert the data from one datatype to another data type using explicit type
conversion.
✓ (TargetDataType) DataValue
Eg: #include<stdio.h>
int main( ) {
// create an integer variable
int number = 35;
printf("Integer Value: %d\n", number);
// explicit type conversion
double value = (double) number;
printf("Double Value: %.2lf", value);
return 0;
}
O/P: Integer Value: 35
Double Value: 35.00
8) How to Create and run C program?
➢ Compilation: After writing and saving the source program,the next step is
compilation.Here we will use a software called as compiler,which converts a
program written in high level language into machine language.The resulatant
file is known as object file in c.The extension is “.obj”.
➢ Linking: Here the software called Linker is used.The linker links the program
with external library files which contains the code for predefined fucntions
and creates an executable file.The extension of the executable file is “.exe”.
➢ Execution: The Operating system executes the executable file which is the
machine code with the help of the CPU and other hardware components.
9) Rules for writing C program?
➢ A data type specifies the type of data that a variable can store such as integer, floating,
character, etc.
➢ This datatype specifies how much memory is to be allocated and what type of values to
be stored in the variable or constant or array.
➢ Basic/Primary/Predefined datatype:
➢ The basic data types are integer-based and floating-point based. C language supports
both signed and unsigned literals.
➢ The memory size of the basic data types may change according to 32 or 64-bit operating
system.
⚫ Derived Datatypes: The data-types that are derived from the primitive or built-in
datatypes are referred to as Derived Data Types. These can be of four types namely:
Arrays,functions and Pointer.
⚫ Enumerated Datatype: Enumeration (or enum) is a user defined data type in C. It is
mainly used to assign names to integral constants, the names make a program easy to
read and maintain.
Syntax: enum flag {const_name1, const_name2, ..., const_nameN};
⚫ Variable: A variable in C is a memory location with some name that helps to store some
form of data and retrieves it when required. We can store different types of data in the
variable and reuse the same variable for storing some other data any number of times.
➢ syntax: data_type variable_name = value; // defining single variable
or
➢ data_type variable_name1, variable_name2; // defining multiple variable
⚫ Constants:
➢ Constant is a value that cannot be changed during program execution; it is fixed.
➢ In C language, a number or character or string of characters is called a constant. And it
can be any data type. Constants are also called as literals.
➢ Integer constants: An integer constant can be decimal integer or octal integer or
hexadecimal integer.A decimal integer value is specified as direct integer and should not
start with zero.
➢ whereas octal integer value is prefixed with ‘0’ and must start with zero.
➢ Hexadecimal value is prefixed with ‘0X’ or ‘0x’
➢ Floating point constants: Floating point constant must contain both integer and decimal
parts.
➢ It can also contain the exponent part.
➢ 3.14 prepresented as 3E-14
➢ Character constants: It is enclosed in single quotation.A character constant has a
maximum length of one character.
➢ Predefined character constants called escape sequences.EVery escape sequence has its
own functionality and every escape sequence is prefixed with ‘\’ symbol.
➢ Using ‘const’ keyword: Of any datatype we can create a constant by using ‘const’
keyword.
➢ Syntax: const datatype constantName;
or
➢ const datatype constantName = value;
13) What are the various input and output statements in C programming
language?
✓ C language has standard libraries that allow input and output in a program. The stdio.h or
standard input output library in C that has methods for input and output.
✓ The input operations are used to read user values from the keyboard.
Formatted I/O in C:
➢ The I/O procedure is known as "formatted I/O". It enables you to read or write data in a
certain format.
➢ Printf() and scanf() are two examples of C routines that handle formatted I/O. The type
and format of the data to be read or written are specified by format strings, which are
used by these operations. The program's execution replaces the placeholders for the data
found in the format strings with the actual data.
Unformatted I/O in C:
➢ Unformatted I/O refers to a category of I/O operations that reads or writes data as a
stream of bytes without regard to any format. Unformatted I/O in C is carried out with
the aid of functions like fread() and fwrite(). Without formatting, these operations are
used to read and write data directly to and from files.
⚫ scanf( ): This function is used to read multiple data values of different datatypes from the
keyboard.
syntax: scanf(“format specifier”, &variableNames);
Eg: scanf(“%d”,&i);
#include <stdio.h>
int main( ) {
char str[100];
int i;
printf( "Enter a value :");
scanf("%s %d", str, &i);
printf( "\nYou entered: %s %d ", str, i);
return 0;
}
O/P: Enter a value : seven 7
You entered: seven 7
⚫ getchar( ) : getchar( ) function is used to read one character at a time from the key board.
Syntax ch = getchar( ); where ch is a char Var
✓ When this function is executed, the computer will wait for a key to be pressed and
assigns the value to the variable when the “enter” key pressed.
#include <stdio.h>
int main( ) {
int c;
printf( "Enter a value :");
c = getchar( );
printf( "\nYou entered: ");
putchar( c );
return 0;
}
O/P: Enter a value : this is test
You entered: t
Eg: //getch( )
void main( ){
char ch;
printf(“\n Enter any character:”);
ch = getch( );
printf(“\n You have entered:%c”,ch);
}
O/P: Enter any character:
You have entered: S
⚫ gets( ): gets( ) function is used to read a string of characters including white spaces.
✓ Note that white spaces in a string cannot be read using scanf( ) with %s format specifier.
Syntax: gets (S); where ‘S’ is a char string variable.
✓ When this function is executed the computer waits for the string to be entered.
Eg: gets( ) & puts( )
#include <stdio.h>
int main( ) {
char str[100];
printf( "Enter a value :");
gets( str );
printf( "\nYou entered: ");
puts( str );
return 0;}
O/P:
Enter a value : this is test
You entered: this is test
⚫ fscanf( ): This is used with the file concept of files. The fscanf( ) is used to read data
values from a file. When we want to use fsanf( ) function the file must be opened in
reading mode.
⚫ printf( ) function: The printf ( ) function, part of the standard C library, is the most
versatile way for a program to display data onscreen.
✓ Printing a text message onscreen is simple.
✓ Call the printf( ) function, passing the desired message enclosed in double quotation
marks.
✓ For example, to display an error that has occurred! onscreen, the user write the following:
✓ printf("An error that has occurred!");
✓ It accepts a string parameter (called the format string), which specifies a method for
rendering a number of other parameters into a string.
⚫ putchar( ): putchar( ) function is used to display one character at a time on the monitor.
Syntax: putchar (ch);
✓ The Computer display the value char of variable ‘ch’ i.e M on the Screen.
⚫ fprintf( ): It is used with the concept of files.The fprintf( ) function is used to print a line
into the file.When we want to use this function the file must be opened in writing mode.