Presentation 4
Presentation 4
C Programming
By,
Ram Darshan C N,
Ravi Chidhroop S,
What is C?
What is
C?
• C is a general-purpose programming language that is widely used for system
programming, developing operating systems, embedded systems, and high-
performance applications
• 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.
Structure of C Program
Header Files Inclusion:
• The purpose of header files inclusion is to include standard or user-defined libraries.
• For example, #include <stdio.h> includes the Standard Input Output library.
• Header files contain declarations for functions and macros. The preprocessor processes these
lines, including the code from the header files into the program.
Main Method Declaration:
• The main method declaration defines the entry point of the program.
• For example, int main() declares the main function.
• The main() function is where execution starts. The int return type indicates that the function
returns an integer, usually indicating the program's termination status.
Body of Main Method:
• The body of the main method encloses the executable statements of the function.
• The body of the main() function contains the code to be executed, and it is enclosed in curly
brackets {}.
Structure of C Program
(contd...)
Statement:
• A statement provides instructions to the compiler.
• For example, printf("Hello World"); prints "Hello World" to the
screen.
• Statements are individual instructions in a program, and each statement
ends with a semicolon ;.
Return Statement:
• The return statement ends the function and returns a value.
• For example, return 0; signifies successful program termination.
• The return statement indicates the end of the main() function. The value 0
typically signifies successful program termination.
Features of C
Procedural Language
• In a procedural language like C step by step, predefined instructions are carried out. C
program may contain more than one function to perform a particular task. New people to
programming will think that this is the only way a particular programming language
works.
• There are other programming paradigms as well in the programming world. Most of the
commonly used paradigm is an object-oriented programming language.
Fast and Efficient
• Newer languages like Java, python offer more features than c programming language but
due to additional processing in these languages, their performance rate gets down
effectively.
• C programming language as the middle-level language provides programmers access to
direct manipulation with the computer hardware but higher-level languages do not allow
this.
• It’s fast because statically typed languages are faster than dynamically typed languages.
Modularity
• The concept of storing C programming language code in the form of libraries for further
future uses is known as modularity. This programming language can do very little on its
own most of its power is held by its libraries. C language has its own library to solve
common problems.
Features of C
Statically Type
• C programming language is a statically typed language. Meaning the type of variable is
checked at the time of compilation but not at run time.
• This means each time a programmer types a program they have to mention the type of
variables used.
General-Purpose Language
• From system programming to photo editing software, the C programming language is used in
various applications. Some of the common applications where it’s used are as follows:
• Operating systems: Windows, Linux, iOS, Android, OXS
• Databases: PostgreSQL, Oracle, MySQL, MS SQL Server, etc.
Rich set of built-in Operators
• It is a diversified language with a rich set of built-in operators which are used in
writing complex or simplified C programs.
Features of C
Standard Library
• C provides a standard library with a wide range of built-in functions for
tasks like input/output operations, memory management, and string
handling. This library simplifies development by offering pre-written code.
Dynamic Memory Allocation
• C allows for dynamic memory allocation using functions like malloc(),
calloc(), realloc(), and free(). This feature enables efficient memory
usage based on runtime needs.
Pointer Arithmetic
• C supports pointer arithmetic, allowing direct manipulation of memory
addresses. This feature is useful for array handling and efficient memory
management.
Functions of C
Structured Programming
• C promotes structured programming with its support for loops,
conditionals, and functions. This helps in writing clear and
maintainable code.
Low-Level Access
• C allows low-level access to memory and hardware, which is essential
for systems programming and embedded systems development.
Preprocessor Directives
• C uses preprocessor directives like #define, #include, and #ifdef to
handle code inclusion, macro definitions, and conditional compilation.
This enhances code flexibility and control.
Compilin
gaC
Program
Compiling of C
Program (contd...)
Preprocessing
• The preprocessing step prepares the code for compilation by handling directives such as
#include and #define.
• The preprocessor generates an intermediate file with all the preprocessor directives
resolved, which is then used in the compilation step.
Compilation
• During compilation, the compiler translates the preprocessed code into assembly language
specific to the target machine.
• This step involves syntax and semantic analysis, along with optimization to produce
efficient assembly code.
Assembly
• The assembly step converts the assembly language code into machine code, resulting in an
object file with binary instructions.
• The assembler processes the assembly code and generates an object file that contains the
machine-readable code.
Compiling of C Program
(contd..,)
Linking
• Linking combines one or more object files and libraries into a single
executable file.
• The linker resolves references to external symbols and integrates
library code, creating a complete executable program.
Loading and Execution
• The loader loads the executable file into memory and prepares it for
execution.
• Once loaded, the operating system starts the program, executing it
from the main function.
Tokens in C
Keywords
• Keywords are reserved words that have a special meaning in C. Examples
include int, return, if, and while. They cannot be used as identifiers for
variables or functions.
Identifiers
• Identifiers are names used to identify variables, functions, arrays, or other
user-defined items. Examples include main, sum, and counter. Identifiers must
start with a letter or underscore and can be followed by letters, digits, or
underscores.
Constants
• Constants are literal values used in C programs. They represent fixed values
that do not change during program execution. Examples include numeric
literals like 10, 3.14, and character literals like 'A'.
Tokens in C
String Literals
• String literals are sequences of characters enclosed in double quotes. For example,
"Hello, World!" is a string literal. They are used to represent text in a program.
Operators
• Operators are symbols that perform operations on operands. Examples include
arithmetic operators like +, -, *, /, and relational operators like ==, !=, <, >. They are
used to perform calculations, comparisons, and other operations.
Punctuation (Separators)
• Punctuation tokens, also known as separators, are used to separate different elements
of the code. Examples include the semicolon (;), comma (,), parentheses (()), curly
braces ({}), and square brackets ([]). They help in defining the structure and
organization of the code.
Keywords in C
https://www.geeksforgeeks.org/c-language-introduction/?ref=lbp
https://www.w3schools.com/c/c_intro.php
Practice
https://www.codechef.com/learn/topic/c
https://www.hackerrank.com/domains/c