beginning c for java programmers c syntax c started in
play

Beginning C for Java Programmers --------------------------------- - PowerPoint PPT Presentation

cnotes.txt Tue Mar 28 15:19:23 2017 1 cnotes.txt Tue Mar 28 15:19:23 2017 2 Beginning C for Java Programmers --------------------------------- C Syntax C started in the early 1970s as a systems-programming --------- language. Much C


  1. cnotes.txt Tue Mar 28 15:19:23 2017 1 cnotes.txt Tue Mar 28 15:19:23 2017 2 Beginning C for Java Programmers --------------------------------- C Syntax C started in the early 1970s as a systems-programming --------- language. Much C syntax was borrowed by Java. Higher level than assembly language. Same control structures as Java. Lower level than most other languages. C99 allows declaring variable in for loop K & R : Kernighan and Ritchie and anywhere in code. (Like Java) Influential 1978 book ANSI C requires that local vars be declared Greatly tamed in 1989: ANSI C "C89" or "C90" before any statements in any {.....} block. Also revised 1999 and 2011. Array syntax is not as "logical" as Java’s

  2. cnotes.txt Tue Mar 28 15:19:23 2017 3 cnotes.txt Tue Mar 28 15:19:23 2017 4 The Preprocessor Macros in the C Preprocessor ---------------- ----------------------------- Unlike Java, the C compiler has a "preprocessor" #define FOO 12*2 that does textual substitutions and "conditional compilation" before the "real compiler" thereafter, wherever the symbol FOO occurs in your runs. code, it is textually replaced by 12*2 . Think find-and-replace. This was borrowed from assembly languages. Macro can have parameters Fancy uses of preprocessor are often deprecated, especially since C99. #define BAR(x) 12*x Still commonly used use for defining constants If we have BAR(3*z) in your code, it is textually and in doing the C equivalent to Java’s "import". replaced by 12*3*z Preprocessor "directives" start with #. No ’;’ at end #undef BAR will "undefine" BAR. After this, BAR(3*z) remains as is.

  3. cnotes.txt Tue Mar 28 15:19:23 2017 5 cnotes.txt Tue Mar 28 15:19:23 2017 6 Perils of Macros Making Strings and Concatenating Tokens ----------------- --------------------------------------- #define SQUARE(x) ((x)*(x)) (optional fancy topic) used much later with In a macro definition, the # operator puts quote marks around a macro argument. int x = 0; int y = SQUARE(x++); #define STRINGIFYME(x) # x What value for x is expected by a programmer STRINGIFYME(owen) expands to "owen" looking only at these lines? The ## operator joins two tokens together. What value does x actually have? #define DECL_FUNC(name) int name ## _xy (int z){\ return(3 + name);} then DECL_FUNC(owen) ends up defining a function called owen_xy that returns 3 + owen

  4. cnotes.txt Tue Mar 28 15:19:23 2017 7 cnotes.txt Tue Mar 28 15:19:23 2017 8 Predefined Macros Conditional Compilation in C ------------------ ---------------------------- __FILE__ and __LINE__ expand to the current src #if ZZ == 1 #ifdef FOO code file name and line number. Good for ...somecode1... ..... auto-generated warning messages etc. #else #else ...somecode2... ..... __func__ name of current function (C99) #endif #endif also #ifndef Check a Boolean condition that can be evaluated at compile time, or chk definedness ** Determines whether somecode1 or somecode2 is seen by the real compiler Only one of the 2 sequences is used **

  5. cnotes.txt Tue Mar 28 15:19:23 2017 9 cnotes.txt Tue Mar 28 15:19:23 2017 10 Conditional Compilation part II #include statement -------------------------------- ------------------ Conditional compilation is often used to tailor #include "foo.h" will cause the contents of your the code for a particular models of computer. file foo.h to be inserted in place of the #include statement. And to ensure that a given "include file" isn’t "copy paste the file here, please" seen too many times (more later) #include is usually used like "import" in Java. It makes available library things. Most C programs have a line #include <stdio.h> since you can’t print otherwise. (note angle brackets for system libraries)

  6. cnotes.txt Tue Mar 28 15:19:23 2017 11 cnotes.txt Tue Mar 28 15:19:23 2017 12 Simple C program Common C Programming Conventions ----------------- -------------------------------- #include <stdio.h> Whereas camelCase is the standard in Java, in C the #define N 5 tradition is to use underscores_to_separate the int g; various parts of a compound identifier. // C99-style comment: program needs a main() UPPER CASE is conventionally used for preprocessor int main(void) { macros used as constants and some macros with int i; arguments (but other macros-with-arguments get written in lower case, pretending to be functions) g = N; for (i=0; i < g; ++i) printf("Hello world"); }

  7. cnotes.txt Tue Mar 28 15:19:23 2017 13 cnotes.txt Tue Mar 28 15:19:23 2017 14 Enums C has no objects ------ ----------------- As in Java, C89 provides enumerated types Methods are called "functions". enum studentkind {Undergrad, Grad}; Equivalent to Java static methods. enum studentkind sk = Undergrad; A program consists of a bunch of functions, segmented into multiple source code files (sometimes called "modules"). Need main(). C allows you to group related data together (just like a class does). Called a "struct". More later. If you want C with objects, you want C++ (or Objective C)

  8. cnotes.txt Tue Mar 28 15:19:23 2017 15 cnotes.txt Tue Mar 28 15:19:23 2017 16 Order of declaration matters Example ---------------------------- -------- /* doubleslashed comments only with C99 */ A function cannot be called before (in source-code order) it has been declared. int foo(int a); /* function prototype */ It can be declared and defined by giving its code double bar(int x, int y) { /* defined */ (like giving code for a static method). return foo( 2*x*y); /* uses foo */ } Or it can just be declared by giving its parameters .... and return type. Called a "function prototype". int foo(int a) { int x; A full definition must then be given afterward if (a==0) return bar(a,a); else return 27; }

  9. cnotes.txt Tue Mar 28 15:19:23 2017 17 cnotes.txt Tue Mar 28 15:19:23 2017 18 Library header files Void parameter list -------------------- ------------------- A function with no parameters uses void in its One of the main things contained in library parameter list. header files (eg, <stdio.h>) are the function prototypes for the routines that int foobar(void) {.....} the library makes available. vs Java’s int foobar() {....} (Often also some constant declarations and data type (struct) definitions) So <stdio.h> contains prototypes for printf scanf various file I/O routines

  10. cnotes.txt Tue Mar 28 15:19:23 2017 19 cnotes.txt Tue Mar 28 15:19:23 2017 20 Order of Parameter Evaluation Static Functions and Extern Variables ----------------------------- ------------------------------------- For functions with multiple parameters, it is If you use the qualifier "static" when defining undefined in which order the parameters are a function, it will not be exported for linking, evaluated. which is the default. { int i=0; Keyword extern is used when a variable is being foo(i++,i++); imported from another module. One of the } modules needs you may have called foo(0,1) or foo(1,0). extern int v; int v; Do not rely on any particular behaviour. to define the value and pass to the linker. Or use extern int v=23;

  11. cnotes.txt Tue Mar 28 15:19:23 2017 21 cnotes.txt Tue Mar 28 15:19:23 2017 22 Stack Frames Widening and Narrowing Rules ------------ ---------------------------- Compilers generate stack frames according to the approved calling conventions for that system. Widening: conversion of a datatype to one that that can handle all the original values (and maybe more) Look at the Resources section: ARM Stack (eg, short to int) Frame Layout (Windows CE 5.0). Narrowing: conversion to a more restricted datatype. More later about "alloca()". Risk a value won’t be representable anymore. Appears consistent with AAPCS. In Java, you need explicit typecasts more often than you do in C. Note the "Frame Pointer". Some calling conventions use frame pointers (aka BP); others don’t. This one int i = 3.14; //narrowing, Java needs typecast requires FP in only some cases. float f = 3; // widening, Java is also ok with this my_arg_should_be_float(3.14); // narrowing, Java cast

  12. cnotes.txt Tue Mar 28 15:19:23 2017 23 cnotes.txt Tue Mar 28 15:19:23 2017 24 Promotion Simple IO in C ---------- ----------------- If i is an int and l is a long, i*l is same as (long)i * l (same as Java) printf for screen output (note: Java now has printf!) also putchar What if i is an int and u is an unsigned int. scanf and getchar for keyboard input What is i*u? printf takes first a "format string", then a Ans: same as (unsigned int)i * u variable # of things No such issue in Java, since no unsigned... eg printf("Hello world!"); How about if c is a signed character and u is printf("value of x=%d, and y=%s", an unsigned int? x, my_str); c*u is same as ( (unsigned int) ( (int) c))* u scanf uses a format string too ---> assuming 2’s complement, using sign extension scanf("%d %d %d", &x, &y, &z); Note use of "address-of" operator & Reason will be explained later.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend