0% found this document useful (0 votes)
5 views23 pages

XII - C LANGUAGE PROGRAMMING NOTES

The document provides an overview of the C character set, including alphabets, digits, special characters, keywords, identifiers, data types, variables, constants, escape sequences, input/output functions, and comments. It outlines rules for naming identifiers and variables, explains data types like int, float, double, and char, and describes the use of printf() and scanf() for output and input respectively. Additionally, it covers the concept of comments in C programming, detailing single-line and multi-line comments.

Uploaded by

askariraza676
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)
5 views23 pages

XII - C LANGUAGE PROGRAMMING NOTES

The document provides an overview of the C character set, including alphabets, digits, special characters, keywords, identifiers, data types, variables, constants, escape sequences, input/output functions, and comments. It outlines rules for naming identifiers and variables, explains data types like int, float, double, and char, and describes the use of printf() and scanf() for output and input respectively. Additionally, it covers the concept of comments in C programming, detailing single-line and multi-line comments.

Uploaded by

askariraza676
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/ 23

The C Character set:

A character set is a set of alphabets, letters and some special characters that are valid in C language.

Alphabets:

Uppercase: A B C ................................... X Y Z

Lowercase: a b c ...................................... x y z

C accepts both lowercase and uppercase alphabets as variables and functions.

Digits:

0 1 2 3 4 5 6 7 8 9

Special Characters:
Special Characters in C Programming
, < > . --

( ) ; $ :

% [ ] # ?

‘ & { } “

^ ! * / |

- \ ~ +

White space Characters:


Blank space, newline, horizontal tab, carriage return and form feed.

C Keywords:
Keywords are predefined, reserved words used in programming that have special meanings to the compiler.
Keywords are part of the syntax and they cannot be used as an identifier. For example:

int money;

Here, int is a keyword that indicates money is a variable of type int (integer). As C is a case sensitive language, all
keywords must be written in lowercase. Here is a list of all keywords allowed in ANSI C.
C Keywords
auto double int struct

break else long switch

case enum register typeof

char extern return union

continue for signed void

do if static while

default goto sizeof volatile

const float short unsigned

All these keywords, their syntax, and application will be discussed in their respective topics.

C Identifiers:
Identifier refers to name given to entities such as variables, functions, structures etc.

Identifiers must be unique. They are created to give a unique name to an entity to identify it during the execution of
the program. For example:

int money;

double accountBalance;

Here, money and accountBalance are identifiers.

Also remember, identifier names must be different from keywords. You cannot use int as
an identifier because int is a keyword. int money;
double accountBalance;

Here, money and accountBalance are identifiers.

Also remember, identifier names must be different from keywords. You cannot use int as an identifier because int is
a keyword.

Rules for naming identifiers:


1. A valid identifier can have letters (both uppercase and lowercase letters), digits and underscores.
2. The first letter of an identifier should be either a letter or an underscore.
3. You cannot use keywords like int, while etc. as identifiers.
4. There is no rule on how long an identifier can be. However, you may run into problems in some compilers if
the identifier is longer than 31 characters.

You can choose any name as an identifier if you follow the above rule, however, give meaningful names to identifiers
that make sense.

C Data Types:
In C programming, data types are declarations for variables. This determines the type and size of data associated
with variables. For example,

int myVar;

Here, myVar is a variable of int (integer) type. The size of int is 4 bytes.

Basic types:
Here's a table containing commonly used types in C programming for quick access.

Type Size (Bytes) Format Specifier


int atleast 2, usually 4 %i, %d
char 1 %c
float 4 %f
double 8 %lf

short int 2 usually %hd

unsigned int atleast 2, usually 4 %u


long int atleast 4, usually 8 %ld, %li
long long int atleast 8 %lld, %lli
unsigned long int atleast 4 %lu

Unsigned long long int atleast 8 %llu

signed char 1 %c
unsigned char 1 %c

long double atleast 10, usually 12 06 16 %Lf

Int:
Integers are whole numbers that can have both zero, positive and negative values but no decimal values. For
example, 0, -5, 10
We can use int for declaring an integer variable.

int id;

Here, id is a variable of type integer.

You can declare multiple variables at once in C programming. For example,

int id, age;

The size of int is usually 4 bytes (32 bits). And, it can take 232 distinct states from -2147483648 to 2147483647.

float and double:


float and double are used to hold real numbers.

float salary;
double price;

In C, floating-point numbers can also be represented in exponential. For example,

float normalizationFactor = 22.442e2;

What's the difference between float and double?

The size of float (single precision float data type) is 4 bytes. And the size of double (double precision float data type)
is 8 bytes.

Char:
Keyword char is used for declaring character type variables. For example,

char test = 'h';

The size of the character variable is 1 byte.

Void:
void is an incomplete type. It means "nothing" or "no type". You can think of void as absent.

For example, if a function is not returning anything, its return type should be void.

Note that, you cannot create variables of void type.


short and long:
If you need to use a large number, you can use a type specifier long. Here's how:

long a;
long long b;
long double c;

Here variables a and b can store integer values. And, c can store a floating-point number.

If you are sure, only a small integer ([−32,767, +32,767] range) will be used, you can use short.

short d;

You can always check the size of a variable using the sizeof() operator.

#include <stdio.h>
int main() {
short a;
long b;
long long c;
long double d;

printf("size of short = %d bytes\n", sizeof(a));


printf("size of long = %d bytes\n", sizeof(b));
printf("size of long long = %d bytes\n", sizeof(c));
printf("size of long double= %d bytes\n", sizeof(d));
return 0;
}

signed and unsigned:


In C, signed and unsigned are type modifiers. You can alter the data storage of a data type by using them:

• signed - allows for storage of both positive and negative numbers


• unsigned - allows for storage of only positive numbers

For example,

// valid codes
unsigned int x = 35;
int y = -35; // signed int
int z = 36; // signed int

// invalid code: unsigned int cannot hold negative integers


unsigned int num = -35;

Here, the variables x and num can hold only zero and positive values because we have used the unsigned modifier.

Considering the size of int is 4 bytes, variable y can hold values from -231 to 231-1, whereas variable x can hold values
from 0 to 232-1.

C Variables and Constants


Variables:
In programming, a variable is a container (storage area) to hold data.

To indicate the storage area, each variable should be given a unique name (identifier). Variable names are just the
symbolic representation of a memory location.

For example:

int playerScore = 95;

Here, playerScore is a variable of int type. Here, the variable is assigned an integer value 95.

The value of a variable can be changed, hence the name variable.

char ch = 'a';
// some code
ch = 'l';

Rules for naming a variable:


• A variable name can only have letters (both uppercase and lowercase letters), digits and underscore.
• The first letter of a variable should be either a letter or an underscore.
• There is no rule on how long a variable name (identifier) can be. However, you may run into problems in
some compilers if the variable name is longer than 31 characters.

Note: You should always try to give meaningful names to variables. For example: firstName is a better variable name
than fn.

C is a strongly typed language. This means that the variable type cannot be changed once it is declared.

For example:

int number = 5; // integer variable


number = 5.5; // error
double number; // error
Here, the type of number variable is int. You cannot assign a floating-point (decimal) value 5.5 to this variable. Also,
you cannot redefine the data type of the variable to double. By the way, to store the decimal values in C, you need
to declare its type to either double or float.

Constants:
If you want to define a variable whose value cannot be changed, you can use the const keyword. This will create a
constant. For example,

const double PI = 3.14;

Notice, we have added keyword const.

Here, PI is a symbolic constant; its value cannot be changed.

const double PI = 3.14;


PI = 2.9; //Error

Escape Sequences:
Sometimes, it is necessary to use characters that cannot be typed or has special meaning in C programming. For
example: newline(enter), tab, question mark etc.

In order to use these characters, escape sequences are used.

Escape Sequences
Escape Sequence Character
\b Backspace

\f Form feed

\n Newline

\r Return

\t Horizontal Tab

\v Vertical Tab

\\ Backslash
\’ Single Quotation Mark

\” Double Quotation Mark

\? Question Mark

\0 Null Character

For example: \n is used for a newline. The backslash \ causes escape from the normal way the characters are
handled by the compiler.
C Input Output (I/O):
C Output:
In C programming, printf() is one of the main output function. The function sends formatted output to the screen.
For example,

#include <stdio.h>
int main()
{
// Displays the string inside quotations
printf("C Programming");
return 0;
}

Output:

C Programming

How does this program work?


• All valid C programs must contain the main() function. The code execution begins from the start of the
main() function.
• The printf() is a library function to send formatted output to the screen. The function prints the string inside
quotations.
• To use printf() in our program, we need to include stdio.h header file using the #include <stdio.h> statement.
• The return 0; statement inside the main() function is the "Exit status" of the program. It's optional.

Example 2: Integer Output:


#include <stdio.h>
int main()
{
int testInteger = 5;
printf("Number = %d", testInteger);
return 0;
}

Output:

Number = 5

We use %d format specifier to print int types. Here, the %d inside the quotations will be replaced by the value of
testInteger.
Example 3: float and double Output:
#include <stdio.h>
int main()
{
float number1 = 13.5;
double number2 = 12.4;

printf("number1 = %f\n", number1);


printf("number2 = %lf", number2);
return 0;
}

Output:

number1 = 13.500000
number2 = 12.400000

To print float, we use %f format specifier. Similarly, we use %lf to print double values.

Example 4: Print Characters:


#include <stdio.h>
int main()
{
char chr = 'a';
printf("character = %c", chr);
return 0;
}

Output

character = a

To print char, we use %c format specifier.

C Input:
In C programming, scanf() is one of the commonly used function to take input from the user. The scanf() function
reads formatted input from the standard input such as keyboards.

Example 5: Integer Input/Output:


#include <stdio.h>
int main()
{
int testInteger;
printf("Enter an integer: ");
scanf("%d", &testInteger);
printf("Number = %d",testInteger);
return 0;
}

Output:

Enter an integer: 4
Number = 4

Here, we have used %d format specifier inside the scanf() function to take int input from the user. When the user
enters an integer, it is stored in the testInteger variable.

Notice, that we have used &testInteger inside scanf(). It is because &testInteger gets the address of testInteger, and
the value entered by the user is stored in that address.

Example 6: Float and Double Input/Output:


#include <stdio.h>
int main()
{
float num1;
double num2;

printf("Enter a number: ");


scanf("%f", &num1);
printf("Enter another number: ");
scanf("%lf", &num2);

printf("num1 = %f\n", num1);


printf("num2 = %lf", num2);

return 0;
}
Output:

Enter a number: 12.523


Enter another number: 10.2
num1 = 12.523000
num2 = 10.200000

We use %f and %lf format specifier for float and double respectively.

Example 7: C Character I/O:


#include <stdio.h>
int main()
{
char chr;
printf("Enter a character: ");
scanf("%c",&chr);
printf("You entered %c.", chr);
return 0;
}

Output:

Enter a character: g
You entered g

When a character is entered by the user in the above program, the character itself is not stored. Instead, an integer
value (ASCII value) is stored.

And when we display that value using %c text format, the entered character is displayed. If we use %d to display the
character, it's ASCII value is printed.

Example 8: ASCII Value:


#include <stdio.h>
int main()
{
char chr;
printf("Enter a character: ");
scanf("%c", &chr);

// When %c is used, a character is displayed


printf("You entered %c.\n",chr);

// When %d is used, ASCII value is displayed


printf("ASCII value is %d.", chr);
return 0;
}
Output:

Enter a character: g
You entered g.
ASCII value is 103.

I/O Multiple Values:


#include <stdio.h>
int main()
{
int a;
float b;

printf("Enter integer and then a float: ");


// Taking multiple inputs
scanf("%d%f", &a, &b);

printf("You entered %d and %f", a, b);


return 0;
}

Output:

Enter integer and then a float: -3


3.4
You entered -3 and 3.400000

Format Specifiers for I/O:


As you can see from the above examples, we use

➢ %d for int
➢ %f for float
➢ %lf for double
➢ %c for char

C Comments:
In programming, comments are hints that a programmer can add to make their code easier to read and understand.
For example,

#include <stdio.h>

int main() {

// print Hello World to the screen


printf("Hello World");
return 0;
}

Output:

Hello World

Here, // print Hello World to the screen is a comment in C programming. Comments are completely ignored by C
compilers.

Types of Comments:
There are two ways to add comments in C:

1. // - Single Line Comment


2. /*...*/ - Multi-line Comment

Single-line Comments in C:
In C, a single line comment starts with //. It starts and ends in the same line. For example,

#include <stdio.h>

int main() {

// create integer variable


int age = 25;

// print the age variable


printf("Age: %d", age);

return 0;
}

Output:

Age: 25

In the above example, // create integer variable and // print the age variable are two single line comments.

We can also use the single line comment along with the code. For example,

int age = 25; // create integer variable

Here, code before // are executed and code after // are ignored by the compiler.

Multi-line Comments in C:
In C programming, there is another type of comment that allows us to comment on multiple lines at once, they are
multi-line comments.

To write multi-line comments, we use the /*. ...*/ symbol. For example,

/* This program takes age input from the user


It stores it in the age variable
And, print the value using printf() */

#include <stdio.h>

int main() {

int age;
printf("Enter the age: ");
scanf("%d", &age);

printf("Age = %d", age);

return 0;
}

Output:

Enter the age: 24


Age = 24

In this type of comment, the C compiler ignores everything from /* to */.

Note: Remember the keyboard shortcut to use comments:

1. Single Line comment: ctrl + / (windows) and cmd + / (mac)


2. Multi line comment: ctrl + shift + / (windows) and cmd + shift + / (mac)

Use of Comments in C
1. Make Code Easier to Understand
If we write comments on our code, it will be easier to understand the code in the future. Otherwise you will end up
spending a lot of time looking at our own code and trying to understand it.

Comments are even more important if you are working in a group. It makes it easier for other developers to
understand and use your code.

2. Using Comments for debugging


While debugging there might be situations where we don't want some part of the code. For example,

In the program below, suppose we don't need data related to height. So, instead of removing the code related to
height, we can simply convert them into comments.

// Program to take age and height input

#include <stdio.h>

int main() {

int age;
// double height;

printf("Enter the age: ");


scanf("%d", &age);

// printf("Enter the height: ");


// scanf("%lf", &height);
printf("Age = %d", age);
// printf("\nHeight = %.1lf", height);

return 0;
}

Now later on, if we need height again, all you need to do is remove the forward slashes. And, they will now become
statements not comments.

Note: Comments are not and should not be used as a substitute to explain poorly written code. Always try to write
clean, understandable code, and then use comments as an addition.

In most cases, always use comments to explain 'why' rather than 'how' and you are good to go.

C Programming Operators:
An operator is a symbol that operates on a value or a variable. For example: + is an operator to perform addition.

C has a wide range of operators to perform various operations.

C Arithmetic Operators:
An arithmetic operator performs mathematical operations such as addition, subtraction, multiplication, division etc
on numerical values (constants and variables).

Operator Meaning of Operators


+ Addition or Unary Plus
- Subtraction or Unary Minus

* Multiplication
/ Division

% Remainder after division (Modulo Division)

Example 1: Arithmetic Operators:


// Working of assignment operators
#include <stdio.h>
int main()
{
int a = 5, c;

c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10
printf("c = %d\n", c);
c -= a; // c is 5
printf("c = %d\n", c);
c *= a; // c is 25
printf("c = %d\n", c);
c /= a; // c is 5
printf("c = %d\n", c);
c %= a; // c = 0
printf("c = %d\n", c);

return 0;
}
Output:

c = 5
c = 10
c = 5
c = 25
c = 5
c = 0

C Relational Operators:
A relational operator checks the relationship between two operands. If the relation is true, it returns 1; if the relation
is false, it returns value 0.

Relational operators are used in decision making and loops.

Operator Meaning of Operators Example


== Addition or Unary Plus 5 == 3 is evaluated to 0

> Subtraction or Unary Minus 5 > 3 is evaluated to 1


< Multiplication 5 < 3 is evaluated to 0

!= Division 5 != 3 is evaluated to 1

>= Remainder after division (Modulo Division) 5 >= 3 is evaluated to 1


<= Less than or equal to 5 <= 3 is evaluated to 0

Example 2: Relational Operators:


// Working of relational operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;

printf("%d == %d is %d \n", a, b, a == b);


printf("%d == %d is %d \n", a, c, a == c);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d > %d is %d \n", a, c, a > c);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d < %d is %d \n", a, c, a < c);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d != %d is %d \n", a, c, a != c);
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d >= %d is %d \n", a, c, a >= c);
printf("%d <= %d is %d \n", a, b, a <= b);
printf("%d <= %d is %d \n", a, c, a <= c);

return 0;
}
Output:

5 == 5 is 1
5 == 10 is 0
5 > 5 is 0
5 > 10 is 0
5 < 5 is 0
5 < 10 is 1
5 != 5 is 0
5 != 10 is 1
5 >= 5 is 1
5 >= 10 is 0
5 <= 5 is 1
5 <= 10 is 1

C Logical Operators:
An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or
false. Logical operators are commonly used in decision making in C programming.

Operator Meaning of Operators Example


If c = 5 and d = 2 then,
Logical AND. True only if all operands are
&& expression ((c==5) &&
true
(d>5)) equals to 0.
If c = 5 and d = 2 then,
Logical OR. True only if either one operand
|| expression ((c==5) || (d>5))
is true
equals to 1.
If c = 5 then, expression
! Logical NOT. True only if the operand is 0
!(c==5) equals to 0.
Example 3: Logical Operators:
// Working ofi logical operator s

include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;

result = (a == b) fifi (c > b);


printf("(a == b) fifi (c > b ) is %d \n", result);

result = (a == b) fifi (c < b);


printf("(a == b) fifi (c < b) is %d \n", result);

result = (a == b) || (c < b);


printf("(a == b) || (c < b) is %d \n", result);

result = (a != b) || (c < b);


printf("(a != b) || (c < b) is %d \n", result);

result = !(a != b);


printf("!(a != b) is %d \n", result);

result = !(a == b);


printf("!(a == b) is %d \n", result);

return 0;
}

Output:
(a == b) fifi (c > b) is 1
(a == b) fifi (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0

Explanation of logical operator program:


• (a == b) && (c > 5) evaluates to 1 because both operands (a == b) and (c > b) is 1 (true).
• (a == b) && (c < b) evaluates to 0 because operand (c < b) is 0 (false).
• (a == b) || (c < b) evaluates to 1 because (a = b) is 1 (true).
• (a != b) || (c < b) evaluates to 0 because both operand (a != b) and (c < b) are 0 (false).
• !(a != b) evaluates to 1 because operand (a != b) is 0 (false). Hence, !(a != b) is 1 (true).
• !(a == b) evaluates to 0 because (a == b) is 1 (true). Hence, !(a == b) is 0 (false).

C Assignment Operators:
An assignment operator is used for assigning a value to a variable. The most common assignment operator is =
Operator Example Same as
= a=b a=b
+= a += b a = a+b

-= a -= b a = a-b

*= a *= b a = a*b
/= a /= b a = a/b

%= a %= b a = a%b
int main()
{
int a = 5, c;
c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10
printf("c = %d\n", c);
c -= a; // c is 5
printf("c = %d\n", c);
c *= a; // c is 25
printf("c = %d\n", c);
c /= a; // c is 5
printf("c = %d\n", c);
c %= a; // c = 0
printf("c = %d\n", c);
return 0;
}
Output:
c = 5
c = 10
c = 5
c = 25
c = 5
c = 0

C Comma Operators:
Comma operators are used to link related expressions together. For example:

int a, b = 5, c;

C The sizeof Operator:


The sizeof is a unary operator that returns the size of data (constants, variables, array, structure, etc).
Output:
Size of int = 4 bytes
Size of float = 4 bytes
Size of double = 8 bytes
Size of char = 1 byte

Operators Precedence in C:
Operator precedence determines the grouping of terms in an expression and decides how an expression is evaluated.
Certain operators have higher precedence than others; for example, the multiplication operator has a higher
precedence than the addition operator.

For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has a higher precedence than +, so it first
gets multiplied with 3*2 and then adds into 7.

Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the
bottom. Within an expression, higher precedence operators will be evaluated first.

Category Operator Associativity


Postfix () [] -> . ++ - - Left to Right
Unary + - ! ~ ++ - - (type)* & sizeof Right to Left

Multiplicative */% Left to Right


Additive +- Left to Right
Shift << >> Left to Right

Relational < <= > >= Left to Right

Equality == != Left to Right


Logical AND & OR && || Left to Right
Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to Left

Comma , Left to Right


Output:
Value of (a + b) * c / d is : 90
Value of ((a + b) * c) / d is : 90
Value of (a + b) * (c / d) is : 90
Value of a + (b * c) / d is : 50

You might also like