PPS Unit 2-
PPS Unit 2-
BY APURVA JOSHI
What is C?
C is a general-purpose, procedural, high-level programming language used in the development
of computer software and applications, system programming, games, and more.
C language was developed by Dennis M. Ritchie at the Bell Telephone Laboratories in 1972.
It is a powerful and flexible language which was first developed for the programming of
the UNIX operating System.
C is one of the most widely used programming languages.
C programming language is known for its simplicity and efficiency. It is the best choice to start
with programming as it gives you a foundational understanding of programming.
The main features of the C language include:
General Purpose and Portable
Low-level Memory Access
Fast Speed
Clean Syntax
These features make the C language suitable for system programming like an operating system or compiler
development.
Why Should We Learn C?
Many later languages have borrowed syntax/features directly or indirectly from the C language like the syntax of
Java, PHP, JavaScript, and many other languages that are mainly based on the C language. C++ is nearly a superset
of C language (Only a few programs may compile in C, but not in C++).
So, if a person learns C programming first, it will help them to learn any modern programming language as well.
Also, learning C helps to understand a lot of the underlying architecture of the operating system like pointers,
working with memory locations, etc.
Writing the First Program in C
#include <stdio.h>
int main() {
int a = 10;
printf("%d", a);
return 0;
}
Structure of the C program
After the above discussion, we can formally assess the basic structure of a C program. By
structure, it is meant that any program can be written in this structure only. Writing a C program
in any other structure will lead to a Compilation Error. The structure of a C program is as follows:
Components of a C Program:
1. Header Files Inclusion – Line 1 [#include <stdio.h>]
The first and foremost component is the inclusion of the Header files in a C program. A header file is a file with
extension .h which contains C function declarations and macro definitions to be shared between several source
files. All lines that start with # are processed by a preprocessor which is a program invoked by the compiler. In the
above example, the preprocessor copies the preprocessed code of stdio.h to our file. The .h files are called header
files in C.
Some of the 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 number generator, and memory allocation
string.h – Defines string handling functions
math.h – Defines common mathematical functions.
Components of a C Program:
2. Main Method Declaration – Line 2 [int main()]
The next part of a C program is to declare the main() function. It is the entry point of a C
program and the execution typically begins with the first line of the main(). The empty brackets
indicate that the main doesn’t take any parameter (See this for more details). The int that was
written before the main indicates the return type of main(). The value returned by the main
indicates the status of program termination.
Components of a C Program:
3. Body of Main Method – Line 3 to Line 6 [enclosed in {}]
The body of a function in the C program refers to statements that are a part of that function. It
can be anything like manipulations, searching, sorting, printing, etc. A pair of curly brackets
define the body of a function. All functions must start and end with curly brackets.
4. Statement – Line 4 [printf(“Hello World”);]
Statements are the instructions given to the compiler. In C, a statement is always terminated by
a semicolon (;). In this particular case, we use printf() function to instruct the compiler to display
“Hello World” text on the screen.
Components of a C Program:
5. Return Statement – Line 5 [return 0;]
The last part of any C function is the return statement. The return statement refers to the return
values from a function. This return statement and return value depend upon the return type of
the function. The return statement in our program returns the value from main(). The returned
value may be used by an operating system to know the termination status of your program. The
value 0 typically means successful termination.
Application of C
Operating systems: C is widely used for developing operating systems such as Unix, Linux, and Windows.
Embedded systems: C is a popular language for developing embedded systems such as microcontrollers, microprocessors, and
other electronic devices.
System software: C is used for developing system software such as device drivers, compilers, and assemblers.
Networking: C is widely used for developing networking applications such as web servers, network protocols, and network
drivers.
Database systems: C is used for developing database systems such as Oracle, MySQL, and PostgreSQL.
Gaming: C is often used for developing computer games due to its ability to handle low-level hardware interactions.
Artificial Intelligence: C is used for developing artificial intelligence and machine learning applications such as neural networks
and deep learning algorithms.
Scientific applications: C is used for developing scientific applications such as simulation software and numerical analysis
tools.
Financial applications: C is used for developing financial applications such as stock market analysis and trading systems.
What are the Most Important Features of
C Language?
Here are some of the most important features of the C language:
Procedural Language
Fast and Efficient
Modularity
Statically Type
General-Purpose Language
Rich set of built-in Operators
Libraries with Rich Functions
Middle-Level Language
Portability
Easy to Extend
Ref: https://www.geeksforgeeks.org/features-of-c-programming-language/?ref=lbp
Compiling a C Program
The compilation is the process of converting the source
code of the C language into machine code. As C is a mid-
level language, it needs a compiler to convert it into an
executable code so that the program can be run on our
machine.
The C program goes through the following phases during
compilation:
What goes inside the compilation process?
A compiler converts a C program into an executable. There are four phases for a C program to
become an executable:
Pre-processing
Compilation
Assembly
Linking
1. Pre-processing
This is the first phase through which source code is passed. This phase includes:
Removal of Comments
Expansion of Macros
Expansion of the included files.
Conditional compilation
The preprocessed output is stored in the filename.i. Let’s see what’s inside filename.i: using $vi filename.i
In the above output, the source file is filled with lots and lots of info, but in the end, our code is preserved.
printf contains now a + b rather than add(a, b) that’s because macros have expanded.
Comments are stripped off.
#include<stdio.h> is missing instead we see lots of code. So header files have been expanded and included in our
source file.
2. Compiling
The next step is to compile filename.i and produce an; intermediate compiled output
file filename.s. This file is in assembly-level instructions.
3. Assembling
In this phase the filename.s is taken as input and turned into filename.o by the assembler. This
file contains machine-level instructions. At this phase, only existing code is converted into
machine language, and the function calls like printf() are not resolved.
4. Linking
This is the final phase in which all the linking of function calls with their definitions is done.
Linker knows where all these functions are implemented. Linker does some extra work also, it
adds some extra code to our program which is required when the program starts and ends. For
example, there is a code that is required for setting up the environment like passing command
line arguments. This task can be easily verified by using $size filename.o and $size filename.
Through these commands, we know how the output file increases from an object file to an
executable file. This is because of the extra code that Linker adds to our program.
Types of comments in C
In C there are two types of comments in C language:
Single-line comment
Multi-line comment
// C program to illustrate
// use of single-line comment
#include <stdio.h>
int main(void)
{
// This is a single-line comment
printf("Welcome to GeeksforGeeks");
return 0;
}
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
1. C Token – Keywords
The 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 the text to be substituted for keywords before compilation by using C preprocessor
directives.
2. C Token – 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.
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.
3. 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.
4. 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++.
5. 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.
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.
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.
int main() {
float myFloatNum = 3.5;
printf("%f\n", myFloatNum); // Default will show 6 digits after the decimal point
printf("%.1f\n", myFloatNum); // Only show 1 digit
printf("%.2f\n", myFloatNum); // Only show 2 digits
printf("%.4f", myFloatNum); // Only show 4 digits
return 0;
}
C The sizeof Operator
#include <stdio.h>
int main() {
int myInt;
float myFloat;
double myDouble;
char myChar;
printf("%lu\n", sizeof(myInt));
printf("%lu\n", sizeof(myFloat));
printf("%lu\n", sizeof(myDouble));
printf("%lu\n", sizeof(myChar));
return 0;
}
C Operators
Operators are used to perform operations on variables and values.
C divides the operators into the following groups:
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Bitwise operators
Arithmetic operators
assignment operators:
comparison operators:
Logical Operators
Bitwise operators
Unary Operators
Increment Decrment operators in C
Operator Precedence and Associativity in C
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.
Solve
Type Conversion
Type Conversion
To get the right result, you need to know how type conversion works.
There are two types of conversion in C:
Implicit Conversion (automatically)