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

Transcript

Ttttran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Transcript

Ttttran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

COM 121: C PROGRAMMING

Overview of C Programming Concepts:


INTRODUCTION

Rapid technological improvements in computer hardware have increased organizations' desire to


purchase computers. When a computer is purchased, a corporation must either build or purchase
software to process the input data and provide the required results.

A problem will never be brought to a computer unless it is determined that it is a candidate for
computer solution. As a result, there is initially an unpleasant feeling that something requires care.
A management may be concerned that sales are declining or that the expense of maintaining a
component inventory is increasing. The natural impulse is to use a computer to solve the problem.
But what's the problem? Can a computer help solve it? Defining the specific problem for the
computer to solve is a key stage in giving a job to the computer. It is critical to understand the true
nature of the problem.

Program
Software, sometimes referred to as computer programs, are structured lists of instructions that,
when carried out, lead a computer to act in a predetermined way. Because human languages are
not understood by computers, computer programs must employ computer languages. Making a
computer executable program that carries out the necessary operations is called programming.
A computer’s native language, which differs among different types of computers, is its machine
language—a set of built-in primitive instructions. These instructions are in the form of binary code,
so in telling the machine what to do, you have to enter binary code. Programming in machine
language is a tedious process. Moreover, the programs are highly difficult to read and modify.

A wide array of problem-oriented languages has been developed, some of the principal ones being
COBOL (Common Business-Oriented Language), FORTRAN (Formula Translation), BASIC
(Beginner’s All-Purpose Symbolic Instruction Code), Java and Pascal.

Programming
Programming is the process of writing instructions for a computer to perform tasks. These
instructions are written in a language that the computer can understand.
Programming Language

A programming language is a vocabulary and set of grammatical rules for instructing a computer
or computing device to perform specific tasks. The term programming language usually refers to
high-level languages, such as BASIC, C, C++, COBOL, Java, FORTRAN, Ada, and Pascal.

Each programming language has a unique set of keywords (words that it understands) and a special
syntax for organizing program instructions

High-Level vs. Low-Level Languages:


High-level languages (like C) are easier for humans to understand and write. Low-level
languages (like Assembly) are closer to machine language.

Why Learn C?

Learning C programming provides a strong foundation for understanding both basic and advanced
programming concepts. It equips students with the skills needed for system-level programming,
performance-critical applications, and a deeper understanding of how software interacts with
hardware. By mastering C, you develop the necessary skills to excel in other languages and fields
of computer science. It’s a timeless language with a wide range of applications in the tech industry

Program Structure
The structure of a C program follows a well-defined format. Understanding this structure is crucial
for writing efficient and error-free C code. Here's a breakdown of the basic components of a C
program, using the following simple example:

Key Components of the Program Structure:

1. Preprocessor Directives (#include): These lines tell the compiler to include certain
libraries or header files before compiling the program. The most common is:

#include <stdio.h>

This includes the Standard Input/Output library, which provides functions like

printf() and scanf().

Preprocessor directives start with # and are processed before the actual compilation begins.

2. Main Function (int main()):

• Every C program must have a main( ) function, as this is where the program starts
executing.
• The syntax for the main function is
int main( ) {
// Statements
return 0;
}

• int indicates that the main function returns an integer value. Returning 0 typically
indicates successful execution of the program.
• The curly braces { } define the body of the function, which contains all the statements
of the program.

3. Output Statement (printf("Hello, World!\n");):

• The printf() function prints the text "Hello, World!" to the screen.
• "Hello, World!\n" is the string of characters to be printed. The \n is an escape sequence
that adds a new line after the text, meaning the cursor moves to the next line.
• printf() is part of the standard input-output library (stdio.h), and it’s used for displaying
output.

4. Return Statement (return 0;):

• return 0; ends the main() function and returns the value 0 to the operating system. A
return value of 0 typically indicates that the program has executed successfully.

General C Program Structure in "Hello, World!" Example:


Execution Process:

• When the program is run, the compiler starts execution from the main() function.
• Inside the main(), the printf() function prints "Hello, World!" on the screen.
• After printing, the return 0; statement tells the system that the program executed
successfully.

How to Compile and Run the Program:

1. Write the Code into editor or IDE (like Code::Blocks or VS Code).


2. Save the File: Save it with a .c extension, for example, hello.c.
3. Compile: Use a compiler like gcc to compile the program:

When you run the program, it will display:


Hello, World!

Why Use "Hello, World!"?

• The "Hello, World!" program is commonly the first program written by beginners because
it’s simple and shows the basic structure of a C program without the need for complex logic
or calculations.
• It demonstrates how to:

• Include a library (stdio.h).


• Define the main() function.
• Use printf() to produce output.
• Use return 0; to indicate successful program execution.

DATA TYPE

In C programming, data types define the type of data that can be stored in a variable and how
much memory that data will use. Each variable in C must be declared with a data type before use,
and the data type determines the type of values the variable can hold (e.g., integer, floating-point
number, character, etc.).
Categories of Data Types in C

1. Basic (Primitive) Data Types


2. Derived Data Types
3. User-Defined Data Types

1. Basic (Primitive) Data Types

These are the fundamental data types provided by C. They include:

• int: Used to store integer values (whole numbers).


• float: Used to store single-precision floating-point numbers (numbers with decimals).
• double: Used to store double-precision floating-point numbers (higher precision than
float).
• char: Used to store single characters (e.g., 'a', 'Z').

EXAMPLE:

int age = 25; // Declares an integer variable 'age'


float height = 5.8; // Declares a float variable 'height'
double pi = 3.14159; // Declares a double variable 'pi'
char grade = 'A'; // Declares a char variable 'grade'

2. Derived Data Types

Derived data types are constructed from basic data types. The most common derived types are:

• Arrays: A collection of elements of the same type, stored in contiguous memory locations.

int numbers[5]; // Array of 5 integers


float scores[10]; // Array of 10 floating-point numbers

• Pointers: Variables that store the address of another variable.

int *ptr; // Pointer to an integer


• Functions: Functions are also a derived data type, where you specify a return type (which
can be any data type, such as int, float, etc.) and parameters.

3. User-Defined Data Types

C also allows the creation of user-defined data types. These include:

• struct (Structure): A structure groups different data types under a single name.

struct Person {
char name[50];
int age;
float height;
};

Detailed Explanation of Basic Data Types

1. Integer Types (int, short, long, unsigned)

• int: The most common data type used for whole numbers.

• short: Smaller integers, generally 2 bytes in size.

• long: Larger integers, typically 4 or 8 bytes depending on the system.

• unsigned int: Only positive integers (no negative values).

Example:
int a = 10; // Standard integer
short b = 200; // Short integer
long c = 100000; // Long integer
unsigned int d = 500; // Unsigned integer (positive values only)

2. Floating-Point Types (float, double, long double)

• float: Used to represent numbers with decimals, but with single precision.

• double: Same as float, but with higher precision and double the size.

• long double: Even higher precision than double (depends on the system).

Example:
float price = 5.99f; // Float value (single precision)
double distance = 120.456; // Double precision
long double pi_value = 3.14159265358979323846L; // Long double (extended precision)
3. Character Type (char)

• Used to store individual characters or small integers.


• Characters are enclosed in single quotes ('A', 'b').

Example:
char grade = 'A'; // Character variable

THE CONCEPT OF VARIABLE IN C PROGRAMMING

Variable is a named storage location in the computer's memory that holds a value. Variables allow
you to store data that can be used and manipulated throughout the program. The value stored in a
variable can be changed during the execution of the program.

Key Concepts of Variables:

1. Variable Declaration: Declaring a variable tells the compiler to reserve a specific amount
of memory for the variable based on its data type.
2. Variable Initialization: Initialization refers to assigning an initial value to a variable when
it is declared.
3. Variable Types: The type of data the variable can hold is determined by its data type (e.g.,
int, float, char).

4. Variable Scope: The scope of a variable refers to the region of the program where the
variable can be accessed (e.g., inside a function, within a block).
5. Variable Naming Rules: Variables must follow specific naming conventions and rules.

1. Declaring a Variable

Before using a variable, it must be declared with a specific data type. The syntax for declaring a
variable in C is:

data_type variable_name;

• data_type: This defines the type of data the variable will store (e.g., int, float, char).
• variable_name: The name you give the variable.
Example:
int age; // Declares an integer variable 'age'
float height; // Declares a floating-point variable 'height'
char grade; // Declares a character variable 'grade'

In this example:

• int age; declares a variable named age that can hold an integer value.
• float height; declares a variable named height to store a floating-point number.
• char grade; declares a variable named grade to store a single character.

2. Initializing a Variable

A variable can also be initialized when it is declared. Initialization is the process of assigning an
initial value to the variable.

Example:
int age = 25; // Declares and initializes 'age' with a value of 25
float height = 5.9; // Declares and initializes 'height' with a value of 5.9
char grade = 'A'; // Declares and initializes 'grade' with the character 'A'

You can also initialize a variable after it is declared:

int age; // Declaration


age = 25; // Initialization

3. Types of Variables

The type of data a variable can hold is determined by its data type. The most common data types
for variables in C are:

• int: Stores integer values (e.g., 10, -5, 100).


• float: Stores floating-point (decimal) values (e.g., 3.14, 0.01).
• double: Stores double-precision floating-point values (e.g., 3.14159).
• char: Stores a single character (e.g., 'A', 'b').
Example:
int count = 10; // Integer variable
float price = 5.99; // Floating-point variable
double pi = 3.14159; // Double-precision floating-point variable
char initial = 'S'; // Character variable

4. Scope of a Variable

The scope of a variable refers to the region of the program where the variable is accessible.

• Local Variables: Declared inside a function or block and can only be used within that function or
block.
• Global Variables: Declared outside of all functions and can be accessed by any function in the
program.

Example of Local Variable:


int main() {
int age = 30; // Local variable: can only be used inside 'main'
printf("Age: %d\n", age);
return 0;
}
Example of Global Variable:
int global_var = 100; // Global variable

int main() {
printf("Global Variable: %d\n", global_var);
return 0;
}

5. Variable Naming Rules

The name of a variable (identifier) must follow certain rules:

• Must begin with a letter (A-Z or a-z) or an underscore (_).


• Can contain letters, digits (0-9), and underscores.
• Cannot contain spaces or special characters (e.g., @, #, $).
• C is case-sensitive, so age, Age, and AGE are different variables.
• Cannot use C keywords (e.g., int, return, while).
Examples of Valid and Invalid Variable Names:
Valid Invalid

age 123age

total_price total price

count1 @count

_score int

. Example Program Using Variables #include <stdio.h>

int main() {
int age = 21; // Declare and initialize an integer variable
float height = 5.9; // Declare and initialize a float variable
char grade = 'A'; // Declare and initialize a char variable

// Output the values of the variables


printf("Age: %d\n", age);
printf("Height: %.1f\n", height);
printf("Grade: %c\n", grade);

return 0; // Indicate successful program execution


}

You might also like