0% found this document useful (0 votes)
2 views15 pages

Building C Programs

The document provides an overview of building C programs, including user input handling, variable declaration, and data types. It explains the use of functions like printf() for output and scanf() for input, along with examples of variable definitions and a simple calculator program. Basic format specifiers for different data types are also outlined.

Uploaded by

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

Building C Programs

The document provides an overview of building C programs, including user input handling, variable declaration, and data types. It explains the use of functions like printf() for output and scanf() for input, along with examples of variable definitions and a simple calculator program. Basic format specifiers for different data types are also outlined.

Uploaded by

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

Building C Programs

1. Write a program that accepts user input,


processes it, and displays the correct output.
2. Define and declare variables in C
3. Apply data types appropriately in C
programs
C Output (Print Text)
To output values or print text in C,
you can use the printf() function:
SYNTAX

•#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
}
C Variables
Variables are containers for storing data values, like numbers and characters.
In C, there are different types of variables (defined with different keywords), for
example:
1. int - stores integers (whole numbers), without decimals, such as 123 or -123
2. float - stores floating point numbers, with decimals, such as 19.99 or -19.99
3. char - stores single characters, such as 'a' or 'B'. Characters are surrounded
by single quotes
Declaring (Creating) Variables

•A declaration in C introduces a
variable to the program and specifies its
type, but it does not allocate memory
for the variable. It tells the compiler
what type of data the variable will hold.
Syntax
int num;
Variable Definition

•A definition not only


declares the variable but
also allocates memory for it.
EXAMPLE

•int num = 10;


C programming, input

The standard function for handling input in


C is: scanf(), which is used to read
formatted data from standard input
(usually the keyboard).
1. #include <stdio.h>
2. int main() {
3. int age;
4. printf("Enter your age: ");
5. scanf("%d", &age);
6. printf("You entered: %d\n",
age);
7. return 0;
8. }
Data Types
a variable in C must be a
specified data type,
and you must use a format
specifier inside
the printf() function to display it:
Basic Format Specifiers
%d or %i int

%f or %F float

%lf double

%c char
1. #include <stdio.h>
2. int main() {
3. int myNum = 5;
4. float myFloatNum = 5.99;
5. char myLetter = 'D';
6. printf("%d\n", myNum);
7. printf("%f\n", myFloatNum);
8. printf("%c\n", myLetter);
9. return 0;
10.}
Simple Calculator in C to Calculate the
Sum of Two Numbers
#include <stdio.h>

int main() {
float num1, num2, sum;
printf("Enter the first number: ");
scanf("%f", &num1);

printf("Enter the second number: ");


scanf("%f", &num2); // Read the second number

sum = num1 + num2;

printf("The sum of %.2f and %.2f is: %.2f\n", num1, num2, sum);

return 0;
}

You might also like