Introduction to C Programming


In basics of C, a data is kept in random access memory (RAM) while executing a C program. This data can be of any form, it can be variable, structure and array that have been declared by the program. The question is where did this data come from, and what can the program do with it?

  • Data or input comes from some location external to the program. Data moved from an external location into RAM, where the program can access it, is called input. The keyboard and disk files are the most common sources of program input.
  • Data can also be sent to a location external to the program; this is called output. The most common destinations for output are the screen, a printer, and disk files.

 Input sources and output destinations are collectively referred to as devices. The keyboard is a device; the screen is a device, and so on. Some devices (the keyboard) are for input only, others (the screen) are for output only, and still others (disk files) are for both input and output. Whatever the device, and whether it’s performing input or output, C carries out all input and output operations by means of streams.

 Input -: In any programming language input means to feed some data into program i.e. Input is any information provided to the program. This can be given in the form of file or from command line. C programming language provides a set of built-in functions to read given input and feed it to the program as per requirement. It can be

  • Keyboard input
  • Mouse input
  • File input

 Output -: In any programming language output means to display some data on screen, printer or in any file. Output is any information (or effect) that a program produces. C programming language provides a set of built-in functions to output required data. It can be

  • Sounds, lights, pictures, text, motion, etc.
  • On a screen
  • In a file on a disk or tape, etc.

 Console Input & Output functions-:

A console is term used for combined use of screen and keyboard. Console I/O functions can be broadly classified into two —formatted and unformatted console I/O functions. The basic difference between them is that the formatted functions allow the input read from the keyboard or the output displayed on the screen/monitor to be formatted as per our requirements.

For example, if values of average marks and percentage marks are to be displayed on the screen, then the details like where this output would appear on the screen, how many spaces would be present between the two values, the number of places after the decimal points, etc. can be controlled using formatted functions.

 Formatted Input & Output (scanf /printf )-:

The functions printf( ), and scanf( ) fall under the category of formatted console I/O functions.

These functions allow us to supply the input in a fixed forma and let us obtain the output in the specified form. Its general form looks like:

 printf ( “format string”, list of variables ) ;

int printf(const char *format, …)

The format string can contain:

  • Characters that are simply printed as they are
  • Conversion specifications that begin with a % sign
  • Escape sequences that begin with a \ sign
  • If successful, the total number of characters written is returned. On failure, a negative number is returned.

Format Specifications  -:

The   %d  and   %f   used in the   printf( )   are called format specifiers.   They tell   printf( )   to print the value of   avg   as a decimal integer   and the value of per as a float.

 scanf() function-:

The C library function int scanf (const char *format, …) reads formatted input from stdin.

Syntax:                          int scanf (const char *format, …)

  • The scanf() function is used to read character, string, numeric data from keyboard
  • Consider below example program where user enters a character. This value is assigned to the variable “ch” and then displayed.
  • Then, user enters a string and this value is assigned to the variable ”str” and then displayed.
  • If successful, the total number of characters written is returned, otherwise a negative number is returned.

sprintf & sscanf-:

sprint() -:

It is used to store formatted data as a string used to create strings as output using formatted data. The syntax of the sprintf() function is as follows:

int sprintf (char *str const char *format, … );

  • str −: This is the pointer to an array of char elements where the resulting C string is stored.
  • format −: The *form parameter will show the format of the output. This is the String that contains the text to be written to buffer. It can optionally contain embedded format tags that are replaced by the values specified in subsequent additional arguments and formatted as requested. Format tags prototype: %[flags][width][.precision][length]specifier.

With sprintf (), you can combine several data variables into a character array. Let’s write a program that stores multiple bits of data into the array and then see the output.

sscanf() -:

In the C Programming Language, the sscanf function reads formatted output from an object pointed to by str.

int sscanf(const char *str, const char *format, …)

  • str -: Function processes as its source to retrieve the data.
  • format -:Contains one or more of the following items: Whitespace character, Non-whitespace character and Format specifiers


Leave a Reply