C Programming 1st Chapter

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

C programming Easy Learning

Introduction to C Chapter
Basic I/O 1

1. What is program?
A program is a set of instructions used to control the behavior of a machine, often
a computer.
2. What is programming language?

Programming Language: A programming language is a formal


language that specifies a set of instructions that can be used to produce various kinds
of output in a machine. Programming languages generally consist of instructions for
a computer.

3. Why C is more popular?


Or, Write down the salient features of C language.

Salient Features of C language: C has become more popular because of the following
features.
• It is easy to learn.
• It is a structured language.
• It produces efficient program.
• It can handle low level activities.
• It can be compiled on a variety of computer platforms that means it is
platform independent.

4. What is C language.

C programming language is a general-purpose, high-level language. It was originally


developed by Dennis M. Ritchie to develop the UNIX operating system at bell
Laboratory in 1971. C was originally first implemented on the DEC PDP-11 computer in
1972.
The UNIX OS was totally written in C by 1973. In 1978, Brian Kernighan and Dennis
Ritchie produced the first publicly available description of C, now known as the K&R
standard. After that the language was formalized in 1988 by the American National
Standard Institute (ANSI) and its known as ANSI standard.
As it is the successor of B language, So, it was named as C language.
C programming Easy Learning

5. Where, C is used?

C is the most widely used programming language in the world for its high-power
productivity. It is used in different field of computer such as.

• Developing operating system such as Windows, UNIX and LINUX.


• Developing application software such as Word, Excel and FoxPro etc.
• Programming language such as Java, Visual basic etc. all are the products of C.
• Developing print spoolers.
• Developing Network Drivers software.
• Database Management System (DBMS) - MySQL is written by C.

6. What is header file of C?


Header file is a file of C library that contains the declarations and definition of library
function.
Example-stdio.h is the header file that contains the declarations and definition for certain
input or output functions such as printf, scanf etc.

7. What is function? How many types of function. Define and give exmaple.
A function is a well-defined program segment that performs some specific tasks.
There are two types of function in C.

i) Library function / Built- in function.


ii) User defined function.

Library function/Built-in function:The functions whose name and syntax are predefined
that can not be changed by the programmer are called Built-in function. They are also
called library functions because the definition of these functions are stored in the header
files of C library.
Example: printf(), scanf() etc are library fucntions.

User-defined function:
The function that is defined by the user is called user defined function. Using defferent
library functions, a programmer makes user defined function. Ex- main() function is an
user defined function.
C programming Easy Learning

8. Describe the purpose of #define and #include directives. Why shouldn’t these
directives end with semicolon?

#define directive:

1. A # define directive is a preprocessor compiler directive and not a statement.


2. # define lines should not end with semicolon.
3. # define generally written in uppercase so that they are easily distinguished
from lowercase variable name.
4. # define directive are usually placed at the beginning before the main ()
function.
5. # define are not declared in declaration section.
6. General form of the # define is – # define macro_name character_sequence.
There must be one or more space between the macro name and the character
name.
Example:
………
………
#define PERIOD 10
void main ()
{
……….
……….
}

#include directive:

1. A# include directive is a preprocessor compiler directive.


2. # include lines should not end with semicolon.
3. # include directive are usually placed at the beginning before the # define
directive.
4. # include are not declared in declaration section.
5. General form of the # include directive is – # include
Example:
# include
# define N 10
main ()
{
………
………
}
C programming Easy Learning

7. What is symbolic constant? What is the convention to declare a symbolic constant?


Or, Write down the advantages of symbolic constant.

Symbolic Constant: A symbolic constant is a name that substitutes for a sequence of

characters. The characters may represent numeric constant, a character constant or a string

constant. Thus, a symbolic constant allows a name to appear in place of a numeric constant, a

character constant or a string.


Declaration of Symbolic Constant: A symbolic constant is declared as follows:

#define symbolic_name constant_value

Valid examples of constant definitions are

#define PASS_MARK 50

#define MAX 200

#define Pl 3.14159

Symbolic constants are sometimes called constant identifiers. Since the symbolic names are
constants they do not appear in declarations. The following rules apply to the #define statement
which defines a symbolic constant.

1. Symbolic names have the same form as variable names. (Symbolic names are written
in CAPITALS to visually distinguish them from the normal variable names which are
written in lowercase letters. This is only a convention, not a rule.)
2. No blank space between the pound sign and the word define is permitted.
3. #define student must not end with a semicolon.
4. A blank space is required between ^define and symbolic name and between the
symbolic name and constant
5. Symbolic names are not declared for data types.

Advantages of Symbolic Constant:

1. It makes the programs easier to understand and readable.


2. It makes the programs more powerful.
3. It makes the program modification easier.
4. Their use is recommended since, they contribute to the development of clear, orderly
program.
C programming Easy Learning

For example, symbolic constants are more readily identified than the information that they
represent, and the symbolic names usually suggest the significance of their associated data
items.

8. Write the purpose of the qualifiers const and volatile.

Const: When any variable has qualified with const keyword in declaration statement then it is
not possible to assign any value or modify it by the program. But indirectly with the help of
pointer its value can be changed.
When any variable is not qualified by const variable then its default qualifier is not const.
There is not any keyword for not const. Its meaning is that value of variable can be changed
after the declaration statement by the program. Example:

What will be output of following program?

#include<stdio.h>
int main()
{
const int a=5;
a++;
printf(“%d”,a);
return 0;
}

Output:
Compiler error, we cannot modify const variable.

Volatile: When any variable has qualified by volatile keyword in declaration statement then
value of variable can be changed by any external device or hardware interrupt. If any variable
has not qualified by volatile keyword in declaration statement, then compiler will take not
volatile as default quantifier. There is not any special keyword for not volatile. Not volatile
means when any variable has qualified by volatile keyword in declaration statement then
value of variable cannot be changed by any external device or hardware interrupt.
C programming Easy Learning

9. Make a table of all Data types of C language:

The table of all fundamental data types of C language is given below-

Data type Data type Size Range Format Specifier

name (byte)

1 -27 to 27-1

Character char %c

unsigned char 1 0 to 27
Unsigned Character
%c

-215 to 215-1

Decimal Integer int 2/4 %d

-231 to

Long Decimal long 4 231-1 %ld

1.7e – 308 to
1.7e+308
Float float 4 %f

3.4e – 4932 to
1.1e+4932
Double (long float) double 8 %lf

3.4e – 4932

Long Double long double 10 to 1.1e+4932 %lf


C programming Easy Learning

10. What is C token? Classify and briefly describe them?

Or, what do you understand by constant, variable and keywords?


In C language, every meaningful individual unit is called C token.
C has six types of tokens-
1. Keywords/reserved words.
2. Identifier.
3. Constants.
4. Strings.
5. Special Symbols.
6. Operators.
Keywords/reserved words: In c language, some words have specific meaning that cannot
be changed, are called keywords or reserved words.
Example- auto, break, while, for, main, if, else etc.
Identifiers: Identifiers are the name of variables, functions and arrays. They are user
defined name.
Example- int num; Here, num is a variable identifirer.
Constants: The fixed values that cannot change during execution of the program are called
constants.
Example: int a=10; Here, 10 is constant.
Strings: A sequence of characters is called string. It is generally written within a double
quotation.
Example- printf(“Hello world”); Here, ‘Hello world’ is a string.

11. What is variable? How many types of variable?


Variable: A variable is an identifier that can hold a single value and the value can be
changed. When a variable is declared, it allocates a memory space according to its data type.
Depending on the declaration place in the program, variable can be classified into two
categories-
i) Local variable.
ii) Global variable.
Local variable: The variable which is declared inside a function and it cannot be accessed by
other function is called Local variable of that function.
Example:
C programming Easy Learning

#include<stdio.h>
int main()
{
int value;
}
Here value is a local variable of main() function.

Global Variable: The variable which is declared outside of all functions and all functions can
access that variable is called Global variable.
Exaple:
#include<stdio.h>
int mark;
int count()
{
……………….
……………….
}
int main()
{
……………….
………………..
}
Here mark is global variable, in this case, count() and main() both functions can use the variable
mark.
12. Write the convention to write a variable in C.
Convention to write the name of a variable or identifier is given below-
1. First character must be an alphabet or underscore.
2. Must be consist of only letters, digits or underscore.
3. Only first 31 characters are significant.
4. Keyword cannot be used.
5. Must not contain white space.
13. Which of following are invalid variable names and why
I. doubles;
II. Minimum;
III. row total;
C programming Easy Learning

IV. 3rd-row;
V. Name.

14. Discuss the scope of a variable.


Or, what are different storage class? Briefly describe.
Or, discuss the visibility mode of a variable in C.
Storage class: A storage class defines the scope and life- time of variables or function within
a C program.
There following storage classes, which can be used in C program-
1. auto
2. register
3. static
4. extern
Auto storage class: Auto storage class is a default storage class for all local variables. The
compiler allocates separate memory for each automatic (auto) variable of different functions,
even if they have the same name. Use auto specifier make a variable automatic. If there is no
specifier before variable then also the compiler treats it as an automatic variable.
int main()
{
int count;
auto int month;
}
Register storage class: Register storage class is used to allocate space of a variable in a
register instead of RAM.
It can't have the unary '&' operator applied to it (as it does not have a memory location).
Example:
int main()
{
register int miles;
scanf(“%d” , miles);
}
C programming Easy Learning

Static storage class: When a local variable is declared as static, the compiler allocates a
permanent memory space for that variable and when a global variable is declared as static it
is known only by its own file, so it cannot be accessed by another file.

void addition()
{
static int a;
}

Extern storage class: When a variable is needed to use in a file that is declared later in that
file or another file then extern storage class is used.

void main()
{
extern int a, b;
printf(“%d %d”, a,b);
}

int a=10, b=20;


C programming Easy Learning

Basic Structure of C programming:

Include header file section

Global declaration section

main ()
{
Function definition / Function body
}

user-defined function

Function definition / Function body

}
C programming Easy Learning

Rule-1: If we want to give command to the computer to print any character/word


/sentence/ message /sting on the output screen then we have to write-

printf(“ Sentence”);

Example of Rule -1.


1. Write a C program that print the following sentence - Hello world ! I am a new
programmar.

#include<stdio.h>
int main()
{
printf(“Hello world ! I am a new programmar”);
return 0;
}

2. Write a C program that print the following sentence in two line.


Hello world !
I am a new programmar.

#include<stdio.h> Here,
int main() \n - new line
{ \t- tab
printf(“Hello world \n”); \a- alert bell.
printf(“I am a new programmar”); \v- vertically distance.
return 0;
}

Self Task: Write the above program ( program no.2) using the \t, \v, \a.
C programming Easy Learning

Rule-2. If any numerical number/character is needed to a program then we have to declare a


variable.
Variable declaration-
data_type variable_name;
Example: int num;

For multiple variables of same type:

data_type variable1, variable2 …variableN;

Example: int num, count, a, i ;

Rule-3. If we want to give a fixed value in a variable the we have to write


Variable_name = value;
It is known as variable
Example: int num = 10;
initialization
int num =20, count= 30; etc

Rule-4. If we want to give the value of a variable as an input (from keyboard) then we have to
write-

scanf(“specifier” , &variable_name);

For multiple variable: scanf(“specifier” , &variable1, &variable2 , … , &variableN);

Rule-5. If we want to print the value of a variable then we have to write-


printf(“specifier”, variable_name);
C programming Easy Learning

Examples of rule-1,2,3,4 and 5:

4. Write a program that initialize the value of a variable and print that value.

Algorithm / Procedure Program

Step-1: We need a variable. So, we will #include < stidio.h >


declare a variable.
int main()
Step-2: Now we will initialize any value for {
that variable.
int a=45;
Step-3: Since we have to print the value, So,
printf(“The value of a = %d”, a);
now, we will command the computer to print
that value. return 0;
}

5. Write a program that takes two integer number and calculate the addition.

Algorithm / Procedure Program

Step-1: We need two variables of integer type. #include<stdio.h>


so firstly, we will declare two variables.
int main()
Step-2: Now we have to give input for that {
variables from the keyboard. So, we will
command to the computer for taking input. int a, b,sum;
scanf(“%d %d”,&a,&b);
Step-3: Then we have to add that variables
and store in another variable. So, we will sum=a+b;
declare another variable and put the result into
printf(“%d”,sum);
that new variable.
return 0;
Step-4: Now We want to see the result on the
monitor. So, Now, we will command to print }
the value of that variable.
C programming Easy Learning

6. Write a program that take the radius of a circle as an input and calculate the area of
the circle.
Algorithm / Procedure Program

Step-1: We need one variable for radius of the #include<stdio.h>


circle. So, we will declare one variable.
int main()
Step-2: Now, we have to give value to the
{
variable from keyboard. So, we will
command to the computer to take an input. double r, area;
Step-3: Now, we will calculate area of a circle scanf(“%lf ” , &r);
using the formula- area = 3.1416*r*r. so, we
need to declare another variable for storing area=3.1416*r*r;
value of the area. printf(“ %lf ” , area);
Step-4: Now, we want to see the area on the return 0;
monitor screen. So, we will command the
computer to print the area. }

7. Write a program that calculate the area of a rectangle.


Algorithm / Procedure Program
Step-1: We need two variables for height and #include<stdio.h>
width rectangle. So, we will declare two
int main()
variables.
{
Step-2: Now, we want to show a message for
requesting to enter the value of height. int height,width,area.
Step-3: Now, we have to give value to the printf(“ Please Enter height”);
variable from keyboard. So, we will
command to the computer to take an input. scanf(“%d” , &height);

Step-4: Again, we want to show a message for printf(“Please Enter width”);


requesting to enter the value of width. scanf(“%d”, &width);
Step-5: Now, we have to give value to the area=height*width;
variable from keyboard. So, we will
command to the computer to take an input printf(“The area= %d” , area);
return 0;
C programming Easy Learning

Step-6: Now, we will calculate area of a }


rectangle using the formula- area =
height*width.
Step-4: Now, we want to see the area on the
monitor screen. So, we will command the
computer to print the area.

8. Write a program that takes three integer numbers and interchange them.
Algorithm / Procedure Program
Step-1: We need three variables for three #include<stdio.h>
integer numbers. So, we will declare three
int main()
variables.
{
Step-2: We want to see a message for entering
the values of the three variables. So, we will int a,b,c, temp;
command the computer to show the message.
printf(“Please Enter numbers”);
Step-3: Then we have to give values for that
three variables from the keyboard. So, we will scanf(“%d %d %d”,&a,&b,&c);
command the computer to take input. temp=a;
Step-4: Now, we will declare another variable a=b;
and assign the value of the first variable to
that temporary variable. By this way b=c;
interchange the values of that three variables. c=temp;
Step-5: Now, we will print the values of all printf(“%d %d %d”,a,b,c);
variables respectively.
return 0;
}
C programming Easy Learning

9. Write a program to calculate the area of any triangle.


Algorithm / Procedure Program

Formula of area of any triangle: #include<stdio. h>


Area = √(s*(s-a) *(s-b) *(s-c)) #include<math. h>
Where, s = (a + b + c)/2
int main(){
√x = sqrt(x)
float a,b,c;
Step-1: We need three variables. So, we will float s,area;
declare three variables.
printf("Enter size of each sides of triangle");
Step-2: We want to see a message for entering scanf("%f %f %f",&a,&b,&c);
the values of the three variables. So, we will s = (a+b+c)/2;
command the computer to show the message. area = sqrt(s*(s-a) *(s-b) *(s-c));
printf("Area of triangle is: %.3f ", area);
Step-3: Then we have to give values for that printf(“The perimeter of the triangle is : %d”, 2*s )
three variables from the keyboard. So, we will
command the computer to take input. return 0;
}
Step-4: Now, we will declare another variable S
and calculate S. Then calculate the area and store
in another variable.
Step-5: Now, we will print the area and the
parameter of the triangle.

10. Write a program to transform the temperature from Celsius to Fahrenheit.


Formula: C/5= (F-32)/9.

Algorithm / Procedure Program

Step-1: We need two variables for Celsius and #include<stdio.h>


Fahrenheit. So, we will declare two variables.
main()
Step-2: We want to see a message for entering
{
the value of Celsius. So, we will command the
computer to show the message. int C, F;

Step-3: Then we have to give value for Celsius printf(“Please Enter the temperature in Celsius
from the keyboard. So, we will command the Scale”) ;
computer to take input. scanf(“%d” , &C);
C programming Easy Learning

Step-4: Now, we will convert the temperature


from Celsius to Fahrenheit.
Step-5: Now, we will print the result.
}

Self-Task: Write the program using the following library functions

pow(a,b)=ab
abs(-a) = a [Finds absolute value of integer]
cos(ϴ)→Finds the cosine value.
sin(ϴ)→Finds the sine value.
tan(ϴ)→Finds the tangent value.
sqrt(a)→√a.
toascii(a)→Finds ASCII value.
tolower(a)→Finds lower case.
toupper(a)-Finds Upper case.

You might also like