Learning C / PDF
Learning C / PDF
Sample of the pdf document :
Source files in C
C files have the extension .c
There are can multiple .c files but only one
file can contain the function main()
• This is because main() is entry point of yourprogram
• There is no restriction on the name of your source files in C (unlike in java the file name has to be same as class name)
Compiling C Programs
Use gcc compiler for
• Compiling and linking .c source files
• Example: <prompt> gcc hello.c
*Produces a file a.out which contains the machine code
(similar to .obj)
*But a.out is not meaning full name
• Example: <prompt> gcc –o hello hello.c
*Option -o gives the file with machine code
*Now hello contains machine code
Compiling C Programs (contd..)
Others options
• gcc –S –o hello.s hello.c
*hello.s contains the assembly language code of the
machine on which C program is compiled
• gcc –o hello hello.s
*Assembly code is translated in machine code
Running/Executing your C program
After the you get the machine code
• Execute/run your program in bash shell
environment by (we will do this for all hws)
*<prompt> ./hello or
*<prompt>./a.out
• If in future you work on tcsh environment then
*<prompt> hello or
Important
All C programs must compile & run on x86
machines
• Use eniac-l machines for your homeworks
• Login into machine:
*<prompt> ssh eniac-l.seas.upenn.edu
• Use bash shell (so no need to change shell like we did
in previous tutorials)
• If you work from home use SecureCRT to login into
eniac-l and use file Ftp to upload your source files
Example 1: circle.c
#define RADIUS 15.0
#include <stdio.h>
int main(){
const float pi = 3.14159;
float area;
float circum;
area = pi * RADIUS * RADIUS;
circum = 2 * pi * RADIUS;
printf("area = %f\n",area);
printf(“circum = %f\n",circum);
#define RADIUS 15.0
#include <stdio.h>
int main(){
const float pi = 3.14159;
float area;
float circum;
printf("area = %f\n",area);
printf(“circum = %f\n",circum);
area = pi * RADIUS * RADIUS;
circum = 2 * pi * RADIUS;
printf("area = %f\n",area);
printf(“circum = %f\n",circum);
Click here for Download PDF / FREE
Learning C / PDF
0 commentaires: