We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 7
C LANGUAGE
Basic concepts of Programming languages : Programming languages are a set of rules,
symbols, and codes used to instruct a computer to perform a specific task or solve a
problem. The basic concepts of programming languages include syntax, semantics,
variables, data types, control structures, functions, and arrays.
Programming Domains : Programming domains are areas of application for
programming languages. Some common programming domains include scientific
computing, business applications, web development, game development, and mobile
app development.
Language Evaluation criteria and language categories : Programming languages are
evaluated based on criteria such as readability, maintainability, performance, and ease
of use. There are different categories of programming languages such as procedural
languages, object-oriented languages, functional languages, and scripting languages.
Evolution of major programming languages : The evolution of major programming
languages can be traced back to the mid-20th century. Some of the earliest
programming languages include Fortran, COBOL, and Lisp. These languages have
evolved over time, and new languages have been developed, such as C, C++, Java,
Python, and JavaScript.
Describing syntax and semantics : The syntax and semantics of each language are
unique, but they all share common programming concepts.
Formal methods of describing syntax : Formal methods of describing syntax include
Backus-Naur Form (BNF), Extended Backus-Naur Form (EBNF), and Syntax Diagrams.
These methods are used to formally specify the syntax of a programming language.
Pseudo code : Pseudo code is a high-level language used to describe an algorithm in a
way that is easy to understand, It is not a specific programming language, but rather a
way of expressing an algorithm using natural language and programming constructs.
Design of Algorithm & Flowchart : The design of an algorithm involves breaking down
a problem into smaller, more manageable parts and then devising a step-by-step plan
to solve each part. A flowchart is a diagram that represents the steps of an algorithm
using symbols and arrows to show the flow of control. A flowchart is a useful tool for
visualizing the steps of an algorithm and identifying potential errors or inefficiencies.
Fundamentals
History and importance of C : C was developed in the early 1970s by Dennis Ritchie
at Bell Labs. It quickly became a popular programming language due to its ability to
create efficient code, its low-level capabilities, and its ease of portability. C is used for
a wide variety of applications, including operating systems, embedded systems,
scientific computing, and game development.Basic structure and execution of C programs : A C program is composed of functions,
which are made up of statements. A statement is a complete instruction that the
computer can execute. A C program is executed from the main() function, which is the
entry point of the program.
Constants : Constants are values that cannot be changed during the execution of a
program. In C, there are several types of constants, including integer constants,
character constants, and floating-point constants.
Variables : Variables are named locations in memory that store data. In C, variables
must be declared with a data type before they can be used.
Data types : Data types in C specify the type of data that a variable can hold. Some
common data types in C include integer, character, and floating-point.
Various type of declarations : There are two types of declarations in C: variable
declarations and function declarations. Variable declarations specify the data type and
name of a variable, while function declarations specify the return type, name, and
parameter list of a function.
Operators types and expressions : Operators in C are used to perform operations on
data. There are several types of operators in C, including arithmetic operators,
relational operators, logical operators, and bitwise operators. Expressions are
combinations of values, variables, and operators that can be evaluated to produce a
result.
Evaluation of expressions : Expressions in C are evaluated based on operator
precedence and associativity, which determine the order in which operators are
applied.
Operator precedence and associativity : Operator precedence in C determines the
order in which operators are applied in an expression. Associativity determines the
order in which operators of the same precedence are applied.
Managing input and output operations : Input and output operations in C are
managed using the printf() and scanf() functions. The printf) function is used to output
data to the console, while the scanf{) function is used to read data from the console.
Decision making and branching : Decision making in C is accomplished using if-else
statements and switch statements. Branching is accomplished using control
statements such as break, continue, and goto. These statements can be used to alter
the normal flow of a program based on specific conditions.
Iteration
while : The while loop in C is used to execute a block of code repeatedly while a
particular condition is true. The condition is checked at the beginning of each iteration
of the loop.
do...while : The do...while loop in C is similar to the while loop, except that the
condition is checked at the end of each iteration, This means that the code inside the
loop will always be executed at least once.
for loop : The for loop in C is a compact way of writing a loop that is designed to
execute a fixed number of times. It has three parts: an initialization statement, a
condition, and an increment statement,+ nested loops : Nested loops in C are loops that are placed inside the body of another
loop. They are useful for iterating over multi-dimensional arrays and performing
operations on each element.
+ break & continue : The break and continue statements in C are used to alter the
normal flow of a loop. The break statement is used to exit a loop prematurely, while
the continue statement is used to skip to the next iteration of a loop.
+ goto statements : The goto statement in C is used to transfer control to another part
of the program. It can be used to create loops or to jump to a particular section of
code. However, its use is generally discouraged as it can make code difficult to read
and maintain.
Array and String
+ One-dimensional array and their declaration and initialization : An array in Cis a
collection of variables of the same data type that are stored in contiguous memory
locations. To declare an array in C, you must specify the data type and the number of
elements in the array. , you would use the following syntax:
For example : int numbers[10]; //To declare an array of 10 integers
To initialize an array in C, you can use an initialization list, which is a comma-separated
list of values enclosed in curly braces.
For example : int numbers[3] = (1, 2, 3);
Two-dimensional arrays are declared and initialized in a similar manner, with the
addition of a second dimension.
For example : int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9};
+ Character arrays (One and Two dimensional) : A character array in C is a special type
of array that stores characters rather than integers. One-dimensional character arrays
are declared and ir ized in the same way as other one-dimensional arrays.
For example : char name[10] = "John";
Two-dimensional character arrays are used to store strings, which are collections of
characters terminated by a null character.
For example : char message(3][10] = {"Hello", "World", "!"};
+ Reading and writing strings : Strings can be read and written using the scanf() and
printf() functions, respectively.
For example : char name[10];
scanf("%s", name);
printf("Hello, %s!\n", name);
Note that the "%¢s" format specifier is used to read and write strings.
+ String-handling functions : C provides a number of built-in functions for working with
strings. Some common string-handling functions include:
1.strlen() : returns the length of a string.
2.strepy(): copies one string to another.
3.streat(): concatenates two strings.
4.strcmp)): compares two string:
For example : char sourcel] = "Hell
char destination[10];
strepy(destination, source);
printf("%s\n", destination); // Output: HelloNeed and elements for user-defined functions : Functions in C are used to break a
program into smaller, more manageable pieces. This makes the code easier to read,
write, and maintain. The elements of a user-defined function in C include a function
header, a function body, and a return statement (if the function returns a value).
Definition of functions : In C, a function is defined using the following syntax :
return_type function_name(parameter list) {
// Function body
}
The return type specifies the data type of the value that the function returns (if any)
The function name is a unique identifier for the function. The parameter list specifies
the type and number of arguments that the function accepts.
Return values and their types : Functions in C can return a value using the return
statement. The return type of the function specifies the data type of the value that is
returned. For example, a function that returns an integer would have a return type of
“int.
Function calls and declaration : A function in C is called using the following syntax:
function_name(argument list);
The argument list contains the values that are passed to the function. The function
must be declared before itis called. The declaration specifies the function name,
return type, and parameter list.
Recursion : Recursion is a programming technique where a function calls itself. This is
useful for solving problems that can be broken down into smaller sub-problems. In C, a
recursive function must have a base case, which is a condition that terminates the
recursion.
Parameter passing : In C, parameters can be passed to a function by value or by
reference. When a parameter is passed by value, a copy of the value is passed to the
function. When a parameter is passed by reference, a pointer to the value is passed to
the function, allowing the function to modify the original value.
Passing arrays and strings to functions : In C, arrays and strings can be passed to
functions as parameters. When an array is passed to a function, a pointer to the first
element of the array is passed. When a string is passed to a function, a pointer to the
first character of the string is passed.
The scope : The scope of a variable in C is the part of the program where the variable
can be accessed. Variables can have either local or global scope. Local variables are
declared inside a function and can only be accessed within that function. Global
variables are declared outside of any function and can be accessed from any part of
the program.
Visibility and lifetime of variables : The visibility of a variable in C is the part of the
program where the variable can be accessed. The lifetime of a variable is the period of
time during which the variable exists in memory. Local variables have automatic
storage duration, which means they are created when the function is called and
destroyed when the function returns. Global variables have static storage duration,
which means they exist for the entire lifetime of the program.
Understanding PointersAccessing the address of a variable : In C, the address of a variable can be obtained
using the "address-of" operator (&). For example, to get the address of a variable "x",
you would use the expression "6x",
Declaration and initialization of pointer variables : In C, a pointer variable is declared
using the following syntax:
data_type *pointer_name;
The "*" indicates that pointer_name is a pointer variable. The data_type specifies the
data type of the variable that the pointer points to. To initialize a pointer, you can
assign it the address of a variable using the "&" operator.
For example : int x = 10;
int “p= 8x;
This creates a pointer variable "p" that points to the address of "x’:
Accessing a variable through its pointer : In C, a variable can be accessed through its
pointer using the "dereference" operator (*). For example, to get the value of the
variable that "p" points to, you would use the expression "*p"
Pointers and arrays : In C, arrays can be accessed using pointers. The name of an array
is a pointer to the first element of the array.
For example : int a[5] = {1, 2, 3, 4, 5};
int “p= a; // equivalent to int *p = &al0}
This creates a pointer "p" that points to the first element of the array "a". The elements
of the array can be accessed through the pointer using the dereference operator (*).
Pointers and function arguments: In C, pointers can be used to pass variables by
reference to a function. This allows the function to modify the original variable.
For example : void swap(int ‘a, int *b) {
int temp = *a;
ta="b;
temp;
}
int main() {
int x = 10, y = 20;
swap(&x, &y); // x is now 20, y is now 10
return 0;
}
This creates a function "swap" that takes two integer pointers as arguments. The
function swaps the values of the variables pointed to by the pointers.
Functions returning pointers : In C, functions can return pointers. This allows the
function to return a pointer to a dynamically allocated memory block.
For example : int “create_array(int size) {
int ‘arr = malloc(size * sizeoflint));
return arr;
}
int main() {
int "a = create_array(5);
// use the array
free(a);
return 0;
}This creates a function "create_array" that dynamically allocates an array of integers
using the "malloc" function. The function returns a pointer to the array. The pointer
can be used to access the elements of the array. The memory allocated by the "malloc"
function should be freed using the "free" function when it is no longer needed.
Structures and Unions
Defining structure : A structure is a user-defined data type in C that groups together
related variables under a single name. The variables can have different data types. In
C, a structure is defined using the keyword "struct".
For example : struct student {char name[50);
int rolino;
float marks;
h
This defines a structure called "student" with three members: "name" of type char
array, "rollno” of type int, and "marks" of type float.
Declaring structure variable and accessing structure members : Once a structure is
defined, you can declare a variable of that type and access its members using the dot
() operator.
For example : struct student s1;
sLrollno = 1;
strepy(si.name, "John");
sLmarks = 90.5;
This declares a variable "s1" of type "student" and assigns values to its members.
Initialization of structure : You can also initialize a structure variable at the time of
declaration.
For example : struct student 1 = {"John' 1, 90.5};
This initializes the members of the "s1" structure vé
the braces.
ible with the values provided in
Operation on individual members and array of structures : You can perform
operations on individual members of a structure variable, such as reading and writing
their values, or performing mathematical operations with them. You can also declare
an array of structure variables, just like you would declare an array of any other data
type.
For example : struct student s[5];
s[O]-rolino = 1;
strepy(s[0].name, "John");
s[0].marks = 90.5;
This declares an array "s" of five structure variables of type "student", and assigns
values to the members of the first variable.
Union : A union is another user-defined data type in C that allows you to store
different types of data in the same memory location. A union is defined using the
“union” keyword.
For example : union myunion {int i;
char c;
float f;
‘
This defines a union called "myunion" with three members:
char, and "f" of type float. Alll three members share the same memory location, and
you can only access one member at a time.* Size of structure : You can determine the size of a structure using the "sizeof"
operator. The size of a structure is the sum of the sizes of its members, plus any
padding bytes that the compiler may add for memory alignment. Here's an example:
For example : struct student {char name[50];
int rollno;
float marks;
i‘
printf("Size of student structure: %lu bytes\n", sizeof(struct student);
This prints the size of the "student" structure in bytes. The "lu" format specifier is used
to print the result as an unsigned long integer.
Vo
+ Formatted and Un-Formatted I/O : In C, formatted I/O functions are used for reading
or writing data to a stream ina specific format. The most commonly used formatted
I/O functions are printf() for output and scanf() for input. Unformatted I/O functions
are used for reading or writing data to a stream as a sequence of bytes. The most
commonly used unformatted I/O functions are fread() and fwrite().
+ File handling (Random, Binary and Sequential) : File handling in C is done using file
pointers. A file pointer is a pointer that holds the address of the file stream that is used
for file I/O operations. The standard library provides several functions for file handling,
including fopen() for opening a file, fclose() for closing a file, fread() for reading from a
file, fwritel) for writing to a file, and fprintf() and fscanf() for formatted I/O operations.
In C, files can be handled in three modes: sequential access, random access, and binary
access.
1. Sequential Access : In this mode, the file is read or written in a sequential manner,
from the beginning to the end. This is the default mode for text files, where data is
stored in a series of lines. In sequential access mode, you can only read or write the file
the order it was created, and you cannot skip any part of the file.
2.Random Access : In this mode, you can read or write the file in any order, and you can
move the file pointer to any position in the file using fseek(). Random access is useful
when you want to read or write specific parts of a file without having to go through
the entire file. Random access is typically used with binary files, where data is not
stored in lines.
3.Binary Access : In this mode, files are read or written as a sequence of bytes, without
any regard for their contents. Binary access is typically used with binary files, where
the data is stored in a particular format and can be read or written as a whole.