Programming in C: Reema Thareja
Programming in C: Reema Thareja
Programming in C: Reema Thareja
Reema Thareja
#include<stdio.h>
int main()
{
printf("\n Welcome to the world of C ");
return 0;
}
FILES USED IN A C PROGRAM
Files in a C program
DATA TYPES IN C
VARIABLES IN C
• A variable is defined as a meaningful name given to the data storage location in computer memory.
• When using a variable, we actually refer to address of the memory where the data is stored. C language
supports two basic kinds of variables.
• Numeric variables can be used to store either integer values or floating point values.
• While an integer value is a whole numbers without a fraction part or decimal point, a floating point
number, can have a decimal point in them.
• Numeric values may also be associated with modifiers like short, long, signed and unsigned.
• By default, C automatically a numeric variable signed..
• Character variables can include any letter from the alphabet or from the ASCII chart and numbers 0 – 9
that are put between single quotes.
Variables
To declare a variable specify data type of the variable followed
by its name.
Variable names should always be meaningful and must reflect
the purpose of their usage in the program.
Character Variables
Variable declaration always ends with a semicolon. Example, Numeric Variable
int emp_num;
float salary;
char grade;
double balance_amount;
unsigned short int acc_no;
CONSTANTS
• Constants are identifiers whose value does not change.
• Constants are used to define fixed values like PI or the charge on an electron so that their value does not
get changed in the program even by mistake.
• To declare a constant, precede the normal variable declaration with const keyword and assign it a value.
For example,
const float pi = 3.14;
• Another way to designate a constant is to use the pre-processor command define.
#define PI 3.14159
When the preprocessor reformats the program to be compiled by the compiler, it replaces each defined
name with its corresponding value wherever it is found in the source program. Hence, it just works like
the Find and Replace command available in a text editor.
Streams in a C
program
Monitor Data
THE PRINTF() FUNCTION
• The printf function is used to display information required to the user and also prints the values of the
variables. Its syntax can be given as
printf (“conversion string”, variable list);
• The parameter control string is a C string that contains the text that has to be written on to the standard
output device. The prototype of the control string can be given as below
%[flags][width][.precision][length]specifier
length Description
flag description
h When the argument is a short int or unsigned short int.
Left-justify within the data given field
- When the argument is a long int or unsigned long int for
width l
integer specifiers.
Displays the data with its numeric sign
+ L
When the argument is a long double (used for floating
(either + or -) point specifiers)
Used to provide additional specifiers
like o, x, X, 0, 0x or 0X for octal and
#
hexa decimal values respectively for specifier Qualifying Input
values different than zero.
c For single character
The number is left-padded with zeroes For decimal values
0 d
(0) instead of spaces
F For floating point numbers
E, e Floating point numbers in exponential format
G, G Floating point numbers in the shorter of e format
o For Octal number.
s For a sequence of (string of) characters
u For Unsigned decimal value
x ,X For Hexadecimal value.
THE SCANF() FUNCTION
• The scanf() is used to read formatted data from the keyboard. The syntax of the scanf() can be given as,
scanf (“control string”, arg1, arg2, ………….argn);
• The control string specifies the type and format of the data that has to be obtained from the keyboard and stored in the memory locations
pointed by the arguments arg1, arg2,…, argn. The prototype of the control string can be give as:
[=%[*][width][modifiers]type=]
• * is an optional argument that suppresses assignment of the input field. That is, it indicates that data should be read from the stream but ignored
(not stored in the memory location).
• width is an optional argument that specifies the maximum number of characters to be read.
• modifiers is an optional argument that can be h, l or L for the data pointed by the corresponding additional arguments. Modifier h is used for
short int or unsigned short int, l is used for long int, unsigned long int or double values. Finally, L is used long double data values.
• Type is same as specifier in printf()
• EXAMPLE OF printf() and scanf():
• int num;
• float fnum;
• char ch, str[10];
• double dnum;
• short snum;
• long int lnum;
• printf(“\n Enter the values : “);
• scanf("%d %f %c %s %e %hd %ld", &num, &fnum, &ch, str, &dnum, &snum, &lnum);
• printf("\n num = %d \n fnum = %.2f \n ch = %c \n str = %s \n dnum = %e \n snum = %hd \n lnum = %ld", num, fnum, ch, str, dnum, snum,
lnum);
OPERATORS IN C
• C language supports a lot of operators to be used in expressions. These operators can be
categorized into the following major groups:
• Arithmetic operators
• Relational Operators
• Equality Operators
• Logical Operators
• Unary Operators
• Conditional Operators
• Bitwise Operators
• Assignment operators
• Comma Operator
OPERATION OPERATOR SYNTAX COMMENT RESULT
• Sizeof Operator
Multiply * a * b result = a * b 27
Divide / a / b result = a / b 3
Addition + a + b result = a + b 12
ARITHMETIC
Subtraction - a - b result = a – b 6
OPERATORS
Modulus % a % b result = a % b 0
RELATIONAL OPERATORS
Also known as a comparison operator, it is an operator that compares two values. Expressions that
contain relational operators are called relational expressions. Relational operators return true or
false value, depending on whether the conditional relationship between the two operands holds or
not.
OPERATOR MEANING EXAMPLE
EQUALITY OPERATORS
• C language supports two kinds of equality operators to compare their operands for strict equality or
inequality. They are equal to (==) and not equal to (!=) operator.
•
The equality operators have lower precedence than the relational operators.
OPERATOR MEANING
A B A &&B A B A || B A !A
0 0 0 0 0 0
0 1
0 1 0 0 1 1
1 0 0 1 0 1
1 0
1 1 1 1 1 1
UNARY OPERATORS
Unary operators act on single operands. C language supports three unary operators. They are
unary minus, increment and decrement operators.
When an operand is preceded by a minus sign, the unary operator negates its value.
The increment operator is a unary operator that increases the value of its operand by 1. Similarly,
the decrement operator decreases the value of its operand by 1. For example,
int x = 10, y;
y = x++;
is equivalent to writing
y = x;
x = x + 1; whereas, y = ++x;
is equivalent to writing
x = x + 1;
y = x;
CONDITIONAL OPERATOR
• The conditional operator operator (?:) is just like an if .. else statement that can be written within
expressions.
• The syntax of the conditional operator is
exp1 ? exp2 : exp3
Here, exp1 is evaluated first. If it is true then exp2 is evaluated and becomes the result of the expression,
otherwise exp3 is evaluated and becomes the result of the expression. For example,
large = ( a > b) ? a : b
• Conditional operators make the program code more compact, more readable, and safer to use as it is
easier both to check and guarantee that the arguments that are used for evaluation.
• Conditional operator is also known as ternary operator as it is neither a unary nor a binary operator; it
takes three operands.
BITWISE OPERATORS
• Bitwise operators perform operations at bit level. These operators include: bitwise AND, bitwise OR,
bitwise XOR and shift operators.
• The bitwise AND operator (&) is a small version of the boolean AND (&&) as it performs operation on
bits instead of bytes, chars, integers, etc.
• The bitwise OR operator (|) is a small version of the boolean OR (||) as it performs operation on bits
instead of bytes, chars, integers, etc.
• The bitwise NOT (~), or complement, is a unary operation that performs logical negation on each bit of
the operand. By performing negation of each bit, it actually produces the ones' complement of the given
binary value.
• The bitwise XOR operator (^) performs operation on individual bits of the operands. The result of XOR
operation is shown in the table
• The assignment operator is responsible for assigning values to the variables. While the equal sign (=) is the
fundamental assignment operator, C also supports other assignment operators that provide shorthand ways to
represent common variable assignments. They are shown in the table.
SIZEOF OPERATOR
• sizeof is a unary operator used to calculate the sizes of data types.
• It can be applied to all data types.
• The operator returns the size of the variable, data type or expression in bytes.
• 'sizeof' operator is used to determine the amount of memory space that the
variable/expression/data type will take. For example,
• sizeof(char) returns 1, that is the size of a character data type. If we have,
int a = 10;
unsigned int result;
result = sizeof(a);
then result = 2,
TYPE CONVERSION AND TYPE CASTING
• Type conversion and type casting of variables refers to changing a variable of one data type into
another.
• While type conversion is done implicitly, casting has to be done explicitly by the programmer. We will
discuss both of them here.
• Type conversion is done when the expression has variables of different data types. So to evaluate the
expression, the data type is promoted from lower to higher level where the hierarchy of data types can
be given as: double, float, long, int, short and char.
• For example, type conversion is automatically done when we assign an integer value to a floating point
variable. For ex,
float x;
int y = 3;
x = y;
Now, x = 3.0,
• Type casting is also known as forced conversion. It is done when the value of a higher data type has to be
converted in to the value of a lower data type. For example, we need to explicitly type cast an integer
variable into a floating point variable.
float salary = 10000.00;
int sal;
sal = (int) salary;
• Typecasting can be done by placing the destination data type in parentheses followed by the variable name
that has to be converted.