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

Grade-9-Introduction-to-C-Programming (1)

ICT programming

Uploaded by

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

Grade-9-Introduction-to-C-Programming (1)

ICT programming

Uploaded by

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

Introduction to

C
Programming
What is C?
· C is a general-purpose programming language that has been widely used since its
creation in the early 1970s.
· It is considered the foundation for many other programming languages such as C++,
Java, and Python.
· C is often used for system programming (like operating systems and hardware drivers),
as well as creating applications.
Why Learn C?
Learning C helps you understand how computers work at a deeper level.
C gives you more control over your computer’s hardware, memory, and processing power.
Many modern languages build upon the concepts and syntax of C, so
mastering it creates a strong foundation for learning other programming languages.
Basic Structure of a C Program
Every C program follows a specific structure, which includes:
#include <stdio.h> // This is a preprocessor directive.
int main() { // The main function, where the program execution begins.
printf("Hello, World!"); // Print statement to display output.
return 0; // Return 0 indicates that the program has successfully executed.
}

Explanation:
1. #include <stdio.h>: This line includes a standard library that allows us to use functions
like printf for output.
2. int main() {}: The main function is where the program starts executing. Every C
program must have a main function.
3. printf("Hello, World!\n");: This prints the text "Hello, World!" to the
screen. The \n represents a new line.
4. return 0;: This signals that the program ended successfully.

Key Components of C Programming


Variables:
· A variable is a space in memory where data is stored. In C, you must
declare variables before using them.
· For example, int age; declares a variable named age that can store integer
values.
Data Types:
· int: Used to store integers (whole numbers), e.g., int age = 15;.
· float: Used to store decimal numbers, e.g., float price = 10.99;.
· char: Used to store individual characters, e.g., char initial = 'A';
· char variable name [length]: Used to store sequences of characters, like words or
sentences. e.g, "Hello"
Input/Output (I/O):
· printf: Outputs data to the screen, e.g., printf("The value is %d", value);.
· scanf: Accepts input from the user, e.g., scanf("%d", &age);.
Example of Declaring and Using Variables in C:
#include <stdio.h>
int main() {
int age = 15; // Declares an integer variable and assigns the value 15
float price 19.99; // Declares a float variable and assigns the value 19.99
char grade = 'A'; // Declares a char variable and assigns the character 'A'
char name[20] = "John"; // Declares a string (character array) and assigns the value
printf("Name: %s\n", name); // Output: Name: John
printf("Age: %d\n", age); // Output: Age: 15
printf("Price: %.2f\n", price); // Output: Price: 19.99
printf("Grade: %c\n", grade); // Output: Grade: A
return 0;
}
· int age = 15;: Declares an integer variable age and assigns it the value 15.
· float price = 19.99;: Declares a float variable price and assigns it the value 19.99.
· char grade = 'A';: Declares a character variable grade and assigns it the value 'A'.
· char name[20] = "John";: Declares a string (character array) variable name with a
maximum size of 20 characters and assigns it the value "John".
· float price = 19.99;: Declares a float variable price and assigns it the value 19.99.
· char grade = 'A';: Declares a character variable grade and assigns it the value 'A'.
· char name[20] = "John";: Declares a string (character array) variable name with a
maximum size of 20 characters and assigns it the value "John".
This program demonstrates how different types of variables can be declared and used to
store and print data in C.
Example 1: Hello World Program
#include <stdio.h> Steps:
1. Open a text editor (e.g., Visual Studio Code, Dev-C++).
int main() { 2. Type the program above.
char name[] = "Hello, World!\n"; 3. Save the file with a .c extension, for example, hello.c.
printf("%s", name);
4. Compile the program using a C compiler (e.g., GCC) and
return 0;
run the executable.
}

OUTPUT:
Hello, World!
Example 2. Simple Addition #include <stdio.h>
of Two Numbers int main() {
A program that takes two int num1, num2, sum;
printf("Enter two numbers: ");
numbers as input and outputs
scanf("%d %d", &num1, &num2);
their sum. sum = num1 + num2;
Explanation: printf("The sum is: %d\n", sum);
· The program asks for two return 0;
}
numbers using scanf, stores
them in num1 and num2,
and calculates their sum.
- printf displays the result.
Example 3. Check Even or Odd Number Example 4. Find the Largest of Three Numbers
A program to check whether a number entered A program to find the largest among three numbers
by the user is even or odd. entered by the user.
#include <stdio.h>
#include <stdio.h> int main() {
int main() { int numi, num2, num3;
int number; printf("Enter three numbers: ");
printf("Enter a number: "); scanf("%d %d %d", &num1, &num2, &num3);
scanf("%d", &number); if (num1 >= num2 && num1 >= num3) {
if (number % 2 == 0) { printf("%d is the largest number.\n", num1);
printf("%d is even.\n", number); } else if (num2 >= num1 && num2 >= num3)
} else { {
printf("%d is odd.\n", number); printf("%d is the largest number.\n", num2);
} } else {
return 0; printf("%d is the largest number.\n", num3);
} }
return 0;
}

You might also like