Dmodule 1 Introduction To C
Dmodule 1 Introduction To C
Assignment questions
INTRODUCTION:
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?
Variables in C
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.
VARIABLES ENDS=========================
Explain the various rules for forming identifiers names. Give examples for valid
and invalid identifiers for the same.
Identifiers
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
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
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);
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
Float 4 byte
Double 8 byte
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=====================================================
==
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=====================================
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.
Run-time error
Sometimes the errors exist during the execution-time even after the
successful compilation known as run-time errors.
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
o Semantic error
Semantic errors are the errors that occurred when the statements are
not understandable by the compiler.
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.
1. printf("format string",argument_list);
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