Nothing Special   »   [go: up one dir, main page]

2.C Language BasicinC

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

DEVA Sir (NOTES)

BASICS IN C- LANGUAGE
Mallesham Devasane (Code: DEVA10)
www.Unacademy.com/@devagatecse
DEVA Sir (NOTES)

Basics in C- Language
C-Tokens:
• The smallest individual unit in a c program is known as a token or a lexical
unit.
• C tokens can be classified as follows:
• Keywords
• Identifiers
• Constants
• Strings
• Special Symbols
• Operators
DEVA Sir (NOTES)

Basics in C- Language
Keywords
• Keywords are nothing but system defined words.
• Keywords are reserved words of the language.
• Keywords cannot be used by the programmer as identifiers.
• C is case sensitive, it means these must be used as it is 32 Keywords in C
Programming
• All Keywords must be written in Lower Case.
DEVA Sir (NOTES)

Basics in C- Language
Keywords:
DEVA Sir (NOTES)

Basics in C- Language
Constants
Integer constants
• Integer constant is a numeric constant.
• Example: 100
Floating-point constants
• A floating point constant is a numeric constant that has either a fractional form
or an exponent form.
• Example: 2.0,0.0000234,-0.22E-5
DEVA Sir (NOTES)

Basics in C- Language
Character constants
• A character constant is a constant which uses single quotation around
characters.
• Example: 'a', 'l', 'm', 'F'
DEVA Sir (NOTES)

Basics in C- Language
String constants
• String constants are the constants which are enclosed in a pair of double-quote
marks.
• Example: "good" ,"x", “123”.
• Every sting constant is automatically terminated with a special character ‘\0’
called the null character which represents the end of the string.
• For example, “hello” will represent “hello\0” in the memory.
• The size of the string is the total number of characters plus one for the null
character.
DEVA Sir (NOTES)

Basics in C- Language
Identifiers
• A 'C' program consist of two types of elements: user defined and system
defined.
• An identifier is a word used by a programmer to name a variable , function, or
label.
• Identifiers consist of letters, underscore and digits, in any order, except that the
first character is not digit.
• Both Upper and lowercase letters can be used
Syntax:
int money;
double account Balance;
DEVA Sir (NOTES)

Basics in C- Language
Special Symbols:
• The following special symbols are used in C having some special meaning and thus, cannot be
used for some other purpose.
• [](){},;: …
• Braces{ }: These opening and ending curly braces marks the start and end of a block of code
containing more than one executable statement.
• Parentheses( ): These special symbols are used to indicate function calls and function
parameters.
• Brackets[ ]: Opening and closing brackets are used as array element reference. These indicate
single and multidimensional subscripts.
DEVA Sir (NOTES)

Basics in C- Language
Variables in C
• Variable is a name given to the memory.
• Each variable has a specific type, which determines the following parameters.
• Size and layout of the variable's memory.
• Range of values that can be stored within that memory
• Set of operations that can be applied to the variable
• The name of a variable can be composed of letters, digits, and the underscore
character. It must begin with either a letter or an underscore.
• Upper and lowercase letters are distinct because C is case-sensitive.
DEVA Sir (NOTES)

Basics in C- Language
Variables in C
There are following basic variable types:
Type Description
char Typically a single octet(one byte). This is an integer type.
int The most natural size of integer for the machine.
float A single-precision floating point value.
double A double-precision floating point value.
void Represents the absence of type.
DEVA Sir (NOTES)

Basics in C- Language
Operators:
• C operators are symbols that triggers an action when applied to C variables and other objects.
The data items on which operators act upon are called operands.
• Depending on the number of operands that an operator can act upon, operators can be
classified as follows:
Unary Operators: Those operators that require only single operand to act upon are known as
unary operators.
Binary Operators: Those operators that require two operands to act upon are called binary
operators.
Ternary Operators: These operators requires three operands to act upon.
DEVA Sir (NOTES)

Basics in C- Language
Data Types:
• A program usually contains different types of data types (integer, float,
character etc.) and need to store the values being used in the program.
• C has different data types for different types of data and can be broadly
classified as:
• Primary Data Types
• Secondary Data Types
DEVA Sir (NOTES)

Basics in C- Language
Primary Data Types
Integer Data Types:
• Integers are whole numbers with a range of values, range of values are
machine dependent.
• Generally an integer occupies 2 bytes memory space and its value range
limited to -32768 to +32767 (that is, -215 to +215 -1).
• A signed integer use one bit for storing sign and rest 15 bits for number.
DEVA Sir (NOTES)

Basics in C- Language
Integer Data type:
• To control the range of numbers and storage space, C
has three classes of integer storage namely short int,
int and long int.
• All three data types have signed and unsigned forms.
• A short int requires half the amount of storage than
normal integer.
• Unlike signed integer, unsigned integers are always
positive and use all the bits for the magnitude of the
number.
• Therefore, the range of an unsigned integer will be
from 0 to 65535.
• The long integers are used to declare a longer range
of values and it occupies 4 bytes of storage space.
DEVA Sir (NOTES)

Basics in C- Language
Integer Data Type:
Syntax:
int <variable name>;
int num1;
short int num2;
long int num3;
Example: 5, 6, 100, 2500.
Integer Data Type Memory Allocation:
DEVA Sir (NOTES)

Basics in C- Language
Floating Point Data Types:
• The float data type is used to store fractional numbers
(real numbers) with 6 digits of precision.
• Floating point numbers are denoted by the keyword
float.
• When the accuracy of the floating point number is
insufficient, we can use the double to define the
number.
• The double is same as float but with longer precision
and takes double space (8 bytes) than float.
• To extend the precision further we can use long double
which occupies 10 bytes of memory space.
DEVA Sir (NOTES)

Basics in C- Language
Floating Point Data Types:
Syntax:
float <variable name>;
float num1;
double num2;
long double num3;

Example: 9.125, 3.1254.


Floating Point Data Type Memory Allocation:
DEVA Sir (NOTES)

Basics in C- Language
Character Data Type:
• Character type variable can hold a single character and are declared by using
the keyword char.
• There are signed and unsigned chars; both occupy 1 byte each, but having
different ranges.
• Unsigned characters have values between 0 and 255, signed characters have
values from –128 to 127.
Syntax:
char <variable name>;
char ch = ‘a’;
Example: a, b, g, S, j.
DEVA Sir (NOTES)

Basics in C- Language
Void Type:
• The void type has no values therefore we cannot declare it as variable as we
did in case of integer and float.
• Void type is also called as generic type.
• Void can be used with generic type variables and also when the function has
no return value
• void x; is wrong statement in C. Basic variables can not be used as void type.
• void *p; is correct statement in C. Pointer variable can be used as void and
later it can hold address of any type based on requirement.
DEVA Sir (NOTES)

Basics in C- Language
Secondary Data Types
Array:
• It is a collection of data of similar data type.
• Example: int num [5];
• Reserve a sequence of 5 location of two bytes each for storing integers.
Pointer:
• Pointer is a variable that stores the address of some other variable.
• Example: int *i;
• The above statement declares i as pointer to integer data type.
DEVA Sir (NOTES)

Basics in C- Language
Secondary Data Types
Structure:
A structure is a collection of data of different data types under one name.
Example:
Struct employees
{
char Name[10];
int Age;
int Salary;
};
DEVA Sir (NOTES)

Basics in C- Language
Secondary Data Types
Union:
It is a collection of data of different types sharing common memory space.
Example:
Union item
{
int m;
float x;
char c;
};
DEVA Sir (NOTES)

Basics in C- Language
Enum:
• An enumeration is a user-defined data type that consists of integral constants.
To define an enumeration, keyword enum is used.
• This data types gives us an opportunity to invent your own data type and
define what values the variable of this data type can take.
Syntax:
enum flag { const1, const2, ..., constN };
• Example:
enum colors { red, green, blue, cyan };
colors foreground, background;
DEVA Sir (NOTES)

Basics in C- Language
Enum:
Here the declaration has two parts:
• The first part declare the data type and specifies its possible values.
• The second part declare variable of this data type.
Now we can give the values to these variables:
foreground=red;
background=blue;
But remember we can’t use values that aren’t in the original declaration. Thus,
the following declaration cause error.
foreground=yellow;
DEVA Sir (NOTES)

Basics in C- Language
Modifiers in C:
• In c language Data Type Modifiers are keywords used to change the
properties of current properties of data type. Data type modifiers are
classified into following types.
• long
• short
• unsigned
• Signed
• Modifiers are prefixed with basic data types to modify (either increase or
decrease) the amount of storage space allocated to a variable.
DEVA Sir (NOTES)

Basics in C- Language
Long:
• This can be used to increased size of the current data type to 2 more bytes, which can be
applied on int or double data types.
• For example int occupy 2 byte of memory if we use long with integer variable then it occupy 4
byte of memory.
DEVA Sir (NOTES)

Basics in C- Language
Long Syntax:
long a; --> by default which represent long int.

Short:
• In general int data type occupies different memory spaces for a different
operating system; to allocate fixed memory space short keyword can be used.
Syntax: short int a; --> occupies 2 bytes of memory space in every operating
system
DEVA Sir (NOTES)

Basics in C- Language
Unsigned:
• This keyword can be used to make the accepting values of a data type is
positive data type.
Syntax: unsigned int a =100; // right
unsigned int a=-100; // wrong
DEVA Sir (NOTES)

Basics in C- Language
Signed:
This keyword accepts both negative or positive value and this is default
properties or data type modifiers for every data type.
Example
int a=10; // right
int a=-10; // right
signed int a=10; // right
signed int a=-10; // right
DEVA Sir (NOTES)

Basics in C- Language
Integer types with their storage size and value range:
Type Size (in Bytes) Range
Char (signed) 1 -128 to 127
Unsigned char 1 0 to 255
Int (signed) 2 or 4 -32768 to 32767
or
-2147483648 to
2147483647
Unsigned int 2 or 4 0 to 65535 or
0 to 4294967295
Short 2 -32768 to 32767
Unsigned short 2 0 to 65535
Long 4 -2147483648 to
2147483647
Unsigned long 4 0 to 4294967295
DEVA Sir (NOTES)

Basics in C- Language
Floating types with their storage size and value range:
Type Size (in Range Precision
Bytes)

Float 4 1.2E-38 to 3.4E+38 6 decimal places


double 8 2.3E-308 to 1.7E+308 15 decimal places
Long double 10 3.4E-4932 to 19 decimal places
1.1E+4932
DEVA Sir (NOTES)

Basics in C- Language
Format Specifiers:
• Format Specifiers in C language tells us which type of data to store and which
type of data to print.
• This statement tells us that it is used in only 2 places: 1) In taking Input, and 2)
In displaying Output
• Example:
int x=5;
printf( ” %d “, x );
The Result will be 5
DEVA Sir (NOTES)

Basics in C- Language
Format Specifiers:
• Function printf of c language does not know what it has to print either it is an
integer or float or long variable.
• %d tells the function printf that it is an integer type variable and x in function
printf tells it to print the integer value from the variable named ‘x’.
DEVA Sir (NOTES)

Basics in C- Language
• Format Specifiers:
• The Following are a list of Format Specifiers for different data types in C language
DEVA Sir (NOTES)

Basics in C- Language
First C program using printf & scanf :
printf( ) function in c language:
• In C programming language, printf() function is used to print the “character, string,
float, integer, octal and hexadecimal values” onto the output screen.
• We use printf() function with %d format specifier to display the value of an integer
variable.
• Similarly %c is used to display character, %f for float variable, %s for string variable,
%lf for double and %x for hexadecimal variable.
• To generate a newline, we use “\n” in C printf() statement.
• It returns the number of characters printed on the output device.
DEVA Sir (NOTES)

Basics in C- Language
EXAMPLE:
#include <stdio.h>
int main() {
char c = 'A';
float f = 10.234;
int i = 150;
double d = 20.123456;
printf("Character is %c \n", c);
printf("Float value is %f \n", f);
printf("Integer value is %d\n" , i);
printf("Double value is %lf \n", d);
printf("Octal value is %o \n", i);
printf("Hexadecimal value is %x \n", i);
return 0;
}
DEVA Sir (NOTES)

Basics in C- Language

Output:
Character is A
Float value is 10.234000
Integer value is 150
Double value is 20.123456
Octal value is 226
Hexadecimal value is 96
DEVA Sir (NOTES)

Basics in C- Language
scanf( ) function in c language:
• In C programming language, scanf() function is used to read character, string, numeric data
from keyboard with the predefined format specifiers.
• It returns the number of values read from the input.
• Format specifiers can be used similar to printf except that variables are used along with
address operator &.
• Example:
int x;
scanf(“%d”, &x);
DEVA Sir (NOTES)

Basics in C- Language
Example:
#include <stdio.h>
int main()
{
char ch;
int num;
printf("Enter any character \n");
scanf("%c", &ch);
printf("Entered character is %c \n", ch);
printf("Enter any number \n");
scanf("%d", &num);
printf("Entered number is %d \n", num);
}
DEVA Sir (NOTES)

Basics in C- Language
Output:
Enter any character
a
Entered character is a
Enter any number
123
Entered number is 123
DEVA Sir (NOTES)

Basics in C- Language
Escape Sequence in C:
• An escape sequence in C language is a sequence of characters that doesn't represent itself
when used inside string literal or character.
• It is composed of two or more characters starting with backslash \.
• For example: \n represents new line.
DEVA Sir (NOTES)

Basics in C- Language
List of Escape Sequences in C:
DEVA Sir (NOTES)

Basics in C- Language
Comments in C:
• Comments in C language are used to provide information about lines of code.
• It is widely used for documenting code.
• There are 2 types of comments in C language.
• Single Line Comments
• Multi Line Comments
DEVA Sir (NOTES)

Basics in C- Language
Single Line Comment:
• Single line comments are represented by double slash \\.
Example:
#include<stdio.h>
int main(){
//printing information
printf("Hello C");
return 0;
}
Output:
Hello C
DEVA Sir (NOTES)

Basics in C- Language
Multi Line Comment:
• Multi line comments are represented by slash asterisk \* ... *\.
• It can occupy many lines of code but it can't be nested.
• Multi line can be written anywhere in C code.
Syntax:
/*
code to be commented
*/
DEVA Sir (NOTES)

Basics in C- Language
Multi Line Comment:
Example:
#include<stdio.h>
int main(){
/*printing information
Multi Line Comment*/
printf("Hello C");
return 0;
}
Output:
Hello C
DEVA Sir (NOTES)

Basics in C- Language
Example:
#include <stdio.h>
int main () {
int a, b;
int c=0; // Initializing as part of declaration
float f;
a = 1;
b = 2;
c = a + b;
printf("value of c : %d \n", c);
f = 10.0/3.0;
printf("value of f : %f \n", f);
return 0;
}
Output:
value of c : 3
value of f : 3.333334
DEVA Sir (NOTES)

Basics in C- Language
Example:
#include<stdio.h>
#include<conio.h>
int add(int p, int q); // function declaration
int main(){
int a,b,c ; // local variables declaration
clrscr();
printf(“Enter two numbers\n”);
scanf(”%d%d”,&a,&b);
c=add(a,b);
printf(“\n Sum of %d and %d is %d ,a,b,c”);
getch();
return 0;
}
DEVA Sir (NOTES)

Basics in C- Language
int add (int p, int q) // function definition
{
int result; // local variables declaration
result = p + q;
return (result);
}

Output:
Enter two Numbers
10
20
Sum of 10 and 20 is 30

You might also like