Transcript
Transcript
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
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:
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
Preprocessor directives start with # and are processed before the actual compilation begins.
• 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.
• 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.
• 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.
• 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.
• 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:
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
EXAMPLE:
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.
• struct (Structure): A structure groups different data types under a single name.
struct Person {
char name[50];
int age;
float height;
};
• int: The most common data type used for whole numbers.
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)
• 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)
Example:
char grade = 'A'; // Character variable
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.
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'
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:
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.
int main() {
printf("Global Variable: %d\n", global_var);
return 0;
}
age 123age
count1 @count
_score int
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