Professional Documents
Culture Documents
FUNCTIONS
As we all know the three essential functions of a computer are reading,
processing and writing data. Majority of the programs take data as
input, and then after processing the processed data is being displayed
which is called information. In C programming you can use scanf() and
printf() predefined function to read and print data.
I/O operations are useful for a program to interact with users. stdlib is
the standard C library for input-output operations. While dealing with
input-output operations in C, there are two important streams that play
their role. These are:
Integer Format
%d
Specifier
Character Format
%c
Specifier
Unsigned Integer
%u
Format Specifier
printf("%d",<variable name>);
READING CHARACTERS IN C
The easiest and simplest of all I/O operations are taking a character as
input by reading that character from standard input (keyboard).
getchar() function can be used to read a single character. This function
is alternate to scanf() function.
Syntax:
var_name = getchar();
Example:
#include<stdio.h>
void main()
{
char title;
title = getchar();
}
There is another function to do that task for files: getc which is used to
accept a character from standard input.
Syntax:
int getc(FILE *stream);
Writing Character In C
Similar to getchar() there is another function which is used to write
characters, but one at a time.
Syntax:
putchar(var_name);
Example:
#include<stdio.h>
void main()
{
char result = 'P';
putchar(result);
putchar('\n');
}
Pointer These are powerful C features which are used to access the
s memory and deal with their addresses.
User Defined Data Types:
Structure, Union, and Enumeration.
C allows the feature called type definition which allows programmers to define their
identifier that would represent an existing data type. There are three such types:
Data
Description
Types
Different data types also have different ranges upto which they can
store numbers. These ranges may vary from compiler to compiler.
Below is list of ranges along with the memory requirement and format
specifiers on 32 bit gcc compiler.
Data Type Memory (bytes) Range Format
Specifier
int 2 -32,768 to 32,767 %d
unsigned short int 2 0 to 65,535 %hu
int 4 -2,147,483,648 to 2,147,483,647 %d
long int 4 -2,147,483,648 to 2,147,483,647 %ld
unsigned char 1 0 to 255 %c
float 4 %f
double 8 %lf
long double 12 %Lf
The gets() function reads a line from stdin(standard input) into the
buffer pointed to by str pointer, until either a terminating newline or
EOF (end of file) occurs. The puts() function writes the string str and a
trailing newline to stdout.
str → This is the pointer to an array of chars where the C string is
stored.
#include<stdio.h>
void main()
{
/* character array of length 100 */
char str[100];
printf("Enter a string");
gets( str );
puts( str );
getch();
}
The main difference between these two functions is that scanf() stops
reading characters when it encounters a space, but gets() reads space
as character too.
If you enter name as Study Tonight using scanf() it will only read and
store Study and will leave the part after space. But gets() function will
read it completely.
COMMENTS
In computer programming, a comment is a programmer-readable
explanation or annotation in the source code of a computer program.
They are added with the purpose of making the source code easier for
humans to understand, and are generally ignored by compilers and
interpreters.[1][2] The syntax of comments in various programming
languages varies considerably.
Comments are sometimes processed in various ways to generate
documentation external to the source code itself by documentation
generators, or used for integration with source code management
systems and other kinds of external programming tools.
The flexibility provided by comments allows for a wide degree of
variability, but formal conventions for their use are commonly part of
programming style guides.
Syntax-:
BLOCK COMMENT
The following code fragments in C demonstrate just a tiny example of
how comments can vary stylistically, while still conveying the same
basic information:
/*
This is the comment body.
Variation One.
*/
INLINE COMMENT (END-OF-LINE)
In this form, all the text from the ASCII characters // to the end of the
line is ignored.
// begin: Variation Three.
// -------------------------
// This is the comment body.
// -------------------------