0% found this document useful (0 votes)
25 views16 pages

C Fundamental Part2

C classroom notes

Uploaded by

Himanshu Narayan
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)
25 views16 pages

C Fundamental Part2

C classroom notes

Uploaded by

Himanshu Narayan
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/ 16

C Programming Notes-Fundamentals (Part II)

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.

4. White space characters:


\t, \v, \n, space, \r, etc.

5. Escape Sequence:
\n, \t, \, \\, etc.

© HIMANSHU NARAYAN PRASAD, VVIT 1


C Programming Notes-Fundamentals (Part II)

Note:
Unicode is a universal character encoding standard that supports over
143,000 characters. [visit website: https://home.unicode.org ]

© HIMANSHU NARAYAN PRASAD, VVIT 2


C Programming Notes-Fundamentals (Part II)

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.

Global variable - Variables declared outside of all functions are global


variables. These variables are accessible for all in given program.

© HIMANSHU NARAYAN PRASAD, VVIT 3


C Programming Notes-Fundamentals (Part II)

Input/Output Function
Input / Output functions are classified into two types-

printf() and scanf() [used for formatted data]


These 2 important functions are defined in <stdio.h> header file.
printf() is used to display formatted output on consol(monitor)
Syntex:

scanf() is used to read formatted data as input and store it at given


location.
Syntex:

© HIMANSHU NARAYAN PRASAD, VVIT 4


C Programming Notes-Fundamentals (Part II)

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.

Difference between Formatted I/O and Unformatted I/O

© HIMANSHU NARAYAN PRASAD, VVIT 5


C Programming Notes-Fundamentals (Part II)

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

%d or %i Signed Integer Prints signed integers int num = 42; 42


printf("%d", num);

%u Unsigned Prints unsigned unsigned int num = 42; 42


Integer integers (non-
printf("%u", num);
negative).

%f Float Prints floating-point float pi = 3.141593; 3.141593


numbers (default 6 printf("%f", pi);
decimal places).

%e or %E Float Prints floating-point float pi = 3141.593; 3.141593e+


numbers in scientific printf("%e", pi); 03
notation.

%g or %G Float Prints floating-point float pi = 3.14159; 3.14159


numbers in either printf("%g", pi);
normal or scientific
notation, depending
on value.

%c Character Prints a single char letter = 'A'; A


character. printf("%c", letter);

%s String Prints a string (null- char str[] = "Hello"; Hello


terminated character printf("%s", str);
array).

%x or %X Unsigned Prints unsigned integer int num = 42; 2a


Hexadecimal in hexadecimal. printf("%x", num);

© HIMANSHU NARAYAN PRASAD, VVIT 6


C Programming Notes-Fundamentals (Part II)

%o Unsigned Octal Prints unsigned integer int num = 42; 52


in octal (base 8). printf("%o", num);

%p Pointer Prints a memory int *ptr = &num; 0x7ffdec


address (pointer). printf("%p", ptr);

%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);

%lf Double Prints a double double pi = 3.141593; 3.141593


precision floating-point printf("%lf", pi);
number.

%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);

Like printf(), format specifier is also used with scanf().

© HIMANSHU NARAYAN PRASAD, VVIT 7


C Programming Notes-Fundamentals (Part II)

List of format specifier used in scan():

Format Input
Data Type Description Code Example
Specifier Example

int num;
%d or %i Signed Integer Reads signed integers. 42
scanf("%d", &num);

Unsigned Reads unsigned integers unsigned int num;


%u 42
Integer (non-negative). scanf("%u", &num);

Reads floating-point float pi;


%f Float 3.141593
numbers. scanf("%f", &pi);

Reads double precision double pi;


%lf Double 3.141593
floating-point numbers. scanf("%lf", &pi);

Reads floating-point float num;


%e or %E Float numbers in scientific 3.141593e+03
notation. scanf("%e", &num);

Reads floating-point float num;


%g or %G Float numbers in normal or 3.14159
scientific notation. scanf("%g", &num);

char letter;
%c Character Reads a single character. A
scanf("%c", &letter);

Reads a string (stops at char str[100];


%s String Hello
whitespace). scanf("%s", str);

Unsigned Reads unsigned integers in unsigned int num;


%x or %X 2a
Hexadecimal hexadecimal format. scanf("%x", &num);

Reads unsigned integers in unsigned int num;


%o Unsigned Octal 52
octal (base 8). scanf("%o", &num);

Reads a pointer (memory int *ptr;


%p Pointer 0x7ffdec
address). scanf("%p", &ptr);

Long Signed Reads a long signed long num;


%ld or %li 100000
Integer integer. scanf("%ld", &num);

Unsigned Long Reads an unsigned long unsigned long num;


%lu 100000
Integer integer. scanf("%lu", &num);

© HIMANSHU NARAYAN PRASAD, VVIT 8


C Programming Notes-Fundamentals (Part II)

Conversion Specifications
It provides control over how data is formatted and displayed

The format for a conversion specification in printf():


%[flags][minimum filed width] . [precision length] specifier

flags: Optional characters (like left-justified, zero-padded).


 - means left justified
 + or non-negative value mean right justified(if total number of
printing character is less than provided field width, then after right
shifting all remaining place will fill(padded) with space)
Minimum field width : the number of characters that will be printed.
Precision length:
 For floating-point numbers -how many digits should appear after
the decimal point
 For strings - defines the maximum number of characters to be
printed.

Specifier : (d for integers, f for floating-point numbers, etc.).

© HIMANSHU NARAYAN PRASAD, VVIT 9


C Programming Notes-Fundamentals (Part II)

Following program will show use of field width and precision in printf()
 // 1. Left-justified, field width ignored (number is longer than 4
characters).

printf("%-4d\n", 123456789); // Output: "123456789"

 // 2. Right-justified, field width ignored (number is longer than 4


characters).

printf("%4d\n", 123456789); // Output: "123456789"

 // 3. Right-justified, field width of 15 (padded with spaces).

printf("%15d\n", 123456789); // Output: " 123456789"

 // 4. Zero-padded, but field width ignored (number is longer


than 4 characters).

printf("%04d\n", 123456789); // Output: "123456789"

 // 5. Floating-point number rounded to 4 decimal places.


printf("%.4f\n", 12.3456789); // Output: "12.3457"

 // 6. Invalid format specifier for integer with precision; results in


undefined behavior.

printf("%4.2d\n", 123456789); // Output: "123456789"


(undefined behavior)

© HIMANSHU NARAYAN PRASAD, VVIT 10


C Programming Notes-Fundamentals (Part II)

 // 7. Floating-point number rounded to 2 decimal places.

printf("%.2f\n", 12.3456789); // Output: "12.35"

 // 8. Left-justified floating-point number rounded to 2 decimal


places.

printf("%-.2f\n", 12.3456789); // Output: "12.35"

 // 9. Floating-point number with a field width of 10 and 3


decimal places (right-justified).

printf("%10.3f\n", 12.3456789); // Output: " 12.346"

 // 10. Left-justified floating-point number rounded to 4 decimal


places.

printf("%-.4f\n", 12.3456789); // Output: "12.3457"

 // 11. Left-justified floating-point number with 3 decimal places.

printf("%-2.3f\n", 12.3456789); // Output: "12.346"

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.

© HIMANSHU NARAYAN PRASAD, VVIT 11


C Programming Notes-Fundamentals (Part II)

The format for a conversion specification in scan():


%[minimum filed width] specifier

Important Point relate to printf()

The number of format specifiers must match the number of arguments.


If the number of arguments doesn't match the number of format
specifiers, printf() may result in undefined behaviour.
Example:
printf("%d %f\n", 10); // Missing one argument
// Output: Undefined behaviour, depends on the compiler

© HIMANSHU NARAYAN PRASAD, VVIT 12


C Programming Notes-Fundamentals (Part II)

The order of arguments must match the order of format specifiers.


The order of format specifiers must correspond to the order of
arguments. If they are out of order, the output will be unpredictable.
Example:
printf("%d %f\n", 3.14, 10); // Incorrect order of arguments
// Output: Undefined behaviour, could print garbage values

Data type of arguments must match the format specifier.


Each format specifier expects a specific data type. Passing the wrong
type will lead to incorrect output or undefined behavior.
Example:
printf("%d\n", 3.14); // Using %d (int) for a float value
// Output: Undefined behaviour, may print a wrong number

Extra arguments are ignored.


If more arguments are passed than the number of format specifiers, the
extra arguments are ignored.
Example:
printf("%d\n", 42, 3.14); // Extra argument (3.14) is ignored
// Output: "42"

Insufficient arguments lead to undefined behavior.


If fewer arguments are provided than required by the format specifiers,
printf() will try to access uninitialized memory, causing undefined
behaviour.
Example:
printf("%d %f\n", 10); // Missing argument for %f
// Output: Undefined behaviour, may print random values

© HIMANSHU NARAYAN PRASAD, VVIT 13


C Programming Notes-Fundamentals (Part II)

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.

 If the item was read successfully, scanf() continues processing


the rest of the format string.
 If any item is not read successfully, scanf() returns immediately
without looking at the rest of the format string (or the remaining
input data).
 scanf() ignores while-space characters (the space, horizontal and
vertical tab, form-feed, and new-line characters) except in %c.

Following diagram shows how scanf() read data-

© HIMANSHU NARAYAN PRASAD, VVIT 14


C Programming Notes-Fundamentals (Part II)

In above example , scanf() read entered data as-

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().

© HIMANSHU NARAYAN PRASAD, VVIT 15


C Programming Notes-Fundamentals (Part II)

In above example, enter number is


25-30.4-5.0e2

1st Conversion specification %d


The first nonblank input character is 1, then scanf() read character 5,then reads the
next character - .
Recognizing that - can’t appear inside an integer, scanf() stores 25 into a and puts
the - character back.

2nd Conversion specification %d


scanf() then reads the characters - , then 3, then 0, then .(period)
Since an integer can’t contain a decimal point, scanf() stores -30 into b and puts the .
character back.

3rd Conversion specification %f


scanf() reads the characters .(period),then 4, then -.
Since a floating-point number can’t contain a minus sign after a digit,
scanf() stores .4 into c and puts the - character back.

4th Conversion specification %f


Lastly, scanf() reads the characters - , then 5, then .(period), then 0, then e,2 and
new-line(last) character.
Since a floating-point number can ’t contains a new-line character,
scanf() stores -5.0x 102 into y and puts the new-line character back.

© HIMANSHU NARAYAN PRASAD, VVIT 16

You might also like