2. HISTORY
Background and Origins
ALGOL (1958): One of the first programming languages that influenced many languages
including C. Introduced by Friedrich, John Backus, Peter Naur, Heinz RutisHauser.
BCPL (Basic Combined Programming Language, 1966): Developed by Martin Richards, it
influenced the development of B language.
B Language (1969): Created by Ken Thompson, it was the direct predecessor of C.
Development of C
1972: Dennis Ritchie developed C at Bell Labs for the Unix operating system.
Evolution of C
1978: The publication of "The C Programming Language" by Brian Kernighan and Dennis
Ritchie, also known as K&R C.
1989/1990: ANSI C and ISO C (C89/C90)
ANSI (American National Standards Institute): Standardized the language, ensuring
portability and consistency.
3. 1999: C99
Improvements: Added new features like inline functions, variable-length arrays,
and several new data types.
2011: C11
Enhancements: Multithreading support, improved Unicode support, and new
standard libraries.
2018: C18
Latest Standard: Mainly bug fixes and minor improvements over C11.
4. Characteristics of C
• Procedural Language: Emphasizes procedures or routines.
• General-purpose: Suitable for system programming, game
development, real-time systems, etc.
• Low-level Language Features: Close to hardware, providing direct
manipulation of hardware and memory.
• Portable: Programs written in C can run on different machines with
minimal or no modification.
Impact of C
• Influence on Other Languages: Many modern languages like C++, C#,
Java, JavaScript, and Python have roots in C.
• Legacy: Continues to be widely used in systems programming,
embedded systems, and high-performance applications.
5. Importance of C Programming
• Foundation for Other Languages:
- C is the foundational language for many modern programming languages like C++, Java,
and C#.
- Understanding C provides a strong base for learning other languages and
understanding their core concepts.
• System-Level Programming:
- C is extensively used in developing operating systems and embedded systems.
- It allows direct manipulation of hardware and memory, providing high performance
and control.
• Performance:
- C is known for its efficiency and speed.
- Programs written in C run faster than those in most other languages due to its low-level
nature and minimal runtime overhead.
6. • Portability:
- C programs can be easily transferred and run on different machines with little to
no modification.
- This feature is crucial for developing cross-platform applications and system
software.
• Standard Library:
- C has a rich set of built-in functions provided by its standard library.
These functions simplify common programming tasks like input/output
operations, string manipulation, and memory management.
• Structured Programming:
- C supports structured programming concepts such as functions, loops, and
conditionals.
These features encourage writing clean, maintainable, and reusable code.
7. • Widely Used in Academia and Industry:
- C is often the first language taught in computer science courses due to its
simplicity and foundational importance.
- It is also extensively used in various industries, including software development,
hardware design, and research.
Applications of C Programming
• Operating Systems:
- Many operating systems, including Unix, Linux, and Windows, are written in C.
- C’s ability to perform low-level operations and interact with hardware makes it
ideal for system-level programming.
8. • Embedded Systems:
- C is commonly used in programming microcontrollers and embedded systems.
- Its efficiency and control over hardware resources are critical for developing
firmware and drivers.
• Game Development:
- C is used in developing game engines and high-performance games.
- Its performance and ability to handle real-time operations are essential for
game programming.
• Database Management Systems:
- Many database management systems, like MySQL, are developed in C.
- The language’s efficiency and ability to manage memory dynamically are crucial
for handling large databases.
9. • Compilers and Interpreters:
- Compilers for various programming languages are often written in C.
- C’s ability to handle complex algorithms and data structures makes it suitable
for compiler development.
• Network Programming:
- C is used in developing network protocols and communication systems.
- Its performance and ability to handle low-level network operations are vital for
network programming.
• Scientific and Engineering Applications:
- C is used in developing simulations, mathematical models, and engineering
applications.
-Its computational efficiency and precision make it ideal for scientific computing.
10. • Text Editors and Utilities:
- Many text editors and utilities in Unix/Linux systems are written in C.
- Its simplicity and direct access to system resources are beneficial for developing
system utilities.
The importance and applications of C programming, emphasize its foundational
role in computer science and its versatility in various domains. Understanding C
equips students with the skills needed for system-level programming and opens
up opportunities in multiple fields, from operating systems to game development
and beyond.
16. SAMPLE PROGRAM
#include <stdio.h> // Preprocessor directive to include the standard input-output
library
// Main function: where the program execution begins
int main() {
// Variable declaration
int num1, num2, sum;
// Statements: assign values to variables
num1 = 10;
num2 = 20;
17. // Expression: calculate the sum of two numbers
sum = num1 + num2;
// Print the result using a function from stdio.h
printf("The sum of %d and %d is %dn", num1, num2, sum);
// Return statement: indicates that the program ended successfully
return 0;
}
Result:
The sum of 10 and 20 is 30
23. CHARACTER SET:
Every program that we draft for the C program consists of various statements. We
use words for constructing these statements. Meanwhile, we use characters for
constructing these statements. These characters must be from the C language
character set.
Types of Characters in C
The C programming language provides support for the following types of
characters. In other words, these are the valid characters that we can use in the C
language:
• Digits
• Alphabets
• Main Characters
All of these serve a different set of purposes, and we use them in different contexts
in the C language.
24. Alphabets
The C programming language provides support for all the alphabets that we
use in the English language. Thus, in simpler words, a C program would easily
support a total of 52 different characters- 26 uppercase and 26 lowercase.
• Lowercase alphabets - a to z (a, b, c, d, e, f,....,x, y, z)
• Uppercase alphabets - A to Z(A, B ,C, D, E, F,....,X, Y, Z)
Digits
The C programming language provides the support for all the digits that help
in constructing/ supporting the numeric values or expressions in a program.
These range from 0 to 9, and also help in defining an identifier. Thus, the C
language supports a total of 10 digits for constructing the numeric values or
expressions in any program.
Digits - 0 to 9(0,1,2,3,4,5,,6,7,8,9)
25. Special Characters
We use some special characters in the C language for some special purposes,
such as logical operations, mathematical operations, checking of conditions,
backspaces, white spaces, etc.
We can also use these characters for defining the identifiers in a much better
way. For instance, we use underscores for constructing a longer name for a
variable, etc.
The C programming language provides support for the following types of
special characters:
Special characters - ` ~ @ ! $ # ^ * % & ( ) [ ] { } < > + = _ – | / ; : ‘ “ , . ?
26. White Spaces
The white spaces in the C programming language contain the following:
• Blank Spaces
• Carriage Return ((/r) escape character)
• Tab
• New Line
27. Purpose of Character Set in C
The character sets help in defining the valid characters that we can use in the
source program or can interpret during the running of the program. For the
source text, we have the source character set, while we have the execution
character set that we use during the execution of any program.
But we have various types of character sets. For instance, one of the
character sets follows the basis of the ASCII character definitions, while the
other set consists of various kanji characters (Japanese).
The type of character set we use will have no impact on the compiler- but we
must know that every character has different, unique values. The C language
treats every character with different integer values. Let us know a bit more
about the ASCII characters.
28. ASCII Values
All the character sets used in the C language have their equivalent ASCII
value. The ASCII value stands for American Standard Code for Information
Interchange value. It consists of less than 256 characters, and we can
represent these in 8 bits or even less.
However, a majority of the ANSI-compatible compilers in C accept these
ASCII characters for both the character sets- the source and the execution.
Every ASCII character will correspond to a specific numeric value.
31. A TO Z (uppercase) - 65 to 90
a to z (lowercase) - 97 to 122
Character Equivalence
Here are all the character sets in ASCII. The ASCII character set has three
types of values,
• Hexadecimal,
• Decimal,
• Octal values
.....
32. Tokens in C
A token in C can be defined as the smallest individual element of the C
programming language that is meaningful to the compiler. It is the basic
component of a C program.
Types of Tokens in C
The tokens of C language can be classified into six types based on the
functions they are used to perform. The types of C tokens are as follows:
Keywords
Identifiers
Constants
Strings
Special Symbols
Operators
34. Keywords
The keywords are pre-defined or reserved words (with special meaning) 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 the text to be substituted for keywords before
compilation by using C preprocessor directives.
C language supports 32 keywords which are given below:
auto double int struct
break else long switch
case enum register typedef
char extern return union
35. const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
Identifiers
Identifiers are used as the general terminology for the naming of variables,
functions, and arrays. These are user-defined names consisting of an
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 identifier
called a statement label can be used in goto statements. Eg., main, x, sum
36. Rules for Naming Identifiers
Certain rules should be followed while naming c identifiers which are as
follows:
• They must begin with a letter or underscore(_).
• They must consist of only letters, digits, or underscore. No other special
character is allowed.
• It should not be a keyword.
• It must not contain white space.
• It should be up to 31 characters long as only the first 31 characters are
significant.
Note: Identifiers are case-sensitive so names like variable and Variable will be
treated as different.
For example,
main: method name; a:variable name
37. C Token – Constants
The constants refer to the variables with fixed values. They are like normal
variables but with the difference that their values can not be modified in the
program once they are defined.
Constants may belong to any of the data types.
Examples of Constants in C
const int c_var = 20;
const int* const ptr = &c_var;
Pi value = 3.14
38. C Token – 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++.
Examples of String
char string[20] = {‘g’, ’e’, ‘e’, ‘k’, ‘s’, ‘f’, ‘o’, ‘r’, ‘g’, ’e’, ‘e’, ‘k’, ‘s’, ‘0’};
char string[20] = “geeksforgeeks”;
char string [] = “geeksforgeeks”;
39. C Token – Special Symbols
The following special symbols are used in C having some special meaning and thus,
cannot be used for some other purpose. Some of these are listed below:
• Brackets[]: Opening and closing brackets are used as array element references. 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 mark the start and end of a block of
code containing more than one executable statement.
• Comma (, ): It is used to separate more than one statement like for separating
parameters in function calls.
• Colon(:): It is an operator that essentially invokes something called an initialization list.
• Semicolon(;): It is known as a statement terminator. It indicates the end of one logical
entity. That’s why each individual statement must be ended with a semicolon.
40. • Asterisk (*): It is used to create a pointer variable and for the multiplication of
variables.
• Assignment operator(=): It is used to assign values and for logical operation
validation.
• Pre-processor (#): The preprocessor is a macro processor that is used
automatically by the compiler to transform your program before actual
compilation.
• Period (.): Used to access members of a structure or union.
• Tilde(~): Bitwise One’s Complement Operator.
C Token – Operators
Operators are symbols that trigger an action when applied to C variables and other
objects. The data items on which operators act are called operands.
Depending on the number of operands that an operator can act upon, operators
can be classified as follows:
41. • Unary Operators: Those operators that require only a 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 can further are classified
into:
- Arithmetic operators
- Relational Operators
- Logical Operators
- Assignment Operators
- Bitwise Operator
Ternary Operator: The operator that requires three operands to act upon is
called the ternary operator. Conditional Operator(?) is also called the ternary
operator.
42. DATA TYPES IN C
Each variable in C has an associated data type. It specifies the type of data that
the variable can store like integer, character, floating, double, etc. Each data
type requires different amounts of memory and has some specific operations
which can be performed over it. The data type is a collection of data with values
having fixed values, meaning as well as its characteristics.
The data types in C can be classified as follows
• Primitive Data Types- Primitive data types are the most basic data types
that are used for representing simple values such as integers, float,
characters, etc.
• User Defined Data Types- The user-defined data types are defined by the
user himself.
• Derived Types- The data types that are derived from the primitive or built-
in datatypes are referred to as Derived Data Types.
44. assignment....
OPERATORS AND EXPRESSIONS:
Operators: An operator in C can be defined as the symbol that helps us to
perform some specific mathematical, relational, bitwise, conditional, or
logical computations on values and variables. The values and variables used
with operators are called operands. So we can say that the operators are the
symbols that perform operations on operands.
Example,
c = a + b;
Here, ‘+’ is the operator known as the addition operator, and ‘a’ and ‘b’ are
operands. The addition operator tells the compiler to add both of the
operands ‘a’ and ‘b’.
45. Types of Operators in C
C language provides a wide range of operators that can be classified into 6 types based on
their functionality:
Arithmetic Operators
Relational Operators
Logical Operators
Assignment Operators
Increment and Decrement Operators
Conditional Operators
Bitwise Operators
Other Operators
Arithmetic Operators
The arithmetic operators are used to perform arithmetic/mathematical operations on
operands. There are 9 arithmetic operators in C language:
48. // C program to illustrate the arithmatic operators
#include <stdio.h>
int main()
{
int a = 25, b = 5;
// using operators and printing results
printf("a + b = %dn", a + b);
printf("a - b = %dn", a - b);
printf("a * b = %dn", a * b);
printf("a / b = %dn", a / b);
printf("a % b = %dn", a % b);
49. printf("+a = %dn", +a);
printf("-a = %dn", -a);
printf("a++ = %dn", a++);
printf("a-- = %dn", a--);
return 0;
}
Result:
a + b = 30
a - b = 20
a * b = 125
a / b = 5
a % b = 0
+a = 25
-a = -25
a++ = 25
a-- = 25
50. Relational Operators
The relational operators in C are used for the comparison of the two
operands. All these operators are binary operators that return true or false
values as the result of comparison.
53. // C program to illustrate the relational operators
#include <stdio.h>
int main()
{
int a = 25, b = 5;
// using operators and printing results
printf("a < b : %dn", a < b);
printf("a > b : %dn", a > b);
printf("a <= b: %dn", a <= b);
printf("a >= b: %dn", a >= b);
printf("a == b: %dn", a == b);
printf("a != b : %dn", a != b);
return 0;
}
Result:
a < b : 0
a > b : 1
a <= b: 0
a >= b: 1
a == b: 0
a != b : 1
54. Logical Operator in C
Logical Operators are used to combine two or more conditions/constraints or
to complement the evaluation of the original condition in consideration. The
result of the operation of a logical operator is a Boolean value either true or
false.
56. Example,
#include <stdio.h>
int main()
{
int a = 25, b = 5;
// using operators and printing results
printf("a && b : %dn", a && b);
printf("a || b : %dn", a || b);
printf("!a: %dn", !a);
return 0;
}
Result:
a && b : 1
a || b : 1
!a: 0
57. Assignment Operators in C
Assignment operators are used to assign value to a variable. The left side
operand of the assignment operator is a variable and the right side operand
of the assignment operator is a value. The value on the right side must be of
the same data type as the variable on the left side otherwise the compiler
will raise an error.
The assignment operators can be combined with some other operators in C
to provide multiple operations using single operator. These operators are
called compound operators.
62. // Working of assignment operators
#include <stdio.h>
int main()
{
int a = 5, c;
c = a; // c is 5
printf("c = %dn", c);
c += a; // c is 10
printf("c = %dn", c);
c -= a; // c is 5
printf("c = %dn", c);
c *= a; // c is 25
printf("c = %dn", c);
c /= a; // c is 5
printf("c = %dn", c);
c %= a; // c = 0
printf("c = %dn", c);
return 0;
}
64. Bitwise Operators in C
The Bitwise operators are used to perform bit-level operations on the
operands. The operators are first converted to bit-level and then the
calculation is performed on the operands. Mathematical operations such as
addition, subtraction, multiplication, etc. can be performed at the bit level for
faster processing.
There are 6 bitwise operators in C:
67. // C program to illustrate the bitwise operators
#include <stdio.h>
int main()
{
int a = 25, b = 5;
// using operators and printing results
printf("a & b: %dn", a & b);
printf("a | b: %dn", a | b);
printf("a ^ b: %dn", a ^ b);
printf("~a: %dn", ~a);
printf("a >> b: %dn", a >>
b);
printf("a << b: %dn", a <<
b);
return 0;
}
Result:
a & b: 1
a | b: 29
a ^ b: 28
~a: -26
a >> b: 0
a << b: 800
68. 6. Other Operators
Apart from the above operators, there are some other operators available in
C used to perform some specific tasks. Some of them are discussed here:
sizeof Operator
sizeof is much used in the C programming language.
It is a compile-time unary operator which can be used to compute the size of
its operand.
The result of sizeof is of the unsigned integral type which is usually denoted
by size_t.
Basically, the sizeof the operator is used to compute the size of the variable
or datatype.
69. Syntax
sizeof (operand)
Comma Operator ( , )
The comma operator (represented by the token) is a binary operator that
evaluates its first operand and discards the result, it then evaluates the
second operand and returns this value (and type).
The comma operator has the lowest precedence of any C operator.
Comma acts as both operator and separator.
Syntax
operand1 , operand2
70. Conditional Operator ( ? : )
The conditional operator is the only ternary operator in C++.
Here, Expression1 is the condition to be evaluated. If the
condition(Expression1) is True then we will execute and return the result of
Expression2 otherwise if the condition(Expression1) is false then we will
execute and return the result of Expression3.
We may replace the use of if..else statements with conditional operators.
Syntax
operand1 ? operand2 : operand3;
71. dot (.) and arrow (->) Operators
Member operators are used to reference individual members of classes,
structures, and unions.
The dot operator is applied to the actual object.
The arrow operator is used with a pointer to an object.
Syntax
structure_variable . member;
and
structure_pointer -> member;
72. Cast Operator
Casting operators convert one data type to another. For example, int(2.2000)
would return 2.
A cast is a special operator that forces one data type to be converted into
another.
The most general cast supported by most of the C compilers is as follows −
[ (type) expression ].
Syntax
(new_type) operand;
73. addressof (&) and Dereference (*) Operators
Pointer operator & returns the address of a variable. For example &a; will
give the actual address of the variable.
The pointer operator * is a pointer to a variable. For example *var; will
pointer to a variable var.
82. PRECEDENCE OF ARITHMETIC OPERATIONS
The concept of operator precedence and associativity in C helps in
determining which operators will be given priority when there are multiple
operators in the expression. It is very common to have multiple operators in C
language and the compiler first evaluates the operater with higher
precedence. It helps to maintain the ambiguity of the expression and helps us
in avoiding unnecessary use of parenthesis.
In this article, we will discuss operator precedence, operator associativity, and
precedence table according to which the priority of the operators in
expression is decided in C language.
86. For example,
x = 7 + 3 * 2;
Here, the multiplication operator "*" has a higher precedence than the
addition operator "+". So, the multiplication 3*2 is performed first and then
adds into 7, resulting in "x = 13".
87. C Program:
#include <stdio.h>
int main() {
int x = 2, y = 3, z = 4;
int result = x + y * z; // result = 14
printf("Result: %dn", result);
int a = 5, b = 6, c = 7;
int result2 = (a + b) * c; // result2 = 77
printf("Result2: %dn", result2);
88. int d = 8, e = 9, f = 10;
int result3 = d + e % f; // result3 = 17
printf("Result3: %dn", result3);
return 0;
}
Result: 14
Result2: 77
Result3: 17