0% found this document useful (0 votes)
5 views

1.Basics of C Programming V3.0

The document provides an overview of C programming, including its definition, history, features, and structure. It explains the importance of C as a foundational programming language and its applications in various domains such as operating systems and embedded systems. Additionally, it covers the environmental setup required to create and execute C programs using Integrated Development Environments (IDEs).

Uploaded by

Shawn Eye
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

1.Basics of C Programming V3.0

The document provides an overview of C programming, including its definition, history, features, and structure. It explains the importance of C as a foundational programming language and its applications in various domains such as operating systems and embedded systems. Additionally, it covers the environmental setup required to create and execute C programs using Integrated Development Environments (IDEs).

Uploaded by

Shawn Eye
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 65

We are on a mission to address the digital

skills gap for 10 Million+ young professionals,


train and empower them to forge a career
path into future tech
Basics of C Programming
16-OCT-2024
Basics of C Programming

Introduction to Programming

• Computer Programming is a sequence of instructions / commands.

• It is used to telling/expressing a computer processor what to do.

• 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

sandwich out of it…”

3 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Example: Making a Sandwich Step-by-Step Instructions

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

Stack the cheese Place another bread


and vegetables on Serve the sandwich
slice on top
the bread slice

4 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Example: Addition of Two Numbers

Step 1: Read two numbers A=10 & B=20

Step 2: Add A, B and assign result to C

C=A+B

Step 3: Display C

5 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Types of Programming

Computer languages

Low level language Middle level language


High level language
(Machine language) (Assembly language)

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

6 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Introduction to C Programming

• The C programming language is a procedural and general-purpose, high-level programming

language that provides low-level access to system memory.

• 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

languages of all time.

• Used in the development of computer software and applications, system programming, games, web

development, and more.

7 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

History of C Programming

• Dennis M. Ritchie an American computer scientist has developed C programming language in 1972

and known as father of C programming.

• Developed to overcome the problems of previous languages such as B, BCPL etc. Initially, C language

was developed to be used in UNIX operating system.

• Inherits many features of previous languages such as B and BCPL.

8 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

History of C Programming

• Algorithmic Language introduced the concept of structured programming

• Basic Combined Programming Language developed by Martin Richards, for writing


system software

• ‘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.

• Original dialect of the C programming language by Kernighan and Ritchie.

• American National Standards Institute (ANSI) defined a commercial standard for ‘C’
language

• It was approved by the International Standards Organization (ISO)

9 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

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

includes several new library functions and updates to existing ones.

• 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

functions for math, threads, and memory manipulation.

• C17 (ISO/IEC 9899:2018): Is the most recent standard and includes updates and clarifications to the

language specification and the library.

10 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Features of C Programming

Simple and Robust

• Easy to learn with lesser number of programming constructs that can power up any complex system.

• Supports a rich set of built-in library functions and a variety of operators

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.

11 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Features of C Programming

Modularity

• C supports modular programming, allowing developers to break down complex programs into

smaller, manageable functions. This enhances code readability and maintainability.

Extensibility

• C language provides a rich set of built-in library function.

• It also allows to build our own library functions and can use them in future.

• Because of modularization of programs, C programs can be extended at anytime.

12 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Features of C Programming

Low Level Access

• C provides low-level access to memory through pointers, allowing programmers to manipulate

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.

13 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Features of C Programming

Recursion

• C supports recursion, allowing functions to call themselves. This can simplify complex problems

and reduce code duplication.

Bit Manipulation:

• C provides operators for bit manipulation, making it suitable for tasks that require low-level data

processing, such as hardware interfacing and graphics programming.

14 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Applications of C Programming

• Used in developing an operating system. Operating systems such as Linux, Apple’s OS X,

Microsoft’s Windows, and Symbian are developed using ‘C’ language.

• Used in embedded systems.

• 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.

15 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Applications of C Programming

• Many popular databases like MySQL, PostgreSQL, and Oracle are written in C

• C is commonly used for network programming, including socket programming, creating

networking protocols, and handling network communication.

• C is used in creating graphics libraries like OpenGL and DirectX, which are crucial for game

development and real-time rendering applications.

• Etc.,

16 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Structure of C Programming

To understand the Structure of a C Program, look at the Documentation Section

image, which shows the different sections of a c Preprocessor directive Section

program. Link Section

Definition Section

Global declaration Section

main() Function Section


{
Declaration part
Executable part
}

SubProgram Section

Function 1
Function 2 User Defined Functions
……………
Function n

17 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Structure of C Programming

➢ Documentation section

• The documentation section is the part of the program where the programmer provides the details

associated with the program.

• Documentation in C programming refers to the practice of adding comments and other information to

your code to explain its purpose, design, and usage.

• 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.

18 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Comments in C Programming

• One important part of good documentation inclusion of appropriate Comments.

• Comments can be included in C program in two ways:

1. Single line comment:

✓ Single line of comment statement after double forward slash (//).

Examples:

//First C Program
//C Program to find addition of two numbers

✓ It applies to single line comments only.

19 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

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 */

20 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

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

declarations that are needed by other files in a C program.

• 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.

21 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

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

not the actual implementation.

22 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Structure of C Programming

Some of C Header files:

• 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

23 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

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

program where that value or expression is needed.


Example:

#define PI 3.14

24 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

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.

• A local variable is a type of variable that we declare inside a block or a function.

Example:
int a,b; //Global declaration
int main(){
int c,d; // Local declaration

}

25 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

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

accepts inputs via parameters).

• The only difference is that the main function is "called" by the operating system when the user runs

the program.

26 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Structure of C Programming

• Each main function contains 2 parts.

– 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;
}

27 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Structure of C Programming

Important Points about C main Function

• It is the function where the program’s execution starts.

• Every program has exactly one main function.

• The name of this function should be “main” not anything else.

• The main function always returns an integer value or void.

• The main function is called by OS, not the user.

28 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Structure of C Programming

➢ Sub Program Section

• Sub programs refer to functions or procedures that perform a specific task or set of tasks and can be

called from other parts of the program.

• It contains all the user defined functions that are called in the main function.

• Example: color(), add(), divide(), etc.

//Addition of two numbers


int add(int a, int b) {
return a + b;
}

29 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Environmental Setup to create and execute C program

An IDE (Integrated Development Environment) is software

that combines commonly used developer tools into a compact

GUI (graphical user interface) application.

• It is a combination of tools like a code editor, code compiler,

and code debugger with an integrated terminal.

• Even a simple text editor like notepad can be used to write

code. However, IDEs offer some stunning features that go

beyond ordinary editing.

30 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Environmental Setup to create and execute C program

• By providing frequently used developer tools all in one simple interface.

• 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.

31 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Environmental Setup to create and execute C program

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.

– Interface also provides code completion for C and C++.

32 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Environmental Setup to create and execute C program

➢ 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 download path.

• Select a location to save the installer file, choose a save location, then click "Save" to continue.

33 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Environmental Setup to create and execute C program

➢ Installing CodeBlocks

• Launch the installer

Start the installer which is located in the location specified

when it was downloaded.

34 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Environmental Setup to create and execute C program

• Run the setup

– After launching the installer, the setup wizard will

appear on screen and click the "Next" button.

• Read the Software Agreement

– After reading, click "I Agree" if you agree to the terms

and want to install the software.

35 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Environmental Setup to create and execute C program

• Choose your components

– Make sure that "Full" installation is selected from the

drop down menu at the top and click next.

36 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Environmental Setup to create and execute C program

• Choose the install path

– By default, Code::Blocks will install under C:\Program


Files\CodeBlocks\.

– To change the path, click the "Browse" button to select


a custom install path and click install.

37 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Environmental Setup to create and execute C program

• Wait for the installation to complete

– The installation will take several minutes to complete and

will show its progress in the window.

• Finish the installation

– Complete install wizard by clicking the "Next" button on the

installer screen, then clicking "Finish" on the completion

page.

38 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Environmental Setup to create and execute C program

Setting up the C File

• 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

• Complete the compiler setup

– If prompted, accept GNU GCC Compiler as the default by clicking the entry for GNU GCC

Compiler, then click "Set as Default". To continue, click "OK".

39 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Environmental Setup to create and execute C program

• Set file associations

– 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.

40 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Environmental Setup to create and execute C program

First C Program

• Open CodeBlocks IDE and create a new file. Click on File


→ New → File

41 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Environmental Setup to create and execute C program

First C Program

• From the New form template window select C/C++ source


and click Go button.

42 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Environmental Setup to create and execute C program

First C Program

• If you see a welcome message, click next to skip the


welcome message. Make sure you have checked Skip this
next time checkbox if you do not want to see this welcome
message again.

• Next, select your language from C/C++ source window and


click Next button.

43 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Environmental Setup to create and execute C program

First C Program

• Give name to your file and specify the location.

• It is always recommended to save all your C programs in a

C workspace (directory).

• In general, C program must be saved with ‘.c’ extension -

Source File

Example: Helloworld.c

44 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Environmental Setup to create and execute C program

First C Program:

Write a program to print a message “Hello, World!”

//My First C Program to print Hello, World!


#include <stdio.h>
int main(){
printf("Hello, World!");
return 0; Output:
} Hello, World!

45 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Environmental Setup to create and execute C program

How to compile and run C program in CodeBlocks

• Once you created your first C program it’s time to compile


and run the program.

• To compile and run a C program, click Build → Build and


run to compile and build your C program, alternatively use
the shortcut key F9. In case, your program contains any
errors.

• Error messages are shown in the Build messages tab below


the code editor.

46 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Environmental Setup to create and execute C program

• Run the program.

– Click the "Build and Run" icon to run your program.

– This function compiles then runs your program in one convenient step.

• View the program.

– 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.

47 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

The Compilation and execution steps of C program

Compilation

• The process of translating source code written in high level to low level machine code is called as

Compilation. The compilation is done by special software known as compiler.

• 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.

48 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

The Compilation and execution steps of C program

The entire C compilation is broken to four stages. Source code(.c) file


(Helloworld.c)

1. Pre-processing
Pre-Processor
2. Compilation

3. Assembling and Compiler

4. Linking
Assembly code
5. Loading
Assembler

Libraries Object file(.o)


(Helloworld.o)

Linker

Loader

49 Basics of C Programming​ | © SmartCliff | Version 2.0 Executable file


(Helloworld.exe)
Basics of C Programming

The Compilation and execution steps of C program

• Upon compiling the program, the C compiler looks for errors - Compiling.

• Stores it as an object file with an extension (.o).

• The library functions specified in the program yet not a part of the program must be linked with the object

file - executable file with an extension (.exe).

• Upon executing the program, the loader loads the program in to the RAM and inform the CPU with the

starting point of the address where this program is loaded.

50 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

The Compilation and execution steps of C program

Pre-processing of source file

• Pre-processor is a small software that accepts C source file and performs below tasks.

✓ Remove comments from the source code.

✓ Macro expansion.

✓ Expansion of included header files.

• After pre-processing it generates a temporary file with .i extension.

• Since, it inserts contents of header files to our source code file.

• Pre-processor generated file is larger than the original source file.

51 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

The Compilation and execution steps of C program

Compilation of pre-processed file

• Compilation accepts temporary pre-processed <file-name>.i file generated by the pre-processor and

performs following tasks.

✓ Check C program for syntax errors.

✓ Translate the file into intermediate code i.e. in assembly language.

✓ Optionally optimize the translated code for better performance.

• After compiling it generates an intermediate code in assembly language as <file-name.s> file.

• It is assembly version of our source code.

52 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

The Compilation and execution steps of C program

Assembling of compiled source code

• Moving on to the next phase of compilation.

• 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

known as object file.

• This file is encoded in low level machine language and cannot be viewed using text editors.

53 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

The Compilation and execution steps of C program

Linking of object files

• Finally, the linker comes in action and performs the final task of compilation process.

• It accepts the intermediate file <file-name.o> generated by the assembler.

• It links all the function calls with their original definition.

• Which means the function printf() gets linked to its original definition.

• Linker generates the final executable file (.exe in windows).

54 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

The Compilation and execution steps of C program

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.

55 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Quiz
1. C language is a

a) High level language b) Middle level language

c) Low level language d) Machine level language

a) High level language

56 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Quiz
2. Compiler generates an ___ file.

a) Executable code b) Object code

c) Assembly code d) None

b) Object code

57 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Quiz
3. What is the only function all C programs must contain?

a) start() b) system()

c) main() d) program()

c) main()

58 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Quiz
4. What punctuation is used to signal the beginning and end
of code blocks?

a) {} b) -> and <-

c) BEGIN and END d) All

a) {}

59 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Quiz
5. Directives are translated by the

a) Pre-processor b) Compiler

c) Linker d) Editor

a) Pre-processor

60 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Quiz
6. _______ generates the executable module of a source
program

a) Loader b) Linker

c) Compiler d) None

b) Linker

61 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Quiz
7. _______ takes executable module generated by a linker as
input

a) Loader b) Linker

c) Compiler d)Preprocessor

a) Loader

62 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Quiz
8. C preprocessors can have compiler specific features.

a) True b) False

c) Depends on the standard d) Depends on the platform

a) True

63 Basics of C Programming​ | © SmartCliff | Version 2.0


Basics of C Programming

Quiz
9. The C source file is processed by the ___.

a) Interpreter b) Compiler

c) Both Interpreter and


d) Assembler
Compiler

a) Compiler

64 Basics of C Programming​ | © SmartCliff | Version 2.0


THANK YOU

You might also like