Day 1 (Fundamentals of C)
Day 1 (Fundamentals of C)
Day 1 (Fundamentals of C)
3. Replace all the trigraph characters in the source code (prog.c) using trigraph preprocessor
>trigraph prog.c
4. We can see the replaced source file using the type command
>type prog.c
#include<stdio.h>
int main()
{
printf("Hello\nWorld");
return 0;
}
a) Integer Literals
Decimal-literal(base 10): A non-zero decimal digit followed by zero or more decimal
digits(0, 1, 2, 3, 4, 5, 6, 7, 8, 9). For example: 56, 78
Octal-literal(base 8): a 0 followed by zero or more octal digits(0, 1, 2, 3, 4, 5, 6, 7). For
example: 045, 076, 06210
Hex-literal(base 16): 0x or 0X followed by one or more hexadecimal digits(0, 1, 2, 3, 4, 5, 6,
7, 8, 9, a, A, b, B, c, C, d, D, e, E, f, F). For example: 0x23A, 0Xb4C, 0xFEA
Binary-literal(base 2): 0b or 0B followed by one or more binary digits(0, 1). For example:
0b101, 0B111
Suffixes: The Prefix of the integer literal indicates the type in which it is to be read. For
example: 12345678901234LL indicates a long long integer value 12345678901234 because
of the suffix LL
These are represented in many ways according to their data types.
i. int: No suffix is required because integer constant is by default assigned as an int data
type.
ii. unsigned int: character u or U at the end of an integer constant.
iii. long int: character l or L at the end of an integer constant.
iv. unsigned long int: character ul or UL at the end of an integer constant.
v. long long int: character ll or LL at the end of an integer constant.
vi. unsigned long long int: character ull or ULL at the end of integer constant.
Here are some examples of integer literals
212 /* Legal */
215u /* Legal */
0xFeeL /* Legal */
078 /* Illegal: 8 is not an octal digit */
032UU /* Illegal: cannot repeat a suffix */
Following are other examples of various types of integer literals
85 /* decimal */
printf("a= %d\n",a);
printf("b= %d\n",b);
printf("c= %d\n",c);
printf("d= %d\n\n",d);
printf("a= %d\n",a);
printf("b= %o\n",b);
printf("c= %X\n",c);
printf("d= %d\n\n",d);
printf("a= %i\n",a);
printf("b= %i\n",b);
printf("c= %i\n",c);
printf("d= %i\n",d);
return 0;
}
Output:
a= 100
b= 100
c= 1610
d= 100
a= 100
b= 144
c= 64A
d= 100
a= 100
b= 100
c= 1610
b) Floating-point Literals
These are used to represent and store real numbers. The real number has an integer part, real
part, fractional part and an exponential part. The Real or Floating-point constants can be
written in two forms:
1. Fractional or Normal form
2. Exponential or Scientific form
While representing the floating-point decimals one must keep two things in mind to produce
valid literals:
• In the decimal form, one must include the decimal point, exponent part o r both,
otherwise, it will lead to an error.
• In the exponential form, one must include the integer part, fractional part or both,
otherwise, it will lead to an error.
Syntax of float literal in exponential form
[+/-] <Mantissa> <e/E> [+/-] <Exponent>
Examples of real literal in exponential notation are:
+1e23, -9e2, +2e-25
Rules for creating an exponential notation
o In exponential notation, the mantissa can be specified either in decimal or fractional
form.
o An exponent can be written in both uppercase and lowercase, i.e., e and E.
Answers
1) Valid
2) Invalid (It is an integer constant)
3) Valid
4) Invalid (Must have a single decimal place)
5) Valid
6) Valid
7) Valid
8) Invalid
9) Valid
10) Invalid (Other symbols can’t b used)
Example program
#include <stdio.h>
int main()
{
float x,y,z;
x=12675.34;
y=145.658E-2;
z=12676.796875;
printf("x= %f\n",x);
printf("y= %f\n",y);
printf("z= %f\n",z);
printf("x= %e\n",x);
printf("y= %e\n",y);
printf("z= %e\n",z);
return 0;
}
Output:
x= 12675.339844
y= 1.456580
z= 12676.796875
x= 1.267534e+04
y= 1.456580e+00
z= 1.267680e+04
c) Character Constants
Character literals are enclosed in single quotes, e.g., 'x' can be stored in a simple variable
of char type. A character literal can be a plain character (e.g., 'x'), an escape sequence (e.g.,
'\t'), or a universal character (e.g., '\u02C0'). There are certain characters in C that represent
special meaning when preceded by a backslash for example, newline (\n) or tab (\t).
A character literal can be represented in the following ways:
Hide Answers
1) Valid
2) Invalid (Single inverted commas are directed towards right)
3) Invalid (Must not be placed in double inverted commas)
4) Invalid (Should have single character only)
5) Valid
6) Valid
7) Valid (\n is considered as single character )
Example program
#include <stdio.h>
int main()
{
char x,y,z,a,b,c,d;
x='a';
y=65;
z='\n';
a='.';
b='5';
c='\067';
d='\0x12';
printf("x= %c\n",x);
printf("y= %c\n",y);
printf("z= %c\n",z);
printf("a= %c\n",a);
printf("b= %c\n",b);
printf("c= %c\n",c);
printf("d= %c\n",d);
a= .
b= 5
c= 7
d= 2
d) String Literals
String literals or constants are enclosed in double quotes "". A string contains characters that
are similar to character literals: plain characters, escape sequences, and universal characters.
Here are some examples of string literals. All the three forms are identical strings.
"Example"
Defining Constants
There are two simple ways in C to define constants −
▪ Using #define preprocessor.
▪ Using const keyword.
The #define Preprocessor
Given below is the form to use #define preprocessor to define a constant −
#define identifier value
When the above code is compiled and executed, it produces the following result −
value of area : 50
The const Keyword
You can use const prefix to declare constants with a specific type as follows −
const type variable = value;
The following example explains it in detail –
#include <stdio.h>
int main()
{
const int LENGTH = 10;
const int WIDTH = 5;
const char NEWLINE = '\n';
ASCII Escape
Character Result
value Sequence
Hexadecimal number \x
Example program
#include <stdio.h>
int main()
{
printf("Example - Escape sequence");
printf("\a");
printf("\nPlacement class\b");
printf("Tab\tNew line\nWhere it will print?");
printf("\nCarriage return how it works?\rPrinting again");
printf("\nExample of\v Vertical tab!");
printf("\n\'Printing single quotes\'");
printf("\n\"Printing single quotes\"");
printf("\nWhat \f is form feed?");
return 0;
}
Output:
Example - Escape sequence
Placement clasTab New line
Where it will print?
Printing againn how it works?
Example of
Vertical tab!
'Printing single quotes'
"Printing single quotes"
What
is form feed?
4. DATA TYPES
Variables in C are associated with data type. Each data type requires an amount of memory
and performs specific operations.
char 1 %c
float 4 %f
double 8 %lf
unsigned long
at least 4 %lu
int
unsigned long
at least 8 %llu
long int
signed char 1 %c
unsigned char 1 %c
return 0;
}
Output:
CHAR_BIT : 8
CHAR_MAX : 127
CHAR_MIN : -128
INT_MAX : 2147483647
INT_MIN : -2147483648
LONG_MAX : 9223372036854775807
LONG_MIN : -9223372036854775808
SCHAR_MAX : 127
SCHAR_MIN : -128
SHRT_MAX : 32767
SHRT_MIN : -32768
UCHAR_MAX : 255
UINT_MAX : 4294967295
ULONG_MAX : 18446744073709551615
USHRT_MAX : 65535
8. The format identifier ‘%i’ is also used for _____ data type?
a) char
b) double
c) float
d) int
Ans: d (Explanation: Both %d and %i can be used as a format identifier for int data type.)