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

Basiscs of C

C has a character set of alphabets, digits, and special characters that can be used as variables, functions, and constants. Data types like int, char, float, and double determine the type and size of variables. Keywords are reserved words with special meanings, while identifiers are unique names given to variables and functions. Comments are used to document code. Various escape sequences represent special characters in strings.

Uploaded by

Bhanesh Katari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Basiscs of C

C has a character set of alphabets, digits, and special characters that can be used as variables, functions, and constants. Data types like int, char, float, and double determine the type and size of variables. Keywords are reserved words with special meanings, while identifiers are unique names given to variables and functions. Comments are used to document code. Various escape sequences represent special characters in strings.

Uploaded by

Bhanesh Katari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

C – Character Set

• A character set is a set of alphabets, letters and some special


characters that are valid in C language.

• C accepts both lowercase and uppercase alphabets as variables and


functions.
Uppercase: A B C ................................... X Y Z
Lowercase: a b c ...................................... x y z

• Digits 0 1 2 3 4 5 6 7 8 9

• White space Characters :


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

Special Characters in C Programming

, < > . _
( ) ; $ :
% [ ] # ?
' & { } "
^ ! * / |
- \ ~ +
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.

• As C is a case sensitive language, all keywords must be written in


lowercase.
C - Keywords
C Keywords
auto double int struct
break else long switch
case enum register typedef
char extern return union
continue for signed void
do if static while
default goto sizeof volatile
const float short unsigned
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.

• Identifier names must be different from keywords i.e. keywords cannot


be used as identifier names.
Rules for naming Identifiers
• A valid identifier can have letters (both uppercase and lowercase
letters), digits and underscores.
• The first letter of an identifier should be either a letter or an
underscore.

• 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.

• Names are case sensitive (myVar and myvar are different variables).
Names cannot contain whitespaces or special characters like !, #, %,
etc.
Variables
• In programming, a variable is a container (storage area) to hold data
and the value of the variable can be changed.

• 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.
• 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
Data Types
• In C programming,
Type
data types are declarations forFormat
Size (bytes)
variables. This
Specifier
int at least 2, usually 4 %d, %i
determines the type and size of data associated with variables.
char 1 %c
float 4 %f
• Data double
types that are derived from 8fundamental data types %lf are derived
types.short
Forintexample: arrays, pointers, function types, structures,
2 usually %hd etc.
unsigned int at least 2, usually 4 %u
long int at least 4, usually 8 %ld, %li
long long int at least 8 %lld, %lli
unsigned long int at least 4 %lu
unsigned long long int at least 8 %llu
signed char 1 %c
unsigned char 1 %c
long double at least 10, usually 12 or 16 %Lf
Integers
• 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.
Integers
• There are three types of integer literals in C programming:
• decimal (base 10)
• octal (base 8)
• hexadecimal (base 16)

• For example:
Decimal: 0, -9, 22 etc
Octal: 021, 077, 033 etc
Hexadecimal: 0x7f, 0x2a, 0x521 etc

• In C programming, octal starts with a 0, and hexadecimal starts with a


0x.
Float & Double
• float and double are used to hold real numbers. They have either a
fractional form or an exponent form. For example:
-2.0
0.0000234
-0.22E-5

• float salary;
• double price;

• 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.
Float & Double
• if you print a floating point number, the output will show many digits
after the decimal point:
float myFloatNum = 3.5;
double myDoubleNum = 19.99;

printf("%f\n", myFloatNum); // Outputs 3.500000


printf("%lf", myDoubleNum); // Outputs 19.990000

• If you want to remove the extra zeros (set decimal precision), you can
use a dot (.) followed by a number that specifies how many digits that
should be shown after the decimal point:
Float & Double
float myFloatNum = 3.5;
printf("%f\n", myFloatNum); // Default will show 6 digits after the decimal point
printf("%.1f\n", myFloatNum); // Only show 1 digit
printf("%.2f\n", myFloatNum); // Only show 2 digits
printf("%.4f", myFloatNum); // Only show 4 digits
Character
• A character is created by enclosing a single character inside single
quotation marks. For example: 'a', 'm', 'F', '2', '}' etc.

• Keyword char is used for declaring character type variables. For


example,

char test = 'h';

• The size of the character variable is 1 byte.


Short & Long
• If you need to use a large number, you can use a type specifier long.
Here's how:
long a;
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;
Short & Long
• 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 & 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
• Here, the variables x 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.
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.

• An escape sequence in C language is a sequence of characters that


doesn't represent itself when used inside string literal or character.

• For example: newline(enter), tab, question mark etc. For example: \n is


used for a newline. The backslash \ causes escape from the normal way
the characters are handled by the compiler.
Escape Sequences
Escape Sequence Meaning
\a Alarm or Beep
\b Backspace
\f Form Feed
\n New Line
\r Carriage Return
\t Tab (Horizontal)
\v Vertical Tab
\\ Backslash
\' Single Quote
\" Double Quote
\? Question Mark
\nnn octal number
\xhh hexadecimal number
\0 Null
Operators in C
• An operator is a symbol that operates on a value or a variable.
Operators are used to perform operations on variables and values.

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

• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Bitwise operators
Arithmetic Operators
Operator Name Description Example

+ Addition Adds together two values x+y

- Subtraction Subtracts one value from x-y


another

* Multiplicatio Multiplies two values x*y


n

/ Division Divides one value by another x/y

% Modulus Returns the division x%y


remainder

++ Increment Increases the value of a ++x


variable by 1

-- Decrement Decreases the value of a --x


variable by 1
Increment & Decrement Operators
• Increment ++ increases the value by 1 whereas decrement --
decreases the value by 1. These two operators are unary operators,
meaning they only operate on a single operand.
• If you use the ++ operator as a prefix like: ++var, the value of var
is incremented by 1; then it returns the value.

• If you use the ++ operator as a postfix like: var++, the original


value of var is returned first; then var is incremented by 1.

• The -- operator works in a similar way to the ++ operator except --


decreases the value by 1.
Assignment Operators
• An assignment operator is used for assigning a value to a variable. The
most common assignment
Operatoroperator is =
Example Same As
= x=5 x=5

+= x += 3 x=x+3

-= x -= 3 x=x-3

*= x *= 3 x=x*3

/= x /= 3 x=x/3

%= x %= 3 x=x%3

&= x &= 3 x=x&3

|= x |= 3 x=x|3

^= x ^= 3 x=x^3

>>= x >>= 3 x = x >> 3

<<= x <<= 3 x = x << 3


Comparison Operators
• A comparison operator checks the relationship between two operands.
If the relation is true, it returns 1; if the relation is false, it returns
value 0.
Operator Meaning of Operator Example

== Equal to 5 == 3 is evaluated to 0

> Greater than 5 > 3 is evaluated to 1

< Less than 5 < 3 is evaluated to 0

!= Not equal to 5 != 3 is evaluated to 1

Greater than or
>= equal to
5 >= 3 is evaluated to 1

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


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 Example

If c = 5 and d = 2 then,
Logical AND. True only
&& expression ((c==5) && (d>5))
if all operands are true
equals to 0.

Logical OR. True only if If c = 5 and d = 2 then,


|| either one operand is expression ((c==5) || (d>5))
true equals to 1.

Logical NOT. True only If c = 5 then, expression


!
if the operand is 0 !(c==5) equals to 0.
Bitwise Operators
• During computation, mathematical operations like: addition,
subtraction, multiplication, division, etc are converted to bit-level which
makes processing faster and saves power.

• Bitwise operators Operators


are used in C programming to perform bit-level
Meaning of operators
operations.
& Bitwise AND

| Bitwise OR

^ Bitwise exclusive OR

~ Bitwise complement

<< Shift left

>> Shift right


Sizeof Operator
• The sizeof is a unary operator that returns the size of data (constants,
variables, array, structure, etc).

#include <stdio.h>
int main()
{
int a;
float b;
double c;
char d;
printf("Size of int=%lu bytes\n",sizeof(a));
printf("Size of float=%lu bytes\n",sizeof(b));
printf("Size of double=%lu bytes\n",sizeof(c));
printf("Size of char=%lu byte\n",sizeof(d));

return 0;
}
Input & Output Statements
• The printf() and scanf() functions are used for input and output in C
language. Both functions are inbuilt library functions, defined in stdio.h
(header file).
• The printf() function is used for output. It prints the given statement to
the console. The syntax of printf() function is

printf("format string",argument_list);

• The format string can be %d (integer), %c (character), %s (string), %f


(float) etc.
Input & Output Statements
• The scanf() function is used for input. It reads the input data from the
console.

scanf("format string",argument_list);
Program 1
• Write a program to read and print a character, integer and floating point
value.
#include <stdio.h>
int main()
{
int a;
float b;
char c;
printf("\nEnter an integer: ");
scanf("%d", &a);
printf("\nEnter a float: ");
scanf("%f", &b);
printf("\nEnter a character: ");
scanf("%c", &c);
printf("%d %f %c ", a,b,c);
return 0;
}
Program 2
• Write a program to float into an integer and also limit number of
decimal points for a float.
#include <stdio.h>

int main()
{
float num1 = 5.678;
int num2 = (int) num1;

printf("%.1f %d", num1, num2);


return 0;
}
Program 3
• Write a program to print the size of different variables.

#include <stdio.h>

int main() {
int myInt;
float myFloat;
double myDouble;
char myChar;

printf("%d\n", sizeof(myInt));
printf("%d\n", sizeof(myFloat));
printf("%d\n", sizeof(myDouble));
printf("%d\n", sizeof(myChar));

return 0;
}
Program 4
• Write a program to print the ASCII value of a character entered by the
user.
#include <stdio.h>
int main() {
char c;
printf("Enter a character: ");
scanf("%c", &c);

// %d displays the integer value of a character


// %c displays the actual character
printf("ASCII value of %c = %d", c, c);

return 0;
}
Program 5
• Write a program to swap two variables without using a third variable
Program 6
• Write a program to find the roots of a quadratic expression.

You might also like