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

Module 2.1 - Input and Output Operations

Input and Output Functions in C

Uploaded by

ajundith
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)
11 views

Module 2.1 - Input and Output Operations

Input and Output Functions in C

Uploaded by

ajundith
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
You are on page 1/ 4

Cebu Institute of Technology – University

Computer Engineering Department


Module 2.1 – Input/Output Operations
Conversion Specifications (Format)
Symbols used to represent variables in output and input functions.
Date Types Output input

int %d %d

float %f (default; displays 6 decimal places) %f (ONLY, no other


%.2f, %.3f, %.4f, etc. symbol)
The value in front of f is the number of decimal places to be displayed.

char %c %c

string %s %s

Input/Output (I/O) Functions


Input Commands
scanf()
• used to accept data from the standard input, usually the keyboard.
• Best used for numeric inputs, but can also be used for char and string inputs (but not really recommended).
• Uses the enter or space key to register/finalize the data. Under the stdio.h library.
Syntax:
scanf(“format”,&variable_name);
Example:
1. Input an int variable: Examples of Wrong Syntax:
int num; scanf(“ %d ”, &num);
scanf(“%d”, &num); -wrong because of the space
scanf(“%d”, num);
-wrong because of the missing &
Use %d to should be placed before symbol
represent int the variable name
2. Input a float variable:
float num; scanf(“%.2f”, &num);
scanf(“%f”, &num); - the other %f symbols can only be
used in printf or output function
scanf(“ %f ”, &num);
Use %f to represent should be placed -there should be no space inside
float before the variable the quotation mark.
name scanf(“%f”, num);
-wrong because of the missing &
symbol
3. Input a char variable
char letter; scanf(“ %c ”, &num);
scanf(“%c”, &letter); -there should be no space inside
the quotation mark.
scanf(“%c”, num);
Use %c to should be placed -wrong because of the missing &
represent char before the variable symbol
name
4. Special variable string
a. String – a series of characters (a word)
b. The C language doesn’t have a data type reserved word for string unlike int, float, and char.
c. How to declare a string variable.
i. Syntax: Example: char firstName[50];
char variable_name[#];
- # : the maximum number of characters in your string/word
d. Input a string variable
char firstName[50]; scanf(“ %s ”, &num);
scanf(“%s”, &firstName); -there should be no space inside
the quotation mark.
Use %s to Optional for string inputs. scanf(“%s”, num);
represent -the & symbol can be omitted for
string string inputs. So, this will not
cause a compiler error

e. Address Operator – the & symbol is responsible in accessing the address of your variable in your
computer’s memory. That is why it is important for scanf().

Engr. Jundith D. Alterado


[email protected]
Cebu Institute of Technology – University
Computer Engineering Department
Module 2.1 – Input/Output Operations
gets()
• Only used for the inputting string variables.
• Under the stdio.h library
Syntax:
gets(variable_name);
Example:

char firstName[50];
gets(firstName);

getch() and getche()


• getch - allows the user to input a character and wait for the enter key. Inputted char will not be echoed but
could be stored if location is specified.
• getche - allows the user to input a character and there is no need for the enter key. Inputted char will be
echoed but could be stored if location is specified.
• Recommended for character inputs.
• Used for single character inputs only.
• Not for numeric inputs
• Under the conio.h library
Syntax:
variable_name = getch();
variable_name = getche();

Example:
#include<conio.h>
int main()
{
char let1, let2;
let1 = getch();
let2 = getche();
}

Output Commands
printf
• Writes formatted output to the standard output device such as the monitor.
• Under the stdio.h library
Syntax:
printf(“format”, &variable_name);
Examples:
1. Output an int variable
int num=5;
printf(“%d”, num);

Use %d to NO & symbol before the


represent int variable name

2. Output a float variable


float num = 3.553;
//6 decimal display
printf(“%f”, num); //3.553000
//3 decimal display
printf(“%.3f”, num); //3.553
//2 decimal display
printf(“%.2f”, num); //3.55

3. Output a char variable


Char letter = ‘a’;
printf(“%c”, letter);

4. Output a string variable


Char name[20] = “Julia”;
printf(“%s”, name);

DO NOT include the square


brackets ([ ])
Engr. Jundith D. Alterado
[email protected]
Cebu Institute of Technology – University
Computer Engineering Department
Module 2.1 – Input/Output Operations
5. Display multiple variables
#include<stdio.h>
int main()
{
int n1 = 3, n2 = 8;
float n3 = 6.5;
char let = ‘y’;
char str[5] = “yes”;
//comments will show how they will look like when
//you run your code
printf(“%d %d”, n1, n2); Variable list
//3 8
printf(“%d %d”, n2, n1);
//8 3
printf(“%d %.2f %c %s”, n1, n3, let, str);
//3 6.50 y yes

The first “format” would represent the first variable on the list.
If you put n3 before n1
Ex: printf(“%d %.2f %c %s”, n3, n1, let, str);
There would be an error because, now %d would represent n3 which is a float variable.
6. Adding a ‘new line’.
What if you want your display to look like this? Use the /n symbol.
3 #include<stdio.h>
6.50 int main()
y {
yes int n1 = 3, n2 = 8;
float n3 = 6.5;
char let = ‘y’;
char str[5] = “yes”;
printf(“%d\n%.2f\n%c\n%s”, n1, n3, let, str);
/* 3
6.50
y
yes */

Note:
• unlike in scanf(), there can be other characters inside the quotation mark (“ “), you can customize the way
you display your output.
• Example: I want my display to be “Number is #” instead of just the number directly.
-# is whatever value the user inputs
• Syntax:
#include<stdio.h> #include<stdio.h>
int main() int main()
{ {
int num; int num1, num2;
scanf(“%d”, &num); scanf(“%d”, &num1);
printf(“Number is %d”, num); scanf(“%d”, &num2);
} printf(“Numbers are %d and %d”, num1, num2);
}

Sample Programs:

1. Create a C program to ask the user for an integer number and display its value.
Expected Display when you run your code:

Input a number: 12 Input a number: 0 Input a number: -9


Inputted value: 12 Inputted value: 0 Inputted value: -9

#include<stdio.h>
int main()
{
int num;
printf(“Input a number: “);
scanf(“%d”, &num);
printf(“Inputted value: %d”, num);
}

Engr. Jundith D. Alterado


[email protected]
Cebu Institute of Technology – University
Computer Engineering Department
Module 2.1 – Input/Output Operations
2. Create a C program to ask the user for two integer numbers and display their values in two separate lines
of text.
Expected Display when you run your code:

Input first number: 12 Input first number: 8


Input second number: 24 Input second number: 16
First Number: 12 First Number: 8
Second Number: 24 Second Number: 16

#include<stdio.h>
int main()
{
int num1, num2;
printf(“Input first number: “);
scanf(“%d”, &num1);
printf(“Input second number: “);
scanf(“%d”, &num2);
printf(“First Number: %d\n”, num1);
printf(“Second Number: %d”, num2);
}

3. Create a C program to ask the user their name and display it.
Expected Display when you run your code:

Enter name: Jundith Alterado


Hello Jundith Alterado! Welcome to CIT-U.

Enter name: Romaine Lorena


Hello Romaine Lorena! Welcome to CIT-U.

#include<stdio.h>
int main()
{
char name[50];
printf(“Enter name: “);
gets(name);
printf(“Hello %s! Welcome to CIT-U!”, name);
}

Engr. Jundith D. Alterado


[email protected]

You might also like