Introduction to Programming - (C Language) - Unit : 1 – VARIABLES AND CONSTANTS
VARIABLES AND CONSTANTS
VARIABLES:
The quantities which can be changed during the execution
of program are called Variables. A variable can be considered as the name of
the cell which can hold the constants. To use any variable it must be declared
with its data type before the first executable statement and they can be
initialized. Naming the variable is very important.
Rules for declaring variables:
1) The variable
name must be start with either alphabets or an underscore and may contain
alphabets, digits, hyphen or underscore.
2) The maximum
length of a variable is 8 characters. But some compilers can accept upto 32 characters.
3) There must not
be any blank spaces or special symbols in a variable name except underscore symbol ( _ ).
4) A variable
name must not be a keyword.
Ex: valid invalid
eno emp
name
empname emp(name
emp-name 45abc
Useful tips in naming variables:
1. Variables
must be related to content stored in it.
2. Length
of the variable is 3-6 letters.
3. Avoid
vowels in variable names.
4. Use
underscores instead of spaces.
Ex: ttl_mrk (total marks)
Declaring & initializing C variable:
· Variables should be
declared in the C program before to use.
· Memory space is not
allocated for a variable while declaration. It happens only on variable
definition.
· Variable initialization
means assigning a value to the variable.
Variable declarations
This is the process of allocating sufficient memory space
for the data in term of variable.
Syntax: Datatype variable_name;
Ex:
int a;
If no input values are assigned by the user than system
will gives a default value called garbage value.
Garbage value
Garbage value can be any value given by system and that is no way
related to correct programs. This is a disadvantage of C programming language
and in C programming it can overcome using variable initialization.
S.No |
Type |
Syntax |
Example |
1 |
Variable declaration |
data_type variable_name; |
int x, y, z; char flat, ch; |
2 |
Variable initialization |
data_type variable_name = value; |
int x = 50, y = 30; char flag = ‘x’, ch=’l’; |
1) Character
constants :
a) Characters Ex:
‘a’, ‘5’, ‘+’, ‘ ‘, ...
b) Strings Ex:
“abc”, “435”, ‘rama”, ...
2) Numeric
Constants :
a) integers Ex:
435, -657, 65535, -32768,...
b) Real numbers
i) Fractional form
Ex: 435.67, 345.00054, ...
ii) Exponential form
Ex: 0.02e3, 1.17e-38, ...
Constants in C
A constant is a value or variable that
can't be changed in the program, for example: 10, 20, 'a', 3.4, "c
programming" etc.
There are different types of constants in C
programming.
List of Constants in C
Constant |
Example |
Decimal Constant |
10, 20, 450 etc. |
Real or Floating-point Constant |
10.3, 20.2, 450.6 etc. |
Octal Constant |
021, 033, 046 etc. |
Hexadecimal Constant |
0x2a, 0x7b, 0xaa etc. |
Character Constant |
'a', 'b', 'x' etc. |
String Constant |
"c", "c
program", "c in javatpoint" etc. |
2 ways to define constant in C
There are two ways to define constant in C programming.
- const keyword
- #define preprocessor
3.
KEYWORDS: These are predefined words. There are 32 keywords in C language.
These keywords can not be used as user-defined variables.
Keywords in C Programming |
|||
auto |
break |
case |
char |
const |
continue |
default |
do |
double |
else |
enum |
extern |
float |
for |
goto |
if |
int |
long |
register |
return |
short |
signed |
sizeof |
static |
struct |
switch |
typedef |
union |
unsigned |
void |
volatile |
while |
BASIC INPUT AND OUTPUT
Input Functions:
scanf(): Used to read input from the standard
input (usually the keyboard) based on a specified format.
Example:
int num;
scanf("%d",
&num);
getchar(): Reads a single character from the
standard input.
Example:
char ch = getchar();
gets(): Reads a line of text from the
standard input. (Note: gets() is generally discouraged due to security risks.
It's better to use fgets() for safe input.)
Example:
char buffer[100];
gets(buffer);
Output Functions:
printf(): Used to print formatted data to the
standard output (usually the console).
Example:
int num = 42;
printf("The number
is: %d\n", num);
putchar(): Writes a single character to the
standard output.
Example:
char ch = 'A';
putchar(ch);
puts(): Writes a string to the standard
output followed by a newline character.
Example:
puts("Hello,
world!");
Formatted Input and
Output:
You can use various format specifiers with
printf() and scanf() to format input and output according to your needs. Some
common format specifiers include %d (for integers), %f (for floating-point
numbers), %c (for characters), and %s (for strings), among others.
Comments
Post a Comment