0% found this document useful (0 votes)
4 views

clanguage

The document contains multiple C programs demonstrating basic operations such as addition, multiplication, division, and swapping of variables. It also explains keywords, constants, data types, and their sizes in C, including primary, derived, and enumerated data types. Additionally, it discusses the use of the 'void' data type and provides examples for better understanding.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

clanguage

The document contains multiple C programs demonstrating basic operations such as addition, multiplication, division, and swapping of variables. It also explains keywords, constants, data types, and their sizes in C, including primary, derived, and enumerated data types. Additionally, it discusses the use of the 'void' data type and provides examples for better understanding.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

/* (1) C Program to add #2 Numbers */

#include <stdio.h>
int main() {
int n1, n2, sum;
printf("Enter two integers: ");
scanf("%d %d", &n1, &n2);
sum = n1 + n2; // calculate the sum
printf("%d + %d = %d", n1, n2, sum);
return 0;
}

/* (2) C Program to Multiply #2 floating data type Numbers */


#include <stdio.h>
int main() {
float a, b, prod;
printf("Enter two numbers: ");
scanf("%f %f", &a, &b);
prod = a * b; // Calculating product
printf("Product = %.2f", prod); // %.2f displays number up to 2 decimal point
return 0;
}

/* (3) C Program to compute quotient and remainder of a division */


#include <stdio.h>
void main() {
int n1, n2, quotient, remainder;
printf("Enter n1: ");
scanf("%d", &n1);
printf("Enter n2: ");
scanf("%d", &n2);
quotient = n1 / n2; // Computes quotient
remainder = n1 % n2; // Computes remainder
printf("Quotient = %d\n", quotient);
printf("Remainder = %d", remainder);
}

/* (4) C Program to swap values in #2 varaiables */


#include<stdio.h>
void main() {
int first, second, temp;
printf("Enter first number: ");
scanf("%lf", &first);
printf("Enter second number: ");
scanf("%d", &second);
printf("\nBefore swapping, first number = %d\n", first);
printf("Before swapping, second number = %d\n", second);
temp = first;
first = second;
second = temp;
printf("\nAfter swapping, first number = %d\n", first);
printf("After swapping, second number = %d", second);
}
Keywords
Keywords in the C programming language are reserved words that have special
meanings and cannot be used as identifiers (names for variables, functions, etc.).
These keywords serve specific purposes in the language. Here is a list of keywords in
the C language:
auto double Int struct
break else long switch
case enum register typedef
char extern return union
const float Short unsigned
continue for signed void
default goto iszeof volatile
do if static while

These keywords are standardized by the C language specifications, and they must be
written in lowercase letters. It's important to note that C is case-sensitive, so auto is
different from Auto or AUTO.

Constants
A constant is a value that remains unchanged during the execution of a program.
Constants can be of various types and they are used to represent fixed values that do
not change during the program's execution. Constants are typically used in programs
to assign names to values that are not expected to change.
Numeric Constants:
Integer Constant: These are whole numbers without a fractional component.
For example: const int myInteger = 42;
Here, myInteger is a constant of type int with a value of 42.
Floating-Point Constants: These are numbers with a fractional component. For
example:
const float myFloat = 3.14;
Here, myFloat is a constant of type float with a value of 3.14.

Character Constant: Character constants are enclosed in single quotes. For example:
const char myChar = 'A';
Here, myChar is a constant of type char with a value of 'A'.
Constants can be defined using the const keyword, which indicates that the value
assigned to the variable should not be changed during the program's execution. The
use of ‘const’ also helps in making the code more readable and self-explanatory, as it
explicitly conveys the intention that the value should not be modified.

Data Types in C
Data types are the type of data stored in a C program. The compiler needs to
understand the type of predefined data it will encounter in the program. A data type
is an attribute that tells a computer how to interpret the value.
C provides several built-in data types, such as integer (int), character (char), floating-
point (float), and double-precision floating-point (double), among others. Each data
type has its own set of possible values and operations that can be performed on it.

Types Of Data Types In C


There are majorly four main categories of Data Type in C:
Data Type Example of Data Type
Integer, Floating-point, double,
Primary Data Type
string.
Derived Data Type Union, structure, array, etc.
Enumerated Data
Enums
Type
Void Data Type Empty Value

Primary Data Types In C


The C programming language has five primitive or primary data types.
1. Integer (int): Refers to positive and negative whole numbers (without decimal),
such as 10, 12, 65, 3400, etc.
Example:
#include <stdio.h>
void main() {
int i = 25;
printf("The integer value is: %d
\n", i);
}

2. Character (char): Refers to all the character sets within single quotes such as ‘a’, ‘A’,
etc.
Example:
#include <stdio.h>
void main() {
char c = 'b';
printf("The character value is: %c
\n", c);
}

3. Floating-point (float): Refers to all the real number values or decimal points, such
as 3.14, 10.09, 5.34, etc.
Example:
#include <stdio.h>
void main() {
float f = 7.2357;
printf("The float value is: %f \n", f);
}
4. Double (double): Used when the range exceeds the numeric values that do not
come under either floating-point or integer data type.
Example:
#include <stdio.h>
void main()
{
double d = 71.2357455;
printf("The double value is: %lf \n", d);
}

Data Type Modifiers In C: Modifiers are C keywords that modify the meaning of
fundamental data types. C Programming Language has four data type modifiers:
 long
 short
 signed
 Unsigned

Size Of Data Types In C


The size of each data type is defined in bits or bytes (8 bits). Each data type in C is
associated with a specific range of values defined as below:
Format Size in
Data Type Range
Specifier bytes
unsigned char %c 0 to 255 1
char %c -128 to 127 1
signed char %c -128 to 127 1
int %d, %i -32,768 to 32,767 2
unsigned int %u 0 to 65,535 2
signed int %d, %i -32,768 to 32,767 (same as int) 2
short int %hd -32,768 to 32,767 2
unsigned short int %hu 0 to 65,535 2
signed short int %hd Same as short int 2
long int %ld, %li -2,147,483,648 to 2,147,483,647 4
signed long int %ld, %li Same as long int 4
unsigned long int %lu 0 to 4,294,967,295 4
float %f 1.7E-38 to 1.7E37 4
double %lf 3.4E-38 to 3.4E37 8
Derived Data Types
Derived data types are primary data types that are grouped together. You can group
many elements of similar data types. These data types are defined by the user. The
following are the derived data types in C:
 Array
 Pointers
 Structure
 Union

Array
An array in C is a collection of multiple values of a similar data type and is stored in
a contiguous memory location. An array can consist of chars, integers, doubles, etc.

Pointer Data Type


The pointer data type is used to store the address of another variable. A pointer can
store the address of variables of any data type.

Structure
It is a data type that can store variables of similar or different data types. For example,
we can use structures to store information about an employee, such as the employee’s
name, employee ID, salary, and more. The ‘struct’ keyword defines a structure.

Union
A union is a group of elements with similar or different data types. In a union, the
memory location is the same for all the elements. Its size will be equal to the memory
required for the largest data type defined. We use the keyword ‘union’ to define a
union.

Enumerated Data Types


Enumerated data types are user-defined data types that consist of integer values.
They are used to define variables that can only assign certain discrete integer values
in the program. They are used to make a program more readable, flexible and
maintainable. We use the keyword ‘enum’ to declare new enumeration types in the
C programming language.
Enum syntax: enum flag {const1, const2, const3………};

Void
The void is just an empty data type that depicts that no value is available. Typically,
the void is used for functions. When we declare a function as void, it doesn’t have to
return anything.
void sum (int a, int b);

You might also like