Professional Documents
Culture Documents
Output: 1,4
int 4
long 8
Common sizes (in bytes):
char 1
short 2
Float Types
• char myName[10];
– Create a string variable called "myName" which
contains 10 elements
strcpy(myName, "Evan");
char myChar;
int x;
x = 3;
myChar = „x';
if (valueIsTrue) { do this.. }
else if (AnotherValueIsTrue){do that.. }
else { do this other thing.. }
Switch Statements
switch(letter)
{
case 'A': printf("A, for apple\n");
break;
case 'B': printf("B, for Bob Garza\n");
break;
}
Constructing Logic
• continue;
– used in a for() loop will cause the current iteration
to stop and will start the next loop iteration
• break;
– used to "break" outside of a loop or conditional
prematurely.
• exit;
– quits the program (please make sure you have
only one exit point to your code)
Operator Precedence
char myName[10]=”yye00”;
printf("my name is: %s \n", myName);
Output: my name is: yye00
Conversion Characters
printf("%s\t%5.1f\t%5.1f\t%5.1f\t%5.1fP/s\t%4.fM\
t%4.fM\t4.fM\n",
hosts[i].hostName,hosts[i].li[R15S],hosts[i].li
[R1M],hosts[i].li[R15M],hosts[i].li[PG],hosts[i
].li[MEM],hosts[i].li[SWP],hosts[i].li[TMP]);
Output:
HOST R15s R1m R15m PAGES MEM SWAP TEMP
c21-208 1.0 1.0 1.0 2.1P/s 6468M 2021M 55776M
Input
/* definitions */
#define NAMELIMIT 40
FILE *fptr;
char filename[NAMELIMIT];
/* in code */
fptr = fopen(filename, "r");
if ( fptr == NULL) {
perror("error opening file");
}
fclose (fptr);
Working with File Pointers
int item;
fscanf (fptr, "%d", &item); /* read */
printf( "%d\n", item); /* print */
fprintf() and fscanf()
w+ read &
overwrite
a+ append
rw
Putting I/O together
system(command);
is equivalent to:
/bin/sh -c command
System calls are expensive computationally.
Most system calls can be accomplished by a
similar C library function: chmod, chdir,rename (see
section 2 and 3 or 3p man pages)
Retrieving Environment Variables
#include <stdio.h>
#include <stdlib.h>
home = getenv("HOME");
host = getenv("HOSTNAME");
return 0;
}
Signals
• Sample code
(modified) to
illustrate catching an
interrupt signal
• Code will run
indefinitely and
when we hit Ctrl-C it
will go to the handler
function
Power of Signals
• If you have more than one version of your code, time to move to
a version control system (check TACC tutorials page for more
info)
• When you have to type more than five words to compile your
code, time to use a make or a build system
• When you have a file that is over 1k lines, time to break it down
into many smaller more manageable files
• When your main function is over 200 lines of code long, time to
read more about functions and start using them. If you know a
software engineer, ask them about design patterns
• And finally, and most importantly, read your own code before
you show it to others, and make sure you do show it to others
for them to read. Code reviews save you time, effort and
frustration
Different Styles
• On Linux:
– Eclipse, Geany, Kdevelop, Nedit, Scite
• On Windows:
– Eclipse, Scite, UltraEdit32, MS Developer Studio
• On Mac
– Eclipse, Scite, Xcode
• Many more IDE's exist, but the IDE I use is
Eclipse
Code Tools