Professional Documents
Culture Documents
Unit II
Basic of C Programming
10 Marks
Learning outcomes:
Identify the given building block of a C program.
Write simple ‗C‘ program using the given arithmetic
expressions
Write a simple ‗C‘ Program demonstrating the given data
type conversion
Write I/O Statements for the given data.
Alphabets - A, B, ….., Y, Z
a, b, ……, y, z
Digits - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Special symbols - ~ ‗ ! @ # % ^ & * ( ) _ - + = | \ { } [ ] : ; " '
<>,.?/
C Constants:
―A constant is an entity that doesn‘t change during program
execution‖. Types of C Constants
C constants can be divided into two major categories:
1. Primary Constants
2. Secondary Constants
These constants are further categorized as shown in Figure 1.2.
Figure 1.2
426.0
-32.76
-48.5792
Rules for Constructing Character Constants
A character constant is a single alphabet, a single digit or a
single special symbol enclosed within single inverted
commas. The maximum length of a character constant can
be 1 character.
Ex.: 'A'
'I'
'5'
'='
Variable
It‘s an entity that may change during program execution is
called as variable.
Or
It‘s the name given to the memory location known as
variable.
Types of C Variables
Variables can contain integer, real or character constants
hence it‘s devided into three catagories. an integer variable
can hold only an integer constant, a real variable can hold
only a real constant and a character variable can hold only a
character constant. .
Rules for Constructing Variables
1. A variable name is any combination of 1 to 31 alphabets,
digits or underscores. Some compilers allow variable
names whose length could be up to 247 characters.
2. The first character in the variable name must be an
alphabet or underscore.
3. No commas or blanks spaces are allowed within a variable
name.
4. No special symbol other than an underscore (as in
gross_sal) can be used in a variable name.
It‘s compulsory to declare the variable before using it in the
program.
Syntax:
variable_type name_of_variable;
Eg: int add;
float per;
Identifiers:
It‘s is a name used to identify a variable, function, or any
other user defined item
Tokens in C
C program consists of various tokens and a token is either a
keyword, an identifier, a constant, a string literal, or a
symbol. For example, the following C statement consists of
five token.
printf("Hello, World! \n");
token1: printf
token2: (
token3: "Hello, World! \n"
token4: )
token5 ;
Datatypes:
Each variable in C has an associated data type. Each data
type requires different amounts of memory and has some
specific operations which can be performed over it.
For example:
int id;
Here, id is a variable of type integer.
You can declare multiple variable at once in C programming.
float - Floating types
It can hold real numbers such as: 2.34, -9.382, 5.0 etc. You can
declare a floating point variable in C by using
either float or double keyword. For example:
float accountBalance;
double bookPrice;
C Keywords
Keywords are the words whose meaning has already defined
to the compiler. The keywords cannot be used as variable
names because if we do so we are trying to assign a new
meaning to the keyword, which is not allowed by the
computer. The keywords are also called ‗Reserved words‘.
There are only 32 keywords available in C.
Figure: 1.3
Rules for creating C programs:
1. Each instruction in a C program is written as a separate
statement.
2. The statements in a program must appear in the same
order in which we wish them to execute.
3. Blank spaces may be inserted between two words to
improve the readability of the statement. However, no
blank spaces are allowed within a variable, constant or
keyword.
4. All statements are entered in small case letters.
Operators in ‘C’
An operator is a symbol that tells the compiler to perform specific
mathematical or logical functions. C language is rich in built-in
operators and provides the following types of operators:
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Misc Operators
The following table shows all the arithmetic operators supported
by the C language.
printf("Multiplication is %d\n", c );
c = a / b;
printf("Division is %d\n", c );
c = a % b;
printf("Modular division is %d\n", c );
//(final value of c is 1)
c=c++;
printf("After increment %d\n", c );
c=c--;
printf("After Decrement is %d\n", c );
}
When you compile and execute the program, it produces the
following result:
Relational Operators
The following table shows all the relational operators supported
by C.
}
else
{
printf("a is not equal to b\n" );
}
if ( a < b )
{
printf("a is less than b\n" );
}
else
{
printf("a is not less than b\n" );
}
if ( a > b )
{
printf("a is greater than b\n" );
}
else
{
printf("a is not greater than b\n" );
}
/* Lets change value of a and b */
a = 5;
b = 20;
if ( a <= b )
{
printf("a is either less than or equal to b\n" );
}
if ( b >= a )
{
printf("b is either greater than or equal to b\n" );
}
}
When you compile and execute the program, it produces the
following output:
Logical Operators
Following table shows all the logical operators supported by C
language.
printf("Condition is true\n" );
}}
When you compile and execute the program, it produces the
following output:
#include <stdio.h>
main( )
{
Operators Precedence in C
Operator precedence determines how an expression is evaluated.
Certain operators have higher precedence than others; for
example, the multiplication operator has a higher precedence
than the addition operator.
For example, x = 7 + 3 * 2;
here, x is assigned 13, not 20 because operator * has a higher
precedence than +, so it first gets multiplied with 3*2 and then
adds into 7.
Type Conversion:
Type Conversion (also called casting) is a way to convert a
variable from one data type to another data type. For example, if
you want to store a 'long' value into a simple integer then you
can type cast 'long' to 'int'. You can convert the values from one
type to another explicitly using the cast operator as follows −
Syntax:
target_variable_name=(target_type) source_variable_name;
Eg:
b=(float) a;
Consider the following example where the cast operator causes
the division of one integer variable by another to be performed
as a floating-point operation
#include <stdio.h>
main()
{
int a=5;
float b;
b=(float) a;
printf("Value of b: %f\n",b);
}
Output:
Value of b:5.000000
#include<stdio.h>
void main()
{
// defining a variable
int i;
/*
displaying message on the screen
asking the user to input a value
*/
printf("Please enter a value...");
/*
reading the value entered by the user
*/
scanf("%d", &i);
/*
displaying the number as output
*/
printf( "\nYou entered: %d", i);
}
When you will compile the above code, it will ask you to enter a
value. When you will enter the value, it will display the value you
have entered on screen.
The %d inside the scanf() or printf() functions. It is known
as format string and this informs the scanf() function, what type
Format
String Meaning
printf("Enter a character:");
c = getchar();
putchar(c);
}
Output:
Enter a character: f
f
When you will compile the above code, it will ask you to enter a
character, then it will display the character have entered?
gets() & puts() functions
The gets() function reads a line from stdin(standard input) until
either a terminating newline or EOF (end of file) occurs.
The puts() function writes the string str and a trailing newline
to stdout.
#include<stdio.h>
void main()
{
/* character array of length 100 */
char str[100];
printf("Enter a string:");
gets( str );
puts( str );
getch();
}
Output:
Enter a string: gramin
gramin
When you will compile the above code, it will ask you to enter a
string. When you will enter the string, it will display the value you
have entered.
Difference between scanf() and gets()
The main difference between these two functions is
that scanf() stops reading characters when it encounters a space,
but gets() reads space as character too.
Comments in C:
In the C Programming Language, you can place comments in
your source code that are not executed as part of the program.
Single Line Comment:
1. Its used to provide one line descriptor of line.
2. Single Line Comment Can be Placed Anywhere
3. Single Line Comment Starts with ‗//‘
4. Any Symbols written after ‗//‘ are ignored by Compiler
Eg: // This is single line comment
Multiline Comment:
1. Its used to write the description of the program in more than
one line
2. Multi line comment can be placed anywhere.
3. Multi line comment starts with /*.
Important Questions:
1. Explain History of c language?
2. Define terms
i. Identifier.
ii. Token
iii. Operator
3. Explain logical and relational operator .
4. Explain conditional operator with example.
5. What are the rules for creating integer, real constants?
6. Write a program to accept a number from user & display whether it‟s even
or odd.
7. Write a program to take marks for 3 subjects, calculate the percentage &
display it.
8. Explain the variable declaration and definition.
9. Explain formatted input and formatted output statements.
10.Explain arithmetic operators with example.
11.Write a program to find whether the character entered through keyboard is a
vowel or consonant.
12.Explain conditional operator with example.
13.Write a „C‟ program to accept two integer Nos from user and print the
result.
14.What is constant? Give any two examples.
15.State four rules for choosing variable name?
16.State different basic data type in „C‟.
17.Write output of the following program.
void main()
{
char str[5]={„a‟,‟b‟,‟c‟,‟d‟,‟e‟,‟/‟};
printf(“%s”,str);
}
18.Define token? List tokens in ‟C‟.
19.Enlist different format specifiers.
20.Program to calculate sum of 5 subjects and find percentage.
21.Program to swap two no‟s.
22.Program to Check Whether a Character is Vowel or Consonant.
By,
Mr. Jadhav S.B.
Sr. Lecturer GPVN