A simple hello program :
(demonstrates the const function in all c programs--the main() function.)
(example-1)
main()
{
puts("hello world guess who is writing a c program");
return(0);
}
(example-1)
main()
{
puts("hello world guess who is writing a c program");
return(0);
}
That's it. In all c programs there is a main function which is followed by a { and closed by a } after a return()function.It doesn't have to be return(0) but that depends upon the type of c compiler you have. Check your compiler before you start your programming.
You saw above that puts function is used to put a whole sentence on the screen; but are there functions that will put characters on the screen/take characters: Yes and next is a table of what they are and what they do. Read them and the examples that follow.
getchar() | Gets a single character from the input/keyboard. |
---|---|
putchar() | Puts a single character on the screen. |
---|---|
The printf function is a function used to print the output to the screen.printf() needs to know if the output is an integer,real,etc example-2
main()
{
printf(hello);
}
Assuming hello was defined earlier say by #define hello "Hello!" the output is Hello!. But if the output is an integer then %d has to be attatched to the printf statement.
main()
{
printf(hello);
}
Assuming hello was defined earlier say by #define hello "Hello!" the output is Hello!. But if the output is an integer then %d has to be attatched to the printf statement.
This above can be shown as printf("I am %d years old",12) which will result in the following result:I am 12 years old
The %d tells that an integer is to be placed here.
Now we will look into a function called scanf().This lets you input from the kewyboard and for that input to be taken by the program and processed.Once again it is important to tell scanf() what type of data is being scanned.
Here is an example of a program that demonstrates both scanf and printf in unison.
example-3
example-3
main() {
int count;
puts("Please enter a number: ");
scanf("%d", &count);
printf("The number is %d",count);
}
int count;
puts("Please enter a number: ");
scanf("%d", &count);
printf("The number is %d",count);
}
No comments:
Post a Comment