Posted in: Uncategorized by 0800781186 on March 14, 2009

Hexadecimal Convertion

This is just a simple script to convert values 15 , 150 and 1500 into the hexadecimal form

#include<stdio.h>
#include<conio.h>

void main()
{

printf(”hex 15 = %x \nhex 150= %x \nhex 1500= %x”,15,150,1500);

getch();

}

The Standard Input and Output (I/O)

<!– @page { margin: 2cm } P { margin-bottom: 0.21cm } H3 { margin-bottom: 0.21cm } H4 { margin-bottom: 0.21cm } TD P { margin-bottom: 0cm } –>Additionally, in C, there are three file streams that are pre-opened for you:

· stdin–The standard input for reading.

· stdout–The standard output for writing.

  • stderr–The standard error for writing error messages.

standard input (stdin) file stream links to your keyboard

standard output (stdout) and the standard error (stderr) file streams point to your terminal screen.

When you executed the printf() function, you were in fact sending the output to the default file stream, stdout, which points to your screen.

Getting the Input from the User

The C language has several functions to direct the computer to read the input from the user (typically through the keyboard.) In this lesson the getc() and getchar() functions are introduced.

Using the getc() Function

The getc() function reads the next character from a file stream, and returns the character as an integer.

The syntax for the getc() function is

#include <stdio.h>
int getc(FILE *stream);

Here FILE *stream declares a file stream (that is, a variable). The function returns the numeric value of the character read. If an end-of-file or error occurs, the function returns EOF.
Example:

Using getc() to input character from keyboard

int ch;
ch = getc( stdin );

An integer variable, ch, is declared; it is assigned the return value from the getc() function

stdin is passed to the getc() function, which indicates that the file stream is from the keyboard.

Using the getchar() Function

The C language provides another function, getchar(), to perform a similar operation to getc(). More precisely, the getchar() function is equivalent to getc(stdin).

The syntax for the getchar() function is

#include <stdio.h>
int getchar(void);

Here void indicates that no argument is needed for calling the function. The function
returns the numeric value of the character read. If an end-of-file or error occurs, the function returns EOF.

So you can use

ch = getc( stdin );

Or
ch = getchar( );

To input a character from your keyboard
Remember that the variable type for getc() and getchar() is an integer, but use %c to print the character.

Printing the Output on the Screen

Besides getc() and getchar() for reading, the C language also provides two functions, putc() and putchar(), for writing. The following two sections introduce these functions.

Using the putc() Function

The putc() function writes a character to the specified file stream, which, in our case, is the standard output pointing to your screen.

The syntax for the putc() function is

#include <stdio.h>
int putc(int c, FILE *stream);

Here the first argument, int c, indicates that the output is a character saved in an integer variable c; the second argument, FILE *stream, specifies a file stream. If successful, putc() returns the character written; otherwise, it returns EOF.

Example of using putc() :

putc(ch, stdout);
the putc functions print a character based on the numeric value in the ch

Another Function for Writing: putchar()

Like putc(), putchar() can also be used to put a character on the screen. The only difference between the two functions is that putchar() needs only one argument to contain the character. You don’t need to specify the file stream, because the standard output (stdout) is the default file stream to putchar().

The syntax for the putchar() function is

#include <stdio.h>
int putchar(int c);

Here int c is the argument that contains the numeric value of a character. The function returns EOF if an error occurs; otherwise, it returns the character that has been written.

Example of using putchar() :

putchar(65);

the output is:

A

Because the decimal value 65 represent A based on the ASCII Character Sets.

Revisiting the printf() Function

The syntax for the printf() function is

#include <stdio.h>
int printf(const char *format-string, ...);

Here const char *format-string is the first argument that contains the format specifier(s); … indicates the expression section that contains the expression(s) to be formatted according to the format specifiers. The number of expressions is determined by the number of the format specifiers inside the first argument.

Please remember that you should use exactly the same number of expressions as the number of format specifiers within the format string.

The following are all the format specifiers that can be used in printf():

%c

The character format specifier.

%d

The integer format specifier.

%i

The integer format specifier (same as %d).

%f

The floating-point format specifier.

%e

The scientific notation format specifier (note the lowercase e).

%E

The scientific notation format specifier (note the uppercase E).

%g

Uses %f or %e, whichever result is shorter.

%G

Uses %f or %E, whichever result is shorter.

%o

The unsigned octal format specifier.

%s

The string format specifier.

%u

The unsigned integer format specifier.

%x

The unsigned hexadecimal format specifier (note the lowercase x).

%X

The unsigned hexadecimal format specifier (note the uppercase X).

%p

Displays the corresponding argument that is a pointer.

%n

Records the number of characters written so far.

%%

Outputs a percent sign (%).

Converting to Hex Numbers

The difference between a decimal number and a hexadecimal number is that the hexadecimal is a base-16 numbering system. A hexadecimal number can be represented by four bits. (24 is equal to 16, which means four bits can produce 16 unique numbers.) Hexadecimal is often written as hex for short.

The hexadecimal numbers 0 through 9 use the same numeric symbols founded in the decimal numbers 0 through 9. uppercase A, B, C, D, E, and F are used to represent, respectively, the hexadecimal numbers 10 through 15. (Similarly, in lowercase, a, b, c, d, e, and f are used to represent these hex numbers.)

use %x or %X format specifiers in printf() for Hexadecimal

Adding the Minimum Field Width

The C language allows you to add an integer between the percent sign (%) and the letter in a format specifier. The integer is called the minimum field width specifier because it specifies the minimum field width and ensures that the output reaches the minimum width. For example, in %10f, 10 is a minimum field width specifier that ensures that the output is at least 10 character spaces wide.

Aligning Output

As you might have noticed in the previous section, all output is right-justified. In other words, by default, all output is placed on the right edge of the field, as long as the field width is longer than the width of the output.

You can change this and force output to be left-justified. To do so, you need to prefix the minimum field specifier with the minus sign (-). For example, %-12d specifies the minimum field width as 12, and justifies the output from the left edge of the field.

Using the Precision Specifier

You can put a period (.) and an integer right after the minimum field width specifier. The combination of the period (.) and the integer makes up a precision specifier. The precision specifier is another important specifier you can use to determine the number of decimal places for floating-point numbers, or to specify the maximum field width (or length) for integers or strings.

For instance, with %10.3f, the minimum field width length is specified as 10 characters long, and the number of decimal places is set to 3. (Remember, the default number of decimal places is 6.) For integers, %3.8d indicates that the minimum field width is 3, and the maximum field width is 8.
Summarized from http://aelinik.free.fr/c/ch05.htm

Deret 01123………………………………..

Problem

deret = 5<input>

Hasil:

01123

Solution

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d,i,x;
printf(”deret = “);
scanf(”%d”, &d);
printf(”\n hasil \n”);

if(d==1)
{
printf(”0″);
}
else
{
if(d==2)
{
printf(”o1″);
}
else
{
printf(”o1″);
x=d-2;
b=0;
c=1;

for(i=0;i<x;i++)
{
a=b+c;
printf(”%d”,a);
b=c;
c=a;

}

}
}
getch();

}

Tabungan Ani

Problem:

Ani mendapatkan bulanan sebesar US$1000 perbulan dari ibunya

Biaya Ani setiap Bulan

Makan perhari = $5

Kebutuhan Pribadi = $50

Salon + Gym = $50

Refreshing = $75

Sisanya Ditabung

Jika bunga Bank 12% per tahun(1% per bulan), berapa tabungan ANi akhir tahun(1 bulan = 30 hari)

Solution:

/* yang ditabungkan per bulan adalah 675$ */

void main()

{

int i,saldo;

for(i=0;i<12;i++)

{

saldo=saldo+675 + (saldo + 675)/100;

}

printf(”saldo akhir tahun %d”, saldo);

getch();

}

Tags:

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment