0% found this document useful (0 votes)
5 views7 pages

C Programming 1 1

Uploaded by

ryadav00575
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 views7 pages

C Programming 1 1

Uploaded by

ryadav00575
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

C-Programming

Programming Principles & Algorithms (BCA-S102T)

1 Introduction to Programming Languages


Programming languages are a cornerstone of computer science and serve as the medium
through which we communicate instructions to a computer. A programming language

nt
is a formal language that consists of a set of instructions used to produce various kinds
of output. They enable us to write programs, which are a collection of statements or
commands that perform a specific task. Every device with a computing component—be

de
it a smartphone, computer, or embedded system—requires programming to function cor-
rectly.
tu
1.1 Why Should We Learn Programming Languages?

• Problem-Solving Skills: Learning to code enhances your ability to think critically


As
and solve complex problems systematically. As you break down a problem into
smaller steps and solve each one, you learn to apply logical reasoning, which is a
valuable skill in any field.

• Automation: Programming allows you to automate repetitive tasks, whether it’s


BC

for data analysis, generating reports, or creating algorithms. Automation leads to


efficiency, allowing you to focus on more complex problems.

• Career Opportunities: With the growth of technology across industries, pro-


e

gramming skills have become indispensable. Whether you aim to become a soft-
ware developer, data scientist, web designer, or work in fields like cybersecurity,
th

programming is the foundation.

• Control over Devices: Knowing how to program allows you to understand and
control the behavior of various devices. You can create applications that interact
with hardware, manipulate data, and even build systems from the ground up.

• Creative Expression: Programming is also an artistic process that allows you


to create new things, such as websites, mobile apps, and games, from scratch. If
you have a creative mindset, learning programming offers an outlet to express those
ideas in the digital world.

https://youtube.com/@thebcastudent
Programming Principles & Algorithms (BCA-S102T) 2

1.2 Types of Programming Languages


• Low-Level Languages: These include assembly languages and machine languages,
which are close to the hardware and provide little to no abstraction from the actual
hardware. These languages are efficient but are complex to program.

– Machine Language: The most basic form of programming language, con-


sisting of binary code (0s and 1s).
– Assembly Language: A step up from machine language that uses mnemonic
codes to represent machine-level instructions.

• High-Level Languages: These provide greater abstraction from the hardware,


making it easier for programmers to write, read, and maintain code. Examples
include C, Python, Java, and JavaScript.

– Compiled Languages: These languages (e.g., C, C++) need to be converted

nt
into machine code by a compiler before execution.
#include <stdio.h>

int main() {
printf("Hello, World!");
return 0; de
tu
}
– Interpreted Languages: These languages (e.g., Python, JavaScript) are
As
executed line by line by an interpreter at runtime.

2 History of the ‘C’ Programming Language


BC

The ’C’ programming language is one of the most influential and widely used program-
ming languages in the world. It was developed by Dennis Ritchie at Bell Laboratories
in 1972. Initially created for developing the UNIX operating system, C quickly became
popular due to its efficiency and flexibility.
e
th

2.1 Key Historical Points


• 1960s: The language BCPL (Basic Combined Programming Language) was devel-
oped, influencing later languages.

• 1970s: C was developed as a successor to the B programming language, with


features from BCPL and B, and it was used to re-implement the UNIX operating
system.

• 1978: The first edition of The C Programming Language by Brian Kernighan and
Dennis Ritchie was published, establishing C as a standard.

• 1989: ANSI C (American National Standards Institute) was established as a stan-


dard version of C, known as C89 or ANSI C.

• 1999 and 2011: The language saw revisions with C99 and C11, which introduced
new features, making C modern and powerful.
Programming Principles & Algorithms (BCA-S102T) 3

3 Structure of a C Program
A typical C program consists of the following components:

• Preprocessor Directives: These are instructions to the compiler to perform spe-


cific pre-processing before the actual compilation. Example: #include <stdio.h>
includes the standard input/output library.

• main() Function: This is the entry point of every C program. It is where execution
starts. Example:

int main() {
return 0;
}

nt
• Variable Declarations: Variables store data that the program manipulates. Ex-
ample:

int a;

de
tu
• Statements and Expressions: These are the actions that the program takes,
such as calculations, loops, or function calls.
As

• Comments: These are non-executable statements used to explain the code. Ex-
ample:
BC

// This is a comment.

• Return Statement: The return statement exits the function. In the main func-
tion, it indicates the program’s exit status.
e
th

An example of a simple C program:

#include <stdio.h>

int main() {
printf("Hello, World!");
return 0;
}

4 Functions as Building Blocks


Functions in C are blocks of code designed to perform a specific task. They allow for
code reusability, modularity, and clarity. Functions reduce repetition by encapsulating
repetitive tasks into reusable blocks of code.
Programming Principles & Algorithms (BCA-S102T) 4

4.1 Function Structure

• Function Declaration: Before using a function, it must be declared with its


return type, name, and parameters (if any). Example:

int add(int, int); // Declaration

• Function Definition: This provides the actual code that will be executed when
the function is called. Example:

int add(int a, int b) {


return a + b;
}

nt
• Function Call: Once a function is defined, it can be invoked by calling its name
with the required parameters. Example:

de
int sum = add(5, 3); // Calling the function
tu
Functions help break down a large problem into smaller, more manageable pieces.
Some key types of functions in C are:
As

• Standard Library Functions: These are built-in functions provided by C li-


braries (e.g., printf(), scanf()).
BC

• User-Defined Functions: These are functions written by the programmer to


perform specific tasks (e.g., int add(int a, int b)).

5 Language Fundamentals
e

The basic building blocks of the C language are its character set, tokens, keywords,
th

identifiers, variables, constants, data types, and comments.

5.1 Character Set The character set in C includes:

• Letters: Both uppercase (A-Z) and lowercase (a-z).

• Digits: (0-9).

• Special Characters: Symbols like +, -, *, /, and others.

• Whitespace Characters: Spaces, tabs, and newline characters.


Programming Principles & Algorithms (BCA-S102T) 5

5.2 C Tokens Tokens are the smallest elements in a C program that have a specific
meaning. They are classified into:

• Keywords: Reserved words that have a special meaning in C (e.g., int, return).

• Identifiers: Names given to variables, functions, and arrays by the programmer


(e.g., main, sum).

• Constants: Literal values used in the program (e.g., 10, 3.14).

• Operators: Symbols that perform operations on variables and values (e.g., +, -).

• Punctuation: Symbols used to separate statements and other components (e.g.,


;, ,).

5.3 Keywords Keywords are predefined, reserved words in C that have special mean-

nt
ings. They cannot be used as identifiers. Examples include:

• int - Defines an integer type.

de
• float - Defines a floating-point type.

• return - Exits from a function.


tu
6 C Language Keywords
As
The following table lists the 32 keywords in the C programming language:
Keyword Keyword Keyword Keyword
auto break case char
const continue default do
BC

double else enum extern


float for goto if
int long register return
short signed sizeof static
e

struct switch typedef union


unsigned void volatile while
th

6.1 Identifiers Identifiers are names given to various program elements such as vari-
ables, functions, and arrays. They must start with a letter or an underscore and can be
followed by letters, digits, or underscores. Example:

int totalAmount; // Valid identifier

6.2 Variables Variables are storage locations with a name and a type, used to hold
data that can be modified during program execution. They must be declared with a data
type before use. Example:

int count; // Variable declaration


count = 10; // Variable initialization
Programming Principles & Algorithms (BCA-S102T) 6

6.3 Constants Constants are values that do not change during the execution of a
program. They can be:

• Numeric Constants: Numbers with decimal points like 3.14, -0.001.

• Character Constants: Single characters enclosed in single quotes like ’a’, ’5’.

6.4 Data Types Data types specify the type of data that a variable can hold. C
provides several built-in data types:

• Basic Data Types:

– int: Represents integer values.


– float: Represents floating-point numbers.
– char: Represents single characters.

nt
• Derived Data Types:

– array: A collection of elements of the same type.

de
– pointer: A variable that holds the address of another variable.
– struct: A collection of variables of different types grouped together.
tu
– union: A data type that allows storing different types of data in the same
memory location.
As
• Void Type: Represents the absence of type and is used for functions that do not
return a value.
Data Type Size (Bytes) Symbol Properties
BC

char 1 c Represents a single character.


short 2 s Short integer type. Typically 16-bit.
int 4 i Standard integer type. Typically 32-bit.
long 4 or 8 l Long integer type. Size depends on the platform.
long long 8 ll Extended long integer type. Typically 64-bit.
e

float 4 f Single-precision floating-point.


th

double 8 d Double-precision floating-point.


long double 8, 12, or 16 ld Extended precision floating-point. Size depends o
unsigned char 1 uc Unsigned version of char.
unsigned short 2 us Unsigned version of short.
unsigned int 4 ui Unsigned version of int.
unsigned long 4 or 8 ul Unsigned version of long.
unsigned long long 8 ull Unsigned version of long long.

6.5 Comments Comments are used to explain the code and make it more readable.
They are ignored by the compiler. C supports two types of comments:

• Single-line Comments: Begin with //. Example:

// This is a single-line comment


Programming Principles & Algorithms (BCA-S102T) 7

• Multi-line Comments: Enclosed between /* and */. Example:

/* This is a multi-line comment


that spans multiple lines */

7 Conclusion
The C language remains foundational for understanding programming principles and
algorithms due to its simplicity, efficiency, and widespread use. Its history reflects the
evolution of programming practices, and its structure and syntax lay the groundwork for
learning other programming languages. Mastering C’s fundamentals, including its history,
basic structure, and language components, provides a strong foundation for advancing in
the field of computer science and programming.

nt
8 References

de
• Kernighan, B. W., & Ritchie, D. M. (1988). The C Programming Language. Pren-
tice Hall.

• Stroustrup, B. (2013). The C++ Programming Language. Addison-Wesley.


tu
As
e BC
th

You might also like