03 HelloWorld in C'
03 HelloWorld in C'
1110101
Programmer
Source Code
Object Code
___________
________________
________________
________________
_
Linker
Loader
1110101
1110100001001
.
111010000111101
Executable
Code
Results
Nihar Ranjan Roy 4
Writing and editing programs
Use any text editor (Ascii) available on your computer.
Example: windows (Edit,TC IDE,Notepad), Linux( Vi,Vim,emacs etc.)
Compiling (windows)
With IDE ( Turbo C)
Press Alt+F9
Command prompt
Issue the following command tcc filename.c Issue the following command tcc filename.c
Linking
Running the program
Windows
With IDE
Press CTRL+F9
Command Prompt
Issue the following command filename(hit enter)
Linux
Issue the following command ./a.out or ./outputfilename( hit enter)
Nihar Ranjan Roy 5
Structure of a C program
/* Preprocessor directives */
/* Global declarations */
#include <stdio.h>
int main(void)
{
int main(void)
{
/*Local declarations
Statements*/
}
/* other functions as required */
{
printf("Hello");
return 0;
}
Nihar Ranjan Roy 6
My First C Program
#include <stdio.h> /* comment */
int main(void)
{
tells compiler about st standdard iinput and ooutput functions (i.e. printf + others)
main function
{
printf("Hello");
return 0;
}
begin
end
flag success
to operating
system
Nihar Ranjan Roy 7
Command Prompt(Windows) : Compiling & Running
Nihar Ranjan Roy 8
Turbo C IDE
Nihar Ranjan Roy 9
The Format of C
Statements are terminated with
semicolons
Indentation is ignored by the compiler.
C is case sensitive - all keywords and
Standard Library functions are lowercase.
Strings are placed in double quotes
#include <stdio.h>
/* comment */
int main(void)
{
printf("Hello");
Strings are placed in double quotes
Newlines are handled via \n
Nihar Ranjan Roy 10
printf("Hello");
return 0;
}
Problem
Write a program in C which asks the user what is your name?. Accepts
users name and greets him as Hello,xxxx where xxxx is the name of
the user.
What is your name?
Nihar
Nihar Ranjan Roy 11
Nihar
Hello, Nihar
2
nd
Program
char name [30] ;
declares a variable named name, which
can hold a string of up to 29
characters (letter, digit, punctuation,
etc.).
#include<stdio.h>
void main()
{
printf ("What's your name? ");
Nihar Ranjan Roy 12
read your name into the variable name.
printf ("What's your name? ");
scanf("%s",name);
printf("Hello, %s\n",name);
}
Problem
Write a program which accepts two integer numbers and displays its
sum.
Enter two numbers:
6 7
The sum is 13
Nihar Ranjan Roy 13
3
rd
Program
#include<stdio.h>
void main ()
{
int a,b,sum;
printf("Enter two numbers: ");
We have made five changes to the
original program. We have
1. replaced the line defining name with one
defining other variables (a, b, and sum, all
integers)
2. changed the message in the printf
Nihar Ranjan Roy 14
printf("Enter two numbers: ");
scanf("%d %d",&a,&b);
sum = a + b;
printf("The sum is %d \n",sum);
}
2. changed the message in the printf
statement
3. changed the format string and variable
list in the scanf statement
4. added the assignment statement sum = a +
b;
5. changed the format string and argument
list in the final printf statement
Conclusion & Queries
Nihar Ranjan Roy 15