GR 6 C Programming Notes 2019-20
GR 6 C Programming Notes 2019-20
C CHARACTER SET
DIGITS
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
SPECIALCHARACTERS
~ tilde,% percent sign,| vertical bar, @ at symbol, + plus
sign,< less than, _ underscore,- minus sign,>greater than,^
caret , # number sign, = equal to, & ampersand , $ dollar sign,
/slash, ( left parenthesis, * asterisk, \ back slash,)right
parenthesis, ′ apostrophe,:colon,[left bracket," quotation
mark,; semicolon, ] right bracket, !exclamation mark,
, comma, { left flower brace, ? Question mark,. dot
operator,}rightflower brace
WHITESPACECHARACTERS
C TOKENS
KEYWORDS:
Keywords are pre-defined words in a C compiler.
Each keyword is meant to perform a specific function in a C
program.
Since keywords are referred names for compiler, they can’t be used
as variable name.
continue for do if
IDENTIFIERS:
C constants refer to the data items that do not change their value
during the program execution. Constant can belong to any data type.
STRINGS
eg: “Welcome2school”
Operators
eg: +,-,*,\
C PROGRAMMING BASICS: -
Below are the steps to be followed for creating and getting the output
for any C program. This is common to all C programs and there is no
exception whether it’s a very small C program or very large C program.
4. Execute or Run:
Once you execute or run the program you get the output.
Types of errors:-
Syntax errors:
Like English language having grammar, every programming language
has its own set of rules or syntax. Syntax error occurs if the rules of a
programming language are misused. Syntax errors are detected at
compile - time.
Run-time errors:
Run-time errors occur during the execution of a program. It is mainly
caused because of some illegal operation or unavailability of desired
conditions for the execution of the program.
eg: Trying to divide a number by zero, insufficient memory, infinitive
loop etc.
After installing the CodeBlocks you can type and execute your C
program as described below.
A SIMPLE C PROGRAM:
printf()
printf() is used to display your messages to the user. These messages
can be used to request input from a user or to display the result.
Syntax for printing a simple message: printf(“Text to be
displayed”);Whatever we type within the double quotes will be
displayed as such on the screen.
eg: printf(“Enter your age”);
Syntax for printing the values stored inside a variable:
printf(“format specifier”,variable1,variable 2);
Matching format specifier must be specified for printing values inside a
variable. (Referthe list of format specifier giver below) printf() can print
any number of variable in a single statement but every variable should
be separated by comma.
eg. int a=10;
float b=11.5;
printf(“%d%f”, a,b);
Here the value stored inside the variables a and b will be displayed.
List of Format specifier:
%d Integer
%f Float
%lf Double
%c Single character
%s String
Escape sequences in C:
An escape sequence is a series of characters that represents certain
special action. It begins with a backslash character (\). The following is a
list of escape sequences.
\\ prints a backslash
scanf()
scanf() is used to read the input value from the keyword.
Syntax: scanf(“format specifier”, &variable1, &variable2);
eg:int a;
float b;
scanf(“%d %f”, &a, &b);
Note:
C language is case sensitive. For example, printf() and scanf() are
different from Printf() and Scanf(). All characters in printf() and
scanf() functions must be in lower case.
DATA TYPES
Data types in C programming language enables the programmers to
appropriately select the data as per requirements of the program and
the associated operations of handling it.
C provides 5 fundamental data types- int, float, double,char and void.
1. int (integer data type)
Integer data type is used to declare a variable that can store numbers
without a decimal.
eg: int a;
int x,y;
Float data type declares a variable that can store numbers containing a
decimal number.
eg: float c;
Double data type also declares variable that can store floating point
numbers. But it is treated as a distinct data type because it occupies
twice as much memory as float and can store floating point numbers
with much larger range and double precision. Thus, double data type is
also referred to as double precision data type.
eg: double z;
5. void
Unlike other primitive data types in c, void data type does not create
any variable but returns an empty set of values. Thus, we can say that it
stores null.
VARIABLES
A variable is a name given to the memory location which holds the
value of the variable for later use. Each variable in C has a specific data
type, which determines the size of the variable's memory; the range of
values that can be stored within that memory; and the set of
operations that can be applied to the variable. The value of a variable
can be changed later in the program.
RULES FOR NAMING C VARIABLE:
total, sum, address1, age_value are some examples for variable name.
All variables must be declared before they are used. More than one
variable of the same data type can be declared in a single statement.
Syntax: data_type variable _name;
Example: int x; float y; char a,b;
OPERATOR
1. Arithmetic Operators
2. Relational Operators
3. Assignment Operators
4. Increment/Decrement Operators
Arithmetic Operators: -
These are used to perform mathematical calculations like addition,
subtraction, multiplication, division and modulus.
The following table shows all the arithmetic operators supported by the
C language.
Relational Operators: -
< Is less than x<y; Checks if the value of left operand is less
than the value of right operand. If yes, then
the condition becomes true.
Logical Operators: -
Operator Description
&& Called Logical AND operator. If both the
expressions evaluate to True, result is True. If
either expression is False, result is False.
Logical OR (||)
!false: true
!true: false
Assignment Operators: -
Operator Description
Increment/Decrement Operators: -
eg1:
#include <stdio.h>
int main()
{
int x=10, y=10, ans;
ans=++x;
printf("Prefix answer:%d",ans);
ans=y++;
printf("\nPostfix answer:%d",ans);
printf("\nValue of x:%d",x);
printf("\nValue of y:%d",y);
return (0);
}
Output:
Prefix answer:11
Postfix answer:10
Value of x:11
Value of y:11
eg2:
#include <stdio.h>
int main ()
{
int x=4, y=5, ans;
ans=++x + y++;
printf("Result: %d",ans);
return (0);
}
Output:
Result:11
C - Decision Making
Decision making is required when one or more conditions is to be
tested in a program. If the condition is true then a statement or group
of statements will be executed and if the condition is false then
another set of statement or statements will be executed.
Shown below is the general form of a typical decision making structure.
if Statement
if-else Statement
In C language, the if…. else statement is used for decision making.
Syntax:
if (expression)
{
statement(s);
}
else
{
statement(s);
}
The expression after the 'if' is called the condition of the if statement. If
the expression is evaluated to true, the statement(s) following 'if'
clause execute and if the expression evaluates to a false value, the
statement following the 'else' clause execute. See the following
pictorial presentation of if-else statement.
Examples:
1. A program to find the largest of 2 numbers: -
#include <stdio.h>
int main()
{
int x, y;
printf("Enter 2 different integers");
scanf("%d %d",&x,&y);
if(x>y)
printf("The largest of the 2 numbers is %d",x);
else
printf("The largest of the 2 numbers is %d",y);
return(0);
}