Lets Learn C Programming
Chapter 01 - Introduction
History
• C is a programming language developed at AT & T’s Bell Laboratories of
USA in 1972.
• It was designed and written by a man named Dennis Ritchie.
• In the late seventies C began to replace the more familiar languages of that
time like PL/I, ALGOL, etc.
• No one pushed C. It wasn’t made the ‘official’ Bell Labs language. Thus,
without any advertisement C’s reputation spread and its pool of users
grew.
• Ritchie seems to have been rather surprised that so many programmers
preferred C to older languages like FORTRAN or PL/I, or the newer ones like
Pascal and APL.
• Possibly why C seems so popular is because it is reliable, simple and easy to
use.
Digiimento Education www.Digiimento.com 2
Benefits of C language
• As a middle-level language, C combines the features of both high-level and
low-level languages. It can be used for low-level programming, such as
scripting for drivers and kernels and it also supports functions of high-level
programming languages, such as scripting for software applications etc.
• C is a structured programming language which allows a complex program
to be broken into simpler programs called functions. It also allows free
movement of data across these functions.
• Various features of C including direct access to machine level hardware
APIs, the presence of C compilers, deterministic resource use and dynamic
memory allocation make C language an optimum choice for scripting
applications and drivers of embedded systems.
• C language is case-sensitive which means lowercase and uppercase letters
are treated differently.
Digiimento Education www.Digiimento.com 3
Benefits of C language
• C is highly portable and is used for scripting system applications which
form a major part of Windows, UNIX, and Linux operating system.
• C is a general-purpose programming language and can efficiently
work on enterprise applications, games, graphics, and applications
requiring calculations, etc.
• C language has a rich library which provides a number of built-in
functions. It also offers dynamic memory allocation.
• C implements algorithms and data structures swiftly, facilitating faster
computations in programs. This has enabled the use of C in
applications requiring higher degrees of calculations like MATLAB and
Mathematica.
Digiimento Education www.Digiimento.com 4
The C language has formed the basis for many languages including
C++, C–, C#, Objective-C, BitC, C-shell, csh, D, Java, JavaScript, Go, Rust,
Julia, Limbo, LPC, PHP, Python, Perl, Seed7, Vala, Verilog and many
more other languages are there.
Digiimento Education www.Digiimento.com 5
Structure of a C program
Digiimento Education www.Digiimento.com 6
Header Files Inclusion:
A header file is a file with extension .h which contains C function declarations and
macro definitions to be shared between several source files.
Some of C 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
Syntax to include a header file in C:
#𝑖𝑛𝑐𝑙𝑢𝑑𝑒 < (ℎ𝑒𝑎𝑑𝑒𝑟_𝑓𝑖𝑙𝑒_𝑛𝑎𝑚𝑒). ℎ >
Digiimento Education www.Digiimento.com 7
Main Method Declaration:
• The next part of a C program is to declare the main() function.
• Syntax to Declare main method:
1. 𝑖𝑛𝑡 𝑚𝑎𝑖𝑛
2. {
3. …
4. }
Digiimento Education www.Digiimento.com 8
Variable Declaration:
• It refers to the variables that are to be used in the function.
• Please note that in 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.
• Example:
1. 𝑖𝑛𝑡 𝑚𝑎𝑖𝑛
2. {
3. 𝒊𝒏𝒕 𝒂;
4. .
5. .
6. }
Digiimento Education www.Digiimento.com 9
Body:
• Body of a function in C program, refers to the operations that are
performed in the functions. It can be anything like manipulations,
searching, sorting, printing, etc.
• Example:
1. 𝑖𝑛𝑡 𝑚𝑎𝑖𝑛
2. {
3. 𝑖𝑛𝑡 𝑎;
4. 𝑝𝑟𝑖𝑛𝑡𝑓 %d, 𝑎 ;
5. .
6. .
7. }
Digiimento Education www.Digiimento.com 10
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.
In any other case, there will be a return statement and the return value will be of the type of the
specified return type.
Example:
1. 𝑖𝑛𝑡 𝑚𝑎𝑖𝑛
2. {
3. 𝑖𝑛𝑡 𝑎;
4. 𝑝𝑟𝑖𝑛𝑡𝑓 %d, 𝑎 ;
5. 𝒓𝒆𝒕𝒖𝒓𝒏 𝟎;
6. }
Digiimento Education www.Digiimento.com 11
Digiimento Education www.Digiimento.com 12
Let us analyze the program line by line.
• Line 1: [ #include <stdio.h> ] In a C program, all lines that start with # are
processed by preprocessor which is a program invoked by the compiler.
• In a very basic term, preprocessor takes a C program and produces another
C program.
• The produced program has no lines starting with #, all such lines are
processed by the preprocessor.
• In the previous example, preprocessor copies the preprocessed code of
stdio.h to our file.
• The .h files are called header files in C. These header files generally contain
declaration of functions.
• We need stdio.h for the function printf() used in the program.
Digiimento Education www.Digiimento.com 13
Line 2 [ int main(void) ]
• There must to be starting point from where execution of compiled C
program begins.
• In C, the execution typically begins with first line of main().
• The void written in brackets indicates that the main doesn’t take any
parameter.
• main() can be written to take parameters also.
• The int written before main indicates return type of main().
• The value returned by main indicates status of program termination.
Digiimento Education www.Digiimento.com 14
Line 3 and 6: [ { and } ]
• In C language, a pair of curly brackets define a scope and mainly used
in functions and control statements like if, else, loops.
• All functions must start and end with curly brackets.
Line 4 [ printf(“DigiiMento Education”); ]
• printf() is a standard library function to print something on standard
output.
• The semicolon at the end of printf indicates line termination.
• In C, semicolon is always used to indicate end of statement.
Digiimento Education www.Digiimento.com 15
Line 5 [ return 0; ]
• The return statement returns the value from main().
• The returned value may be used by operating system to know
termination status of your program.
• The value 0 typically means successful termination
Digiimento Education www.Digiimento.com 16
C Programming Language Standard
• What to do when a C program produces different results in two
different compilers?
𝑣𝑜𝑖𝑑 𝑚𝑎𝑖𝑛() {
The above program fails in gcc as the return type of main is void, but it
compiles in Turbo C. How do we decide whether it is a legitimate C
program or not?
Digiimento Education www.Digiimento.com 17
Consider the following program as another example. It produces
different results in different compilers.
Digiimento Education www.Digiimento.com 18
Consider the following program as another example. It produces
different results in different compilers.
2 1 3 - using g++ 4.2.1 on Linux.i686 1 2 3 - using SunStudio C++ 5.9 on SunOS.x86pc
1 2 3 - using SunStudio C++ 5.9 on Linux.i686 1 2 3 - using g++ 4.2.1 on SunOS.sun4u
2 1 3 - using g++ 4.2.1 on SunOS.x86pc 1 2 3 - using SunStudio C++ 5.9 on SunOS.sun4u
Digiimento Education www.Digiimento.com 19
Which compiler is right?
• The answer to all such questions is C standard. In all
Year C Standard
such cases we need to see what C standard says about
such programs. 1972 Birth
• What is C standard? 1978 K&R C
The latest C standard is ISO/IEC 9899:2011, also known 1989/1990 ANSI C and ISO C
as C11 as the final draft was published in 2011.
1999 C99
• Before C11, there was C99.
2011 C11
• The C11 final draft is available
2017/2018 C18
• http://www.open-
std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
Digiimento Education www.Digiimento.com 20
Can we know behaviour of all programs from C standard?
• C standard leaves some behavior of many C constructs as undefined
and some as unspecified to simplify the specification and allow some
flexibility in implementation.
• For example, in C the use of any automatic variable before it has been
initialized yields undefined behavior and order of evaluations of
subexpressions is unspecified.
• This specifically frees the compiler to do whatever is easiest or most
efficient.
Digiimento Education www.Digiimento.com 21
Undefined Behavior
• undefined behavior (UB) is the result of executing computer code whose behavior is not
prescribed by the language specification to which the code adheres, for the current state of the
program.
• This happens when the translator of the source code makes certain assumptions, but these
assumptions are not satisfied during execution.
• In the standards for these languages the semantics of certain operations is described as
undefined. These cases typically represent unambiguous bugs in the code, for example indexing
an array outside of its bounds.
• An implementation is allowed to assume that such operations never occur in correct standard-
conforming program code.
• In the case of C/C++, the compiler is allowed to give a compile-time diagnostic in these cases, but
is not required to: the implementation will be considered correct whatever it does in such cases,
analogous to don't-care terms in digital logic.
• It is the responsibility of the programmer to write code that never invokes undefined behavior,
although compiler implementations are allowed to issue diagnostics when this happens.
• This assumption can make various program transformations valid or simplify their proof of
correctness, giving flexibility to the implementation. As a result, the compiler can often make
more optimizations. It also allows more compile-time checks by both compilers and static
program analysis.
Digiimento Education www.Digiimento.com 22
• In the C community, undefined behavior may be humorously referred to as "nasal
demons", after a comp.std.c post that explained undefined behavior as allowing
the compiler to do anything it chooses, even "to make demons fly out of your
nose".
• Under some circumstances there can be specific restrictions on undefined
behavior.
• For example, the instruction set specifications of a CPU might leave the behavior
of some forms of an instruction undefined, but if the CPU supports memory
protection then the specification will probably include a blanket rule stating that
no user-accessible instruction may cause a hole in the operating system's
security;
• so an actual CPU would be permitted to corrupt user registers in response to such
an instruction, but would not be allowed to, for example, switch into supervisor
mode.
Digiimento Education www.Digiimento.com 23
The value of x cannot be negative and, given that signed integer overflow is undefined
behavior in C, the compiler can assume that value < 2147483600 will always be false.
The code is therefore semantically equivalent to:
Had the compiler been forced to
assume that signed integer overflow
has wraparound behavior, then the
transformation above would not have
been legal.
Digiimento Education www.Digiimento.com 24
Benefits of Undefined behavior
• Documenting an operation as undefined behavior allows compilers to assume that
this operation will never happen in a conforming program. This gives the compiler
more information about the code and this information can lead to more optimization
opportunities.
• Another benefit from allowing signed integer overflow to be undefined is that it
makes it possible to store and manipulate a variable's value in a processor register
that is larger than the size of the variable in the source code.
• For example, if the type of a variable as specified in the source code is narrower than
the native register width (such as "int" on a 64-bit machine, a common scenario),
then the compiler can safely use a signed 64-bit integer for the variable in the
machine code it produces, without changing the defined behavior of the code.
• If a program depended on the behavior of a 32-bit integer overflow, then a compiler
would have to insert additional logic when compiling for a 64-bit machine, because
the overflow behavior of most machine instructions depends on the register width.
• A further important benefit of undefined signed integer overflow is that it enables,
though does not require, erroneous overflows to be detected at compile-time or by
static program analysis, or by run-time checks such as the Clang and GCC sanitizers
and valgrind; if such overflow were defined with semantics such as wrap-around
then compile-time checks would not be possible.
Digiimento Education www.Digiimento.com 25
Risks
• C and C++ standards have several forms of undefined behavior throughout, which offer increased liberty
in compiler implementations and compile-time checks at the expense of undefined run-time behavior if
present.
• In particular, the ISO C standard has an appendix listing common sources of undefined behavior.
Moreover, compilers are not required to diagnose code that relies on undefined behavior.
• Hence, it is common for programmers, even experienced ones, to rely on undefined behavior either by
mistake, or simply because they are not well-versed in the rules of the language that can span hundreds
of pages.
• This can result in bugs that are exposed when a different compiler, or different settings, are used.
Testing or fuzzing with dynamic undefined behavior checks enabled, e.g. the Clang sanitizers, can help to
catch undefined behavior not diagnosed by the compiler or static analyzers.
• In scenarios where security is critical, undefined behavior can lead to security vulnerabilities in
software.
• When GCC's developers changed their compiler in 2008 such that it omitted certain overflow checks
that relied on undefined behavior, CERT issued a warning against the newer versions of the compiler.
• Linux Weekly News pointed out that the same behavior was observed in PathScale C, Microsoft Visual
C++ 2005 and several other compilers;the warning was later amended to warn about various compilers.
Digiimento Education www.Digiimento.com 26
Digiimento Education www.Digiimento.com 27
In C and C++, the comparison of pointers to objects is only strictly defined if the pointers point to members of the
same object, or elements of the same array
Reaching the end of a value-returning function (other than main()) without a return statement results in
undefined behavior if the value of the function call is used by the caller:
Digiimento Education www.Digiimento.com 28
Modifying behavior in sequence point
• Modifying an object between two sequence points more than once
produces undefined behavior. There are considerable changes in what
causes undefined behavior in relation to sequence points as of C++11.
The following example will however cause undefined behavior in both
C++ and C.
• When modifying an object between two sequence points, reading the
value of the object for any other purpose than determining the value
to be stored is also undefined behaviour
Digiimento Education www.Digiimento.com 29
• In C/C++ bitwise shifting a value by a number of bits which is either a negative number or
is greater than or equal to the total number of bits in this value results in undefined
behavior.
• The safest way (regardless a compiler vendor) is to always keep the number of bits to
shift (the right operand of the << and >> bitwise operators) within the range:
< 0, 𝑠𝑖𝑧𝑒𝑜𝑓(𝑣𝑎𝑙𝑢𝑒) ∗ 𝐶𝐻𝐴𝑅_𝐵𝐼𝑇 − 1 >
(where value is the left operand).
Digiimento Education www.Digiimento.com 30
Unspecified behavior
• Unspecified behavior is behavior that may vary on different
implementations of a programming language
• A program can be said to contain unspecified behavior when its source
code may produce an executable that exhibits different behavior when
compiled on a different compiler, or on the same compiler with different
settings, or indeed in different parts of the same executable.
• While the respective language standards or specifications may impose a
range of possible behaviors, the exact behavior depends on the
implementation and may not be completely determined upon examination
of the program's source code.
• Unspecified behavior will often not manifest itself in the resulting
program's external behavior, but it may sometimes lead to differing
outputs or results, potentially causing portability problems.
Digiimento Education www.Digiimento.com 31
• where f and g both modify b, the result stored in a
may be different depending on whether f(b) or g(b) is
evaluated first.
• In the C and C++ languages, this also applies to
function arguments.
• The resulting program will write its two lines of output
in an unspecified order.
• In other languages, such as Java, the order of
evaluation of operands and function arguments is
explicitly defined
Digiimento Education www.Digiimento.com 32
Escape Sequences in C
In C programming language, there are 256
numbers of characters in character set. The
entire character set is divided into 2 parts
i.e. the ASCII characters set and the
extended ASCII characters set. But apart
from that, some other characters are also
there which are not the part of any
characters set, known as ESCAPE characters.
Digiimento Education www.Digiimento.com 33
Some coding examples of escape characters
Digiimento Education www.Digiimento.com 34
Some coding examples of escape characters
• The output is dependent upon compiler.
Digiimento Education www.Digiimento.com 35
Some coding examples of escape characters
Digiimento Education www.Digiimento.com 36
Some coding examples of escape characters
Digiimento Education www.Digiimento.com 37
Some coding examples of escape characters
Digiimento Education www.Digiimento.com 38
Some coding examples of escape characters
Digiimento Education www.Digiimento.com 39
Output: (Depend upon compiler)
It Contains two escape sequence means it after printing the \ the compiler read the next \ as as new line
character i.e. \n
Digiimento Education www.Digiimento.com 40
Digiimento Education www.Digiimento.com 41
Digiimento Education www.Digiimento.com 42
Explanation : Here 000 is one to three octal digits(0….7) means there must be atleast one octal digit after \
and maximum three.Here 072 is the octal notation, first it is converted to decimal notation that is the ASCII
value of char ‘:’. At the place of \072 there is : and the output is A:5.
Digiimento Education www.Digiimento.com 43
Digiimento Education www.Digiimento.com 44
C/C++ Tokens
• A token is the smallest element of a program that is meaningful to the
compiler. Tokens can be classified as follows:
• Keywords
• Identifiers
• Constants
• Strings
• Special Symbols
• Operators
Digiimento Education www.Digiimento.com 45
Keywords
• Keywords are pre-defined or reserved words in a programming
language.
• Each keyword is meant to perform a specific function in a program.
• Since keywords are referred names for a compiler, they can’t be used
as variable names because by doing so, we are trying to assign a new
meaning to the keyword which is not allowed.
• You cannot redefine keywords. However, you can specify text to be
substituted for keywords before compilation by using C/C++
preprocessor directives.
• C language supports 32 keywords which are given below:
Digiimento Education www.Digiimento.com 46
in C++ there are 31 additional keywords other than C Keywords they are:
Digiimento Education www.Digiimento.com 47
Identifiers:
• Identifiers are used as the general terminology for naming of variables,
functions and arrays.
• These are user defined names consisting of arbitrarily long sequence of
letters and digits with either a letter or the underscore(_) as a first
character.
• Identifier names must differ in spelling and case from any keywords.
• You cannot use keywords as identifiers; they are reserved for special use.
• Once declared, you can use the identifier in later program statements to
refer to the associated value.
• A special kind of identifier, called a statement label, can be used in goto
statements.
Digiimento Education www.Digiimento.com 48
There are certain rules that should be followed while naming c
identifiers:
1. They must begin with a letter or underscore(_).
2. They must consist of only letters, digits, or underscore. No other
special character is allowed.
3. It should not be a keyword.
4. It must not contain white space.
5. It should be up to 31 characters long as only first 31 characters are
significant.
Digiimento Education www.Digiimento.com 49
Digiimento Education www.Digiimento.com 50
• In the above program there are 2 identifiers:
• main: method name.
• a: variable name.
Digiimento Education www.Digiimento.com 51
Constants:
• Constants are also like normal variables. But, only difference is, their
values can not be modified by the program once they are defined.
• Constants refer to fixed values. They are also called as literals.
• Constants may belong to any of the data type.
Syntax:
const data_type variable_name;
(or) const data_type *variable_name;
Digiimento Education www.Digiimento.com 52
Types of Constants:
• Integer constants – Example: 0, 1, 1218, 12482
• Real or Floating point constants – Example: 0.0, 1203.03, 30486.184
• Octal & Hexadecimal constants – Example: octal: (013 )8 =
(11)10, Hexadecimal: (013)16 = (19)10
• Character constants -Example: ‘a’, ‘A’, ‘z’
• String constants -Example: “Digiimento”
Digiimento Education www.Digiimento.com 53
Strings:
• Strings are nothing but an array of characters ended with a null
character (‘\0’).
• This null character indicates the end of the string.
• Strings are always enclosed in double quotes.
• Whereas, a character is enclosed in single quotes in C and C++.
Digiimento Education www.Digiimento.com 54
• Declarations for String:
char string[20] = {‘D’, ’i’, ‘g’, ‘i’, ‘i’, ‘M’, ‘e’, ‘n’, ‘t’, ’o’, ‘e’, ‘\0’};
char string[20] = “Digiimento”;
char string [] = “DigiiMento”;
Difference between above declarations are:
• when we declare char as “string[20]”, 20 bytes of memory space is
allocated for holding the string value.
• When we declare char as “string[]”, memory space will be allocated as
per the requirement during execution of the program.
Digiimento Education www.Digiimento.com 55
Special Symbols:
• The following special symbols are used in C having some special meaning and
thus, cannot be used for some other purpose.[] () {}, ; * = #Brackets[]: Opening
and closing brackets are used as array element reference. These indicate single
and multidimensional subscripts.
• Parentheses(): These special symbols are used to indicate function calls and
function parameters.
• Braces{}: These opening and ending curly braces marks the start and end of a
block of code containing more than one executable statement.
• comma (, ): It is used to separate more than one statements like for separating
parameters in function calls.
• semi colon : It is an operator that essentially invokes something called an
initialization list.
• asterick (*): It is used to create pointer variable.
• assignment operator: It is used to assign values.
• pre processor(#): The preprocessor is a macro processor that is used
automatically by the compiler to transform your program before actual
compilation.
Digiimento Education www.Digiimento.com 56
Operators:
• Operators are symbols that triggers an action when applied to C variables and
other objects. The data items on which operators act upon are called operands.
Depending on the number of operands that an operator can act upon, operators
can be classified as follows:
• Unary Operators: Those operators that require only single operand to act upon
are known as unary operators.
For Example increment and decrement operators
• Binary Operators: Those operators that require two operands to act upon are
called binary operators. Binary operators are classified into :
• Arithmetic operators
• Relational Operators
• Logical Operators
• Assignment Operators
• Conditional Operators
• Bitwise Operators
• Ternary Operators: These operators requires three operands to act upon. For
Example Conditional operator(?:).
Digiimento Education www.Digiimento.com 57
Space Intentionally Left Blank
Digiimento Education www.Digiimento.com 58
References
• GeeksForGeeks
• WikiPedia
• Yashwant Kanetkar
Digiimento Education www.Digiimento.com 59