C Programming Code Examples
C > Code Snippets Code Examples
read input
/* read input */
//Header file: #include <stdio.h>
//Declaration: int scanf(const char *format, ...);
// The scanf() Format Specifiers
//Code Meaning
//%a: Read a floating-point value (C99 only)
//%A: Same as %a (C99 only)
//%c: Read a single character
//%d: Read a decimal integer
//%i: Read an integer in either decimal, octal, or hexadecimal format
//%e: Read a floating-point number
//%E: Same as %e
//%f: Read a floating-point number
//%F: Same as %f (C99 only)
//%g: Read a floating-point number
//%G: Same as %g
//%o: Read an octal number
//%s: Read a string
//%x: Read a hexadecimal number
//%X: Same as %x
//%p: Read a pointer
//%n: Receive an integer value equal to the number of characters read so far
//%u: Read an unsigned decimal integer
//%[ ]: Scan for a set of characters
//%%: Read a percent sign
#include <stdio.h>
int main(void)
{
char str[80], str2[80];
int i;
scanf("%79s", str); //scanf up to 79 chars into str
return 0;
}
When we say Input, it means to feed some data into a program. An input can be given in the form of a file or from the command line. C programming provides a set of built-in functions to read the given input and feed it to the program as per requirement. When we say Output, it means to display some data on screen, printer, or in any file. C programming provides a set of built-in functions to output the data on the computer screen as well as to save it in text or binary files. C programming treats all the devices as files. So devices such as the display are addressed in the same way as files and the following three files are automatically opened when a program executes to provide access to the keyboard and screen.
In C, the "main" function is treated the same as every function, it has a return type (and in some cases accepts inputs via parameters). The only difference is that the main function is "called" by the operating system when the user runs the program. Thus the main function is always the first code executed when a program starts. main() function is a user defined, body of the function is defined by the programmer or we can say main() is programmer/user implemented function, whose prototype is predefined in the compiler. Hence we can say that main() in c programming is user defined as well as predefined because it's prototype is predefined. main() is a system (compiler) declared function whose defined by the user, which is invoked automatically by the operating system when program is being executed.
The return statement terminates the execution of a function and returns control to the calling function. Execution resumes in the calling function at the point immediately following the call. A return statement can also return a value to the calling function. A return statement causes your function to exit and hand back a value to its caller. The point of functions, in general, is to take in inputs and return something. The return statement is used when a function is ready to return a value to its caller.
Read formatted data from stdin. Reads data from stdin and stores them according to the parameter format into the locations pointed by the additional arguments. The additional arguments should point to already allocated objects of the type specified by their corresponding format specifier within the format string. In C programming, scanf() is one of the commonly used function to take input from the user. The scanf() function reads formatted input from the standard input such as keyboards. The scanf() function enables the programmer to accept formatted inputs to the application or production code. Moreover, by using this function, the users can provide dynamic input values to the application.
#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program. Here are the two types of file that can be included using #include:
C Program code remove 'trailing white space' characters is 'lot more easier' and faster than removing leading white space characters. As in this case we need not to perform any 'char'
C program to input number of days from user and convert it to years, weeks and days. How to convert days to years, weeks in C. Input days from user. Store it in some variable say...
C programming code to read elements in an array and find frequency of each element in an array. C Program to count the occurrence of each element in an array. Logic to count...