Programming Methodology UNIT 3
Programming Methodology UNIT 3
4 Marks
1. Write down about the History of C language?
• C is a procedural programming language.
• It was developed by Dennis Ritchie in the year 1972.
• It was mainly developed as a system programming language to write an operating system.
•C is a case-sensitive language.
• The Unix operating system and virtually all Unix applications are written in the C language.
• C is a successor of B language which was introduced around 1970
• The language was formalized in 1988 by the American National Standard Institute (ANSI).
• By 1973 UNIX OS almost totally written in C.
• Today C is the most widely used System Programming Language.
Alphabets
Alphabets has a two case.
Digit
C programming support 10 digit which are used to construct numerical value.
Digit 0-9.
ASCII CODE: 0=48 to 9=57
Special character
C programming supports set of special symbols that include symbols to perform mathematical
operation, check condition and other special symbol.
White space
In computer programming, white space is a character that represent vertical space in
typography.
A variable is a name of the memory location. It is used to store data. Its value can be
changed, and it can be reused many times.
It is a way to represent memory location through symbol so that it can be easily
identified.
Let's see the syntax to declare a
1. int a;
2. int _ab;
3. int a30;
1. int 2;
2. int a b;
3. int long;
3. What is Data type ? Explain in detail about basic and derived data type.
A data type in C refers to the type of data used to store the information.
o For example, the name of a person would be an array of characters, while the
age will be in integers.
o Whereas, the marks of a student would require a data type that can store decimal
values.
1. Basic Data Types:
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.
Let's see the basic data types. Its size is given according to 32-bit architecture.
Float 4 byte
Double 8 byte
The primitive or basic data types are used to store single values
of different forms, but what if you need to store more values of
the same data type? Here, derived data types allow you to
combine the basic data types and store multiple values in a
single variable.
Derived data types are defined by the user itself, which means
that you can aggregate as many elements of similar data types
as required. There are four types of derived data types:
Array
Pointer
Structure
Union
Array:
For example, you can store float values like the marks of a
student, integer values like the roll number, and so on.
Pointer:
To define a structure, the keyword ‘struct’ is used as shown in the below program.
Union:
The size of a union will be equal to the memory needed for the
largest variable inside it.
4. What are symbolic constants in C program and write the way for defining
symbolic constants with an Example.
In the C language, the symbolic constants are defined using the keyword #define.
Examples:
#define PI 3.141593
#define MAX 80
Usually, in C programming symbolic constants are defined just above the main function.
int main () {
c = a + b;
printf("value of c : %d \n", c);
f = 70.0/3.0;
printf("value of f : %f \n", f);
return 0;
}
When the above code is compiled and executed, it produces the following result −
value of c : 30
value of f : 23.333334
The same concept applies on function declaration where you provide a
function name at the time of its declaration and its actual definition can be
given anywhere else. For example −
// function
declaration int func();
int main() {
// function
call int i =
func();
}
// function definition
int func() {
return 0;
}
10 Marks
1. Write in detail the various components of C program
Header Files:
The first and foremost component is the inclusion of the Header files in a C program.
• A header file is a file with extension .h
• Which contains C function declarations and macro definitions to be
shared between several source files.
Example of Header files:
stddef.h – Defines several useful types and macros.
stdint.h – Defines exact width integer types.
stdio.h – Defines core input and output functions
stdlib.h – Defines numeric conversion functions, pseudo-random
network generator, memory allocation
string.h – Defines string handling functions
math.h – Defines common mathematical functions
Main Method:
The next part of a C program is to declare the main() function.
e.g. main()
{}
Variable declaration:
The next part of any C program is the variable declaration.
-In the C program, no variable can be used without being declared.
-Also in a C program, the variables are to be declared before any operation in the
function.
Ex. main()
{
int a;
Body of program:
Body of a function in C program, refers to the operations that are
performed in the functions. Example:
int main()
{
int x;
printf("%d", x); . .
Return Statement:
The last part in any C program is the return statement.
-The return statement refers to the returning of the values from a function.
-This return statement and return value depend upon the return type of the
function.
For example: if the return type is void, then there will be
no return statement.
Procedural Language:
In a procedural language like C step by step predefined instructions are carried
out.
C program may contain more than one function to perform a particular task.
Most of the commonly used paradigm is an object-oriented programming
language.
Modularity:
The concept of storing C programming language code in the form of libraries
for further future uses is known as modularity.
This programming language van does very little on its own most of its power
is held by its libraries.
C language has its own library to solve common problems like in this we can
use a particular function by using a header file stored in its library.
Statically Type:
C programming language is a statically typed language.
Meaning the type of variable is checked at the time of compilation but not at run
time.
This means each time a programmer type a program they have to mention the type
of variables used.
Middle-Level Language:
As it is a middle-level language so it has the combined form of both capabilities of
assembly language and features of the high-level language.
Portability:
C language is lavishly portable as programs that are written in C language can run
and compile on any system with either none or small changes.
Keywords are predefined, reserved words in C and each of which is associated with
specific features.
These words help us to use the functionality of C language. They have special
meaning to the compilers.
There are total 32 keywords in C.
Do if static while
3. Identifiers:
Types of identifiers
o Internal identifier
o External identifier
Internal Identifier
If the identifier is not used in the external linkage, then it is known as an internal identifier.
The internal identifiers can be local variables.
External Identifier
If the identifier is used in the external linkage, then it is known as an external identifier. The
external identifiers can be function names, global variables.
1. int main()
2. {
3. int a=10;
4. int A=20;
5. printf("Value of a is : %d",a);
6. printf("\nValue of A is :%d",A);
7. return 0;
8. }
Output
Value of a is :10
Value of Ais:20
The above output shows that the values of both the variables, 'a' and 'A' are different.
Therefore, we conclude that the identifiers are case sensitive.
4. Strings:
This null character indicates that string has ended. Strings are always enclosed with double
quotes(“ “).
Example
value : %f
Value : %s
Output
Character Value : H
String Value : demodotcom
5. Operators
An operator is simply a symbol that is used to perform operations. There can be many types
of operations like arithmetic, logical, bitwise, etc.
There are following types of operators to perform different types of operations in C language.
o Arithmetic Operators
o Relational Operators
o Shift Operators
o Logical Operators
o Bitwise Operators
o Ternary or Conditional Operators
o Assignment Operator
o Misc Operator
6. Constants in C
A constant is a value assigned to the variable which will remain the same throughout the
program, i.e., the constant value cannot be changed.
Constant Example
7. Special characters in C
Some special characters are used in C, and they have a special meaning which cannot be used
for another purpose.
o Square brackets [ ]: The opening and closing brackets represent the single and
multidimensional subscripts.
o Simple brackets ( ): It is used in function declaration and function calling. For
example, printf() is a pre-defined function.
o Curly braces { }: It is used in the opening and closing of the code. It is used in the
opening and closing of the loops.
o Comma (,): It is used for separating for more than one statement and for example,
separating function parameters in a function call, separating the variable when
printing the value of more than one variable using a single printf statement.
o Hash/pre-processor (#): It is used for pre-processor directive. It basically denotes
that we are using the header file.
o Asterisk (*): This symbol is used to represent pointers and also used as an operator
for multiplication.
o Tilde (~): It is used as a destructor to free memory.
o Period (.): It is used to access a member of a structure or a union.
o
5. What is variable in C program? Write down the rules for defining the variables. Explain in
detail about its types.
A variable is a name of the memory location. It is used to store data. Its value can be
changed, and it can be reused many times.
It is a way to represent memory location through symbol so that it can be easily
identified.
Let's see the syntax to declare a
1. int a;
2. int _ab;
3. int a30;
1. int 2;
2. int a b;
3. int long;
Types of Variables in C
1. local variable
2. global variable
3. static variable
4. automatic variable
5. external variable
Local Variable:
A variable that is declared inside the function or block is called a local variable.
1. void function1(){
2. int x=10;//local variable
3. }
You must have to initialize the local variable before it is used.
Global Variable:
A variable that is declared outside the function or block is called a global variable.
Any function can change the value of the global variable. It is available to all the
functions.
It must be declared at the start of the block
A variable that is declared with the static keyword is called static variable.
It retains its value between multiple function calls.
1. void function1(){
2. int x=10;//local variable
3. static int y=10;//static variable
4. x=x+1;
5. y=y+1;
6. printf("%d,%d",x,y);
7. }
If you call this function many times, the local variable will print the same value for
each function call, e.g, 11,11,11 and so on. But the static variable will print the
incremented value in each function call, e.g. 11, 12, 13 and so on.
Automatic Variable
All variables in C that are declared inside the block, are automatic variables by default.
We can explicitly declare an automatic variable using auto keyword
1. void main(){
2. int x=10;//local variable (also automatic)
3. auto int y=20;//automatic variable
4. }
External Variable
myfile.h
1. #include "myfile.h"
2. #include <stdio.h>
3. void printValue(){
4. printf("Global variable: %d", global_variable);
5. }
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.
Let's see the basic data types. Its size is given according to 32-bit architecture.
Float 4 byte
Double 8 byte
The primitive or basic data types are used to store single values of different
forms, but what if you need to store more values of the same data type? Here,
derived data types allow you to combine the basic data types and store multiple
values in a single variable.
Derived data types are defined by the user itself, which means that you can
aggregate as many elements of similar data types as required. There are four
types of derived data types:
Array
Pointer
Structure
Union
Array:
Pointer:
A pointer with no address is known as the null pointer, while a pointer with
no data type is called a void or general-purpose pointer.
Structure:
For example, structures can be used to store information about a student, including the
name, the roll number, marks, and more. The record of each student will be
represented by an object of the structure.
To define a structure, the keyword ‘struct’ is used as shown in the below program.
Union:
Union is also a collection of elements with similar or different data types, but the
memory location is the same for all the elements.
It is a special kind of data type in C language, where you can declare many variables,
but only one variable can store the value at a given time.
Union is defined by the ‘union’ keyword, where each object will represent a single
record.
The size of a union will be equal to the memory needed for the largest variable inside
it.
Enumeration is a user-defined data type used to assign names to the integral constants
and enhance the readability of your program.
There are two main reasons why enumerations are better than ‘#define’:
Void is a data type in C language that does not refer to any value of any type.
You can declare the void pointers to take the address of variables from any data type.
These pointers are often called ‘generic pointers.