C Fundamental Part2
C Fundamental Part2
Character Set in C
In C, the character set means set of characters which C programming
language recognize and use.
It includes letters, digits, symbols, and control characters.
Character set used is generally based on either the ASCII or Unicode
encoding schemes.
ASCII (American Standard Code for Information Interchange) includes
128 characters.
Character set:[ASCII C]
1. Letters:
Uppercase letters: A to Z
Lowercase letters: a to z
2. Digits:
Numerals: 0 to 9
3. Special Characters:
+, -, *, /, %, <, >, <=, >=, ==, !=, &&, ||, !,etc.
5. Escape Sequence:
\n, \t, \, \\, etc.
Note:
Unicode is a universal character encoding standard that supports over
143,000 characters. [visit website: https://home.unicode.org ]
Variables
Variables are used to store data that can be modified during the
execution of a program.
C is static typed language, so variable type(data type ) must be declared
before use.
Scope of Variables:
The scope of a variable defines the region of the program in which it can
be accessed.
Local variable and Global variable are the two primary types of variables
based on their scope.
Local variable - Variables declared inside a function or { }(block) are local
to that function or block. These are only accessible within the function or
block in which they are declared.
Input/Output Function
Input / Output functions are classified into two types-
Example –
getc() , gets(), puts(),..etc are unformatted I/O function. These are best
for simple I/O operations like printing strings or reading characters.
Unformatted I/O are simple to use in comparison of Formatted I/O.
Format specifier
A format specifier is a placeholder that defines how a value should be
formatted when displayed. They are used in printf() or scanf() function.
List of format specifier used in prinf():
Format Data Type Description Code Example Output
Specifier Example
%ld or %li Long Integer Prints a long signed long num = 100000L; 100000
integer. printf("%ld", num);
%lu Unsigned Long Prints an unsigned long unsigned long num = 100000
Integer integer. 100000UL;
printf("%lu", num);
%lld Long Long Prints a long long long long num = 123456789
Integer signed integer. 123456789LL;
printf("%lld", num);
%llu Unsigned Long Prints an unsigned long unsigned long long 123456789
Long long integer. num = 123456789ULL;
printf("%llu", num);
Format Input
Data Type Description Code Example
Specifier Example
int num;
%d or %i Signed Integer Reads signed integers. 42
scanf("%d", &num);
char letter;
%c Character Reads a single character. A
scanf("%c", &letter);
Conversion Specifications
It provides control over how data is formatted and displayed
Following program will show use of field width and precision in printf()
// 1. Left-justified, field width ignored (number is longer than 4
characters).
Above program show use in printf(), but we can also use scanf() .
In scanf(), conversion specifications control how data is read from input,
particularly focusing on field width and type.
Scanf()
scanf() will read the line, converting its characters to the numbers they
represent.
It is essentially a “pattern-matching’’ function that tries to match up
groups of input characters with conversion specifications.
Like the printf() function, scanf() is controlled by the format string.
When it is called, scanf() begins processing the information in the string,
starting at the left.
For each conversion specification in the format string, scanf() tries to
locate an item of the appropriate type in the input data, skipping blank
space.
scanf() then reads the item, stopping when it encounters a character
that can’t possibly belong to the item.
S S S S 4 N - 1 0 S S S S . 6 N S S S S - 2 . e 2 N
Here S represents Space and N represents New Line. All S and N are skipped.
When scanf() encounters a character that can ’t be part of the current item, the
character is “put back” to be read again during the scanning of the next input item
or during the next call of scanf().