Day 1 (Fundamentals of C)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Placement Training – Day 1 (Fundamentals of C)

1. WHY TRIGRAPH CHARACTERS WERE USED?


In C language we use different characters on different purposes. Say for example {} are used
to define a scope, [] are used to define dimension to an array, \ is used to write any escape
sequence, # is an initial character to any pre-processor statement, | is a bitwise OR, ^ is a bitwise
XOR and ~ is a negation operator.
Keyboards used with some platforms were missing some of these characters. So it was not
possible to write C code on these machines.
Trigraph characters:
▪ Some of the characters like {}, [], \, |, ~ and ^ are missing in the above keyboard.
Hence practically it may not be possible to write a C program using this
keyboard.
▪ To solve this problem C suggested to use combination of 3 characters to produce
a single character called trigraph character.
▪ A trigraph is a sequence of three characters, the first two of which are question
marks
▪ C supports the following 9 trigraph characters.
Trigraph sequence Equal character
??= #
??( [
??) ]
??/ \
??< {
??> }
??! |
??’ ^
??- ~
▪ The trigraphs preprocessor replaces all occurrences of trigraph sequences by
their single-character equivalents before any other processing.
1. Open the text editor to write the program
>edit prog.c
2. Type the program, save (alt+f, s) and exit (alt+f, x) from the editor

Ramya Devi M AP CSE | Hindusthan College of Engineering and Technology


/* program to print hello world */
??=include<stdio.h>
int main()
??<
printf("Hello??/nWorld");
return 0;
??>

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;
}

2. C - CONSTANTS AND LITERALS


A literal is a value that is expressed as itself. For example, the number 25 or the string "Hello
World" are both literals.
A constant is a data type that substitutes a literal. Constants are useful in situations where
• a specific, unchanging value is to be used at various times during the software
program
• you want to more easily understand the software code
A variable in a program can change its value during the course of execution of the program.
A constant retains the same value throughout the program. Constants can be of any of the
basic data types like an integer constant, a floating constant, a character constant, or a string
literal. There are enumeration constants as well. Constants are treated just like regular
variables except that their values cannot be modified after their definition.

Ramya Devi M AP CSE | Hindusthan College of Engineering and Technology


Constant Literal

const PI = 3.14; var radius = 5;


Example
const v = 0.5; var circumference = 2 * 3.14 * radius;

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 */

Ramya Devi M AP CSE | Hindusthan College of Engineering and Technology


0213 /* octal */
0x4b /* hexadecimal */
0b01 /* binary */
30 /* int */
30u /* unsigned int */
30l /* long */
30ul /* unsigned long */
Example program:
#include <stdio.h>
int main()
{
int a=100;
int b=0144;
int c=0x64a;
int d=0b1100100;

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

Ramya Devi M AP CSE | Hindusthan College of Engineering and Technology


d= 100

Following format specifiers are used:


• %d - to print value in integer format
• %o - to print value in octal format
• %x - to print value in hexadecimal format (letters will print in lowercase)
• %X - to print value in hexadecimal format (letters will print in uppercase)

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.

Ramya Devi M AP CSE | Hindusthan College of Engineering and Technology


o We can use both the signs, i.e., positive and negative, before the mantissa and exponent.
o Spaces are not allowed
Here are some examples of floating-point literals −
3.14159 /* Legal */
314159E-5L /* Legal */
510E /* Illegal: incomplete exponent */
210f /* Illegal: no decimal or exponent */
.e55 /* Illegal: missing integer or fraction */
Guess which are valid and invalid:
1) 45.0
2) -123
3) -345.56
4) 23.67.56
5) 354.78E8
6) -34.78E-4
7) 267e6
8) 12,345.36
9) 0.40E-2
10) $45.32
11)34.
12).786
13)34e2.3

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)

Ramya Devi M AP CSE | Hindusthan College of Engineering and Technology


11)Valid
12)Valid
13)Invalid(exponential part must not have decimal points)

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:

Ramya Devi M AP CSE | Hindusthan College of Engineering and Technology


o It can be represented by specifying a single character within single quotes. For
example, 'a', 'b', etc.
o We can specify the escape sequence character within single quotes to represent a
character literal. For example, '\n', '\a', '\b'.
o We can also use the ASCII in integer to represent a character literal. For example, the
ascii value of 65 is 'A'.
o The octal and hexadecimal notation can be used as an escape sequence to represent a
character literal. For example, '\023', '\0x12'

Guess which are valid and invalid:


1) ‘X’
2) `v`
3) “c”
4) ‘hello’
5) ‘5’
6) ‘=’
7) ‘\n’

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

Ramya Devi M AP CSE | Hindusthan College of Engineering and Technology


return 0;
}
Output:
x= a
y= A
z=

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';

Ramya Devi M AP CSE | Hindusthan College of Engineering and Technology


int area;
area = LENGTH * WIDTH;
printf("value of area : %d", area);
printf("%c", NEWLINE);
return 0;
}
Output:
value of area : 50

3. BACKSLASH CHARACTER CONSTANT OR ESCAPE SEQUENCES


In C Programming Language escape sequence is a special string comprise a backslash (\)
followed by a letter or by a combination of digits used to control output on monitor. Escape
sequences are typically used to represent actions such as newline, carriage returns, tab
movements and non-printing characters over the monitor. The following table lists the common
ANSI escape sequences and their meaning.

ASCII Escape
Character Result
value Sequence

Null 000 \0 Null

Alarm (bell) 007 \a Beep Sound

Back space 008 \b Moves previous position

Horizontal tab 009 \t Moves next horizontal tab

New line 010 \n Moves next Line

Vertical tab 011 \v Moves next vertical tab

Form feed 012 \f Moves initial position of next page

Carriage return 013 \r Moves beginning of the line

Double quote 034 \" Present Double quotes

Single quote 039 \' Present Apostrophe

Question mark 063 \? Present Question Mark

Ramya Devi M AP CSE | Hindusthan College of Engineering and Technology


Back slash 092 \\ Present back slash

Octal number \000

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.

Type Size (bytes) Format Specifier

int at least 2, usually 4 %d, %i

char 1 %c

float 4 %f

Ramya Devi M AP CSE | Hindusthan College of Engineering and Technology


Type Size (bytes) Format Specifier

double 8 %lf

short int 2 usually %hd

unsigned int at least 2, usually 4 %u

long int at least 4, usually 8 %ld, %li

long long int at least 8 %lld, %lli

unsigned long
at least 4 %lu
int

unsigned long
at least 8 %llu
long int

signed char 1 %c

unsigned char 1 %c

at least 10, usually


long double %Lf
12 or 16

To check the size:


#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <float.h>

int main(int argc, char** argv) {

printf("CHAR_BIT : %d\n", CHAR_BIT);


printf("CHAR_MAX : %d\n", CHAR_MAX);
printf("CHAR_MIN : %d\n", CHAR_MIN);
printf("INT_MAX : %d\n", INT_MAX);
printf("INT_MIN : %d\n", INT_MIN);
printf("LONG_MAX : %ld\n", (long) LONG_MAX);
printf("LONG_MIN : %ld\n", (long) LONG_MIN);
printf("SCHAR_MAX : %d\n", SCHAR_MAX);
printf("SCHAR_MIN : %d\n", SCHAR_MIN);
printf("SHRT_MAX : %d\n", SHRT_MAX);

Ramya Devi M AP CSE | Hindusthan College of Engineering and Technology


printf("SHRT_MIN : %d\n", SHRT_MIN);
printf("UCHAR_MAX : %d\n", UCHAR_MAX);
printf("UINT_MAX : %u\n", (unsigned int) UINT_MAX);
printf("ULONG_MAX : %lu\n", (unsigned long) ULONG_MAX);
printf("USHRT_MAX : %d\n", (unsigned short) USHRT_MAX);

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

TCS C MCQ Questions – Variables and Data Types


1. Which of the following is not valid variable name declaration?
a) int __v1;
b) int __1v;
c) int __V1;
d) None
Ans: d

2. Which of the following is not a valid variable name declaration?


a) int _v1;
b) int v_1;
c) int 1_v;
d) int _1v
Ans: c (Explanation:Variable name can’t start with a digit.)

3. Variable names beginning with underscore is not encouraged. Why?


a) It is not standard form
b) To avoid conflicts since assemblers and loaders use such names
c) To avoid conflicts since library routines use such names
d) To avoid conflicts with environment variables of an operating system
Ans: c

4. Which is not a valid C variable name?


a) int number;

Ramya Devi M AP CSE | Hindusthan College of Engineering and Technology


b) float rate;
c) int variable_count;
d) int $main;
Ans: d

5.Which of the following is true for variable names in C?


a) They can contain alphanumeric characters as well as special characters
b) It is not an error to declare a variable to be one of the keywords(like goto, static)
c) Variable names can’t start with a digit
d) Variable can be of any length
Ans: c

6. What will be the output?


#include <stdio.h>
int main()
{
int main = 5;
printf(“%d”, main);
return 0;
}
a) compile-time error
b) run-time error
c) run without any error and prints 5
d) experience infinite looping
Ans: c (Explanation: A C program can have same function name and same variable name.)

7. Which of the following cannot be a variable name in C?


a) friend
b) true
c) volatile
d) export
Ans: c (Explanation: volatile is C keyword)

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

9. Which of the following is a User-defined data type?


a) struct {char name[10], int age};
b) typedef enum {Mon, Tue, Wed, Thu, Fri} Workdays;
c) typedef int Boolean;
d) all of the mentioned
Answer: d

10. What is short int in C programming?


a) Basic datatype of C
b) Qualifier

Ramya Devi M AP CSE | Hindusthan College of Engineering and Technology


c) short is the qualifier and int is the basic datatype
d) All of the mentioned
Ans: c

11. What is the output of this C code?


#include <stdio.h>
int main()
{
signed char chr;
chr = 128;
printf(“%d\n”, chr);
return 0;
}
a) 128
b) -128
c) Depends on the compiler
d) None of the mentioned
Ans: b (Explanation: signed char will be a negative number.)

12. What is the size of an int data type?


a) 4 Bytes
b) 8 Bytes
c) Depends on the system/compiler
d) Cannot be determined
Ans: c

13. Which of the datatypes have size that is variable?


a) int
b) struct
c) float
d) double
Ans: b (Explanation:Since the size of the structure depends on its fields, it has a variable
size.)

14. What is the output of this C code?


#include <stdio.h>
int main()
{
float x = ‘a’;
printf(“%f”, x);
return 0;
}
a) 97.000000
b) run time error
c) a.0000000
d) a
Ans: a (Explanation: Since the ASCII value of a is 97, the same is assigned to the float
variable and printed.)

Ramya Devi M AP CSE | Hindusthan College of Engineering and Technology


15. Which is correct with respect to size of the datatypes?
a) char > int > float
b) int > char > float
c) char < int < double
d) double > char > int
Ans: c

Ramya Devi M AP CSE | Hindusthan College of Engineering and Technology

You might also like