You are on page 1of 13

FE-class2 & 5 JYOTHI ARUN

#include <stdio.h> int main() { printf("Hello world\n"); return 0; }


OUTPUT: Hello world

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


void main() { clrscr(); printf("Let's learn C programming."); getch(); } OUTPUT: Let's learn C programming.

#include

<stdio.h> #include <conio.h> These lines are called the preprocessors. Commands, like printf, are contained in these prepreprocessor not the compiler itself. Just remember you need the stdio.h, or standard input & output, to be able to use printf, scanf and such. Conio is needed for the clrscr or clear screen. main() This where the actual program begin and end. Void means that it does not return a value.

clrscr();

Clears the screen.


printf("Let's

learn C programming."); Prints whatever is inside the quotes inside the parenthesis. Another command similar to printf is scanf which allows for the user's input.

getch();

This allows you , time to read the ouput on the screen. Without it the program would just exit quickly and you wouldn't be able to see the output.

semicolon(;)

at the end of each statement except for main(). These semicolon indicate where the statement ends. A statement can have many statement within itself and is contained in within the curly brace ({}) just like main().

Commonly
\n

used escape sequences are:

(newline) \t (tab) \v (vertical tab) \f (new page) \b (backspace)

#include <stdio.h> #include <conio.h> char name[8]; int a,b,sum; main() { clrscr(); printf("What is your name: "); scanf("%s", &name); printf("Please enter two numbers:"); scanf("%d %d", &a, &b); sum=a+b; printf("\nYour name is %s.\n", name); printf("%d + %d = %d", a, b, sum); getch(); return(0); }

printf("What is your name:"); scanf("%s", &name); The first line ask the user their name and the scanf gets the input from the user. We use the specifier %s because it is a string variable. There should be an ampersand before the the variable where inputted data will be stored.

printf("Please enter two numbers:"); scanf("%d %d", &a, &b);

Here,

the user is asked to input two numbers and the numbers will be stored in variable a and b. We used %d for integers and there should always be an ampersand in front of the variable.

sum=a+b;

Adds

the two numbers together and store the answer in the variable sum.

printf("Your name is %s.\n", name); printf("%d + %d = %d", a, b, sum); When displaying variables, you have the comment inside the string and you replace where you want the value of a variable to be with a specifier. The string is followed by a comma and the variable.

#include <stdio.h> #include <conio.h> char name[8]; int a,b,c,sum; float ave; main() { clrscr(); printf("What is your name: "); scanf("%s", &name); printf("Please enter three numbers:"); scanf("%d %d %d", &a, &b, &c); sum=a+b+c; ave=sum/3; printf("Your name is %s.\n", name); printf("%d + %d + %d = %d\n", a, b, c, sum); printf("%d / 3 = %f", sum, ave); getch(); return(0); }

You might also like