The main thing which is neccessary for a programmer is to take the input from the user ,store it in the computer memory,make some necessary computation and show the result to the user. That is how a CPU Cycle will work :
Input -> Process -> Output
Input : Inputs can be of several type either character,string,float,decimal etc.
1.String input : String is a set of characters.
Example : what is the score ?
such inputs would be taken from the statements below :
char ch[20];
scanf(”%s”,ch);
The first statement will allocate the “Memory” for the variable ch.
The second statement will take the input from the user where “%s” is neccessary which maps the input to the allocated memory which is of string type.
Alternatively :
gets (ch)
The gets method will take the string as an argument.
Here ch is a string variable.
2. Integer Input : how to take the input of numeric values such as :
Integer : Range from -32768 to 32767 (for 16 bit compilers)
such inputs would be taken from the statements below :
int value;
scanf(”%d”,&value);
The first statement will allocate the “Memory” for the variable value.
The second statement will take the input from the user where “%d” is neccessary which maps the input to the allocated memory which is of integer type.
Note : Remember to use ” & ” with the variable as i done in the second statement otherwise it would assign the unknown value.
Others :
You can store all of the normal C types with scanf by using different placeholders:
- float (floating point values) uses %f and & operator with variable .
For example : scanf(”%f”,&value);
- char (single character values) uses %c and & operator with variable
For example : scanf(”%c”,&ch);
Outputs :
To print output to thedisplay screen we use : printf ,puts statements.
String Output :
1) Here is the simplest printf statement:
printf("Hello");
This call to printf has a format string that tells printf to send the word “Hello” to standard out.
2) The standard form for the puts character is
puts (ch)
Where ch is a string variable.
Integer Output :
The following line shows how to output the value of a variable using printf.
printf("%d", b);
The %d is a placeholder that will be replaced by the value of the variable b when the printf statement is executed. Often, you will want to embed the value within some other words.
You can also use multiple %d placeholders in one printf statement:
printf("%d + %d = %d\n", a, b, c);
In the printf statement, it is extremely important that the number of operators in the format string corresponds exactly with the number and type of the variables following it. For example, if the format string contains three %d operators, then it must be followed by exactly three parameters and they must have the same types in the same order as those specified by the operators.
You can print all of the normal C types with printf by using different placeholders:
- int (integer values) uses %d
- float (floating point values) uses %f
- char (single character values) uses %c
- character strings (arrays of characters, discussed later) use %s




Recent Comments