1.Basics of C Programming V3.0
1.Basics of C Programming V3.0
Introduction to Programming
• It is somewhat similar to one person giving commands / instructions / orders to other in order to carry out
a specific work/task.
• Example:
• “Does this, then do that, then take the two results, stack them up this way, and repeat until you make a
1 2 3 4 5
Slice the bread Take two slices Spread butter on Slice cheese Cut Vegetables
loaf of bread the bread slices
6 7 8
C=A+B
Step 3: Display C
Types of Programming
Computer languages
Use mnemonics to
Use ‘1’s & ‘0’s to create Similar to human
create instructions Ex.
instructions language
ADD, SUB
FORTRAN, C, C++,
Binary Language Assembly language
JAVA
Introduction to C Programming
• It is considered as the base for other programming languages, that is why it is known as mother
language.
• The C language comes with a set of special characteristics, making it one of the most widely used
• Used in the development of computer software and applications, system programming, games, web
History of C Programming
• Dennis M. Ritchie an American computer scientist has developed C programming language in 1972
• Developed to overcome the problems of previous languages such as B, BCPL etc. Initially, C language
History of C Programming
• ‘B’ was introduced by Ken Thompson with multiple features of ‘BCPL.’ UNIX operating
system at AT&T and Bell Laboratories.
• ‘C’ programming language contains all the features of ‘ALGOL’, ‘BCPL’ and ‘B’.
associated with the UNIX operating system by Dennis Ritchie.
• American National Standards Institute (ANSI) defined a commercial standard for ‘C’
language
History of C Programming
• C99 (ISO/IEC 9899:1999): Introduced several new features, including variable-length arrays, flexible
array members, complex numbers, inline functions, and designated initializers. This standard also
• C11 (ISO/IEC 9899:2011): Introduced several new features, including _Generic, static_assert, and the
atomic type qualifier. This standard also includes several updates to the library, including new
• C17 (ISO/IEC 9899:2018): Is the most recent standard and includes updates and clarifications to the
Features of C Programming
• Easy to learn with lesser number of programming constructs that can power up any complex system.
Efficiency
• C is known for its high performance and efficient use of resources. Programs written in C can execute
quickly, which is crucial for system-level programming.
Portability
• C programs can be run on various hardware platforms with minimal modification. This makes C a
portable language, allowing developers to write code that can be easily transferred between different
systems.
Features of C Programming
Modularity
• C supports modular programming, allowing developers to break down complex programs into
Extensibility
• It also allows to build our own library functions and can use them in future.
Features of C Programming
hardware directly. This is especially useful in systems programming and embedded systems.
Structured Programming
• C follows structured programming principles, enabling clear control flow through constructs like loops,
conditionals, and functions. This encourages the use of logical structures in program design.
Features of C Programming
Recursion
• C supports recursion, allowing functions to call themselves. This can simplify complex problems
Bit Manipulation:
• C provides operators for bit manipulation, making it suitable for tasks that require low-level data
Applications of C Programming
• Used for developing browsers and their extensions. Google’s Chrome is built using ‘C’ programming
language.
• Many compilers and interpreters for other programming languages are written in C.
• C is widely used in developing device drivers, network drivers, and firmware for hardware devices.
Applications of C Programming
• Many popular databases like MySQL, PostgreSQL, and Oracle are written in C
• C is used in creating graphics libraries like OpenGL and DirectX, which are crucial for game
• Etc.,
Structure of C Programming
Definition Section
SubProgram Section
Function 1
Function 2 User Defined Functions
……………
Function n
Structure of C Programming
➢ Documentation section
• The documentation section is the part of the program where the programmer provides the details
• Documentation in C programming refers to the practice of adding comments and other information to
• Good documentation can make your code easier to understand, maintain, and debug and can also
make it more accessible to other developers who may need to use or modify your code in the
future.
Comments in C Programming
Examples:
//First C Program
//C Program to find addition of two numbers
Comments in C
2. Multi-line comment:
• It starts with forward slash followed by asterisk (/*) and ends with asterisk followed by forward slash (*/).
• Represented as /* any_text */ .
• Used to insert multi-line comment. It can be used for single and multiple line comments.
Example:
/* C program to illustrate
use of
multi-line comment */
Structure of C Programming
➢ Link Section
• Linking refers to adding necessary header files that are useful in your code.
• Header files in C are files that contain function prototypes, data type definitions, macros, and other
• They are typically included at the beginning of a source code file using the #include directive, which
tells the preprocessor to insert the contents of the specified header file into the source code.
Structure of C Programming
• The Header files will have library functions that need to be linked to the main program.
• This leads to the compiler being told to link the header files to the system libraries.
Example:
#include<stdio.h>
Note: stdio.h is a header file which includes all standard I/O operations. Basically it contains the
prototypes of all the input output functions used in C, remember it contains only function type signature
Structure of C Programming
allocation
Structure of C Programming
➢ Definition section
• In this section, it includes a pre-processor directive we can define the symbolic constants.
• Once defined, the symbolic constant can be used across the program.
• The #define preprocessor directive in C is used to define symbolic constants, also known as macros.
• A macro is a piece of code that represents a value or expression, and it can be used anywhere in the
#define PI 3.14
Structure of C Programming
• Variables that will have a scope across the complete program i.e., Main function and all other functions.
• Global variables need to be declared in the global declaration section that is outside of all the
functions.
Example:
int a,b; //Global declaration
int main(){
int c,d; // Local declaration
…
}
Structure of C Programming
➢ Main() Function
• It is the entry point of any C program that is, the point at which execution of program is started.
• In C, the "main" function is treated the same as every function, it has a return type (and in some cases
• The only difference is that the main function is "called" by the operating system when the user runs
the program.
Structure of C Programming
– Declaration part
– Executable part
• Both the declaration and execution part are inside the curly braces{}.
• The declaration part declare all the variables used in the execution part.
• All statements in the declaration and executable parts end with a semicolon (;).
Example:
int main(){
int a=10; //declaration part
printf(" %d", a); //executable part
return 0;
}
Structure of C Programming
Structure of C Programming
• Sub programs refer to functions or procedures that perform a specific task or set of tasks and can be
• It contains all the user defined functions that are called in the main function.
• There are lots of Integrated Development Environments (IDE) available for windows / Linux /Mac
operating system which one can use to work easily with C programming language.
• For example: Dev C++, Microsoft Visual C++, Code::blocks, Eclipse, etc.
CodeBlocks IDE
• CodeBlocks is a free and cross platform IDE for C, C++, and Fortran on Windows, MacOS, and Linux.
Features of CodeBlocks
– Multiple compilers like GCC, clang, MSVC++, Borland C++ 5.5, and many more.
– Provides a very fast custom build system that supports parallel builds (utilizing your CPU's extra cores).
– Debugger provides full breakpoints, displays user-defined watches, call stacks, CPU registers, etc.
➢ Downloading CodeBlocks:
• Download the latest version of Code::Blocks from their official site codeblocks.org.
https://www.codeblocks.org/downloads/binaries/
• Navigate to the download page. Under the Windows section, select the "mingw-setup" variant; this is an
all-in-one installer that includes all necessary tools. Select either download link to continue.
• Select a location to save the installer file, choose a save location, then click "Save" to continue.
➢ Installing CodeBlocks
page.
• Launch Code::Blocks
– To launch the program, double click the Code::Blocks icon the installer placed on your desktop.
– The program can be found under Start--> All Programs ---> Code::Blocks --> CodeBlocks.exe
– If prompted, accept GNU GCC Compiler as the default by clicking the entry for GNU GCC
– If prompted, select the option to associate Code::Blocks with C and C++ file types, then click "OK".
This will allow you to open these types of files in Code::Blocks by default.
First C Program
First C Program
First C Program
First C Program
C workspace (directory).
Source File
Example: Helloworld.c
First C Program:
– This function compiles then runs your program in one convenient step.
– After running, a terminal window will pop up with the message "Hello World."
– The process should return 0. If a different value appears, there may be an issue with your program.
– The execution time will vary based on the speed of your computer.
Compilation
• The process of translating source code written in high level to low level machine code is called as
• The compiler checks source code for any syntactical or structural errors and generates object code with
extension .obj (in Windows) or .o (in Linux) if source code is error free.
1. Pre-processing
Pre-Processor
2. Compilation
4. Linking
Assembly code
5. Loading
Assembler
Linker
Loader
• Upon compiling the program, the C compiler looks for errors - Compiling.
• The library functions specified in the program yet not a part of the program must be linked with the object
• Upon executing the program, the loader loads the program in to the RAM and inform the CPU with the
• Pre-processor is a small software that accepts C source file and performs below tasks.
✓ Macro expansion.
• Compilation accepts temporary pre-processed <file-name>.i file generated by the pre-processor and
• Assembler accepts the compiled source code (compilation.s) and translates to low level machine code.
• After successful assembling it generates <file-name.o> (in Linux) or <file-name.obj> (in Windows) file
• This file is encoded in low level machine language and cannot be viewed using text editors.
• Finally, the linker comes in action and performs the final task of compilation process.
• Which means the function printf() gets linked to its original definition.
Linker Loader
Generates the executable module of a source Loads the executable module to the main memory
program. for execution.
Takes the object code generated by an assembler, Takes executable module generated by a linker as
as input. input.
Combines all the object modules of a source code Allocates the addresses to an executable module in
to generate an executable module. main memory for execution.
Quiz
1. C language is a
Quiz
2. Compiler generates an ___ file.
b) Object code
Quiz
3. What is the only function all C programs must contain?
a) start() b) system()
c) main() d) program()
c) main()
Quiz
4. What punctuation is used to signal the beginning and end
of code blocks?
a) {}
Quiz
5. Directives are translated by the
a) Pre-processor b) Compiler
c) Linker d) Editor
a) Pre-processor
Quiz
6. _______ generates the executable module of a source
program
a) Loader b) Linker
c) Compiler d) None
b) Linker
Quiz
7. _______ takes executable module generated by a linker as
input
a) Loader b) Linker
c) Compiler d)Preprocessor
a) Loader
Quiz
8. C preprocessors can have compiler specific features.
a) True b) False
a) True
Quiz
9. The C source file is processed by the ___.
a) Interpreter b) Compiler
a) Compiler