CCP Unit 3 Notes Part A
CCP Unit 3 Notes Part A
Part A
Part B
• 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 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:
or
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:
or
#include <stdio.h>
void main() {
char name[20];
int age;
#include <stdio.h>
int main() {
int integer = 42;
float floating = 3.14159;
char character = 'A';
char string[] = "Hello, World!";
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 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.
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.
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():
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);
}
1. toascii
2. toupper
3. tolower
4. isalpha
5. isnumeric
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