Lab1-CProgramming

Lab1-CProgramming - LAB 1 C Programming PROGRAMMING IN C...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
LAB 1 C Programming
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
PROGRAMMING IN C Most operating systems are implemented in lower level languages C and assembly are the common choices You’ve acquired the basic programming skill in C++. In here, we are going to give you an overview of C programming. Mainly highlighting the features that you might not have seen in C++
Background image of page 2
STRUCTURE OF C PROGRAMS The program structures of C and C++ are almost identical To compile, use “ gcc gcc Lab1-Example1.c o example1 To run it ./example1 // Lab1-Example1.c #include <stdio.h> int main() { printf( "Beware the differences between C & C++ !!\n" ); getchar(); return 0; }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
I/O C doesn't have stream operators; you cannot use cin and cout For output to screen, use printf() How to use it? Read its Linux manpage: man 3 printf int printf(char *format, arg list . ..) This function takes a variable number of arguments Prints to standard output the list of arguments according to the specified format string The first one is a string describing the formatting information of the rest of the arguments
Background image of page 4
PRINTF() In the formatting string, the character “%” is special The following character of it describes the format of a value to be displayed. %d for int, %f for float, %s for string Example Output is: integer is: 10 , float is: 6.54 , string is: " So what " int abc=10; float efg=6.54321; printf("integer is: %4d , float is: %.2f , string is: \" %s \" \n", abc , efg , " So what ");
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
SCANF() For input from keyboard, use scanf() or . . . man 3 scanf int scanf(char *format, args. ...) Reads from standard input and puts input in address of variables specified in args list. Format control string similar to printf() Example // Lab1-Example2.c #include <stdio.h> int main() { int number; float fnum; printf( "Please enter a number then follow by a float number: " ); scanf( "%d %f", &number, &fnum ); printf( "You entered %d & %f\n", number, fnum ); getchar(); return 0; }
Background image of page 6
In earlier versions of C, declarations could appear only at a block's beginning, before any other statements. #include <stdio.h>
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/01/2010 for the course CS 2342 taught by Professor Dr during the Spring '10 term at HKU.

Page1 / 23

Lab1-CProgramming - LAB 1 C Programming PROGRAMMING IN C...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online