0% found this document useful (0 votes)
4 views

C Complete

Uploaded by

sarim1282au
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

C Complete

Uploaded by

sarim1282au
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

C programming language was developed in 1972 by Dennis Ritchie at bell laboratories of

AT&T (American Telephone & Telegraph), located in the U.S.A. Dennis Ritchie is known as
the founder of the c language. It was developed to overcome the problems of previous
languages such as B, BCPL, etc.
C is considered as a middle-level language because it supports the feature of both low-level
and high-level languages. C language program is converted into assembly code, it supports
pointer arithmetic (low-level), but it is machine independent (a feature of high-level).

• A Low-level language is specific to one machine. It is machine dependent, fast to run.


But it is not easy to understand.
• A High-Level language is not specific to one machine, i.e., machine independent. It is
easy to understand.

Features of C Language
• C is a simple language in the sense that it provides a structured approach (to break
the problem into parts), the rich set of library functions, data types, etc.
• Although, C is intended to do low-level programming. It is used to develop system
applications such as kernel, driver, etc. It also supports the features of a high-level
language. That is why it is known as mid-level language.
• C is a structured programming language in the sense that we can break the program
into parts using functions. So, it is easy to understand and modify. Functions also
provide code reusability.
• It supports the feature of dynamic memory allocation, we can free the allocated
memory at any time by calling the free() function.
• The compilation and execution time of C language is fast since there are lesser inbuilt
functions and hence the lesser overhead.
• C provides the feature of pointers. We can directly interact with the memory by using
the pointers.
• In C, we can call the function within the function. It provides code reusability for
every function. Recursion enables us to use the approach of backtracking.
Compilation process in c
The compilation is a process of converting the source code into object code. It is done with
the help of the compiler. The compiler checks the source code for the syntactical or
structural errors, and if the source code is error-free, then it generates the object code. The
compilation process can be divided into four steps, i.e., Pre-processing, Compiling,
Assembling, and Linking.
The preprocessor takes the source code as an input, and it removes all the comments from
the source code. The preprocessor takes the preprocessor directive and interprets it. For
example, if <stdio.h>, the directive is available in the program, then the preprocessor
interprets the directive and replace this directive with the content of the 'stdio.h' file.
1. Preprocessor: The source code is the code which is written in a text editor and the
source code file is given an extension ".c". This source code is first passed to the
preprocessor, and then the preprocessor expands this code. After expanding the
code, the expanded code is passed to the compiler.
2. Compiler: The code which is expanded by the preprocessor is passed to the compiler.
The compiler converts this code into assembly code. Or we can say that the C
compiler converts the pre-processed code into assembly code.
3. Assembler: The assembly code is converted into object code by using an assembler.
The name of the object file generated by the assembler is the same as the source
file. The extension of the object file in DOS is '.obj,' and in UNIX, the extension is 'o'.
If the name of the source file is 'hello.c', then the name of the object file would be
'hello.obj'.
4. Linker: Mainly, all the programs written in C use library functions. These library
functions are pre-compiled, and the object code of these library files is stored with
'.lib' (or '.a') extension. The main working of the linker is to combine the object code
of library files with the object code of our program. Sometimes the situation arises
when our program refers to the functions defined in other files; then linker plays a
very important role in this. It links the object code of these files to our program.
Therefore, we conclude that the job of the linker is to link the object code of our
program with the object code of the library files and other files. The output of the
linker is the executable file. The name of the executable file is the same as the source
file but differs only in their extensions. In DOS, the extension of the executable file is
'.exe', and in UNIX, the executable file can be named as 'a.out'. For example, if we are
using printf() function in a program, then the linker adds its associated code in an
output file

Variables in C
A variable is the name of the memory location. It is used to store information. Its value can
be altered and reused several times. It is a way to represent memory location through
symbols so that it can be easily identified. A variable is a designated memory region that
stores a specified data type value. Each variable has a unique identifier, its name, and a data
type describing the type of data it may hold.
Data Types
A data type specifies the type of data that a variable can store such as integer, floating,
character, etc. The basic data types are integer-based and floating-point based. C language
supports both signed and unsigned literals.

1. Int: An int takes up 4 bytes of memory on most devices, allowing it to store values
between around -2 billion and +2 billion.
2. Char: There are 256 characters that can be represented by a single char, which
takes up one byte of memory. Characters such as 'A', 'b', '5', or '$' are enclosed in
single quotes.
3. Float: It can store values with an accuracy of about 6 decimal places and a range of
about 3.4 x 1038 in 4 bytes of memory.
4. Double: Double type, which uses 8 bytes of memory and has an accuracy of
about 15 decimal places, yields larger values. C treats floating point numbers as
doubles by default if no explicit type is supplied.

Derived Data Type: Beyond the fundamental data types, C also supports derived data
types, including arrays, pointers, structures, and unions. These data types give
programmers the ability to handle heterogeneous data, directly modify memory, and build
complicated data structures.

Keywords in C
Keywords in C are reserved words that have predefined meanings and are part of the C language
syntax. These keywords cannot be used as variable names, function names, or any other identifiers
within the program except for their intended purpose. They are used to define the structure flow and
behavior of a C program.

Keyword Identifier

Keyword is a pre-defined word. The identifier is a user-defined word

It can be written in both lowercase and


It must be written in a lowercase letter.
uppercase letters.

Its meaning is pre-defined in the c compiler. Its meaning is not defined in the c compiler.
It is a combination of alphanumeric
It is a combination of alphabetical characters.
characters.

It does not contain the underscore character. It can contain the underscore character.

C Format Specifier
The Format specifier is a string used in the formatted input and output functions. The format
string determines the format of the input and output. The format string always starts with a
'%' character.
The commonly used format specifiers in printf() function are:

Format specifier Description

It is used to print the signed integer value where signed integer means
%d or %i
that the variable can hold both positive and negative values.

It is used to print the unsigned integer value where the unsigned


%u
integer means that the variable can hold only positive value.

It is used to print the octal unsigned integer where octal integer value
%o
always starts with a 0 value.

It is used to print the hexadecimal unsigned integer where the


%x hexadecimal integer value always starts with a 0x value. In this,
alphabetical characters are printed in small letters such as a, b, c, etc.

It is used to print the hexadecimal unsigned integer, but %X prints the


%X
alphabetical characters in uppercase such as A, B, C, etc.

It is used for printing the decimal floating-point values. By default, it


%f
prints the 6 values after '.'.
%p It is used to print the address in a hexadecimal form.

%c It is used to print the unsigned character.

%s It is used to print the strings.

Escape Sequence in C
Programming languages like C have escape sequences as a standard feature. They enable the
inclusion of special characters and control patterns in strings and character constants that
are difficult to represent or write directly.
An escape sequence consists of a backslash () and a character that stands in for a special
character or control sequence. During the compilation process, the C compiler substitutes
any escape sequences it comes across with the relevant character or control sequence. It
enables the use of difficult-to-represent characters like newlines, tabs, quotations, and
backslashes.

Escape Sequence Meaning

\a Alarm or Beep

\b Backspace

\f Form Feed

\n New Line

\r Carriage Return

\t Tab (Horizontal)

\0 Null
Errors
Errors are the problems or the faults that occur in the program, which makes the behavior of
the program abnormal, and experienced developers can also make these faults. Programming
errors are also known as the bugs or faults, and the process of removing these bugs is known
as debugging. These errors are detected either during the time of compilation or execution.

Syntax error: Syntax errors are also known as the compilation errors as they occurred
at the compilation time, or we can say that the syntax errors are thrown by the compilers.
These errors are mainly occurred due to the mistakes while typing or do not follow the
syntax of the specified programming language. These mistakes are generally made by
beginners only because they are new to the language. These errors can be easily debugged
or corrected.

Run-time error: Sometimes the errors exist during the execution-time even after the
successful compilation known as run-time errors. When the program is running, and it is not
able to perform the operation is the main cause of the run-time error. The division by zero is
the common example of the run-time error.

Linker error: Linker errors are mainly generated when the executable file of the
program is not created. This can be happened either due to the wrong function prototyping
or usage of the wrong header file. For example, the main.c file contains the sub() function
whose declaration and definition is done in some other file such as func.c. During the
compilation, the compiler finds the sub() function in func.c file, so it generates two object
files, i.e., main.o and func.o. At the execution time, if the definition of sub() function is not
found in the func.o file, then the linker error will be thrown. The most common linker error
that occurs is that we use Main() instead of main().

Logical error : The logical error is an error that leads to an undesired output. These
errors produce the incorrect output, but they are error-free, known as logical errors. These
types of mistakes are mainly done by beginners. The occurrence of these errors mainly
depends upon the logical thinking of the developer. If the programmers sound logically
good, then there will be fewer chances of these errors.

Semantic error: Semantic errors are the errors that occurred when the statements are
not understandable by the compiler.
Compile-time Runtime

The compile-time errors are the errors The runtime errors are the errors which are not
which are produced at the compile-time, generated by the compiler and produce an
and they are detected by the compiler. unpredictable result at the execution time.

In this case, the compiler prevents the code


In this case, the compiler does not detect the error,
from execution if it detects an error in the
so it cannot prevent the code from the execution.
program.

It contains the syntax and semantic errors


It contains the errors such as division by zero,
such as missing semicolon at the end of the
determining the square root of a negative number.
statement.

Struct Union

When the variables are declared in a When the variable is declared in the union,
structure, the compiler allocates memory to the compiler allocates memory to the largest
each variables member. The size of a size variable member. The size of a union is
structure is equal or greater to the sum of the equal to the size of its largest data member
sizes of each data member. size.

Each variable member occupied a unique Variables members share the memory space
memory space. of the largest size variable.

Changing the value of a member will not Changing the value of one member will also
affect other variables members. affect other variables members.

We can initialize multiple variables of a In union, only the first data member can be
structure at a time. initialized.

All variable members store some value at any Exactly only one data member stores a value
point in the program. at any particular instance in the program.
The structure allows initializing multiple Union allows initializing only one variable
variable members at once. member at once.

It is used for storing one at a time from


It is used to store different data type values.
different data type values.

It allows accessing and retrieving any data It allows accessing and retrieving any one
member at a time. data member at a time.

Call by value Call by reference

A copy of the value is passed into the An address of value is passed into the
function function

Changes made inside the function is limited Changes made inside the function validate
to the function only. The values of the outside of the function also. The values of
actual parameters do not change by the actual parameters do change by
changing the formal parameters. changing the formal parameters.

Actual and formal arguments are created at Actual and formal arguments are created at
the different memory location the same memory location

The C Programming Language is a procedural-oriented programming language. It was


invented by Dennis Ritchie in 1972. It was designed specifically as a System Programming
Language for the development of various operating systems. Low-level memory access, a
small collection of keywords, and a straightforward style make the C Language ideal for
system programming, such as compiler and kernel development.
C Language is classified as a Middle-Level Programming language because it is close to both
machines (low-level) and humans (high-level). The C language program is translated into
assembly code, which enables low-level pointer arithmetic and it is machine independent
that is a property of high-level languages.
Dynamic Memory Allocation in C
Dynamic Memory Allocation in C provides several functions to allocate, reallocate, and free
memory at runtime, enabling flexible management of memory according to a program's
needs. These methods are essential for creating data structures that can grow or shrink
dynamically, such as linked lists or dynamic arrays.
Malloc Method in C
The malloc() function in C, crucial for dynamic memory allocation, allocates a specified byte
size on the heap and returns a void pointer to this space. Found in <stdlib.h>, it does not
initialize the allocated memory, leaving it with garbage values. Cast the void pointer to the
desired data type to use the allocated memory.
Calloc Method in C
calloc() in C is also a method that is used to allocate memory blocks in the heap section. Still,
it is generally used to allocate a sequence of memory blocks (contiguous memory) like
an array of elements. It is also present in <stdlib.h> header file.

malloc() method calloc() method

It is generally used to allocate It is generally used to allocate


a single memory block of the given size contiguous (multiple) blocks of memory
(in bytes) during the runtime of a of given size (in bytes) during the
program. runtime of a program.
It doesn't initializes the allocated It initializes all the allocated memory
memory block and contains some blocks with 0 (zero) value.
garbage value.

malloc() takes a single arugment i.e. the calloc() takes two arguments i.e. the
size of memory block that is to be number of elements and the size of one
allocated. element that are to be allocated.

syntax: (cast-data-type *)malloc(size-in- syntax: (cast-data-type *)calloc(num,


bytes) size-in-bytes)
Example : float *ptr = (float Example : float *ptr = (float *)calloc(10,
*)malloc(sizeof(float)); sizeof(float));
free() Method in C
The free() function in C plays a critical role in memory management by deallocating memory
blocks previously allocated with malloc() or calloc(). This helps prevent memory leaks and
ensures efficient use of memory.
realloc() Method in
The realloc() function in C is utilized for resizing a memory block previously allocated
with malloc() or calloc(). This versatile function can either increase or decrease the allocated
memory size and even allocate or deallocate memory.
Syntax
void *realloc(void *ptr, size_t new_size);
• ptr is a pointer to the memory block to resize.
• new_size is the new memory size in bytes.
• Example: int *arr = (int *)realloc(arr, new_size * sizeof(int));

File handling
File handling involves a series of operations including creation, opening, reading, writing,
and closing of files. To accomplish these tasks, C provides a set of functions such as fopen(),
fwrite(), fread(), fseek(), fprintf(), among others. These functions enable input, output, and
various file operations in C programs.

Function Description

Retrieves input from a file using a formatted string and variable


fscanf()
argument list.

fgets() Obtains a complete line of text from the file.

fgetc() Reads a single character from the file.

Function Description

This function, akin to printf(), uses a formatted string and variable arguments list to print
fprintf()
output to the file.

fputs() Prints an entire line in the file along with a newline character at the end.

fputc() Writes a single character into the file.

fputw() Writes a number to the file.

fwrite() Writes the specified number of bytes to the binary file.


A string is essentially an array of characters terminated by the null character '\0'. Unlike a
generic character array, a C string always concludes with this unique terminator, indicating
its endpoint. Represented as a one-dimensional array, each character within occupies one
byte of memory. For instance, when declaring "char s[10]", the array is implicitly initialized
with a null terminator. This distinct characteristic ensures that operations on C strings rely on
this sentinel value to determine string boundaries, facilitating efficient string manipulation
and processing in C programs.

Function Description

strlen() It returns the string's length.

It returns the specified value if the value specified is less than the string length,
strnlen()
otherwise the string length.

strcmp() It compares two strings and returns 0 if the strings are the same.

strncmp() It compares two strings only to n characters.

strcat() It concatenates two strings and returns the concatenated string.

strncat() It concatenates n characters of one string to another string.

strcpy() It copies one string into another.

strncpy() It copies the first n characters of one string into another.

strchr() It finds out the first occurrence of a given character in a string.

strrchr() It finds out the last occurrence of a given character in a string.

strstr() It finds out the first occurrence of a given string in a string.

It finds out the first occurrence of a given string in a string where the search is limited to
strnstr()
n characters.

strcasecmp() It compares two strings without sensitivity to the case.

strncasecmp() It compares n characters of one string to another without sensitivity to the case.

You might also like