2. 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.
• Each variable in C has a specific type, which
determines the size and layout 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.
3. Variable Declaration in C
type variable_list;
•type
must be a valid C data type.
•variable_list
may consist of one or more identifier names
separated by commas.
Example:
int i, j, k;
char c, ch;
float f, salary;
double d;
5. scanf Function
int scanf (control string, arg1, arg2, …, argn)
• Reads data from standard input and stores
according to the control string into the
locations pointed by arguments.
Control String:
any Whitespace character--- read and ignore
6. Format Specifier of scanf
%[*][width] [length]specifier
Specifier Description
i decimal , octal, hexadecimal digits
d,u signed integers , unsigned decimal integer
o unsigned octal
x unsigned hexadecimal integer
f,e,g floating point
c character
s string of characters(stop taking input when the first whitespace found)
% % followed by percent matches % to the input
[characters] Any no of characters specified between the brackets
[^characters] Any no of characters none of them specified as characters between the
brackets
9. Format Specifier of scanf
%[*][width] [length]specifier
Sub-specifier Description
* An optional starting asterisk indicated that the data is to be read from
the input stream but ignored
width Specifies the maximum number of characters to be read in the current
reading operation
length hh , h , l , ll , L
12. scanf Function
int scanf (control string, arg1, arg2, …, argn)
Control String:
Non-whitespace characters, except format
specifier(%) --- this causes the function to read
the next character from the stream , compare
it to this non-whitespace character.
if it matches, then ignore that NWCharacter
Else the function fails and stop taking inputs
14. string I/O operations
Declaration:
char str[10];
Initialization:
char str[30]=“hello world”;
Or,
char str[30]={‘h’ , ’e’ , ’l’ , ’l’ , ’o’ , ’ ’ , ’w’ , ’o’ , ’r’ , ’l’ , ’d’ };
h e l l o w o r l d 0 …
15. string I/O operations
Printing string:
printf(“%s”,str);
Or, puts(str);
Taking string input:
scanf(“%s”,str); //if the string contains no whitespace character
Or, scanf(“%[^n]”,str);
Or, gets(str);