0% found this document useful (0 votes)
46 views18 pages

Dmodule 1 Introduction To C

1. C programming language was developed in 1972 by Dennis Ritchie and can be defined as a mother language, system programming language, procedure-oriented language, and structured programming language. 2. Variables are names used to store data in C and have certain naming rules. Constants are used to store unchanging values and can be defined using the const keyword or #define preprocessor. 3. Identifiers are used to represent programming elements like variables and functions and have rules like starting with a letter or underscore and not being a keyword.

Uploaded by

svam81118
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
46 views18 pages

Dmodule 1 Introduction To C

1. C programming language was developed in 1972 by Dennis Ritchie and can be defined as a mother language, system programming language, procedure-oriented language, and structured programming language. 2. Variables are names used to store data in C and have certain naming rules. Constants are used to store unchanging values and can be defined using the const keyword or #define preprocessor. 3. Identifiers are used to represent programming elements like variables and functions and have rules like starting with a letter or underscore and not being a keyword.

Uploaded by

svam81118
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 18

Module-1

Introduction to C: Introduction to computers, input and output devices,


designing efficient programs. Introduction to C, Structure of C program, Files
used in a C program, Compilers, Compiling and executing C programs,
variables, constants, Input/output statements in C,

Assignment questions

1)Explain the structure of C program in detail. Write a sample program to


demonstrate the components in the structure of C program===imp
2)Explain the various rules for forming identifiers names. Give examples for
valid and invalid identifiers for the same. ===imp
3)What is variable? What are the rules to construct variable and also give
examples for each rule? ===imp
4)Explain constants with simple example program. ===imp
5)Explain all basic derived data types and files and software supported by c
language
6)Draw a flowchart and C program which takes as input p,t,r. Compute the
simple interest and display the result.
7)Define computer. Describe the various types of computers based on speed,
memory and cost.
8)Demonstrate formatted input and output statements in C with suitable
example
9)Discuss different types of error occur in program ===imp
10)Write a short note on the characteristics of a computer
11)Mention various softcopy devices and explain hardcopy devices ===imp
12)Describe any 2 input and output devices ===imp
13)Design an algorithm, flowchart and program to compute area of a circle.
14)Explain the SDLC life cycle for the efficient design of a program with a neat
diagram. .===imp
15)List and explain the functions of major parts of computer.
16)Explain the organization of a computer with the help of a neat block
diagram.( Explain the components of a computer with a neat diagram.)===imp
17)explain applications of a computer and generation of a computer
18)Explain programming paradiagms

INTRODUCTION:

C programming language was developed in 1972 by Dennis Ritchie


C can be defined as
1. Mother language
because most of the compilers, JVMs, Kernels, etc. are written in C
language, and most of the programming languages follow C syntax and c
topics like array string and so on , for example, C++, Java, C#, etc.
2. System programming language
Because by using c we are going to create and develop system oriented
softwares.
3. Procedure-oriented programming language
Because A procedural language specifies a series of steps for the program
to solve the problem.
A procedural language breaks the program into functions, data
structures, etc. C is a procedural language. In C, variables and function
prototypes must be declared before being used
4. Structured programming language
Because we break the program into parts using functions. It makes the
program easier to understand and modify.
5. Mid-level programming language
because it supports the feature of both low-level and high-level languages.
low-level is machine dependent, fast to run. But it is not easy to
understand. high-level is machine independent. It is easy to understand.

Features of C Language

1. Simple
C is a simple language in the sense that it provides a structured
approach (to break the problem into parts), the rich set of library
functions, data types, etc.
2. Machine Independent or Portable
c programs can be executed on different machines with some machine
specific changes. Therefore, C is a machine independent language.
3. Mid-level programming language
C is intended to do low-level programming. It is used to develop system
applications such as kernel, driver, etc. It also supports the features of a
high-level language. That is why it is known as mid-level language.
4. structured programming language
C is a structured programming language in the sense that we can break
the program into parts using functions. So, it is easy to understand and
modify. Functions also provide code reusability.
5. Rich Library
C provides a lot of inbuilt functions that make the development fast.
6. Memory Management
It supports the feature of dynamic memory allocation. In C language, we
can free the allocated memory at any time by calling the free() function.
7. Fast Speed
The compilation and execution time of C language is fast since there are
lesser inbuilt functions and hence the lesser overhead.
8. Pointers
C provides the feature of pointers. We can directly interact with the
memory by using the pointers. We can use pointers for memory,
structures, functions, array, etc.
9. Recursion
In C, we can call the function within the function. It provides code
reusability for every function. Recursion enables us to use the approach
of backtracking.
10. Extensible
C language is extensible because it can easily adopt new features.

Use of C Language?

C has a wide range of real-world applications, including the following:


1. Development of video games
2. Applications using graphical user interfaces
3. Databases and computer operating systems
4. Browsers on the internet
5. Computational and graphical methods
6. Banking
7. Cloud computing and distributed systems
8. Compilers
9. Embedded systems are systems that are integrated into a larger system.
10. Integrated software libraries for enterprises
11. Server applications on a large scale
12. Compilers of code
13. Jvm jdk jre are developed by c
14. Kernel, os, drivers are developed by c
15. Financial application development, like trading apps
16. Embedded systems, systems application
What is variable? What are the rules to construct variable and also give
examples for each rule?

Variables in C

A variable is a name of the memory location. It is used to store data. Its


value can be changed, and it can be reused many times.

syntax
type variable_list;

EXAMPLE PROGRAM
#include <stdio.h>
int main() {
int a=10;
char b='A';
float c=2.2;
printf("%d\n",a);
printf("%c\n",b);
printf("%f\n",c);
return 0;
}

OUTPUT
10
A
2.200000
Here, a, b, c are variables. The int, float, char are the data types.

Rules for defining variables

o A variable can have alphabets, digits, and underscore.


o A variable name can start with the alphabet, and underscore only. It can't
start with a digit.
o No whitespace is allowed within the variable name.
o A variable name must not be any reserved word or keyword, e.g. int, float,
etc.
Valid variable names:
1. int a;
2. int _ab;
3. int a30;

Invalid variable names:


1. int 2;
2. int a b;
3. int long;

VARIABLES ENDS=========================

Explain the various rules for forming identifiers names. Give examples for valid
and invalid identifiers for the same.

Identifiers

identifier is a collection of alphanumeric characters that begins either with an


alphabetical character or an underscore, which are used to represent various
programming elements such as variables, functions, arrays, structures, unions,
labels, etc.

Rules for constructing C identifiers


o The first character of an identifier should be either an alphabet or an
underscore, and then it can be followed by any of the character, digit, or
underscore.
o It should not begin with any numerical digit.
o In identifiers, both uppercase and lowercase letters are distinct.
Therefore, we can say that identifiers are case sensitive.
o Commas or blank spaces cannot be specified within an identifier.
o Keywords cannot be represented as an identifier.
o The length of the identifiers should not be more than 31 characters.
o Identifiers should be written in such a way that it is meaningful, short,
and easy to read.
o
Example of valid identifiers
1. total, sum, average, _m _, sum_1, etc.
Example of invalid identifiers
1. 2sum (starts with a numerical digit)
2. int (reserved word)
3. char (reserved word)
4. m+n (special character, i.e., '+')
identifiers ENDS ====================

Explain constants with simple example program.

Constants
constants are essential because they give us a mechanism to store unchanging
values that hold true throughout the course of the program.

List of Constants in C
Constant Example

Decimal Constant 10, 20, 450 etc.

Real or Floating-point 10.3, 20.2, 450.6 etc.


Constant

Octal Constant 021, 033, 046 etc.

Hexadecimal Constant 0x2a, 0x7b, 0xaa etc.

Character Constant 'a', 'b', 'x' etc.

String Constant "c", "c program", "c is mother


language" etc.

2 ways to define constant in C


There are two ways to define constant in C
1. const keyword
2. #define preprocessor

1. #include<stdio.h>
2. int main(){
3. const float PI=3.14;
4. printf("The value of PI is: %f",PI);
5. return 0;
6. }
Output:
The value of PI is: 3.140000

BY USING #define preprocessor

1. #include <stdio.h>
2. #define PI 3.14
3. main() {
4. printf("%f",PI);
5. }
Output:

3.140000

Decimal Constant
A whole number represented in base 10 is known as a decimal constant. It has
digits that range from 0 to 9
Octal Constant:
A base 8 value is represented by an octal constant. It is prefixed with a '0'
(zero) to show that it is an octal constant and has digits ranging from 0 to 7.
String Constant:
A series of characters wrapped in double quotes is represented by a string
constant. It is a character array that ends with the null character \0.
Real or Floating-Point Constant:
A fractional component or exponentiation of a number is represented by a real
or floating-point constant. It can be expressed with a decimal point, the
letter "E", or the symbol "e" in exponential or decimal notation.
Hexadecimal Constant:
A base-16 value is represented by a hexadecimal constant. It uses letters A to
F (or a to f) and numbers 0 to 9 to represent values from 10 to 15. It is prefixed
with '0x' or '0X' to identify it as a hexadecimal constant.
Character Constant
A character constant represents a single character that is enclosed in single
quotes.
Example program

#include <stdio.h>
int main() {
int decimal = 42;
float real = 3.14;
int hexadecimal = 0x2A; // Hexadecimal representation of decimal 42
char string[] = "Hello, World!";
printf("The string constant is: %s\n", string);

printf("The hexadecimal constant is: %x\n", hexadecimal);


int octal = 052; // Octal representation of decimal 42
printf("The octal constant is: %o\n", octal);
char character = 'A';
printf("The character constant is: %c\n", character);
printf("The real constant is: %f\n", real);
printf("The decimal constant is: %d\n", decimal);
return 0;
}

Output
The string constant is: Hello, World!
The hexadecimal constant is: 2a
The octal constant is: 52
The character constant is: A
The real constant is: 3.140000
The decimal constant is: 42The decimal constant is: 42

CONSTANTS END=======================================

Explain all basic and derived data types and files and software supported by
c language

Data Types
A data type specifies the type of data that a variable can store such as integer,
floating, character, etc.
There are the following data types in C language.
Types Data Types

Basic Data Type int, char, float, double

Derived Data Type array, pointer, structure, union

Enumeration Data Type enum

Void Data Type void


The basic data types are integer-based and floating-point based. C language
supports both signed and unsigned literals.
Let's see the basic data types. Its size is given according to 32-bit architecture.
Data Types Memory Size Range

Char 1 byte −128 to 127

signed char 1 byte −128 to 127

unsigned char 1 byte 0 to 255

Short 2 byte −32,768 to 32,767

signed short 2 byte −32,768 to 32,767

unsigned short 2 byte 0 to 65,535

Int 2 byte −32,768 to 32,767


signed int 2 byte −32,768 to 32,767

unsigned int 2 byte 0 to 65,535

short int 2 byte −32,768 to 32,767

signed short int 2 byte −32,768 to 32,767

unsigned short int 2 byte 0 to 65,535

long int 4 byte -2,147,483,648 to 2,147,483,647

signed long int 4 byte -2,147,483,648 to 2,147,483,647

unsigned long int 4 byte 0 to 4,294,967,295

Float 4 byte

Double 8 byte

long double 10 byte


Int:
Integers are entire numbers without any fractional or decimal parts, and the int
data type is used to represent them.
Char:
Floating numbers can be used to represent fractional units or numbers with
decimal places.
Individual characters are represented by the char data type.
Float:
To represent integers, use the floating data type. Floating numbers can be used
to represent fractional units or numbers with decimal places.

example

#include <stdio.h>
int main() {
int a=10;
char b='A';
float c=2.2;
double d= 3.141593;
printf("%d\n",a);
printf("%c\n",b);
printf("%f\n",c);
printf("%lf\n",d);
return 0;
}

Output
10
A
2.200000
3.141593
Derived Data Type
C also supports derived data types, including arrays, pointers,
structures, and unions. These data types give programmers the ability to handle
heterogeneous data, directly modify memory, and build complicated data
structures.
Array:
An array, a derived data type, it is used to store a sequence of fixed-size
elements of the same type. Elements we can store.
The index is used to access the elements of the array, with a 0 index for the first
entry. The size of the array is fixed at declaration time and cannot be changed
during program execution. The array components are placed in adjacent
memory regions.
Pointer:
A pointer is a derived data type that keeps track of another data type's memory
address. When a pointer is declared, the data type it refers to is stated first, and
then the variable name is preceded by an asterisk (*). You can have incorrect
access and change the value of variable using pointers by specifying the memory
address of the variable. Pointers are commonly used in tasks such as function
pointers, data structures, and dynamic memory allocation.
Structure:
A structure is a derived data type that enables the creation of composite data
types by allowing the grouping of many data types under a single name. It gives
you the ability to create your own unique data structures by fusing together
variables of various sorts.
1. A structure's members or fields are used to refer to each variable within
it.
2. Any data type, including different structures, can be a member of a
structure.
3. A structure's members can be accessed by using the dot (.) operator.
Union:
A derived data type called a union enables you to store various data types in the
same memory address. In contrast to structures, where each member has a
separate memory space, members of a union all share a single memory space.
A value can only be held by one member of a union at any given moment. When
you need to represent many data types interchangeably, unions come in handy.
Like structures, you can access the members of a union by using the dot
(.) operator.
Header file
A header file is a file with extension .h which contains C function declarations
and macro definitions to be shared between several source files. There are two
types of header files: the files that the programmer writes and the files that
comes with your compiler.
It helps to reduce the complexity and number of lines of code. It also gives you
the benefit of reusing the functions that are declared in header files to different
.
Compiler is a software which converts c language source code into object code
that is in the binary format.
Linker is a software which takes object code and header files and other
program related files as input converts executable file.
Loader is a software which loads the executable file on to the file for execution
purpose.
FILES, DATA TYPES, SOWTWARES
ENDS=====================================================
==

Explain the structure of C program in detail. Write a sample program to


demonstrate the components in the structure of C program

EXPLAIN Structure of a c program


Include header file section
Global declaration section
/* comments*/
Main()
{
/* comments*/
Declaration part
Executable part
}
User-defined functions
{
Code part
}
Example program
1. #include <stdio.h>
2. int main(){
3. clrscr();
4. printf("Hello C Language");
5. return 0;
6. }
Output
Hello C Language

Include header file section


In this section we are going to define all most all inbuilt functions that are used
in c program. These files ends with extension .h
#include <stdio.h> includes the standard input output library functions. The
printf() and scanf() functions are defined in stdio.h .
Global declaration section
If any variable declare outside of all the function that variable we technically
called as global variable. Global variable can be used by all the functions. This
can be done under Global declaration section.
/* comments*/

The comments in C are human-readable explanations or notes in the source


code of a C program. A comment makes the program easier to read and
understand. These are the statements that are not executed by the compiler or
an interpreter.
We are going to write comment between /* and */

int main() after main we are going to write empty opening and closing
parentheses after that inside opening and closing braces we are going to define
Declaration part and Executable part
The main() function is the entry point of every program in c language.
Declaration part
Here we are going to declare and defining the variable that are used in the
executable part

executable part
here we are going to execute the statements
User-defined functions
In the c program we are going to define n number of user defined functions.
These functions can be defined by user. It is optional and it can be defined
before and after the main function
EXPLAIN Structure of a c program
END=====================================

There are mainly five types of errors exist in C programming:


Errors are the problems or the faults that occur in the program, which
makes the behavior of the program abnormal, Programming errors are
also known as the bugs or faults, and the process of removing these
bugs is known as debugging.

o Syntax error
o Run-time error
o Linker error
o Logical error
o Semantic error

Syntax error

syntax errors are thrown by the compilers. These errors are mainly
occurred due to the mistakes while typing or do not follow the syntax
of the specified programming language.
Example
1. int a; // this is the correct form
2. Int a; // this is an incorrect form.

Commonly occurred syntax errors are:

o If we miss the parenthesis (}) while writing the code.


o Displaying the value of a variable without its declaration.
o If we miss the semicolon (;) at the end of the statement.

Run-time error

Sometimes the errors exist during the execution-time even after the
successful compilation known as run-time errors.

The division by zero is the common example of the run-time error.


Linker error

Linker errors are mainly generated when the executable file of the
program is not created. This can be happened either due to the wrong
function prototyping or usage of the wrong header file. For example,
the main.c file contains the sub() function whose declaration and
definition is done in some other file such as func.c. During the
compilation, the compiler finds the sub() function in func.c file, so it
generates two object files, i.e., main.o and func.o. At the execution
time, if the definition of sub() function is not found in the func.o file,
then the linker error will be thrown. The most common linker error that
occurs is that we use Main() instead of main().

Logical error

The logical error is an error that leads to an undesired output. These


errors produce the incorrect output, but they are error-free, known as
logical errors.

o Semantic error
Semantic errors are the errors that occurred when the statements are
not understandable by the compiler.

The following can be the cases for the semantic error:

Typecompatibility
int b = "javatpoint";

Arrayindexoutofbound

inta[10];
a[10] = 34;

Input/output statements in C,
printf() and scanf() in C

The printf() and scanf() functions are used for input and output in C
language. Both functions are inbuilt library functions, defined in stdio.h
(header file).

printf() function

The printf() function is used for output. It prints the given statement to
the console.

The syntax of printf() function is given below:

1. printf("format string",argument_list);

The format string can be %d (integer), %c (character), %s (string), %f


(float) etc.

scanf() function

The scanf() function is used for input. It reads the input data from the
console.
1. scanf("format string",argument_list);
Program to print sum of 2 numbers
1. #include<stdio.h>
2. int main(){
3. int x=0,y=0,result=0;
4.
5. printf("enter first number:");
6. scanf("%d",&x);
7. printf("enter second number:");
8. scanf("%d",&y);
9.
10. result=x+y;
11. printf("sum of 2 numbers:%d ",result);
12.
13. return 0;
14. }

Output

enter first number:9


enter second number:9
sum of 2 numbers:18

You might also like