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

CCP Unit 3 Notes Part A

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

CCP Unit 3 Notes Part A

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

Unit 3: Syllabus

Part A

Input and output with C

• Formatted I/O functions - printf and scanf,


• control strings and escape sequences,
• output specifications with printf functions;
• Unformatted I/O functions to read and display single
character and a string
◦ getchar, putchar, gets and puts functions.
• Character handling library functions -
◦ toascii, toupper, tolower, isalpha, isnumeric

Part B

C Operators & Expressions

• Arithmetic operators
• Relational operators
• Logical operators
• Assignment operators
• Increment & Decrement operators
• Operator Precedence and Associatively
• Evaluation of arithmetic expressions
• Mathematical library functions in C using math.h
• Type casting & conversion.
Input and output with C

Formatted I/O functions - printf and scanf

Input Output functions in C


Computer programming requires input/output (I/O) operations. Using I/O operations, the data is
read and written to and from various sources, including files, keyboards, and screens. I/O operations
can be either formatted or unformatted in the C computer language. In this blog post, we will cover
the distinctions between formatted and unformatted I/O in C, as well as code samples and outputs.

Formatted I/O in C
The I/O procedure is known as "formatted I/O". It enables you to read or write data in a certain
format. Printf() and scanf() are two examples of C routines that handle formatted I/O. The type and
format of the data to be read or written are specified by format strings, which are used by these
operations. The program's execution replaces the placeholders for the data found in the format
strings with the actual data.

1) printf():

printf() function is used in a C program to display any value like float, integer, character, string, etc
on the console screen. It is a pre-defined function that is already declared in the stdio.h(header file).

Syntax:

printf(“Format Specifier”, var1, var2, …., varn);

or

printf(“Format Specifier”, Variable List);

2) scanf():

scanf() function is used in the C program for reading or taking any value from the keyboard by the
user, these values can be of any data type like integer, float, character, string, and many more. This
function is declared in stdio.h(header file), that’s why it is also a pre-defined function. In scanf()
function we use &(address-of operator) which is used to store the variable value on the memory
location of that variable.

Syntax:

scanf(“Format Specifier”, &var1, &var2, …., &varn);

or

scanf(“Format Specifier”, Address List);


Example for Formated I/O Program

#include <stdio.h>
void main() {
char name[20];
int age;

printf("Enter your name: ");


scanf("%s", name);

printf("Enter your age: ");


scanf("%d", &age);

printf("Your name is %s and your age is %d\n", name, age);


}

Control Strings / Format Specifier


Example:

#include <stdio.h>

int main() {
int integer = 42;
float floating = 3.14159;
char character = 'A';
char string[] = "Hello, World!";

printf("Integer: %d\n", integer); // %d for integer


printf("Float: %f\n", floating); // %f for float
printf("Character: %c\n", character); // %c for character
printf("String: %s\n", string); // %s for string
printf("Hexadecimal: %x\n", integer); // %x for hexadecimal
printf("Octal: %o\n", integer); // %o for octal
printf("Unsigned: %u\n", integer); // %u for unsigned integer
printf("Scientific: %e\n", floating); // %e for scientific notation

return 0;
}

Escape Sequence in C
C Program to demonstrate escape squence

#include <stdio.h>
void main() {
printf("Hello, World!\n"); // New line
printf("Hello,\tWorld!\n"); // Horizontal tab
printf("Hello, \"World!\"\n"); // Double quote
printf("Hello, \\World!\\\n"); // Backslash
printf("Hello, \bWorld!\n"); // Backspace
printf("Hello, \rWorld!\n"); // Carriage return
printf("Hello, \vWorld!\n"); // Vertical tab
}

Unformatted Input/Output functions

Unformatted I/O functions are used only for character data type or character array/string and
cannot be used for any other datatype. These functions are used to read single input from the user at
the console and it allows to display the value at the console.

Why they are called unformatted I/O?

These functions are called unformatted I/O functions because we cannot use format
specifiers in these functions and hence, cannot format these functions according to our needs.

The following unformatted I/O functions


getchar()
putchar()
gets()
puts()

1)getchar():

The getchar() function is used to read only a first single character from the keyboard
whether multiple characters is typed by the user and this function reads one character at one time
until and unless the enter key is pressed. This function is declared in stdio.h(header file)

Syntax:

Variable-name = getchar();

Example:

#include <stdio.h>
// Driver code
void main()
{
// Declaring a char type variable
char ch;
printf("Enter the character: ");
// Taking a character from keyboard
ch = getchar();
// Displays the value of ch
printf("%c", ch);
}

2)putchar():

The putchar() function is used to display a single character at a time by passing that character
directly to it or by passing a variable that has already stored a character. This function is declared in
stdio.h(header file)

Syntax:

putchar(variable_name);

Example:

#include <stdio.h>
void main()
{
char ch;
printf("Enter any character: ");
// Reads a character
ch = getchar();
// Displays that character
putchar(ch);
}

3)gets():

gets() function reads a group of characters or strings from the keyboard by the user and these
characters get stored in a character array. This function allows us to write space-separated texts or
strings. This function is declared in stdio.h(header file).

Syntax:
//Declare a char type variable of any length
char str[length of string in number];
gets(str);

Example:
#include <stdio.h>
void main()
{
// Declaring a char type array of length 50 characters
char name[50];
printf("Please enter some texts: ");
// Reading a line of character or a string
gets(name);
// Displaying this line of character or a string
printf("You have entered: %s", name);
}
4)puts():

In C programming puts() function is used to display a group of characters or strings which is


already stored in a character array. This function is declared in stdio.h(header file).

Syntax:

puts(identifier_name);

Example:

#include <stdio.h>
int main()
{
char name[50];
printf("Enter your text: ");
// Reads string from user
gets(name);
printf("Your text is: ");
// Displays string
puts(name);
}

Character handeling functions in C

1. toascii
2. toupper
3. tolower
4. isalpha
5. isnumeric

ctype.h header file contains all character functions

1. toascii
Converts a character to its ASCII value.

#include <stdio.h>
#include <ctype.h>

int main() {
char ch = 'A';
printf("ASCII value of %c is %d\n", ch, toascii(ch));
return 0;
}

2. toupper
Converts a lowercase letter to uppercase.

#include <stdio.h>
#include <ctype.h>
int main() {
char ch = 'a';
printf("Uppercase of %c is %c\n", ch, toupper(ch));
return 0;
}

3. tolower
Converts an uppercase letter to lowercase.

#include <stdio.h>
#include <ctype.h>

int main() {
char ch = 'A';
printf("Lowercase of %c is %c\n", ch, tolower(ch));
return 0;
}

4. isalpha
Checks if a character is an alphabetic letter.

#include <stdio.h>
#include <ctype.h>

int main() {
char ch = 'A';
if (isalpha(ch)) {
printf("%c is an alphabetic character.\n", ch);
} else {
printf("%c is not an alphabetic character.\n", ch);
}
return 0;
}

5. isnumeric
Checks if a character is a digit.

#include <stdio.h>
#include <ctype.h>

int main() {
char ch = '5';
if (isnumeric(ch)) {
printf("%c is a numeric character.\n", ch);
} else {
printf("%c is not a numeric character.\n", ch);
}
return 0;
}

These functions are part of the <ctype.h> library in C, which provides a range of functions for
character handling

You might also like