C Complete
C Complete
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).
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
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:
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 octal unsigned integer where octal integer value
%o
always starts with a 0 value.
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.
\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.
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 allows accessing and retrieving any data It allows accessing and retrieving any one
member at a time. data member at a time.
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
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.
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
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.
Function Description
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.
It finds out the first occurrence of a given string in a string where the search is limited to
strnstr()
n characters.
strncasecmp() It compares n characters of one string to another without sensitivity to the case.